#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define THREADS_PER_BLOCK 1024
#define THRESHOLD 67108864

__global__ void encrypt(int n, char *m, char *k, char *c){
	int j, i =  blockIdx.x*blockDim.x + threadIdx.x;
	if (i < n){
		for(j = 1; j <= 100; j++){
			 c[i] = m[i] ^ k[i];
		}
	}
}

int main(){
	/* Serial on Host */
	FILE *fp_m, *fp_k;
	char *m, *k, *c, ch; /* Host copies */
	char *d_m, *d_k, *d_c; /* Device copies */
	int s_m, s_k, j,i;
	float msecPerencrypt, msecTotal = 0.0f;
	//printf("setup ...\n");
	//get size of files to malloc data
	if (!(fp_m = fopen("../../file.txt", "r")))
		perror("failed to read message file\n");
	
	while( fscanf(fp_m,"%c",&ch) != EOF ){
		s_m++;
	}

	if (!(fp_k = fopen("../../key.txt", "r")))
		perror("failed to read key\n");
	
	while( fscanf(fp_k,"%c",&ch) != EOF ){
		s_k++;
	}
	
	//printf("mallocs cpu...\n");

	//malloc space for m, k, c
	if ( !(m = (char *)malloc(sizeof(char)*s_m)) ){ 
		printf("Failed on malloc for m\n");
		exit(EXIT_FAILURE);
	}

	if ( !(k = (char *)malloc(sizeof(char)*s_k)) ){ 
		printf("Failed on malloc for k\n");
		exit(EXIT_FAILURE);
	}

	if ( !(c = (char *)malloc(sizeof(char)*s_m)) ){ 
		printf("Failed on malloc for c\n");
		exit(EXIT_FAILURE);
	}

	/* Alloc space for device copies of m, k, c */
	cudaError_t error;
	printf("mallocs gpu...\n");
	error = cudaMalloc((void **)&d_m, s_m);
	error = cudaMalloc((void **)&d_k, s_k);
	error = cudaMalloc((void **)&d_c, s_m);

	fseek(fp_m, 0, 0);
	fseek(fp_k, 0, 0);

	//read into buffers
	printf("read data...\n");
	for( j = 0; fscanf(fp_m,"%c",&ch) != EOF; j++ ){
		m[j] = ch;
	}
	for( j = 0; fscanf(fp_k,"%c",&ch) != EOF; j++ ){
		k[j] = ch;
	}

	/* Copy inputs to device */
	printf("Copy to device...\n");
 	cudaMemcpy(d_m, m, s_m, cudaMemcpyHostToDevice);
 	cudaMemcpy(d_k, k, s_k, cudaMemcpyHostToDevice);
	
	printf("Setting up streams...\n");
	int sections = s_m/THRESHOLD;
	int rem = s_m%THRESHOLD;
	cudaStream_t stream[sections];
	
	for(i = 0; i < sections; i++){
		cudaStreamCreate(&stream[i]);	
	}
	printf("moving on...\n");

	cudaDeviceSynchronize();

    	// Allocate CUDA events that we'll use for timing
    	cudaEvent_t start;
    	error = cudaEventCreate(&start);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to create start event (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	cudaEvent_t stop;
    	error = cudaEventCreate(&stop);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to create stop event (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	// Record the start event
    	error = cudaEventRecord(start, NULL);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to record start event (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

	/* Parallel on Device */
 	/* Launch encrypt() kernel on GPU with N blocks */
	for(i = 0; i < sections-1; i++ ){
 		encrypt<<<(s_m+THREADS_PER_BLOCK-1)/THREADS_PER_BLOCK,THREADS_PER_BLOCK>>>(s_m, d_m, d_k, d_c);
		cudaStreamSynchronize(stream[i]);
	}
 	
	// Record the stop event
    	error = cudaEventRecord(stop, NULL);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to record stop event (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	// Wait for the stop event to complete
    	error = cudaEventSynchronize(stop);

    	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to synchronize on the stop event (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	//float msecTotal = 0.0f;
  
	/* Copy result back to host */
 	error = cudaMemcpy(c, d_c, s_m, cudaMemcpyDeviceToHost);

	if (error != cudaSuccess)
    	{
        	printf("cudaMemcpy (c,d_c) returned error code %d, line(%d)\n", error, __LINE__);
        	exit(EXIT_FAILURE);
    	}

	error = cudaEventElapsedTime(&msecTotal, start, stop);

	if (error != cudaSuccess)
    	{
        	fprintf(stderr, "Failed to get time elapsed between events (error code %s)!\n", cudaGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

	msecPerencrypt = msecTotal / 1;
    	printf( "Performance= %.06f sec\n", msecPerencrypt/1000.0 );

	//validate for correctness
 	for (j = 0; j < s_m; j++){
		if( c[j] != (m[j]^k[j]) ){
			printf("WRONG! c[%d] != m[%d]^k[%d] ==> c='%c',m^k=%c\n", j,j,j,c[j],m[j]^k[j]);
			//exit(EXIT_FAILURE);
		}
	}

	// Compute and print the performance
    	//float msecPerencrypt = msecTotal / 1;
	
	/* Cleanup */
	/* Destroy streams */
	//for (j = 0; j < sections; j++){
    		//cudaStreamDestroy(stream[j]);
  	//}

 	free(m); free(k); free(c);
 	cudaFree(d_m); cudaFree(d_k); cudaFree(d_c);
 	fclose(fp_m); fclose(fp_k);

	return 0;
}

	.file	"tmpxft_002e6c11_00000000-6_enc2.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2043:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2043:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z31__device_stub__Z7encryptiPcS_S_iPcS_S_
	.type	_Z31__device_stub__Z7encryptiPcS_S_iPcS_S_, @function
_Z31__device_stub__Z7encryptiPcS_S_iPcS_S_:
.LFB2065:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movl	%edi, 28(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movq	%rcx, (%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	28(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%rsp, %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z7encryptiPcS_S_(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L2:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2065:
	.size	_Z31__device_stub__Z7encryptiPcS_S_iPcS_S_, .-_Z31__device_stub__Z7encryptiPcS_S_iPcS_S_
	.globl	_Z7encryptiPcS_S_
	.type	_Z7encryptiPcS_S_, @function
_Z7encryptiPcS_S_:
.LFB2066:
	.cfi_startproc
	endbr64
	jmp	_Z31__device_stub__Z7encryptiPcS_S_iPcS_S_
	.cfi_endproc
.LFE2066:
	.size	_Z7encryptiPcS_S_, .-_Z7encryptiPcS_S_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"r"
.LC2:
	.string	"../../file.txt"
.LC3:
	.string	"failed to read message file\n"
.LC4:
	.string	"%c"
.LC5:
	.string	"../../key.txt"
.LC6:
	.string	"failed to read key\n"
.LC7:
	.string	"Failed on malloc for m\n"
.LC8:
	.string	"Failed on malloc for k\n"
.LC9:
	.string	"Failed on malloc for c\n"
.LC10:
	.string	"mallocs gpu...\n"
.LC11:
	.string	"read data...\n"
.LC12:
	.string	"Copy to device...\n"
.LC13:
	.string	"Setting up streams...\n"
.LC14:
	.string	"moving on...\n"
.LC15:
	.string	"Failed to create start event (error code %s)!\n"
.LC16:
	.string	"Failed to create stop event (error code %s)!\n"
.LC17:
	.string	"Failed to record start event (error code %s)!\n"
.LC18:
	.string	"Failed to record stop event (error code %s)!\n"
.LC19:
	.string	"Failed to synchronize on the stop event (error code %s)!\n"
.LC20:
	.string	"cudaMemcpy (c,d_c) returned error code %d, line(%d)\n"
.LC21:
	.string	"Failed to get time elapsed between events (error code %s)!\n"
.LC23:
	.string	"Performance= %.06f sec\n"
.LC24:
	.string	"WRONG! c[%d] != m[%d]^k[%d] ==> c='%c',m^k=%c\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2040:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC2(%rip), %rdi
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	.cfi_offset 15, -24
	.cfi_offset 14, -32
	.cfi_offset 13, -40
	.cfi_offset 12, -48
	.cfi_offset 3, -56
	leaq	.LC1(%rip), %rbx
	movq	%rbx, %rsi
	subq	$136, %rsp
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	xorl	%eax, %eax
	movl	$0x00000000, -124(%rbp)
	call	fopen@PLT
	movq	%rax, -152(%rbp)
	testq	%rax, %rax
	jne	.L10
	leaq	.LC3(%rip), %rdi
	call	perror@PLT
.L10:
	movq	-152(%rbp), %rdi
	leaq	-125(%rbp), %r15
	xorl	%eax, %eax
	leaq	.LC4(%rip), %rsi
	movq	%r15, %rdx
	call	__isoc23_fscanf@PLT
	incl	%eax
	je	.L50
	incl	-140(%rbp)
	jmp	.L10
.L50:
	movq	%rbx, %rsi
	leaq	.LC5(%rip), %rdi
	call	fopen@PLT
	movq	%rax, -160(%rbp)
	testq	%rax, %rax
	jne	.L13
	leaq	.LC6(%rip), %rdi
	call	perror@PLT
.L13:
	movq	-160(%rbp), %rdi
	xorl	%eax, %eax
	movq	%r15, %rdx
	leaq	.LC4(%rip), %rsi
	call	__isoc23_fscanf@PLT
	incl	%eax
	je	.L51
	incl	%r13d
	jmp	.L13
.L51:
	movslq	-140(%rbp), %rax
	movq	%rax, %rdi
	movq	%rax, -136(%rbp)
	call	malloc@PLT
	leaq	.LC7(%rip), %rsi
	movq	%rax, %r12
	testq	%rax, %rax
	je	.L46
	movslq	%r13d, %r13
	movq	%r13, %rdi
	call	malloc@PLT
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L16
	leaq	.LC8(%rip), %rsi
.L46:
	movl	$2, %edi
	xorl	%eax, %eax
.L47:
	call	__printf_chk@PLT
.L48:
	movl	$1, %edi
	call	exit@PLT
.L16:
	movq	-136(%rbp), %rdi
	call	malloc@PLT
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	movq	%rax, -168(%rbp)
	testq	%rax, %rax
	je	.L47
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	movq	%r12, %r14
	call	__printf_chk@PLT
	movq	-136(%rbp), %rsi
	leaq	-120(%rbp), %rdi
	call	cudaMalloc@PLT
	leaq	-112(%rbp), %rdi
	movq	%r13, %rsi
	call	cudaMalloc@PLT
	movq	-136(%rbp), %rsi
	leaq	-104(%rbp), %rdi
	call	cudaMalloc@PLT
	movq	-152(%rbp), %rdi
	xorl	%edx, %edx
	xorl	%esi, %esi
	call	fseek@PLT
	movq	-160(%rbp), %rdi
	xorl	%edx, %edx
	xorl	%esi, %esi
	call	fseek@PLT
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L18:
	movq	-152(%rbp), %rdi
	xorl	%eax, %eax
	movq	%r15, %rdx
	leaq	.LC4(%rip), %rsi
	call	__isoc23_fscanf@PLT
	incl	%eax
	je	.L52
	movb	-125(%rbp), %al
	incq	%r14
	movb	%al, -1(%r14)
	jmp	.L18
.L52:
	movq	%rbx, %r14
.L20:
	movq	-160(%rbp), %rdi
	xorl	%eax, %eax
	movq	%r15, %rdx
	leaq	.LC4(%rip), %rsi
	call	__isoc23_fscanf@PLT
	incl	%eax
	je	.L53
	movb	-125(%rbp), %al
	incq	%r14
	movb	%al, -1(%r14)
	jmp	.L20
.L53:
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	-136(%rbp), %rdx
	movq	-120(%rbp), %rdi
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	-112(%rbp), %rdi
	movq	%r13, %rdx
	movq	%rbx, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	-140(%rbp), %eax
	movl	$67108864, %ecx
	cltd
	idivl	%ecx
	movq	%rsp, %rcx
	cltq
	leaq	15(,%rax,8), %rdx
	movq	%rax, %r15
	movq	%rdx, %rax
	andq	$-4096, %rdx
	andq	$-16, %rax
	subq	%rdx, %rcx
.L22:
	cmpq	%rcx, %rsp
	je	.L23
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L22
.L23:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L24
	orq	$0, -8(%rsp,%rax)
.L24:
	movq	%rsp, %r14
	xorl	%r13d, %r13d
	movq	%r14, %rdx
.L25:
	cmpl	%r15d, %r13d
	jge	.L54
	movq	%rdx, %rdi
	movq	%rdx, -176(%rbp)
	incl	%r13d
	call	cudaStreamCreate@PLT
	movq	-176(%rbp), %rdx
	addq	$8, %rdx
	jmp	.L25
.L54:
	leaq	.LC14(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	call	cudaDeviceSynchronize@PLT
	leaq	-96(%rbp), %rdi
	call	cudaEventCreate@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L27
	call	cudaGetErrorString@PLT
	leaq	.LC15(%rip), %rdx
	movq	%rax, %rcx
	jmp	.L49
.L27:
	leaq	-88(%rbp), %rdi
	call	cudaEventCreate@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L28
	call	cudaGetErrorString@PLT
	leaq	.LC16(%rip), %rdx
	movq	%rax, %rcx
	jmp	.L49
.L28:
	movq	-96(%rbp), %rdi
	xorl	%esi, %esi
	xorl	%r13d, %r13d
	call	cudaEventRecord@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L29
	call	cudaGetErrorString@PLT
	leaq	.LC17(%rip), %rdx
	movq	%rax, %rcx
	jmp	.L49
.L31:
	movl	$4194305, %eax
	movl	$1024, %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	salq	$10, %rax
	movl	$1, -60(%rbp)
	movq	%rax, -68(%rbp)
	movl	-140(%rbp), %eax
	addl	$1023, %eax
	cltd
	idivl	%ecx
	movl	$4194305, %edx
	movl	$1, %ecx
	salq	$10, %rdx
	movl	%eax, -80(%rbp)
	movabsq	$4294967297, %rax
	movq	%rax, -76(%rbp)
	movq	-80(%rbp), %rdi
	movl	-72(%rbp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L30
	movq	-104(%rbp), %rcx
	movq	-112(%rbp), %rdx
	movq	-120(%rbp), %rsi
	movl	-140(%rbp), %edi
	call	_Z31__device_stub__Z7encryptiPcS_S_iPcS_S_
.L30:
	movq	(%r14,%r13,8), %rdi
	incq	%r13
	call	cudaStreamSynchronize@PLT
.L29:
	leal	-1(%r15), %eax
	cmpl	%r13d, %eax
	jg	.L31
	movq	-88(%rbp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L32
	call	cudaGetErrorString@PLT
	leaq	.LC18(%rip), %rdx
	movq	%rax, %rcx
	jmp	.L49
.L32:
	movq	-88(%rbp), %rdi
	call	cudaEventSynchronize@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L33
	call	cudaGetErrorString@PLT
	leaq	.LC19(%rip), %rdx
	movq	%rax, %rcx
	jmp	.L49
.L33:
	movq	-136(%rbp), %rdx
	movq	-104(%rbp), %rsi
	movl	$2, %ecx
	movq	-168(%rbp), %rdi
	call	cudaMemcpy@PLT
	movl	%eax, %edx
	testl	%eax, %eax
	je	.L34
	movl	$154, %ecx
	leaq	.LC20(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L48
.L34:
	movq	-88(%rbp), %rdx
	movq	-96(%rbp), %rsi
	leaq	-124(%rbp), %rdi
	call	cudaEventElapsedTime@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L35
	call	cudaGetErrorString@PLT
	leaq	.LC21(%rip), %rdx
	movq	%rax, %rcx
.L49:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L48
.L35:
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	-124(%rbp), %xmm0
	leaq	.LC23(%rip), %rsi
	divsd	.LC22(%rip), %xmm0
	call	__printf_chk@PLT
	xorl	%r14d, %r14d
	leaq	.LC24(%rip), %r13
.L36:
	cmpl	%r14d, -140(%rbp)
	jle	.L55
	movq	-168(%rbp), %rax
	movsbl	(%rax,%r14), %r9d
	movb	(%r12,%r14), %al
	xorb	(%rbx,%r14), %al
	cmpb	%al, %r9b
	je	.L37
	movsbl	%al, %eax
	pushq	%rcx
	movq	%r13, %rsi
	movl	$2, %edi
	pushq	%rax
	movl	%r14d, %edx
	movl	%r14d, %r8d
	movl	%r14d, %ecx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	popq	%rsi
	popq	%rdi
.L37:
	incq	%r14
	jmp	.L36
.L55:
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	-168(%rbp), %rdi
	call	free@PLT
	movq	-120(%rbp), %rdi
	call	cudaFree@PLT
	movq	-112(%rbp), %rdi
	call	cudaFree@PLT
	movq	-104(%rbp), %rdi
	call	cudaFree@PLT
	movq	-152(%rbp), %rdi
	call	fclose@PLT
	movq	-160(%rbp), %rdi
	call	fclose@PLT
	movq	-56(%rbp), %rax
	subq	%fs:40, %rax
	je	.L39
	call	__stack_chk_fail@PLT
.L39:
	leaq	-40(%rbp), %rsp
	xorl	%eax, %eax
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE2040:
	.size	main, .-main
	.section	.rodata.str1.1
.LC25:
	.string	"_Z7encryptiPcS_S_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2068:
	.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	.LC25(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z7encryptiPcS_S_(%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
.LFE2068:
	.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
.LC22:
	.long	0
	.long	1083129856
	.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 <unistd.h>

#define THREADS_PER_BLOCK 1024
#define THRESHOLD 67108864

__global__ void encrypt(int n, char *m, char *k, char *c){
	int j, i =  blockIdx.x*blockDim.x + threadIdx.x;
	if (i < n){
		for(j = 1; j <= 100; j++){
			 c[i] = m[i] ^ k[i];
		}
	}
}

int main(){
	/* Serial on Host */
	FILE *fp_m, *fp_k;
	char *m, *k, *c, ch; /* Host copies */
	char *d_m, *d_k, *d_c; /* Device copies */
	int s_m, s_k, j,i;
	float msecPerencrypt, msecTotal = 0.0f;
	//printf("setup ...\n");
	//get size of files to malloc data
	if (!(fp_m = fopen("../../file.txt", "r")))
		perror("failed to read message file\n");
	
	while( fscanf(fp_m,"%c",&ch) != EOF ){
		s_m++;
	}

	if (!(fp_k = fopen("../../key.txt", "r")))
		perror("failed to read key\n");
	
	while( fscanf(fp_k,"%c",&ch) != EOF ){
		s_k++;
	}
	
	//printf("mallocs cpu...\n");

	//malloc space for m, k, c
	if ( !(m = (char *)malloc(sizeof(char)*s_m)) ){ 
		printf("Failed on malloc for m\n");
		exit(EXIT_FAILURE);
	}

	if ( !(k = (char *)malloc(sizeof(char)*s_k)) ){ 
		printf("Failed on malloc for k\n");
		exit(EXIT_FAILURE);
	}

	if ( !(c = (char *)malloc(sizeof(char)*s_m)) ){ 
		printf("Failed on malloc for c\n");
		exit(EXIT_FAILURE);
	}

	/* Alloc space for device copies of m, k, c */
	hipError_t error;
	printf("mallocs gpu...\n");
	error = hipMalloc((void **)&d_m, s_m);
	error = hipMalloc((void **)&d_k, s_k);
	error = hipMalloc((void **)&d_c, s_m);

	fseek(fp_m, 0, 0);
	fseek(fp_k, 0, 0);

	//read into buffers
	printf("read data...\n");
	for( j = 0; fscanf(fp_m,"%c",&ch) != EOF; j++ ){
		m[j] = ch;
	}
	for( j = 0; fscanf(fp_k,"%c",&ch) != EOF; j++ ){
		k[j] = ch;
	}

	/* Copy inputs to device */
	printf("Copy to device...\n");
 	hipMemcpy(d_m, m, s_m, hipMemcpyHostToDevice);
 	hipMemcpy(d_k, k, s_k, hipMemcpyHostToDevice);
	
	printf("Setting up streams...\n");
	int sections = s_m/THRESHOLD;
	int rem = s_m%THRESHOLD;
	hipStream_t stream[sections];
	
	for(i = 0; i < sections; i++){
		hipStreamCreate(&stream[i]);	
	}
	printf("moving on...\n");

	hipDeviceSynchronize();

    	// Allocate CUDA events that we'll use for timing
    	hipEvent_t start;
    	error = hipEventCreate(&start);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to create start event (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	hipEvent_t stop;
    	error = hipEventCreate(&stop);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to create stop event (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	// Record the start event
    	error = hipEventRecord(start, NULL);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to record start event (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

	/* Parallel on Device */
 	/* Launch encrypt() kernel on GPU with N blocks */
	for(i = 0; i < sections-1; i++ ){
 		encrypt<<<(s_m+THREADS_PER_BLOCK-1)/THREADS_PER_BLOCK,THREADS_PER_BLOCK>>>(s_m, d_m, d_k, d_c);
		hipStreamSynchronize(stream[i]);
	}
 	
	// Record the stop event
    	error = hipEventRecord(stop, NULL);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to record stop event (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	// Wait for the stop event to complete
    	error = hipEventSynchronize(stop);

    	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to synchronize on the stop event (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

    	//float msecTotal = 0.0f;
  
	/* Copy result back to host */
 	error = hipMemcpy(c, d_c, s_m, hipMemcpyDeviceToHost);

	if (error != hipSuccess)
    	{
        	printf("hipMemcpy (c,d_c) returned error code %d, line(%d)\n", error, __LINE__);
        	exit(EXIT_FAILURE);
    	}

	error = hipEventElapsedTime(&msecTotal, start, stop);

	if (error != hipSuccess)
    	{
        	fprintf(stderr, "Failed to get time elapsed between events (error code %s)!\n", hipGetErrorString(error));
        	exit(EXIT_FAILURE);
    	}

	msecPerencrypt = msecTotal / 1;
    	printf( "Performance= %.06f sec\n", msecPerencrypt/1000.0 );

	//validate for correctness
 	for (j = 0; j < s_m; j++){
		if( c[j] != (m[j]^k[j]) ){
			printf("WRONG! c[%d] != m[%d]^k[%d] ==> c='%c',m^k=%c\n", j,j,j,c[j],m[j]^k[j]);
			//exit(EXIT_FAILURE);
		}
	}

	// Compute and print the performance
    	//float msecPerencrypt = msecTotal / 1;
	
	/* Cleanup */
	/* Destroy streams */
	//for (j = 0; j < sections; j++){
    		//cudaStreamDestroy(stream[j]);
  	//}

 	free(m); free(k); free(c);
 	hipFree(d_m); hipFree(d_k); hipFree(d_c);
 	fclose(fp_m); fclose(fp_k);

	return 0;
}

	.text
	.file	"enc2.hip"
	.globl	_Z22__device_stub__encryptiPcS_S_ # -- Begin function _Z22__device_stub__encryptiPcS_S_
	.type	_Z22__device_stub__encryptiPcS_S_,@function
_Z22__device_stub__encryptiPcS_S_:      # @_Z22__device_stub__encryptiPcS_S_
	.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	4(%rsp), %rax
	movl	%edi, (%rax)
	leaq	40(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	32(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	24(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%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	$_Z7encryptiPcS_S_, %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_end0:
	.size	_Z22__device_stub__encryptiPcS_S_, .Lfunc_end0-_Z22__device_stub__encryptiPcS_S_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x408f400000000000              # double 1000
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$104, %rsp
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	movl	$0, -48(%rbp)
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %rbx
	testq	%rax, %rax
	je	.LBB1_36
.LBB1_1:                                # %.preheader160
	movq	$-1, %r15
	leaq	-41(%rbp), %r14
                                        # implicit-def: $r12d
.LBB1_2:                                # =>This Inner Loop Header: Depth=1
	movl	$.L.str.3, %esi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r15
	incl	%r12d
	cmpl	$-1, %eax
	jne	.LBB1_2
# %bb.3:
	movq	%rbx, -120(%rbp)                # 8-byte Spill
	movl	$.L.str.4, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r14
	testq	%rax, %rax
	je	.LBB1_37
.LBB1_4:                                # %.preheader159
	movq	$-1, %r13
	leaq	-41(%rbp), %rbx
.LBB1_5:                                # =>This Inner Loop Header: Depth=1
	movl	$.L.str.3, %esi
	movq	%r14, %rdi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r13
	cmpl	$-1, %eax
	jne	.LBB1_5
# %bb.6:
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, -64(%rbp)                 # 8-byte Spill
	testq	%rax, %rax
	je	.LBB1_38
# %bb.7:
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, -56(%rbp)                 # 8-byte Spill
	testq	%rax, %rax
	movq	-120(%rbp), %rbx                # 8-byte Reload
	je	.LBB1_39
# %bb.8:
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, -72(%rbp)                 # 8-byte Spill
	testq	%rax, %rax
	je	.LBB1_40
# %bb.9:
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	leaq	-104(%rbp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	leaq	-96(%rbp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	leaq	-88(%rbp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	%rbx, %rdi
	xorl	%esi, %esi
	xorl	%edx, %edx
	callq	fseek
	movq	%r14, -112(%rbp)                # 8-byte Spill
	movq	%r14, %rdi
	xorl	%esi, %esi
	xorl	%edx, %edx
	callq	fseek
	movl	$.Lstr.4, %edi
	callq	puts@PLT
	leaq	-41(%rbp), %rdx
	movl	$.L.str.3, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$-1, %eax
	je	.LBB1_12
# %bb.10:
	movq	-64(%rbp), %r14                 # 8-byte Reload
.LBB1_11:                               # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movb	-41(%rbp), %al
	movb	%al, (%r14)
	movl	$.L.str.3, %esi
	movq	%rbx, %rdi
	leaq	-41(%rbp), %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r14
	cmpl	$-1, %eax
	jne	.LBB1_11
.LBB1_12:                               # %.preheader124
	leaq	-41(%rbp), %rdx
	movl	$.L.str.3, %esi
	movq	-112(%rbp), %rbx                # 8-byte Reload
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$-1, %eax
	je	.LBB1_15
# %bb.13:
	movq	-56(%rbp), %r14                 # 8-byte Reload
.LBB1_14:                               # %.lr.ph129
                                        # =>This Inner Loop Header: Depth=1
	movb	-41(%rbp), %al
	movb	%al, (%r14)
	movl	$.L.str.3, %esi
	movq	%rbx, %rdi
	leaq	-41(%rbp), %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r14
	cmpl	$-1, %eax
	jne	.LBB1_14
.LBB1_15:                               # %._crit_edge
	movl	$.Lstr.5, %edi
	callq	puts@PLT
	movq	-104(%rbp), %rdi
	movq	-64(%rbp), %rsi                 # 8-byte Reload
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	-96(%rbp), %rdi
	movq	-56(%rbp), %rsi                 # 8-byte Reload
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$.Lstr.6, %edi
	callq	puts@PLT
	leal	67108863(%r15), %r13d
	testl	%r15d, %r15d
	cmovnsl	%r15d, %r13d
	movq	%rsp, -144(%rbp)                # 8-byte Spill
	sarl	$26, %r13d
	leaq	15(,%r13,8), %rax
	andq	$-16, %rax
	movq	%rsp, %rcx
	subq	%rax, %rcx
	movq	%rcx, -128(%rbp)                # 8-byte Spill
	movq	%rcx, %rsp
	cmpl	$67108864, %r15d                # imm = 0x4000000
	jl	.LBB1_18
# %bb.16:                               # %.lr.ph132.preheader
	movq	%r13, %r14
	movq	-128(%rbp), %rbx                # 8-byte Reload
.LBB1_17:                               # %.lr.ph132
                                        # =>This Inner Loop Header: Depth=1
	movq	%rbx, %rdi
	callq	hipStreamCreate
	addq	$8, %rbx
	decq	%r14
	jne	.LBB1_17
.LBB1_18:                               # %._crit_edge133
	movl	$.Lstr.7, %edi
	callq	puts@PLT
	callq	hipDeviceSynchronize
	leaq	-136(%rbp), %rdi
	callq	hipEventCreate
	testl	%eax, %eax
	jne	.LBB1_43
# %bb.19:
	leaq	-80(%rbp), %rdi
	callq	hipEventCreate
	testl	%eax, %eax
	jne	.LBB1_44
# %bb.20:
	movq	-136(%rbp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%eax, %eax
	jne	.LBB1_45
# %bb.21:                               # %.preheader
	cmpl	$134217728, %r15d               # imm = 0x8000000
	jl	.LBB1_26
# %bb.22:                               # %.lr.ph135
	shrl	$10, %r12d
	btsq	$32, %r12
	cmpl	$3, %r13d
	movl	$2, %ebx
	cmovgel	%r13d, %ebx
	movabsq	$4294967296, %r13               # imm = 0x100000000
	decl	%ebx
	xorl	%r14d, %r14d
	addq	$1024, %r13                     # imm = 0x400
.LBB1_23:                               # =>This Inner Loop Header: Depth=1
	movq	%r12, %rdi
	movl	$1, %esi
	movq	%r13, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_25
# %bb.24:                               #   in Loop: Header=BB1_23 Depth=1
	movq	-104(%rbp), %rsi
	movq	-96(%rbp), %rdx
	movq	-88(%rbp), %rcx
	movl	%r15d, %edi
	callq	_Z22__device_stub__encryptiPcS_S_
.LBB1_25:                               #   in Loop: Header=BB1_23 Depth=1
	movq	-128(%rbp), %rax                # 8-byte Reload
	movq	(%rax,%r14,8), %rdi
	callq	hipStreamSynchronize
	incq	%r14
	cmpq	%r14, %rbx
	jne	.LBB1_23
.LBB1_26:                               # %._crit_edge136
	movq	-80(%rbp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	testl	%eax, %eax
	jne	.LBB1_46
# %bb.27:
	movq	-80(%rbp), %rdi
	callq	hipEventSynchronize
	testl	%eax, %eax
	movq	-112(%rbp), %r14                # 8-byte Reload
	movq	-64(%rbp), %r12                 # 8-byte Reload
	movq	-56(%rbp), %r13                 # 8-byte Reload
	jne	.LBB1_47
# %bb.28:
	movq	-88(%rbp), %rsi
	movq	-72(%rbp), %rdi                 # 8-byte Reload
	movq	%r15, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB1_48
# %bb.29:
	movq	-136(%rbp), %rsi
	movq	-80(%rbp), %rdx
	leaq	-48(%rbp), %rdi
	callq	hipEventElapsedTime
	testl	%eax, %eax
	jne	.LBB1_49
# %bb.30:
	cvtss2sd	-48(%rbp), %xmm0
	divsd	.LCPI1_0(%rip), %xmm0
	movl	$.L.str.21, %edi
	movb	$1, %al
	callq	printf
	testl	%r15d, %r15d
	jle	.LBB1_35
# %bb.31:                               # %.lr.ph139.preheader
	movl	%r15d, %ebx
	xorl	%r15d, %r15d
.LBB1_32:                               # %.lr.ph139
                                        # =>This Inner Loop Header: Depth=1
	movq	-72(%rbp), %rax                 # 8-byte Reload
	movsbl	(%rax,%r15), %r8d
	movb	(%r13,%r15), %al
	xorb	(%r12,%r15), %al
	cmpb	%al, %r8b
	je	.LBB1_34
# %bb.33:                               #   in Loop: Header=BB1_32 Depth=1
	movsbl	%al, %r9d
	movl	$.L.str.22, %edi
	movl	%r15d, %esi
	movl	%r15d, %edx
	movl	%r15d, %ecx
	xorl	%eax, %eax
	callq	printf
.LBB1_34:                               #   in Loop: Header=BB1_32 Depth=1
	incq	%r15
	cmpq	%r15, %rbx
	jne	.LBB1_32
.LBB1_35:                               # %._crit_edge140
	movq	%r12, %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
	movq	-72(%rbp), %rdi                 # 8-byte Reload
	callq	free
	movq	-104(%rbp), %rdi
	callq	hipFree
	movq	-96(%rbp), %rdi
	callq	hipFree
	movq	-88(%rbp), %rdi
	callq	hipFree
	movq	-120(%rbp), %rdi                # 8-byte Reload
	callq	fclose
	movq	%r14, %rdi
	callq	fclose
	movq	-144(%rbp), %rsp                # 8-byte Reload
	xorl	%eax, %eax
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa %rsp, 8
	retq
.LBB1_36:
	.cfi_def_cfa %rbp, 16
	movl	$.L.str.2, %edi
	callq	perror
	jmp	.LBB1_1
.LBB1_37:
	movl	$.L.str.5, %edi
	callq	perror
	jmp	.LBB1_4
.LBB1_38:
	movl	$.Lstr, %edi
	jmp	.LBB1_41
.LBB1_39:
	movl	$.Lstr.1, %edi
	jmp	.LBB1_41
.LBB1_40:
	movl	$.Lstr.2, %edi
.LBB1_41:
	callq	puts@PLT
	jmp	.LBB1_42
.LBB1_43:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.14, %esi
	jmp	.LBB1_50
.LBB1_44:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.15, %esi
	jmp	.LBB1_50
.LBB1_45:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.16, %esi
	jmp	.LBB1_50
.LBB1_46:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.17, %esi
	jmp	.LBB1_50
.LBB1_47:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	jmp	.LBB1_50
.LBB1_48:
	movl	$.L.str.19, %edi
	movl	%eax, %esi
	movl	$156, %edx
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB1_42
.LBB1_49:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.20, %esi
.LBB1_50:
	movq	%rbx, %rdi
	movq	%rax, %rdx
	xorl	%eax, %eax
	callq	fprintf
.LBB1_42:
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	main, .Lfunc_end1-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	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z7encryptiPcS_S_, %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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z7encryptiPcS_S_,@object       # @_Z7encryptiPcS_S_
	.section	.rodata,"a",@progbits
	.globl	_Z7encryptiPcS_S_
	.p2align	3, 0x0
_Z7encryptiPcS_S_:
	.quad	_Z22__device_stub__encryptiPcS_S_
	.size	_Z7encryptiPcS_S_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"../../file.txt"
	.size	.L.str, 15

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

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"failed to read message file\n"
	.size	.L.str.2, 29

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"../../key.txt"
	.size	.L.str.4, 14

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"failed to read key\n"
	.size	.L.str.5, 20

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"Failed to create start event (error code %s)!\n"
	.size	.L.str.14, 47

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"Failed to create stop event (error code %s)!\n"
	.size	.L.str.15, 46

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"Failed to record start event (error code %s)!\n"
	.size	.L.str.16, 47

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"Failed to record stop event (error code %s)!\n"
	.size	.L.str.17, 46

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"Failed to synchronize on the stop event (error code %s)!\n"
	.size	.L.str.18, 58

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"hipMemcpy (c,d_c) returned error code %d, line(%d)\n"
	.size	.L.str.19, 52

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"Failed to get time elapsed between events (error code %s)!\n"
	.size	.L.str.20, 60

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	"Performance= %.06f sec\n"
	.size	.L.str.21, 24

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"WRONG! c[%d] != m[%d]^k[%d] ==> c='%c',m^k=%c\n"
	.size	.L.str.22, 47

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z7encryptiPcS_S_"
	.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	"Failed on malloc for m"
	.size	.Lstr, 23

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Failed on malloc for k"
	.size	.Lstr.1, 23

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Failed on malloc for c"
	.size	.Lstr.2, 23

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"mallocs gpu..."
	.size	.Lstr.3, 15

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"read data..."
	.size	.Lstr.4, 13

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"Copy to device..."
	.size	.Lstr.5, 18

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"Setting up streams..."
	.size	.Lstr.6, 22

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"moving on..."
	.size	.Lstr.7, 13

	.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 _Z22__device_stub__encryptiPcS_S_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z7encryptiPcS_S_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z7encryptiPcS_S_       ; -- Begin function _Z7encryptiPcS_S_
	.globl	_Z7encryptiPcS_S_
	.p2align	8
	.type	_Z7encryptiPcS_S_,@function
_Z7encryptiPcS_S_:                      ; @_Z7encryptiPcS_S_
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x2c
	s_load_b32 s3, s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[4:5], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_i32_e64 s3, v4
	s_cbranch_execz .LBB0_3
; %bb.1:                                ; %.preheader
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x8
	s_load_b64 s[0:1], s[0:1], 0x18
	v_ashrrev_i32_e32 v5, 31, v4
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s4, v4
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v5, vcc_lo
	v_add_co_u32 v2, vcc_lo, s6, v4
	v_add_co_ci_u32_e32 v3, vcc_lo, s7, v5, vcc_lo
	v_add_co_u32 v4, vcc_lo, s0, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s1, v5, vcc_lo
	s_movk_i32 s0, 0x64
.LBB0_2:                                ; =>This Inner Loop Header: Depth=1
	global_load_u8 v6, v[0:1], off
	global_load_u8 v7, v[2:3], off
	s_add_i32 s0, s0, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lg_u32 s0, 0
	s_waitcnt vmcnt(0)
	v_xor_b32_e32 v6, v7, v6
	global_store_b8 v[4:5], v6, off
	s_cbranch_scc1 .LBB0_2
.LBB0_3:                                ; %.loopexit
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z7encryptiPcS_S_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.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 8
		.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	_Z7encryptiPcS_S_, .Lfunc_end0-_Z7encryptiPcS_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 192
; NumSgprs: 18
; NumVgprs: 8
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 8
; 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:
      - .offset:         0
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z7encryptiPcS_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z7encryptiPcS_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     8
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
