#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <cuda_profiler_api.h>
#include <assert.h>

#define min(x,y) (y + ((x - y) & ((x - y) >> (sizeof(long) * 8 - 1))))
 
const int Tile_Width = 1;
const int WIDTH = 3;

void print_matrix(long *m) {
	for (int i = 0; i < WIDTH; i++)
		for (int j = 0; j < WIDTH; j++)
			printf("%ld%c", m[i * WIDTH + j], " \n"[j == WIDTH-1]);
	printf("\n");
}

// Convenience function for checking CUDA runtime API results
// can be wrapped around any runtime API call. No-op in release builds.
inline cudaError_t checkCuda(cudaError_t result) {
#if defined(DEBUG) || defined(_DEBUG)
	if (result != cudaSuccess) {
		fprintf(stderr, "CUDA Runtime Error: %s\n", cudaGetErrorString(result));
		assert(result == cudaSuccess);
	}
#endif
	return result;
}


__global__ void reduction(long* Pd, long* Nd, int ndsize) {
	long Pvalue = LONG_MAX;

	int i = blockIdx.y * Tile_Width + threadIdx.y;
	int j = blockIdx.x * Tile_Width + threadIdx.x;

	for (int k = 0; k < ndsize; ++k) {
		Pvalue = min(Pvalue, Nd[k * WIDTH * WIDTH + i * WIDTH + j]);
	}

	Pd[i * WIDTH + j] = Pvalue;
}

void *emalloc(size_t size) {
	void *memory = malloc(size);

	if (!memory) {
		fprintf(stderr, "ERROR: Failed to malloc.\n");
		exit(1);
	}

	return memory;
}


void file_to_matrix(FILE *path_matr, long* m, int ind) {
	/*Reads a file and get the matrix 3x3 from it*/
	// Creating matrix
	char line[20];
	fscanf(path_matr, " %[^\n]", line);
	// Filling matrix
	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < WIDTH; j++) {
			fscanf(path_matr, "%ld", &m[(ind * WIDTH * WIDTH) + i * WIDTH + j]);
		}
	}
}


int main(int argc, char* argv[]) {
	if (argc != 2) {
		fprintf(stderr, "Syntax: %s <matrix file>\n", argv[0]);
		return EXIT_FAILURE;
	}

	FILE *path_matr = fopen(argv[1], "r");
	if (path_matr == NULL) {
		fprintf(stderr, "ERROR: Invalid file to matrices.\n");
		exit(1);
	}

	int n_matr; // Number of matrices
	fscanf(path_matr, "%d", &n_matr);


	long* M = (long*) emalloc(n_matr * WIDTH * WIDTH * sizeof(long*));
	long* P = (long*) emalloc(WIDTH * WIDTH * sizeof(long));
 
	for (int i = 0; i < n_matr; i++) {
		file_to_matrix(path_matr, M, i);
	}

	checkCuda( cudaSetDevice(0) );
	cudaDeviceReset();

	// allocate device matrices (linearized)
	//printf("Allocate device matrices (linearized)...\n");
	long* Nd = NULL;
	long* Pd = NULL;

	checkCuda( cudaMalloc((void**) &Nd, n_matr * WIDTH * WIDTH * sizeof(long)) );
	checkCuda( cudaMalloc((void**) &Pd, WIDTH * WIDTH * sizeof(long)) );

	struct timeval begin, end;
	gettimeofday(&begin, NULL);
	// copy host memory to device

	// cudaMemcpy -> faz copias de vetores do host para o device
	checkCuda( cudaMemcpy(Nd, M, n_matr * WIDTH * WIDTH * sizeof(long), cudaMemcpyHostToDevice) );
	checkCuda( cudaMemcpy(Pd, P, WIDTH * WIDTH * sizeof(long), cudaMemcpyHostToDevice) );
	// execute the kernel
	printf("Execute the kernel...\n");


	int GridSize = (WIDTH + Tile_Width-1) / Tile_Width;
	dim3 gridDim(GridSize, GridSize);
	dim3 blockDim(Tile_Width, Tile_Width);

	cudaProfilerStart();
	reduction<<< gridDim, blockDim >>>(Pd, Nd, n_matr);
	cudaProfilerStop();

	// copy result from device to host
	checkCuda( cudaMemcpy( P, Pd, WIDTH * WIDTH * sizeof(long),cudaMemcpyDeviceToHost) );
	gettimeofday(&end, NULL);

	double gpuTime = 1000000*(double)(end.tv_sec - begin.tv_sec);
	gpuTime +=	(double)(end.tv_usec - begin.tv_usec);

	// print times
	printf("Execution Time (microseconds): %9.2f\n", gpuTime);

	print_matrix(P);

	// clean up memory
	free(M);
	free(P);
	checkCuda( cudaFree(Nd) );
	checkCuda( cudaFree(Pd) );

	return 0;
}

	.file	"tmpxft_00304afd_00000000-6_cuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"%ld%c"
.LC2:
	.string	"\n"
.LC0:
	.string	" \n"
	.text
	.globl	_Z12print_matrixPl
	.type	_Z12print_matrixPl, @function
_Z12print_matrixPl:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	.LC0(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
.L3:
	imulq	$24, %rbp, %r12
	xorl	%ebx, %ebx
	addq	%r13, %r12
.L4:
	xorl	%eax, %eax
	cmpl	$2, %ebx
	movq	(%r12,%rbx,8), %rdx
	leaq	.LC1(%rip), %rsi
	sete	%al
	movl	$2, %edi
	incq	%rbx
	movsbl	(%r14,%rax), %ecx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	cmpq	$3, %rbx
	jne	.L4
	incq	%rbp
	cmpq	$3, %rbp
	jne	.L3
	popq	%rbx
	.cfi_def_cfa_offset 40
	leaq	.LC2(%rip), %rsi
	popq	%rbp
	.cfi_def_cfa_offset 32
	movl	$2, %edi
	popq	%r12
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z12print_matrixPl, .-_Z12print_matrixPl
	.section	.rodata.str1.1
.LC3:
	.string	"ERROR: Failed to malloc.\n"
	.text
	.globl	_Z7emallocm
	.type	_Z7emallocm, @function
_Z7emallocm:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rcx
	.cfi_def_cfa_offset 16
	call	malloc@PLT
	testq	%rax, %rax
	jne	.L9
	movq	stderr(%rip), %rdi
	leaq	.LC3(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L9:
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z7emallocm, .-_Z7emallocm
	.section	.rodata.str1.1
.LC4:
	.string	" %[^\n]"
.LC5:
	.string	"%ld"
	.text
	.globl	_Z14file_to_matrixP8_IO_FILEPli
	.type	_Z14file_to_matrixP8_IO_FILEPli, @function
_Z14file_to_matrixP8_IO_FILEPli:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC5(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	$3, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rsi, %r12
	leaq	.LC4(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edx, %ebx
	leal	(%rbx,%rbx,8), %ebx
	movslq	%ebx, %rbx
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	leaq	(%r12,%rbx,8), %r13
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	4(%rsp), %rdx
	call	__isoc23_fscanf@PLT
.L13:
	movq	%r13, %r12
	xorl	%ebx, %ebx
.L14:
	movq	%r12, %rdx
	movq	%r15, %rsi
	movq	%rbp, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	incl	%ebx
	addq	$8, %r12
	cmpl	$3, %ebx
	jne	.L14
	addq	$24, %r13
	decl	%r14d
	jne	.L13
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L16
	call	__stack_chk_fail@PLT
.L16:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z14file_to_matrixP8_IO_FILEPli, .-_Z14file_to_matrixP8_IO_FILEPli
	.globl	_Z31__device_stub__Z9reductionPlS_iPlS_i
	.type	_Z31__device_stub__Z9reductionPlS_iPlS_i, @function
_Z31__device_stub__Z9reductionPlS_iPlS_i:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L19
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z9reductionPlS_i(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L19:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L21
	call	__stack_chk_fail@PLT
.L21:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z31__device_stub__Z9reductionPlS_iPlS_i, .-_Z31__device_stub__Z9reductionPlS_iPlS_i
	.globl	_Z9reductionPlS_i
	.type	_Z9reductionPlS_i, @function
_Z9reductionPlS_i:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z31__device_stub__Z9reductionPlS_iPlS_i
	.cfi_endproc
.LFE2057:
	.size	_Z9reductionPlS_i, .-_Z9reductionPlS_i
	.section	.rodata.str1.1
.LC6:
	.string	"Syntax: %s <matrix file>\n"
.LC7:
	.string	"r"
.LC8:
	.string	"ERROR: Invalid file to matrices.\n"
.LC9:
	.string	"%d"
.LC10:
	.string	"Execute the kernel...\n"
.LC12:
	.string	"Execution Time (microseconds): %9.2f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$104, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	cmpl	$2, %edi
	je	.L25
	movq	(%rsi), %rcx
	movq	stderr(%rip), %rdi
	leaq	.LC6(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %eax
	jmp	.L24
.L25:
	movq	8(%rsi), %rdi
	leaq	.LC7(%rip), %rsi
	call	fopen@PLT
	movq	%rax, %r12
	testq	%rax, %rax
	jne	.L27
	movq	stderr(%rip), %rdi
	leaq	.LC8(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L27:
	leaq	12(%rsp), %rdx
	leaq	.LC9(%rip), %rsi
	movq	%rax, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	imull	$9, 12(%rsp), %edi
	xorl	%r13d, %r13d
	movslq	%edi, %rdi
	salq	$3, %rdi
	call	_Z7emallocm
	movl	$72, %edi
	movq	%rax, %rbp
	call	_Z7emallocm
	movq	%rax, %rbx
.L28:
	cmpl	%r13d, 12(%rsp)
	jle	.L33
	movl	%r13d, %edx
	movq	%rbp, %rsi
	movq	%r12, %rdi
	incl	%r13d
	call	_Z14file_to_matrixP8_IO_FILEPli
	jmp	.L28
.L33:
	xorl	%edi, %edi
	call	cudaSetDevice@PLT
	call	cudaDeviceReset@PLT
	imull	$9, 12(%rsp), %esi
	xorl	%eax, %eax
	leaq	16(%rsp), %rdi
	movq	%rax, 16(%rsp)
	movq	%rax, 24(%rsp)
	movslq	%esi, %rsi
	salq	$3, %rsi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$72, %esi
	call	cudaMalloc@PLT
	xorl	%esi, %esi
	leaq	56(%rsp), %rdi
	call	gettimeofday@PLT
	imull	$9, 12(%rsp), %edx
	movq	16(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	movslq	%edx, %rdx
	salq	$3, %rdx
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$72, %edx
	call	cudaMemcpy@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, 40(%rsp)
	movabsq	$12884901891, %rax
	movq	%rax, 32(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	cudaProfilerStart@PLT
	movl	52(%rsp), %ecx
	movl	40(%rsp), %esi
	xorl	%r9d, %r9d
	movq	44(%rsp), %rdx
	movq	32(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L30
	movl	12(%rsp), %edx
	movq	16(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	_Z31__device_stub__Z9reductionPlS_iPlS_i
.L30:
	call	cudaProfilerStop@PLT
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdi
	movl	$72, %edx
	call	cudaMemcpy@PLT
	leaq	72(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	movq	72(%rsp), %rax
	subq	56(%rsp), %rax
	leaq	.LC12(%rip), %rsi
	cvtsi2sdq	%rax, %xmm1
	movq	80(%rsp), %rax
	subq	64(%rsp), %rax
	movl	$2, %edi
	mulsd	.LC11(%rip), %xmm1
	cvtsi2sdq	%rax, %xmm0
	movb	$1, %al
	addsd	%xmm1, %xmm0
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	call	_Z12print_matrixPl
	movq	%rbp, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
.L24:
	movq	88(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L31
	call	__stack_chk_fail@PLT
.L31:
	addq	$104, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC13:
	.string	"_Z9reductionPlS_i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC13(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z9reductionPlS_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2059:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC11:
	.long	0
	.long	1093567616
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <hip/hip_runtime_api.h>
#include <assert.h>

#define min(x,y) (y + ((x - y) & ((x - y) >> (sizeof(long) * 8 - 1))))
 
const int Tile_Width = 1;
const int WIDTH = 3;

void print_matrix(long *m) {
	for (int i = 0; i < WIDTH; i++)
		for (int j = 0; j < WIDTH; j++)
			printf("%ld%c", m[i * WIDTH + j], " \n"[j == WIDTH-1]);
	printf("\n");
}

// Convenience function for checking CUDA runtime API results
// can be wrapped around any runtime API call. No-op in release builds.
inline hipError_t checkCuda(hipError_t result) {
#if defined(DEBUG) || defined(_DEBUG)
	if (result != hipSuccess) {
		fprintf(stderr, "CUDA Runtime Error: %s\n", hipGetErrorString(result));
		assert(result == hipSuccess);
	}
#endif
	return result;
}


__global__ void reduction(long* Pd, long* Nd, int ndsize) {
	long Pvalue = LONG_MAX;

	int i = blockIdx.y * Tile_Width + threadIdx.y;
	int j = blockIdx.x * Tile_Width + threadIdx.x;

	for (int k = 0; k < ndsize; ++k) {
		Pvalue = min(Pvalue, Nd[k * WIDTH * WIDTH + i * WIDTH + j]);
	}

	Pd[i * WIDTH + j] = Pvalue;
}

void *emalloc(size_t size) {
	void *memory = malloc(size);

	if (!memory) {
		fprintf(stderr, "ERROR: Failed to malloc.\n");
		exit(1);
	}

	return memory;
}


void file_to_matrix(FILE *path_matr, long* m, int ind) {
	/*Reads a file and get the matrix 3x3 from it*/
	// Creating matrix
	char line[20];
	fscanf(path_matr, " %[^\n]", line);
	// Filling matrix
	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < WIDTH; j++) {
			fscanf(path_matr, "%ld", &m[(ind * WIDTH * WIDTH) + i * WIDTH + j]);
		}
	}
}


int main(int argc, char* argv[]) {
	if (argc != 2) {
		fprintf(stderr, "Syntax: %s <matrix file>\n", argv[0]);
		return EXIT_FAILURE;
	}

	FILE *path_matr = fopen(argv[1], "r");
	if (path_matr == NULL) {
		fprintf(stderr, "ERROR: Invalid file to matrices.\n");
		exit(1);
	}

	int n_matr; // Number of matrices
	fscanf(path_matr, "%d", &n_matr);


	long* M = (long*) emalloc(n_matr * WIDTH * WIDTH * sizeof(long*));
	long* P = (long*) emalloc(WIDTH * WIDTH * sizeof(long));
 
	for (int i = 0; i < n_matr; i++) {
		file_to_matrix(path_matr, M, i);
	}

	checkCuda( hipSetDevice(0) );
	hipDeviceReset();

	// allocate device matrices (linearized)
	//printf("Allocate device matrices (linearized)...\n");
	long* Nd = NULL;
	long* Pd = NULL;

	checkCuda( hipMalloc((void**) &Nd, n_matr * WIDTH * WIDTH * sizeof(long)) );
	checkCuda( hipMalloc((void**) &Pd, WIDTH * WIDTH * sizeof(long)) );

	struct timeval begin, end;
	gettimeofday(&begin, NULL);
	// copy host memory to device

	// cudaMemcpy -> faz copias de vetores do host para o device
	checkCuda( hipMemcpy(Nd, M, n_matr * WIDTH * WIDTH * sizeof(long), hipMemcpyHostToDevice) );
	checkCuda( hipMemcpy(Pd, P, WIDTH * WIDTH * sizeof(long), hipMemcpyHostToDevice) );
	// execute the kernel
	printf("Execute the kernel...\n");


	int GridSize = (WIDTH + Tile_Width-1) / Tile_Width;
	dim3 gridDim(GridSize, GridSize);
	dim3 blockDim(Tile_Width, Tile_Width);

	hipProfilerStart();
	reduction<<< gridDim, blockDim >>>(Pd, Nd, n_matr);
	hipProfilerStop();

	// copy result from device to host
	checkCuda( hipMemcpy( P, Pd, WIDTH * WIDTH * sizeof(long),hipMemcpyDeviceToHost) );
	gettimeofday(&end, NULL);

	double gpuTime = 1000000*(double)(end.tv_sec - begin.tv_sec);
	gpuTime +=	(double)(end.tv_usec - begin.tv_usec);

	// print times
	printf("Execution Time (microseconds): %9.2f\n", gpuTime);

	print_matrix(P);

	// clean up memory
	free(M);
	free(P);
	checkCuda( hipFree(Nd) );
	checkCuda( hipFree(Pd) );

	return 0;
}

	.text
	.file	"cuda.hip"
	.globl	_Z12print_matrixPl              # -- Begin function _Z12print_matrixPl
	.type	_Z12print_matrixPl,@function
_Z12print_matrixPl:                     # @_Z12print_matrixPl
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	xorl	%r14d, %r14d
.LBB0_1:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_2 Depth 2
	xorl	%r15d, %r15d
.LBB0_2:                                #   Parent Loop BB0_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	(%rbx,%r15,8), %rsi
	xorl	%eax, %eax
	cmpq	$2, %r15
	sete	%al
	movsbl	.L.str.1(%rax), %edx
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpq	$3, %r15
	jne	.LBB0_2
# %bb.3:                                #   in Loop: Header=BB0_1 Depth=1
	incq	%r14
	addq	$24, %rbx
	cmpq	$3, %r14
	jne	.LBB0_1
# %bb.4:
	movl	$10, %edi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	putchar@PLT                     # TAILCALL
.Lfunc_end0:
	.size	_Z12print_matrixPl, .Lfunc_end0-_Z12print_matrixPl
	.cfi_endproc
                                        # -- End function
	.globl	_Z24__device_stub__reductionPlS_i # -- Begin function _Z24__device_stub__reductionPlS_i
	.type	_Z24__device_stub__reductionPlS_i,@function
_Z24__device_stub__reductionPlS_i:      # @_Z24__device_stub__reductionPlS_i
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z9reductionPlS_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z24__device_stub__reductionPlS_i, .Lfunc_end1-_Z24__device_stub__reductionPlS_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z7emallocm                     # -- Begin function _Z7emallocm
	.type	_Z7emallocm,@function
_Z7emallocm:                            # @_Z7emallocm
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	malloc
	testq	%rax, %rax
	je	.LBB2_2
# %bb.1:
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.LBB2_2:
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rcx
	movl	$.L.str.3, %edi
	movl	$25, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z7emallocm, .Lfunc_end2-_Z7emallocm
	.cfi_endproc
                                        # -- End function
	.globl	_Z14file_to_matrixP8_IO_FILEPli # -- Begin function _Z14file_to_matrixP8_IO_FILEPli
	.type	_Z14file_to_matrixP8_IO_FILEPli,@function
_Z14file_to_matrixP8_IO_FILEPli:        # @_Z14file_to_matrixP8_IO_FILEPli
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	%edx, %r14d
	movq	%rsi, %r15
	movq	%rdi, %rbx
	movq	%rsp, %rdx
	movl	$.L.str.4, %esi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	leal	(%r14,%r14,8), %eax
	cltq
	leaq	(%r15,%rax,8), %r14
	xorl	%r15d, %r15d
.LBB3_1:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_2 Depth 2
	xorl	%r12d, %r12d
.LBB3_2:                                #   Parent Loop BB3_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leaq	(%r14,%r12), %rdx
	movl	$.L.str.5, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	addq	$8, %r12
	cmpq	$24, %r12
	jne	.LBB3_2
# %bb.3:                                #   in Loop: Header=BB3_1 Depth=1
	incq	%r15
	addq	$24, %r14
	cmpq	$3, %r15
	jne	.LBB3_1
# %bb.4:
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z14file_to_matrixP8_IO_FILEPli, .Lfunc_end3-_Z14file_to_matrixP8_IO_FILEPli
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI4_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$2, %edi
	jne	.LBB4_1
# %bb.2:
	movq	8(%rsi), %rdi
	movl	$.L.str.7, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB4_8
# %bb.3:
	movq	%rax, %r15
	xorl	%ebp, %ebp
	leaq	4(%rsp), %r12
	movl	$.L.str.9, %esi
	movq	%rax, %rdi
	movq	%r12, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%r12), %eax
	leal	(%rax,%rax,8), %eax
	movslq	%eax, %rdi
	shlq	$3, %rdi
	callq	_Z7emallocm
	movq	%rax, %rbx
	movl	$72, %edi
	callq	_Z7emallocm
	movq	%rax, %r14
	cmpl	$0, (%r12)
	jle	.LBB4_4
.LBB4_9:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%r15, %rdi
	movq	%rbx, %rsi
	movl	%ebp, %edx
	callq	_Z14file_to_matrixP8_IO_FILEPli
	incl	%ebp
	cmpl	4(%rsp), %ebp
	jl	.LBB4_9
.LBB4_4:                                # %._crit_edge
	xorl	%r15d, %r15d
	xorl	%edi, %edi
	callq	hipSetDevice
	callq	hipDeviceReset
	leaq	16(%rsp), %r13
	movq	%r15, (%r13)
	leaq	8(%rsp), %r12
	movq	%r15, (%r12)
	movl	4(%rsp), %eax
	leal	(%rax,%rax,8), %eax
	movslq	%eax, %rsi
	shlq	$3, %rsi
	movq	%r13, %rdi
	callq	hipMalloc
	movl	$72, %esi
	movq	%r12, %rdi
	callq	hipMalloc
	leaq	24(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r13), %rdi
	movl	4(%rsp), %eax
	leal	(%rax,%rax,8), %eax
	movslq	%eax, %rdx
	shlq	$3, %rdx
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r12), %rdi
	movl	$72, %edx
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$.Lstr, %edi
	callq	puts@PLT
	callq	hipProfilerStart
	movabsq	$12884901891, %rdi              # imm = 0x300000003
	movabsq	$4294967297, %rdx               # imm = 0x100000001
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_6
# %bb.5:
	movq	8(%rsp), %rdi
	movq	16(%rsp), %rsi
	movl	4(%rsp), %edx
	callq	_Z24__device_stub__reductionPlS_i
.LBB4_6:
	callq	hipProfilerStop
	movq	8(%rsp), %rsi
	movl	$72, %edx
	movq	%r14, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	leaq	40(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r12), %rax
	movq	8(%r12), %rcx
	subq	24(%rsp), %rax
	cvtsi2sd	%rax, %xmm1
	mulsd	.LCPI4_0(%rip), %xmm1
	subq	32(%rsp), %rcx
	cvtsi2sd	%rcx, %xmm0
	addsd	%xmm1, %xmm0
	movl	$.L.str.11, %edi
	movb	$1, %al
	callq	printf
	movq	%r14, %rdi
	callq	_Z12print_matrixPl
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
.LBB4_7:
	movl	%r15d, %eax
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB4_1:
	.cfi_def_cfa_offset 112
	movq	stderr(%rip), %rdi
	movq	(%rsi), %rdx
	movl	$.L.str.6, %esi
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %r15d
	jmp	.LBB4_7
.LBB4_8:
	movq	stderr(%rip), %rcx
	movl	$.L.str.8, %edi
	movl	$33, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end4:
	.size	main, .Lfunc_end4-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z9reductionPlS_i, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB6_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%ld%c"
	.size	.L.str, 6

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	" \n"
	.size	.L.str.1, 3

	.type	_Z9reductionPlS_i,@object       # @_Z9reductionPlS_i
	.section	.rodata,"a",@progbits
	.globl	_Z9reductionPlS_i
	.p2align	3, 0x0
_Z9reductionPlS_i:
	.quad	_Z24__device_stub__reductionPlS_i
	.size	_Z9reductionPlS_i, 8

	.type	.L.str.3,@object                # @.str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.3:
	.asciz	"ERROR: Failed to malloc.\n"
	.size	.L.str.3, 26

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	" %[^\n]"
	.size	.L.str.4, 7

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%ld"
	.size	.L.str.5, 4

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Syntax: %s <matrix file>\n"
	.size	.L.str.6, 26

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"r"
	.size	.L.str.7, 2

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"ERROR: Invalid file to matrices.\n"
	.size	.L.str.8, 34

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"%d"
	.size	.L.str.9, 3

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Execution Time (microseconds): %9.2f\n"
	.size	.L.str.11, 38

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z9reductionPlS_i"
	.size	.L__unnamed_1, 18

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Execute the kernel..."
	.size	.Lstr, 22

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z24__device_stub__reductionPlS_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z9reductionPlS_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z9reductionPlS_i       ; -- Begin function _Z9reductionPlS_i
	.globl	_Z9reductionPlS_i
	.p2align	8
	.type	_Z9reductionPlS_i,@function
_Z9reductionPlS_i:                      ; @_Z9reductionPlS_i
; %bb.0:
	s_load_b32 s6, s[0:1], 0x10
	v_bfe_u32 v1, v0, 10, 10
	s_load_b128 s[0:3], s[0:1], 0x0
	s_mov_b32 s4, -1
	s_brev_b32 s5, -2
	v_and_b32_e32 v0, 0x3ff, v0
	v_dual_mov_b32 v2, s4 :: v_dual_add_nc_u32 v1, s15, v1
	v_mov_b32_e32 v3, s5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshl_add_u32 v1, v1, 1, v1
	v_add3_u32 v0, s14, v0, v1
	s_waitcnt lgkmcnt(0)
	s_cmp_lt_i32 s6, 1
	s_cbranch_scc1 .LBB0_3
; %bb.1:                                ; %.lr.ph.preheader
	s_delay_alu instid0(VALU_DEP_1)
	v_mov_b32_e32 v4, v0
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_ashrrev_i32_e32 v5, 31, v4
	s_add_i32 s6, s6, -1
	s_cmp_eq_u32 s6, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[5:6], 3, v[4:5]
	v_add_nc_u32_e32 v4, 9, v4
	v_add_co_u32 v5, vcc_lo, s2, v5
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e32 v6, vcc_lo, s3, v6, vcc_lo
	global_load_b64 v[5:6], v[5:6], off
	s_waitcnt vmcnt(0)
	v_sub_co_u32 v1, vcc_lo, v2, v5
	v_sub_co_ci_u32_e32 v2, vcc_lo, v3, v6, vcc_lo
	v_ashrrev_i32_e32 v3, 31, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_and_b32_e32 v1, v3, v1
	v_and_b32_e32 v3, v3, v2
	v_add_co_u32 v2, vcc_lo, v1, v5
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, v3, v6, vcc_lo
	s_cbranch_scc0 .LBB0_2
.LBB0_3:                                ; %Flow36
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v1, 31, v0
	v_lshlrev_b64 v[0:1], 3, v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s0, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_store_b64 v[0:1], v[2:3], off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z9reductionPlS_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 20
		.amdhsa_user_sgpr_count 14
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 7
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z9reductionPlS_i, .Lfunc_end0-_Z9reductionPlS_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 252
; NumSgprs: 18
; NumVgprs: 7
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 7
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 20
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z9reductionPlS_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z9reductionPlS_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     7
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
