#include <stdio.h>
#include <stdlib.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#define CUDA_CHK(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
   if (code != cudaSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

void read_file(const char*, int*);
int get_text_length(const char * fname);

#define A 15
#define B 27
#define M 256
#define A_MMI_M -17


__device__ int modulo(int a, int b){
	int r = a % b;
	r = (r < 0) ?  r+ b : r;
	return r;
}

__global__ void decrypt_kernel(int *d_message, int length, int parte) {

	if (parte == 1) {
		// Parte a:
		d_message[threadIdx.x] =  modulo(A_MMI_M * (d_message[threadIdx.x] - B), M);
	} else if (parte == 2) {
		// Parte b:
		d_message[blockIdx.x*blockDim.x + threadIdx.x] =  modulo(A_MMI_M * (d_message[blockIdx.x*blockDim.x + threadIdx.x] - B), M);
	} else if (parte == 3) {
			// Parte c:
			for (int i = 0; i < length / blockDim.x / gridDim.x + 1; ++i) {
				d_message[blockIdx.x*blockDim.x + gridDim.x*blockDim.x*i + threadIdx.x] =  modulo(A_MMI_M * (d_message[blockIdx.x*blockDim.x + gridDim.x*blockDim.x*i + threadIdx.x] - B), M);
			}
		}
}

int main(int argc, char *argv[])
{
	int *h_message;
	int *d_message;
	unsigned int size;
	int nb; // cuda grid size (# blocks per grid)

	const char * fname;

	if (argc < 4) 
		printf("Debes ingresar el nombre del archivo a desencriptar, el tamaño del bloque (1024 como máximo) y la parte del practico 2 a ejecutar: 1, 2 o 3.\n");
	else
		fname = argv[1];
		int parte = atoi(argv[3]); // cuda block size (# threads per block ) max 1024
		int n = atoi(argv[2]);

	if (parte < 1 || parte > 3) {
		printf("%d\n",parte);
		printf("Debe ingresar como segundo parámetro la parte del ejercicio a ejecutar: 1, 2 o 3.\n");
		exit(1);
	} else if (n > 1024) {
		printf("El tamaño máximo del bloque es 1024. La arquitectura actual a utilizar soporta un máximo de 1024 threads por bloque.\n");
		exit(1);
	}

	int length = get_text_length(fname);

	size = length * sizeof(int);

	// Parte a y c:
	if (parte == 1 || parte == 3) {
		nb = 16;
	// parte b:
	} else if (parte == 2) {
		if (length % n == 0 ){
			nb = length/n;
		}else
			nb = length / n + 1;
	}

	// reservar memoria para el mensaje
	h_message = (int *)malloc(size);

	// leo el archivo de la entrada
	read_file(fname, h_message);

	// reservo memoria en la GPU
	CUDA_CHK(cudaMalloc((void**)&d_message, size));

	// copio los datos a la memoria de la GPU
	CUDA_CHK(cudaMemcpy(d_message, h_message, size, cudaMemcpyHostToDevice));

	// configuro la grilla de threads
	dim3 gridSize(nb,1);
	dim3 blockSize(n, 1, 1);

	// ejecuto el kernel
	decrypt_kernel <<< gridSize, blockSize >>> (d_message, length, parte);

	// Obtengo los posibles errores en la llamada al kernel
	CUDA_CHK(cudaGetLastError());

	// Obligo al Kernel a llegar al final de su ejecucion y hacer obtener los posibles errores
	CUDA_CHK(cudaDeviceSynchronize());



	// copio los datos nuevamente a la memoria de la CPU
	CUDA_CHK(cudaMemcpy(h_message, d_message, size, cudaMemcpyDeviceToHost));

	// despliego el mensaje

	// Si estoy en la primera parte despliego solamente la parte desencriptada.
	if (parte == 1)
		length = n;
	
	for (int i = 0; i < length; i++) {
		printf("%c", (char)h_message[i]);
	}
	printf("\n");

	// libero la memoria en la GPU
	cudaFree(d_message);

	// libero la memoria en la CPU
	free(h_message);

	return 0;
}

	
int get_text_length(const char * fname)
{
	FILE *f = NULL;
	f = fopen(fname, "r"); //read and binary flags

	size_t pos = ftell(f);    
	fseek(f, 0, SEEK_END);    
	size_t length = ftell(f); 
	fseek(f, pos, SEEK_SET);  

	fclose(f);

	return length;
}

void read_file(const char * fname, int* input)
{
	// printf("leyendo archivo %s\n", fname );

	FILE *f = NULL;
	f = fopen(fname, "r"); //read and binary flags
	if (f == NULL){
		fprintf(stderr, "Error: Could not find %s file \n", fname);
		exit(1);
	}

	//fread(input, 1, N, f);
	int c; 
	while ((c = getc(f)) != EOF) {
		*(input++) = c;
		printf("%d",c);
	}
	

	fclose(f);
}

	.file	"tmpxft_0033e573_00000000-6_decrypt.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._Z9gpuAssert9cudaErrorPKcib.str1.1,"aMS",@progbits,1
.LC0:
	.string	"GPUassert: %s %s %d\n"
	.section	.text._Z9gpuAssert9cudaErrorPKcib,"axG",@progbits,_Z9gpuAssert9cudaErrorPKcib,comdat
	.weak	_Z9gpuAssert9cudaErrorPKcib
	.type	_Z9gpuAssert9cudaErrorPKcib, @function
_Z9gpuAssert9cudaErrorPKcib:
.LFB2027:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L8
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	%edi, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movl	%edx, 12(%rsp)
	movq	%rsi, (%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r9d
	movq	(%rsp), %r8
	leaq	.LC0(%rip), %rdx
	movq	stderr(%rip), %rdi
	movq	%rax, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	testb	%bpl, %bpl
	je	.L2
	movl	%ebx, %edi
	call	exit@PLT
.L2:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
.L8:
	.cfi_restore 3
	.cfi_restore 6
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9gpuAssert9cudaErrorPKcib, .-_Z9gpuAssert9cudaErrorPKcib
	.text
	.globl	_Z6moduloii
	.type	_Z6moduloii, @function
_Z6moduloii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z6moduloii, .-_Z6moduloii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"r"
	.text
	.globl	_Z15get_text_lengthPKc
	.type	_Z15get_text_lengthPKc, @function
_Z15get_text_lengthPKc:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC1(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	call	fopen@PLT
	movq	%rax, %rbx
	movq	%rax, %rdi
	call	ftell@PLT
	movl	$2, %edx
	xorl	%esi, %esi
	movq	%rbx, %rdi
	movq	%rax, %r12
	call	fseek@PLT
	movq	%rbx, %rdi
	call	ftell@PLT
	movq	%r12, %rsi
	movq	%rbx, %rdi
	xorl	%edx, %edx
	movq	%rax, %rbp
	call	fseek@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	movl	%ebp, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z15get_text_lengthPKc, .-_Z15get_text_lengthPKc
	.section	.rodata.str1.1
.LC2:
	.string	"Error: Could not find %s file \n"
.LC3:
	.string	"%d"
	.text
	.globl	_Z9read_filePKcPi
	.type	_Z9read_filePKcPi, @function
_Z9read_filePKcPi:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC3(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rsi, %rbp
	leaq	.LC1(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	pushq	%rdx
	.cfi_def_cfa_offset 48
	call	fopen@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L17
	movq	stderr(%rip), %rdi
	movq	%r12, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC2(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L17:
	movq	%rbx, %rdi
	call	getc@PLT
	movl	%eax, %edx
	cmpl	$-1, %eax
	je	.L22
	movl	%edx, 0(%rbp)
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	addq	$4, %rbp
	call	__printf_chk@PLT
	jmp	.L17
.L22:
	popq	%rax
	.cfi_def_cfa_offset 40
	movq	%rbx, %rdi
	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
	jmp	fclose@PLT
	.cfi_endproc
.LFE2031:
	.size	_Z9read_filePKcPi, .-_Z9read_filePKcPi
	.globl	_Z36__device_stub__Z14decrypt_kernelPiiiPiii
	.type	_Z36__device_stub__Z14decrypt_kernelPiiiPiii, @function
_Z36__device_stub__Z14decrypt_kernelPiiiPiii:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	32(%rsp), %rdi
	movl	%esi, 4(%rsp)
	leaq	44(%rsp), %rsi
	movl	%edx, (%rsp)
	leaq	16(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L23
	pushq	24(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z14decrypt_kernelPiii(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 144
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L23:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z36__device_stub__Z14decrypt_kernelPiiiPiii, .-_Z36__device_stub__Z14decrypt_kernelPiiiPiii
	.globl	_Z14decrypt_kernelPiii
	.type	_Z14decrypt_kernelPiii, @function
_Z14decrypt_kernelPiii:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z36__device_stub__Z14decrypt_kernelPiiiPiii
	.cfi_endproc
.LFE2057:
	.size	_Z14decrypt_kernelPiii, .-_Z14decrypt_kernelPiii
	.section	.rodata.str1.1
.LC4:
	.string	"Debes ingresar el nombre del archivo a desencriptar, el tama\303\261o del bloque (1024 como m\303\241ximo) y la parte del practico 2 a ejecutar: 1, 2 o 3.\n"
.LC5:
	.string	"%d\n"
.LC6:
	.string	"Debe ingresar como segundo par\303\241metro la parte del ejercicio a ejecutar: 1, 2 o 3.\n"
.LC7:
	.string	"El tama\303\261o m\303\241ximo del bloque es 1024. La arquitectura actual a utilizar soporta un m\303\241ximo de 1024 threads por bloque.\n"
.LC8:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/maxibove13/GPGPU/master/practico_2/decrypt.cu"
.LC9:
	.string	"%c"
.LC10:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	cmpl	$3, %edi
	jg	.L29
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L30
.L29:
	movq	8(%rsi), %r15
.L30:
	movq	24(%rbx), %rdi
	call	atoi@PLT
	movq	16(%rbx), %rdi
	movl	%eax, %r13d
	call	atoi@PLT
	movl	%eax, %ebx
	leal	-1(%r13), %eax
	cmpl	$2, %eax
	jbe	.L31
	leaq	.LC5(%rip), %rsi
	movl	%r13d, %edx
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC6(%rip), %rsi
	jmp	.L46
.L31:
	cmpl	$1024, %ebx
	jle	.L32
	leaq	.LC7(%rip), %rsi
.L46:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L32:
	movq	%r15, %rdi
	call	_Z15get_text_lengthPKc
	movl	$16, %r8d
	movl	%eax, %r12d
	leal	0(,%rax,4), %r14d
	movl	%r13d, %eax
	andl	$-3, %eax
	decl	%eax
	je	.L33
	movl	%r12d, %eax
	cltd
	idivl	%ebx
	cmpl	$1, %edx
	sbbl	$-1, %eax
	movl	%eax, %r8d
.L33:
	movq	%r14, %rdi
	movl	%r8d, 12(%rsp)
	call	malloc@PLT
	movq	%r15, %rdi
	leaq	.LC8(%rip), %r15
	movq	%rax, %rsi
	movq	%rax, %rbp
	call	_Z9read_filePKcPi
	leaq	24(%rsp), %rdi
	movq	%r14, %rsi
	call	cudaMalloc@PLT
	movl	$1, %ecx
	movl	$94, %edx
	movq	%r15, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	movq	24(%rsp), %rdi
	movq	%r14, %rdx
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$1, %ecx
	movl	$97, %edx
	movq	%r15, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	movl	12(%rsp), %r8d
	movl	%ebx, 44(%rsp)
	xorl	%r9d, %r9d
	movabsq	$4294967297, %rax
	movl	%r8d, 32(%rsp)
	xorl	%r8d, %r8d
	movq	%rax, 36(%rsp)
	movq	32(%rsp), %rdi
	movq	%rax, 48(%rsp)
	movl	40(%rsp), %esi
	movl	52(%rsp), %ecx
	movq	44(%rsp), %rdx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L34
	movq	24(%rsp), %rdi
	movl	%r13d, %edx
	movl	%r12d, %esi
	call	_Z36__device_stub__Z14decrypt_kernelPiiiPiii
.L34:
	call	cudaGetLastError@PLT
	movl	$1, %ecx
	movl	$107, %edx
	movq	%r15, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	call	cudaDeviceSynchronize@PLT
	movl	$1, %ecx
	movl	$110, %edx
	movq	%r15, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	movq	24(%rsp), %rsi
	movq	%r14, %rdx
	movq	%rbp, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movl	$1, %ecx
	movl	$115, %edx
	movq	%r15, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	decl	%r13d
	leaq	.LC9(%rip), %r13
	cmovne	%r12d, %ebx
	xorl	%r12d, %r12d
.L36:
	cmpl	%r12d, %ebx
	jle	.L47
	movsbl	0(%rbp,%r12,4), %edx
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%r12
	call	__printf_chk@PLT
	jmp	.L36
.L47:
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L38
	call	__stack_chk_fail@PLT
.L38:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	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
.LFE2029:
	.size	main, .-main
	.section	.rodata.str1.1
.LC11:
	.string	"_Z14decrypt_kernelPiii"
	.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	.LC11(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z14decrypt_kernelPiii(%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
	.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 <stdio.h>
#include <stdlib.h>
#include "hip/hip_runtime.h"


#define CUDA_CHK(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(hipError_t code, const char *file, int line, bool abort=true)
{
   if (code != hipSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", hipGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

void read_file(const char*, int*);
int get_text_length(const char * fname);

#define A 15
#define B 27
#define M 256
#define A_MMI_M -17


__device__ int modulo(int a, int b){
	int r = a % b;
	r = (r < 0) ?  r+ b : r;
	return r;
}

__global__ void decrypt_kernel(int *d_message, int length, int parte) {

	if (parte == 1) {
		// Parte a:
		d_message[threadIdx.x] =  modulo(A_MMI_M * (d_message[threadIdx.x] - B), M);
	} else if (parte == 2) {
		// Parte b:
		d_message[blockIdx.x*blockDim.x + threadIdx.x] =  modulo(A_MMI_M * (d_message[blockIdx.x*blockDim.x + threadIdx.x] - B), M);
	} else if (parte == 3) {
			// Parte c:
			for (int i = 0; i < length / blockDim.x / gridDim.x + 1; ++i) {
				d_message[blockIdx.x*blockDim.x + gridDim.x*blockDim.x*i + threadIdx.x] =  modulo(A_MMI_M * (d_message[blockIdx.x*blockDim.x + gridDim.x*blockDim.x*i + threadIdx.x] - B), M);
			}
		}
}

int main(int argc, char *argv[])
{
	int *h_message;
	int *d_message;
	unsigned int size;
	int nb; // cuda grid size (# blocks per grid)

	const char * fname;

	if (argc < 4) 
		printf("Debes ingresar el nombre del archivo a desencriptar, el tamaño del bloque (1024 como máximo) y la parte del practico 2 a ejecutar: 1, 2 o 3.\n");
	else
		fname = argv[1];
		int parte = atoi(argv[3]); // cuda block size (# threads per block ) max 1024
		int n = atoi(argv[2]);

	if (parte < 1 || parte > 3) {
		printf("%d\n",parte);
		printf("Debe ingresar como segundo parámetro la parte del ejercicio a ejecutar: 1, 2 o 3.\n");
		exit(1);
	} else if (n > 1024) {
		printf("El tamaño máximo del bloque es 1024. La arquitectura actual a utilizar soporta un máximo de 1024 threads por bloque.\n");
		exit(1);
	}

	int length = get_text_length(fname);

	size = length * sizeof(int);

	// Parte a y c:
	if (parte == 1 || parte == 3) {
		nb = 16;
	// parte b:
	} else if (parte == 2) {
		if (length % n == 0 ){
			nb = length/n;
		}else
			nb = length / n + 1;
	}

	// reservar memoria para el mensaje
	h_message = (int *)malloc(size);

	// leo el archivo de la entrada
	read_file(fname, h_message);

	// reservo memoria en la GPU
	CUDA_CHK(hipMalloc((void**)&d_message, size));

	// copio los datos a la memoria de la GPU
	CUDA_CHK(hipMemcpy(d_message, h_message, size, hipMemcpyHostToDevice));

	// configuro la grilla de threads
	dim3 gridSize(nb,1);
	dim3 blockSize(n, 1, 1);

	// ejecuto el kernel
	decrypt_kernel <<< gridSize, blockSize >>> (d_message, length, parte);

	// Obtengo los posibles errores en la llamada al kernel
	CUDA_CHK(hipGetLastError());

	// Obligo al Kernel a llegar al final de su ejecucion y hacer obtener los posibles errores
	CUDA_CHK(hipDeviceSynchronize());



	// copio los datos nuevamente a la memoria de la CPU
	CUDA_CHK(hipMemcpy(h_message, d_message, size, hipMemcpyDeviceToHost));

	// despliego el mensaje

	// Si estoy en la primera parte despliego solamente la parte desencriptada.
	if (parte == 1)
		length = n;
	
	for (int i = 0; i < length; i++) {
		printf("%c", (char)h_message[i]);
	}
	printf("\n");

	// libero la memoria en la GPU
	hipFree(d_message);

	// libero la memoria en la CPU
	free(h_message);

	return 0;
}

	
int get_text_length(const char * fname)
{
	FILE *f = NULL;
	f = fopen(fname, "r"); //read and binary flags

	size_t pos = ftell(f);    
	fseek(f, 0, SEEK_END);    
	size_t length = ftell(f); 
	fseek(f, pos, SEEK_SET);  

	fclose(f);

	return length;
}

void read_file(const char * fname, int* input)
{
	// printf("leyendo archivo %s\n", fname );

	FILE *f = NULL;
	f = fopen(fname, "r"); //read and binary flags
	if (f == NULL){
		fprintf(stderr, "Error: Could not find %s file \n", fname);
		exit(1);
	}

	//fread(input, 1, N, f);
	int c; 
	while ((c = getc(f)) != EOF) {
		*(input++) = c;
		printf("%d",c);
	}
	

	fclose(f);
}

	.text
	.file	"decrypt.hip"
	.globl	_Z29__device_stub__decrypt_kernelPiii # -- Begin function _Z29__device_stub__decrypt_kernelPiii
	.type	_Z29__device_stub__decrypt_kernelPiii,@function
_Z29__device_stub__decrypt_kernelPiii:  # @_Z29__device_stub__decrypt_kernelPiii
	.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	$96, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rcx
	movl	%esi, (%rcx)
	movq	%rsp, %rsi
	movl	%edx, (%rsi)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z14decrypt_kernelPiii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$112, %rsp
	.cfi_adjust_cfa_offset -112
	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_end0:
	.size	_Z29__device_stub__decrypt_kernelPiii, .Lfunc_end0-_Z29__device_stub__decrypt_kernelPiii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function 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	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	cmpl	$3, %edi
	jg	.LBB1_2
# %bb.1:
	movl	$.Lstr, %edi
	callq	puts@PLT
                                        # implicit-def: $r13
	jmp	.LBB1_3
.LBB1_2:
	movq	8(%rbx), %r13
.LBB1_3:
	movq	24(%rbx), %rdi
	callq	atoi
	movl	%eax, %r15d
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %ebx
	leal	-4(%r15), %eax
	cmpl	$-4, %eax
	jbe	.LBB1_4
# %bb.5:
	cmpl	$1025, %ebx                     # imm = 0x401
	jge	.LBB1_6
# %bb.8:
	movabsq	$4294967296, %r12               # imm = 0x100000000
	movq	%r13, %rdi
	callq	_Z15get_text_lengthPKc
	movl	%eax, %r14d
	leal	(,%r14,4), %ecx
	movl	%r15d, %eax
	orl	$2, %eax
	cmpl	$3, %eax
	movq	%r15, 16(%rsp)                  # 8-byte Spill
	movl	%ebx, 4(%rsp)                   # 4-byte Spill
	jne	.LBB1_10
# %bb.9:
	movq	%r12, %rbp
	leaq	16(%r12), %rbx
	jmp	.LBB1_11
.LBB1_10:
	movl	%r14d, %eax
	cltd
	idivl	%ebx
	movl	%eax, %ebx
	cmpl	$1, %edx
	sbbl	$-1, %ebx
	movq	%r12, %rbp
	orq	%r12, %rbx
.LBB1_11:
	movl	%ecx, %r12d
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r15
	movq	%r13, %rdi
	movq	%rax, %rsi
	callq	_Z9read_filePKcPi
	leaq	8(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB1_12
# %bb.14:                               # %_Z9gpuAssert10hipError_tPKcib.exit
	movq	8(%rsp), %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB1_15
# %bb.16:                               # %_Z9gpuAssert10hipError_tPKcib.exit56
	movl	4(%rsp), %edx                   # 4-byte Reload
	orq	%rbp, %rdx
	movq	%rbx, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_18
# %bb.17:
	movq	8(%rsp), %rdi
	movl	%r14d, %esi
	movq	16(%rsp), %rdx                  # 8-byte Reload
                                        # kill: def $edx killed $edx killed $rdx
	callq	_Z29__device_stub__decrypt_kernelPiii
.LBB1_18:
	callq	hipGetLastError
	testl	%eax, %eax
	jne	.LBB1_19
# %bb.21:                               # %_Z9gpuAssert10hipError_tPKcib.exit58
	callq	hipDeviceSynchronize
	testl	%eax, %eax
	jne	.LBB1_22
# %bb.23:                               # %_Z9gpuAssert10hipError_tPKcib.exit60
	movq	8(%rsp), %rsi
	movq	%r15, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB1_24
# %bb.25:                               # %_Z9gpuAssert10hipError_tPKcib.exit62
	cmpl	$1, 16(%rsp)                    # 4-byte Folded Reload
	cmovel	4(%rsp), %r14d                  # 4-byte Folded Reload
	testl	%r14d, %r14d
	jle	.LBB1_28
# %bb.26:                               # %.lr.ph.preheader
	movl	%r14d, %ebx
	xorl	%r14d, %r14d
.LBB1_27:                               # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movsbl	(%r15,%r14,4), %edi
	callq	putchar@PLT
	incq	%r14
	cmpq	%r14, %rbx
	jne	.LBB1_27
.LBB1_28:                               # %._crit_edge
	movl	$10, %edi
	callq	putchar@PLT
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	%r15, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$24, %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
.LBB1_4:
	.cfi_def_cfa_offset 80
	movl	$.L.str.1, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.Lstr.2, %edi
	jmp	.LBB1_7
.LBB1_6:
	movl	$.Lstr.1, %edi
.LBB1_7:
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.LBB1_12:
	movl	%eax, %r13d
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.10, %esi
	movl	$.L.str.4, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$94, %r8d
	jmp	.LBB1_13
.LBB1_15:
	movl	%eax, %r13d
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.10, %esi
	movl	$.L.str.4, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$97, %r8d
.LBB1_13:
	xorl	%eax, %eax
	callq	fprintf
	movl	%r13d, %edi
	callq	exit
.LBB1_19:
	movl	%eax, %ebx
	movq	stderr(%rip), %r14
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.10, %esi
	movl	$.L.str.4, %ecx
	movq	%r14, %rdi
	movq	%rax, %rdx
	movl	$107, %r8d
	jmp	.LBB1_20
.LBB1_22:
	movl	%eax, %ebx
	movq	stderr(%rip), %r14
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.10, %esi
	movl	$.L.str.4, %ecx
	movq	%r14, %rdi
	movq	%rax, %rdx
	movl	$110, %r8d
	jmp	.LBB1_20
.LBB1_24:
	movl	%eax, %ebx
	movq	stderr(%rip), %r14
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.10, %esi
	movl	$.L.str.4, %ecx
	movq	%r14, %rdi
	movq	%rax, %rdx
	movl	$115, %r8d
.LBB1_20:
	xorl	%eax, %eax
	callq	fprintf
	movl	%ebx, %edi
	callq	exit
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z15get_text_lengthPKc          # -- Begin function _Z15get_text_lengthPKc
	.type	_Z15get_text_lengthPKc,@function
_Z15get_text_lengthPKc:                 # @_Z15get_text_lengthPKc
	.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
	movl	$.L.str.7, %esi
	callq	fopen
	movq	%rax, %rbx
	movq	%rax, %rdi
	callq	ftell
	movq	%rax, %r14
	movq	%rbx, %rdi
	xorl	%esi, %esi
	movl	$2, %edx
	callq	fseek
	movq	%rbx, %rdi
	callq	ftell
	movq	%rax, %r15
	movq	%rbx, %rdi
	movq	%r14, %rsi
	xorl	%edx, %edx
	callq	fseek
	movq	%rbx, %rdi
	callq	fclose
	movl	%r15d, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z15get_text_lengthPKc, .Lfunc_end2-_Z15get_text_lengthPKc
	.cfi_endproc
                                        # -- End function
	.globl	_Z9read_filePKcPi               # -- Begin function _Z9read_filePKcPi
	.type	_Z9read_filePKcPi,@function
_Z9read_filePKcPi:                      # @_Z9read_filePKcPi
	.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	%rsi, %rbx
	movq	%rdi, %r15
	movl	$.L.str.7, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB3_5
# %bb.1:                                # %.preheader
	movq	%rax, %r14
.LBB3_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	callq	getc
	cmpl	$-1, %eax
	je	.LBB3_4
# %bb.2:                                # %.lr.ph
                                        #   in Loop: Header=BB3_3 Depth=1
	movl	%eax, (%rbx)
	addq	$4, %rbx
	movl	$.L.str.9, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB3_3
.LBB3_4:                                # %._crit_edge
	movq	%r14, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.LBB3_5:
	.cfi_def_cfa_offset 32
	movq	stderr(%rip), %rdi
	movl	$.L.str.8, %esi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end3:
	.size	_Z9read_filePKcPi, .Lfunc_end3-_Z9read_filePKcPi
	.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	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z14decrypt_kernelPiii, %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_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__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	.LBB5_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
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z14decrypt_kernelPiii,@object  # @_Z14decrypt_kernelPiii
	.section	.rodata,"a",@progbits
	.globl	_Z14decrypt_kernelPiii
	.p2align	3, 0x0
_Z14decrypt_kernelPiii:
	.quad	_Z29__device_stub__decrypt_kernelPiii
	.size	_Z14decrypt_kernelPiii, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"%d\n"
	.size	.L.str.1, 4

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/maxibove13/GPGPU/master/practico_2/decrypt.hip"
	.size	.L.str.4, 109

	.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: Could not find %s file \n"
	.size	.L.str.8, 32

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

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"GPUassert: %s %s %d\n"
	.size	.L.str.10, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z14decrypt_kernelPiii"
	.size	.L__unnamed_1, 23

	.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	"Debes ingresar el nombre del archivo a desencriptar, el tama\303\261o del bloque (1024 como m\303\241ximo) y la parte del practico 2 a ejecutar: 1, 2 o 3."
	.size	.Lstr, 143

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"El tama\303\261o m\303\241ximo del bloque es 1024. La arquitectura actual a utilizar soporta un m\303\241ximo de 1024 threads por bloque."
	.size	.Lstr.1, 120

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Debe ingresar como segundo par\303\241metro la parte del ejercicio a ejecutar: 1, 2 o 3."
	.size	.Lstr.2, 83

	.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 _Z29__device_stub__decrypt_kernelPiii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z14decrypt_kernelPiii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z14decrypt_kernelPiii  ; -- Begin function _Z14decrypt_kernelPiii
	.globl	_Z14decrypt_kernelPiii
	.p2align	8
	.type	_Z14decrypt_kernelPiii,@function
_Z14decrypt_kernelPiii:                 ; @_Z14decrypt_kernelPiii
; %bb.0:
	s_load_b128 s[4:7], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	s_cmp_lt_i32 s7, 2
	s_cbranch_scc1 .LBB0_9
; %bb.1:                                ; %NodeBlock
	s_cmp_gt_i32 s7, 2
	s_mov_b32 s2, -1
	s_cbranch_scc0 .LBB0_7
; %bb.2:                                ; %LeafBlock27
	s_cmp_eq_u32 s7, 3
	s_cbranch_scc0 .LBB0_6
; %bb.3:                                ; %.preheader
	s_clause 0x1
	s_load_b32 s3, s[0:1], 0x1c
	s_load_b32 s2, s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	s_and_b32 s3, s3, 0xffff
	v_cvt_f32_u32_e32 v2, s2
	v_cvt_f32_u32_e32 v1, s3
	s_sub_i32 s9, 0, s3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_rcp_iflag_f32_e32 v2, v2
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_u32_f32_e32 v1, v1
	v_readfirstlane_b32 s8, v1
	v_mul_f32_e32 v1, 0x4f7ffffe, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_mul_i32 s9, s9, s8
	v_cvt_u32_f32_e32 v1, v1
	s_mul_hi_u32 s9, s8, s9
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_add_i32 s8, s8, s9
	v_readfirstlane_b32 s11, v1
	s_mul_hi_u32 s8, s6, s8
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_i32 s9, s8, s3
	s_sub_i32 s6, s6, s9
	s_add_i32 s9, s8, 1
	s_sub_i32 s10, s6, s3
	s_cmp_ge_u32 s6, s3
	s_cselect_b32 s8, s9, s8
	s_cselect_b32 s6, s10, s6
	s_add_i32 s9, s8, 1
	s_cmp_ge_u32 s6, s3
	s_cselect_b32 s6, s9, s8
	s_sub_i32 s8, 0, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_i32 s8, s8, s11
	s_mul_hi_u32 s8, s11, s8
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s11, s11, s8
	s_mul_hi_u32 s8, s6, s11
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_i32 s9, s8, s2
	s_sub_i32 s6, s6, s9
	s_add_i32 s9, s8, 1
	s_sub_i32 s10, s6, s2
	s_cmp_ge_u32 s6, s2
	s_cselect_b32 s8, s9, s8
	s_cselect_b32 s6, s10, s6
	s_add_i32 s9, s8, 1
	s_cmp_ge_u32 s6, s2
	s_cselect_b32 s6, s9, s8
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v3, s6, s6, 1
	s_and_b32 vcc_lo, exec_lo, s6
	s_mov_b32 s6, 0
	s_cbranch_vccnz .LBB0_6
; %bb.4:                                ; %.lr.ph
	v_mad_u64_u32 v[1:2], null, s15, s3, v[0:1]
	v_mov_b32_e32 v2, 0
	s_mul_i32 s2, s2, s3
	s_movk_i32 s3, 0xef
.LBB0_5:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[4:5], 2, v[1:2]
	s_add_i32 s6, s6, 1
	v_add_nc_u32_e32 v1, s2, v1
	v_add_co_u32 v4, vcc_lo, s4, v4
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v5, vcc_lo, s5, v5, vcc_lo
	v_cmp_ge_u32_e32 vcc_lo, s6, v3
	global_load_b32 v8, v[4:5], off
	s_and_b32 vcc_lo, exec_lo, vcc_lo
	s_waitcnt vmcnt(0)
	v_mad_u64_u32 v[6:7], null, v8, s3, 0xcb
	s_delay_alu instid0(VALU_DEP_1)
	v_and_b32_e32 v6, 0xff, v6
	global_store_b32 v[4:5], v6, off
	s_cbranch_vccz .LBB0_5
.LBB0_6:                                ; %Flow32
	s_mov_b32 s2, 0
.LBB0_7:                                ; %Flow34
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 vcc_lo, exec_lo, s2
	s_mov_b32 s2, 0
	s_cbranch_vccnz .LBB0_10
; %bb.8:
	s_load_b32 s0, s[0:1], 0x1c
	s_waitcnt lgkmcnt(0)
	s_and_b32 s0, s0, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mad_u64_u32 v[1:2], null, s15, s0, v[0:1]
	s_mov_b32 s0, -1
	s_branch .LBB0_11
.LBB0_9:
	s_mov_b32 s2, -1
.LBB0_10:                               ; %Flow33
	s_mov_b32 s0, 0
                                        ; implicit-def: $vgpr1
.LBB0_11:                               ; %Flow33
	s_and_b32 vcc_lo, exec_lo, s2
	s_cbranch_vccz .LBB0_15
; %bb.12:                               ; %LeafBlock
	s_cmp_eq_u32 s7, 1
	s_cbranch_scc0 .LBB0_14
; %bb.13:
	v_mov_b32_e32 v1, v0
	s_mov_b32 s0, -1
	s_branch .LBB0_15
.LBB0_14:
                                        ; implicit-def: $vgpr1
.LBB0_15:                               ; %Flow36
	s_and_b32 vcc_lo, exec_lo, s0
	s_cbranch_vccz .LBB0_17
; %bb.16:                               ; %.loopexit.sink.split
	v_mov_b32_e32 v2, 0
	s_movk_i32 s0, 0xef
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	v_add_co_u32 v0, vcc_lo, s4, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v1, vcc_lo
	global_load_b32 v4, v[0:1], off
	s_waitcnt vmcnt(0)
	v_mad_u64_u32 v[2:3], null, v4, s0, 0xcb
	v_and_b32_e32 v2, 0xff, v2
	global_store_b32 v[0:1], v2, off
.LBB0_17:                               ; %.loopexit
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z14decrypt_kernelPiii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.amdhsa_user_sgpr_count 15
		.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 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 9
		.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	_Z14decrypt_kernelPiii, .Lfunc_end0-_Z14decrypt_kernelPiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 604
; NumSgprs: 18
; NumVgprs: 9
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 9
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.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
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z14decrypt_kernelPiii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z14decrypt_kernelPiii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     9
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
