#include <stdio.h>
#include <string.h>

#define THREADS_PER_BLOCK 32

__global__ void minA_cuda(int* a, int* b, int len, int n_output) {
	int b_index = threadIdx.x + blockIdx.x * THREADS_PER_BLOCK;
    int a_index = b_index * 2;
    
    if (b_index < n_output && a_index < len) {
	    if (a_index == len-1) {
	    	b[b_index] = a[a_index];
	    }
	    else {
		    int v1 = a[a_index];
		    int v2 = a[a_index + 1];
		    if (v2 < v1) {
		    	b[b_index] = v2;
		    }
		    else {
		    	b[b_index] = v1;
		    }
		}
	}
}

__global__ void last_digit_cuda(int* a, int* b, int len) {
	int index = threadIdx.x + blockIdx.x * THREADS_PER_BLOCK;
	if (index < len) 
		b[index] = a[index] % 10;
}

void last_digit() {
	//Get input data from files
    char buff[50000];
    int inp[10000];
    buff[0] = ' ';
    char* token;
    FILE* fp = fopen("inp.txt", "r");
    fgets(buff+1,sizeof(buff), fp);
    token = strtok(buff, ",");
    int len = 0; 
    while(token != NULL) {
		inp[len] = atoi(token+1);
		len++;
		token = strtok(NULL, ",");
    }

    int* A = (int* )malloc(sizeof(int) * len);
    int* B = (int* )malloc(sizeof(int) * len);

    for (int i = 0; i < len; i++) {
    	A[i] = inp[i];
    }

    //Cuda stuff
    int *d_a, *d_b;
    cudaMalloc(&d_a, sizeof(int) * len);
    cudaMalloc(&d_b, sizeof(int) * len);
    cudaMemcpy(d_a, A, sizeof(int) * len, cudaMemcpyHostToDevice);

    last_digit_cuda<<<(len + THREADS_PER_BLOCK)/THREADS_PER_BLOCK, THREADS_PER_BLOCK>>>(d_a, d_b, len);
    cudaDeviceSynchronize();
    cudaMemcpy(B, d_b, sizeof(int) * len, cudaMemcpyDeviceToHost);

    //put results in file
	fclose(fp);
	FILE* fp_end = fopen("q1b.txt", "w");
	for (int i = 0; i < len; i++) {
		fprintf(fp_end, "%d", B[i]);
		if (i != len-1) {
			fprintf(fp_end, "%s", ", ");
		}
	}

	//Free up memory
	cudaFree(d_a);
	cudaFree(d_b);
	free(A);
	free(B);
	fclose(fp_end);
}

void minA() {
	//Get input from files
    char buff[50000];
    int inp[10000];
    buff[0] = ' ';
    char* token;
    FILE* fp = fopen("inp.txt", "r");
    fgets(buff+1,sizeof(buff), fp);
    token = strtok(buff, ",");
    int len = 0; 
    while(token != NULL) {
		inp[len] = atoi(token+1);
		len++;
		token = strtok(NULL, ",");
    }

    //Copy input to array of proper size
    int* A = (int* )malloc(sizeof(int) * len);
    for (int i = 0; i < len; i++) {
    	A[i] = inp[i];
    }

    //Cuda stuff
    int B_size = (len + 1) / 2;
    int* B;

    while (len != 1) {
    	B = (int* )malloc(sizeof(int) * B_size);

		int *d_a, *d_b;
	    cudaMalloc(&d_a, sizeof(int) * len);
	    cudaMalloc(&d_b, sizeof(int) * B_size);
	    cudaMemcpy(d_a, A, sizeof(int) * len, cudaMemcpyHostToDevice);

	    minA_cuda<<<(B_size + THREADS_PER_BLOCK)/THREADS_PER_BLOCK, THREADS_PER_BLOCK>>>(d_a, d_b, len, B_size);
	    cudaDeviceSynchronize();
	    cudaMemcpy(B, d_b, sizeof(int) * B_size, cudaMemcpyDeviceToHost);

	    cudaFree(d_a);
	    cudaFree(d_b);
	    memcpy(A, B, B_size * sizeof(int));
	    free(B);
	    len = B_size;
	    B_size = (len + 1) / 2;
    }

    //Print output to file
    fclose(fp);
	FILE* fp_end = fopen("q1a.txt", "w");
	fprintf(fp_end, "%d", A[0]);
	fclose(fp_end);
}

int main(int argc,char **argv)
{
    minA();
    cudaDeviceReset();
    last_digit();
    return 0;
}

	.file	"tmpxft_002033f3_00000000-6_q1.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z32__device_stub__Z9minA_cudaPiS_iiPiS_ii
	.type	_Z32__device_stub__Z9minA_cudaPiS_iiPiS_ii, @function
_Z32__device_stub__Z9minA_cudaPiS_iiPiS_ii:
.LFB2054:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%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	_Z9minA_cudaPiS_ii(%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
.LFE2054:
	.size	_Z32__device_stub__Z9minA_cudaPiS_iiPiS_ii, .-_Z32__device_stub__Z9minA_cudaPiS_iiPiS_ii
	.globl	_Z9minA_cudaPiS_ii
	.type	_Z9minA_cudaPiS_ii, @function
_Z9minA_cudaPiS_ii:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_Z32__device_stub__Z9minA_cudaPiS_iiPiS_ii
	.cfi_endproc
.LFE2055:
	.size	_Z9minA_cudaPiS_ii, .-_Z9minA_cudaPiS_ii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
.LC1:
	.string	"inp.txt"
.LC2:
	.string	","
.LC3:
	.string	"w"
.LC4:
	.string	"q1a.txt"
.LC5:
	.string	"%d"
	.text
	.globl	_Z4minAv
	.type	_Z4minAv, @function
_Z4minAv:
.LFB2028:
	.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
	leaq	-86016(%rsp), %r11
	.cfi_def_cfa 11, 86072
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$4072, %rsp
	.cfi_def_cfa_offset 90144
	leaq	.LC0(%rip), %rsi
	leaq	.LC1(%rip), %rdi
	leaq	.LC2(%rip), %rbp
	movq	%fs:40, %rax
	movq	%rax, 90072(%rsp)
	xorl	%eax, %eax
	movb	$32, 40072(%rsp)
	leaq	40072(%rsp), %rbx
	call	fopen@PLT
	movl	$50000, %edx
	movl	$49999, %esi
	leaq	40073(%rsp), %rdi
	movq	%rax, %rcx
	movq	%rax, %r14
	call	__fgets_chk@PLT
	movq	%rbx, %rdi
	movq	%rbp, %rsi
	xorl	%ebx, %ebx
	call	strtok@PLT
.L9:
	testq	%rax, %rax
	je	.L18
	leaq	1(%rax), %rdi
	call	atoi@PLT
	movq	%rbp, %rsi
	xorl	%edi, %edi
	movl	%eax, 72(%rsp,%rbx,4)
	incq	%rbx
	call	strtok@PLT
	jmp	.L9
.L18:
	movslq	%ebx, %rax
	movl	%ebx, %r12d
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 16(%rsp)
	call	malloc@PLT
	movq	%rax, %rbp
	xorl	%eax, %eax
.L11:
	cmpl	%eax, %r12d
	jle	.L19
	movl	72(%rsp,%rax,4), %edx
	movl	%edx, 0(%rbp,%rax,4)
	incq	%rax
	jmp	.L11
.L19:
	movabsq	$4294967328, %r15
	incl	%ebx
	sarl	%ebx
.L13:
	cmpl	$1, %r12d
	je	.L20
	movslq	%ebx, %rax
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movslq	%r12d, %rdx
	leaq	32(%rsp), %rdi
	salq	$2, %rdx
	movq	%rax, %r13
	movq	%rdx, %rsi
	movq	%rdx, 24(%rsp)
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	leaq	40(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	24(%rsp), %rdx
	movq	32(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leal	32(%rbx), %eax
	movl	$32, %ecx
	xorl	%r9d, %r9d
	cltd
	xorl	%r8d, %r8d
	movq	%r15, 60(%rsp)
	idivl	%ecx
	movl	$1, 68(%rsp)
	movq	%r15, %rdx
	movl	$1, %ecx
	movl	%eax, 48(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 52(%rsp)
	movq	48(%rsp), %rdi
	movl	56(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L14
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	movl	%ebx, %ecx
	movl	%r12d, %edx
	call	_Z32__device_stub__Z9minA_cudaPiS_iiPiS_ii
.L14:
	call	cudaDeviceSynchronize@PLT
	movq	8(%rsp), %rdx
	movq	%r13, %rdi
	movl	%ebx, %r12d
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rcx
	movq	%r13, %rsi
	movq	%rbp, %rdi
	movq	8(%rsp), %rdx
	call	__memcpy_chk@PLT
	movq	%r13, %rdi
	call	free@PLT
	leal	1(%rbx), %eax
	movl	$2, %ecx
	cltd
	idivl	%ecx
	movl	%eax, %ebx
	jmp	.L13
.L20:
	movq	%r14, %rdi
	call	fclose@PLT
	leaq	.LC3(%rip), %rsi
	leaq	.LC4(%rip), %rdi
	call	fopen@PLT
	movl	0(%rbp), %ecx
	movl	$2, %esi
	leaq	.LC5(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	90072(%rsp), %rax
	subq	%fs:40, %rax
	je	.L16
	call	__stack_chk_fail@PLT
.L16:
	addq	$90088, %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
.LFE2028:
	.size	_Z4minAv, .-_Z4minAv
	.globl	_Z38__device_stub__Z15last_digit_cudaPiS_iPiS_i
	.type	_Z38__device_stub__Z15last_digit_cudaPiS_iPiS_i, @function
_Z38__device_stub__Z15last_digit_cudaPiS_iPiS_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	.L21
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z15last_digit_cudaPiS_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
.L21:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z38__device_stub__Z15last_digit_cudaPiS_iPiS_i, .-_Z38__device_stub__Z15last_digit_cudaPiS_iPiS_i
	.globl	_Z15last_digit_cudaPiS_i
	.type	_Z15last_digit_cudaPiS_i, @function
_Z15last_digit_cudaPiS_i:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z38__device_stub__Z15last_digit_cudaPiS_iPiS_i
	.cfi_endproc
.LFE2057:
	.size	_Z15last_digit_cudaPiS_i, .-_Z15last_digit_cudaPiS_i
	.section	.rodata.str1.1
.LC6:
	.string	"q1b.txt"
.LC7:
	.string	", "
.LC8:
	.string	"%s"
	.text
	.globl	_Z10last_digitv
	.type	_Z10last_digitv, @function
_Z10last_digitv:
.LFB2027:
	.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
	leaq	-86016(%rsp), %r11
	.cfi_def_cfa 11, 86072
.LPSRL1:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL1
	.cfi_def_cfa_register 7
	subq	$4040, %rsp
	.cfi_def_cfa_offset 90112
	leaq	.LC0(%rip), %rsi
	leaq	.LC1(%rip), %rdi
	leaq	.LC2(%rip), %r12
	movq	%fs:40, %rax
	movq	%rax, 90040(%rsp)
	xorl	%eax, %eax
	movb	$32, 40040(%rsp)
	leaq	40040(%rsp), %rbx
	call	fopen@PLT
	movl	$50000, %edx
	movl	$49999, %esi
	leaq	40041(%rsp), %rdi
	movq	%rax, %rcx
	movq	%rax, %rbp
	call	__fgets_chk@PLT
	movq	%rbx, %rdi
	movq	%r12, %rsi
	xorl	%ebx, %ebx
	call	strtok@PLT
.L27:
	testq	%rax, %rax
	je	.L37
	leaq	1(%rax), %rdi
	call	atoi@PLT
	movq	%r12, %rsi
	xorl	%edi, %edi
	movl	%eax, 40(%rsp,%rbx,4)
	incq	%rbx
	call	strtok@PLT
	jmp	.L27
.L37:
	movl	%ebx, %r12d
	movslq	%ebx, %rbx
	salq	$2, %rbx
	movq	%rbx, %rdi
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r14
	call	malloc@PLT
	movq	%rax, %r13
	xorl	%eax, %eax
.L29:
	cmpl	%eax, %r12d
	jle	.L38
	movl	40(%rsp,%rax,4), %edx
	movl	%edx, (%r14,%rax,4)
	incq	%rax
	jmp	.L29
.L38:
	movq	%rsp, %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	8(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r14, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leal	32(%r12), %eax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	sarl	$5, %eax
	movl	$134217729, %edx
	movl	$1, %ecx
	movl	$1, 36(%rsp)
	movl	%eax, 16(%rsp)
	salq	$5, %rdx
	movabsq	$4294967297, %rax
	movq	%rax, 20(%rsp)
	movq	16(%rsp), %rdi
	movl	24(%rsp), %esi
	movq	%rdx, 28(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L31
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdi
	movl	%r12d, %edx
	call	_Z38__device_stub__Z15last_digit_cudaPiS_iPiS_i
.L31:
	call	cudaDeviceSynchronize@PLT
	movq	8(%rsp), %rsi
	movq	%rbx, %rdx
	movq	%r13, %rdi
	movl	$2, %ecx
	leaq	.LC5(%rip), %r15
	call	cudaMemcpy@PLT
	movq	%rbp, %rdi
	xorl	%ebp, %ebp
	call	fclose@PLT
	leaq	.LC3(%rip), %rsi
	leaq	.LC6(%rip), %rdi
	call	fopen@PLT
	movq	%rax, %rbx
.L32:
	cmpl	%ebp, %r12d
	jle	.L39
	movl	0(%r13,%rbp,4), %ecx
	movq	%r15, %rdx
	movl	$2, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	leal	-1(%r12), %eax
	cmpl	%ebp, %eax
	je	.L33
	leaq	.LC7(%rip), %rcx
	leaq	.LC8(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
.L33:
	incq	%rbp
	jmp	.L32
.L39:
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	%r14, %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	90040(%rsp), %rax
	subq	%fs:40, %rax
	je	.L35
	call	__stack_chk_fail@PLT
.L35:
	addq	$90056, %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
.LFE2027:
	.size	_Z10last_digitv, .-_Z10last_digitv
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	_Z4minAv
	call	cudaDeviceReset@PLT
	call	_Z10last_digitv
	xorl	%eax, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	main, .-main
	.section	.rodata.str1.1
.LC9:
	.string	"_Z15last_digit_cudaPiS_i"
.LC10:
	.string	"_Z9minA_cudaPiS_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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	.LC9(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z15last_digit_cudaPiS_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC10(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z9minA_cudaPiS_ii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	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	%rbx
	.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 <hip/hip_runtime.h>
#include <stdio.h>
#include <string.h>

#define THREADS_PER_BLOCK 32

__global__ void minA_cuda(int* a, int* b, int len, int n_output) {
	int b_index = threadIdx.x + blockIdx.x * THREADS_PER_BLOCK;
    int a_index = b_index * 2;
    
    if (b_index < n_output && a_index < len) {
	    if (a_index == len-1) {
	    	b[b_index] = a[a_index];
	    }
	    else {
		    int v1 = a[a_index];
		    int v2 = a[a_index + 1];
		    if (v2 < v1) {
		    	b[b_index] = v2;
		    }
		    else {
		    	b[b_index] = v1;
		    }
		}
	}
}

__global__ void last_digit_cuda(int* a, int* b, int len) {
	int index = threadIdx.x + blockIdx.x * THREADS_PER_BLOCK;
	if (index < len) 
		b[index] = a[index] % 10;
}

void last_digit() {
	//Get input data from files
    char buff[50000];
    int inp[10000];
    buff[0] = ' ';
    char* token;
    FILE* fp = fopen("inp.txt", "r");
    fgets(buff+1,sizeof(buff), fp);
    token = strtok(buff, ",");
    int len = 0; 
    while(token != NULL) {
		inp[len] = atoi(token+1);
		len++;
		token = strtok(NULL, ",");
    }

    int* A = (int* )malloc(sizeof(int) * len);
    int* B = (int* )malloc(sizeof(int) * len);

    for (int i = 0; i < len; i++) {
    	A[i] = inp[i];
    }

    //Cuda stuff
    int *d_a, *d_b;
    hipMalloc(&d_a, sizeof(int) * len);
    hipMalloc(&d_b, sizeof(int) * len);
    hipMemcpy(d_a, A, sizeof(int) * len, hipMemcpyHostToDevice);

    last_digit_cuda<<<(len + THREADS_PER_BLOCK)/THREADS_PER_BLOCK, THREADS_PER_BLOCK>>>(d_a, d_b, len);
    hipDeviceSynchronize();
    hipMemcpy(B, d_b, sizeof(int) * len, hipMemcpyDeviceToHost);

    //put results in file
	fclose(fp);
	FILE* fp_end = fopen("q1b.txt", "w");
	for (int i = 0; i < len; i++) {
		fprintf(fp_end, "%d", B[i]);
		if (i != len-1) {
			fprintf(fp_end, "%s", ", ");
		}
	}

	//Free up memory
	hipFree(d_a);
	hipFree(d_b);
	free(A);
	free(B);
	fclose(fp_end);
}

void minA() {
	//Get input from files
    char buff[50000];
    int inp[10000];
    buff[0] = ' ';
    char* token;
    FILE* fp = fopen("inp.txt", "r");
    fgets(buff+1,sizeof(buff), fp);
    token = strtok(buff, ",");
    int len = 0; 
    while(token != NULL) {
		inp[len] = atoi(token+1);
		len++;
		token = strtok(NULL, ",");
    }

    //Copy input to array of proper size
    int* A = (int* )malloc(sizeof(int) * len);
    for (int i = 0; i < len; i++) {
    	A[i] = inp[i];
    }

    //Cuda stuff
    int B_size = (len + 1) / 2;
    int* B;

    while (len != 1) {
    	B = (int* )malloc(sizeof(int) * B_size);

		int *d_a, *d_b;
	    hipMalloc(&d_a, sizeof(int) * len);
	    hipMalloc(&d_b, sizeof(int) * B_size);
	    hipMemcpy(d_a, A, sizeof(int) * len, hipMemcpyHostToDevice);

	    minA_cuda<<<(B_size + THREADS_PER_BLOCK)/THREADS_PER_BLOCK, THREADS_PER_BLOCK>>>(d_a, d_b, len, B_size);
	    hipDeviceSynchronize();
	    hipMemcpy(B, d_b, sizeof(int) * B_size, hipMemcpyDeviceToHost);

	    hipFree(d_a);
	    hipFree(d_b);
	    memcpy(A, B, B_size * sizeof(int));
	    free(B);
	    len = B_size;
	    B_size = (len + 1) / 2;
    }

    //Print output to file
    fclose(fp);
	FILE* fp_end = fopen("q1a.txt", "w");
	fprintf(fp_end, "%d", A[0]);
	fclose(fp_end);
}

int main(int argc,char **argv)
{
    minA();
    hipDeviceReset();
    last_digit();
    return 0;
}

	.text
	.file	"q1.hip"
	.globl	_Z24__device_stub__minA_cudaPiS_ii # -- Begin function _Z24__device_stub__minA_cudaPiS_ii
	.type	_Z24__device_stub__minA_cudaPiS_ii,@function
_Z24__device_stub__minA_cudaPiS_ii:     # @_Z24__device_stub__minA_cudaPiS_ii
	.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), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%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	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	$_Z9minA_cudaPiS_ii, %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	_Z24__device_stub__minA_cudaPiS_ii, .Lfunc_end0-_Z24__device_stub__minA_cudaPiS_ii
	.cfi_endproc
                                        # -- End function
	.globl	_Z30__device_stub__last_digit_cudaPiS_i # -- Begin function _Z30__device_stub__last_digit_cudaPiS_i
	.type	_Z30__device_stub__last_digit_cudaPiS_i,@function
_Z30__device_stub__last_digit_cudaPiS_i: # @_Z30__device_stub__last_digit_cudaPiS_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	$_Z15last_digit_cudaPiS_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	_Z30__device_stub__last_digit_cudaPiS_i, .Lfunc_end1-_Z30__device_stub__last_digit_cudaPiS_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z10last_digitv                 # -- Begin function _Z10last_digitv
	.type	_Z10last_digitv,@function
_Z10last_digitv:                        # @_Z10last_digitv
	.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	$90024, %rsp                    # imm = 0x15FA8
	.cfi_def_cfa_offset 90080
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	leaq	40017(%rsp), %r14
	movb	$32, -1(%r14)
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %rbx
	movq	%r14, %rdi
	movl	$50000, %esi                    # imm = 0xC350
	movq	%rax, %rdx
	callq	fgets
	leaq	40016(%rsp), %rdi
	movl	$.L.str.2, %esi
	callq	strtok
	xorl	%ebp, %ebp
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	testq	%rax, %rax
	je	.LBB2_3
# %bb.1:                                # %.lr.ph
                                        #   in Loop: Header=BB2_2 Depth=1
	incq	%rax
	movq	%rax, %rdi
	callq	atoi
	movl	%eax, 16(%rsp,%rbp,4)
	incq	%rbp
	movl	$.L.str.2, %esi
	xorl	%edi, %edi
	callq	strtok
	jmp	.LBB2_2
.LBB2_3:                                # %._crit_edge
	movl	%ebp, %r12d
	leaq	(,%r12,4), %r13
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, %r14
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, %r15
	testl	%ebp, %ebp
	je	.LBB2_5
# %bb.4:                                # %.lr.ph48.preheader
	leaq	16(%rsp), %rsi
	movq	%r14, %rdi
	movq	%r13, %rdx
	callq	memcpy@PLT
.LBB2_5:                                # %._crit_edge49
	leaq	8(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	movq	%rsp, %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	movq	(%rbp), %rdi
	movq	%r14, %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leal	32(%r12), %edi
	shrl	$5, %edi
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$32, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_7
# %bb.6:
	movq	8(%rsp), %rdi
	movq	(%rsp), %rsi
	movl	%r12d, %edx
	callq	_Z30__device_stub__last_digit_cudaPiS_i
.LBB2_7:
	callq	hipDeviceSynchronize
	movq	(%rsp), %rsi
	movq	%r15, %rdi
	movq	%r13, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%rbx, %rdi
	callq	fclose
	movl	$.L.str.3, %edi
	movl	$.L.str.4, %esi
	callq	fopen
	movq	%rax, %rbx
	testq	%r12, %r12
	je	.LBB2_12
# %bb.8:                                # %.lr.ph52
	leal	-1(%r12), %r13d
	xorl	%ebp, %ebp
.LBB2_9:                                # =>This Inner Loop Header: Depth=1
	movl	(%r15,%rbp,4), %edx
	movl	$.L.str.5, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	fprintf
	cmpq	%rbp, %r13
	je	.LBB2_11
# %bb.10:                               #   in Loop: Header=BB2_9 Depth=1
	movl	$.L.str.7, %edi
	movq	%rbx, %rsi
	callq	fputs@PLT
.LBB2_11:                               #   in Loop: Header=BB2_9 Depth=1
	incq	%rbp
	cmpq	%rbp, %r12
	jne	.LBB2_9
.LBB2_12:                               # %._crit_edge53
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	%rbx, %rdi
	callq	fclose
	addq	$90024, %rsp                    # imm = 0x15FA8
	.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
.Lfunc_end2:
	.size	_Z10last_digitv, .Lfunc_end2-_Z10last_digitv
	.cfi_endproc
                                        # -- End function
	.globl	_Z4minAv                        # -- Begin function _Z4minAv
	.type	_Z4minAv,@function
_Z4minAv:                               # @_Z4minAv
	.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	$90056, %rsp                    # imm = 0x15FC8
	.cfi_def_cfa_offset 90112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	leaq	40049(%rsp), %rbx
	movb	$32, -1(%rbx)
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r14
	movq	%rbx, %rdi
	movl	$50000, %esi                    # imm = 0xC350
	movq	%rax, %rdx
	callq	fgets
	leaq	40048(%rsp), %rdi
	movl	$.L.str.2, %esi
	callq	strtok
	xorl	%r15d, %r15d
	testq	%rax, %rax
	je	.LBB3_12
# %bb.1:                                # %.lr.ph.preheader
	xorl	%ebx, %ebx
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	incq	%rax
	movq	%rax, %rdi
	callq	atoi
	movl	%eax, 48(%rsp,%rbx)
	movl	$.L.str.2, %esi
	xorl	%edi, %edi
	callq	strtok
	addq	$4, %rbx
	incl	%r15d
	testq	%rax, %rax
	jne	.LBB3_2
# %bb.3:                                # %._crit_edge
	movabsq	$17179869180, %rax              # imm = 0x3FFFFFFFC
	andq	%rax, %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %rbp
	testl	%r15d, %r15d
	je	.LBB3_4
# %bb.5:                                # %._crit_edge49
	leaq	48(%rsp), %rsi
	movq	%rbp, %rdi
	movq	%rbx, %rdx
	callq	memcpy@PLT
	cmpl	$1, %r15d
	jne	.LBB3_6
	jmp	.LBB3_11
.LBB3_12:                               # %._crit_edge.thread
	xorl	%edi, %edi
	callq	malloc
	movq	%rax, %rbp
	jmp	.LBB3_6
.LBB3_4:
	xorl	%r15d, %r15d
.LBB3_6:                                # %.lr.ph54.preheader
	movq	%r14, 16(%rsp)                  # 8-byte Spill
	movabsq	$4294967296, %rcx               # imm = 0x100000000
	leal	1(%r15), %r12d
	shrl	%r12d
	addq	$32, %rcx
	movq	%rcx, 32(%rsp)                  # 8-byte Spill
	movq	%rbp, 24(%rsp)                  # 8-byte Spill
.LBB3_7:                                # %.lr.ph54
                                        # =>This Inner Loop Header: Depth=1
	movslq	%r12d, %rbx
	leaq	(,%rbx,4), %r13
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	movslq	%r15d, %r14
	shlq	$2, %r14
	leaq	8(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	%rsp, %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	movq	8(%rsp), %rdi
	movq	%rbp, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leal	32(%rbx), %eax
	addl	$63, %ebx
	testl	%eax, %eax
	cmovnsl	%eax, %ebx
	sarl	$5, %ebx
	movabsq	$4294967296, %rax               # imm = 0x100000000
	orq	%rax, %rbx
	movq	%rbx, %rdi
	movl	$1, %esi
	movq	32(%rsp), %rdx                  # 8-byte Reload
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_9
# %bb.8:                                #   in Loop: Header=BB3_7 Depth=1
	movq	8(%rsp), %rdi
	movq	(%rsp), %rsi
	movl	%r15d, %edx
	movl	%r12d, %ecx
	callq	_Z24__device_stub__minA_cudaPiS_ii
.LBB3_9:                                #   in Loop: Header=BB3_7 Depth=1
	callq	hipDeviceSynchronize
	movq	(%rsp), %rsi
	movq	40(%rsp), %r14                  # 8-byte Reload
	movq	%r14, %rdi
	movq	%r13, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rbp                  # 8-byte Reload
	movq	%rbp, %rdi
	movq	%r14, %rsi
	movq	%r13, %rdx
	callq	memcpy@PLT
	movq	%r14, %rdi
	callq	free
	leal	1(%r12), %eax
	shrl	$31, %eax
	addl	%r12d, %eax
	incl	%eax
	sarl	%eax
	movl	%r12d, %r15d
	cmpl	$1, %r12d
	movl	%eax, %r12d
	jne	.LBB3_7
# %bb.10:
	movq	16(%rsp), %r14                  # 8-byte Reload
.LBB3_11:                               # %._crit_edge55
	movq	%r14, %rdi
	callq	fclose
	movl	$.L.str.8, %edi
	movl	$.L.str.4, %esi
	callq	fopen
	movq	%rax, %rbx
	movl	(%rbp), %edx
	movl	$.L.str.5, %esi
	movq	%rax, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movq	%rbx, %rdi
	callq	fclose
	addq	$90056, %rsp                    # imm = 0x15FC8
	.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
.Lfunc_end3:
	.size	_Z4minAv, .Lfunc_end3-_Z4minAv
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	_Z4minAv
	callq	hipDeviceReset
	callq	_Z10last_digitv
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.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:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z9minA_cudaPiS_ii, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z15last_digit_cudaPiS_i, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.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	_Z9minA_cudaPiS_ii,@object      # @_Z9minA_cudaPiS_ii
	.section	.rodata,"a",@progbits
	.globl	_Z9minA_cudaPiS_ii
	.p2align	3, 0x0
_Z9minA_cudaPiS_ii:
	.quad	_Z24__device_stub__minA_cudaPiS_ii
	.size	_Z9minA_cudaPiS_ii, 8

	.type	_Z15last_digit_cudaPiS_i,@object # @_Z15last_digit_cudaPiS_i
	.globl	_Z15last_digit_cudaPiS_i
	.p2align	3, 0x0
_Z15last_digit_cudaPiS_i:
	.quad	_Z30__device_stub__last_digit_cudaPiS_i
	.size	_Z15last_digit_cudaPiS_i, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"inp.txt"
	.size	.L.str, 8

	.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	","
	.size	.L.str.2, 2

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"q1b.txt"
	.size	.L.str.3, 8

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"w"
	.size	.L.str.4, 2

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

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

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"q1a.txt"
	.size	.L.str.8, 8

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z9minA_cudaPiS_ii"
	.size	.L__unnamed_1, 19

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z15last_digit_cudaPiS_i"
	.size	.L__unnamed_2, 25

	.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

	.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__minA_cudaPiS_ii
	.addrsig_sym _Z30__device_stub__last_digit_cudaPiS_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z9minA_cudaPiS_ii
	.addrsig_sym _Z15last_digit_cudaPiS_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z9minA_cudaPiS_ii      ; -- Begin function _Z9minA_cudaPiS_ii
	.globl	_Z9minA_cudaPiS_ii
	.p2align	8
	.type	_Z9minA_cudaPiS_ii,@function
_Z9minA_cudaPiS_ii:                     ; @_Z9minA_cudaPiS_ii
; %bb.0:
	s_load_b64 s[4:5], s[0:1], 0x10
	v_lshl_add_u32 v0, s15, 5, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_lshlrev_b32_e32 v1, 1, v0
	s_waitcnt lgkmcnt(0)
	v_cmp_gt_i32_e32 vcc_lo, s5, v0
	v_cmp_gt_i32_e64 s2, s4, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, vcc_lo, s2
	s_and_saveexec_b32 s3, s2
	s_cbranch_execz .LBB0_9
; %bb.1:
	s_load_b128 s[0:3], s[0:1], 0x0
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s0, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v3, vcc_lo
	s_add_i32 s0, s4, -1
	v_cmp_ne_u32_e32 vcc_lo, s0, v1
	global_load_b32 v4, v[2:3], off
	v_ashrrev_i32_e32 v1, 31, v0
	s_and_saveexec_b32 s0, vcc_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_xor_b32 s1, exec_lo, s0
	s_cbranch_execz .LBB0_7
; %bb.2:
	global_load_b32 v2, v[2:3], off offset:4
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_co_u32 v0, s0, s2, v0
	v_add_co_ci_u32_e64 v1, s0, s3, v1, s0
	s_mov_b32 s0, exec_lo
	s_waitcnt vmcnt(0)
	v_cmpx_ge_i32_e64 v2, v4
	s_xor_b32 s0, exec_lo, s0
	s_cbranch_execz .LBB0_4
; %bb.3:
	global_store_b32 v[0:1], v4, off
                                        ; implicit-def: $vgpr0_vgpr1
                                        ; implicit-def: $vgpr2
.LBB0_4:                                ; %Flow
	s_and_not1_saveexec_b32 s0, s0
	s_cbranch_execz .LBB0_6
; %bb.5:
	global_store_b32 v[0:1], v2, off
.LBB0_6:                                ; %Flow39
	s_or_b32 exec_lo, exec_lo, s0
                                        ; implicit-def: $vgpr0
                                        ; implicit-def: $vgpr4
.LBB0_7:                                ; %Flow40
	s_and_not1_saveexec_b32 s0, s1
	s_cbranch_execz .LBB0_9
; %bb.8:
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	s_waitcnt vmcnt(0)
	global_store_b32 v[0:1], v4, off
.LBB0_9:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z9minA_cudaPiS_ii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 24
		.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 5
		.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	_Z9minA_cudaPiS_ii, .Lfunc_end0-_Z9minA_cudaPiS_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 280
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; 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
	.protected	_Z15last_digit_cudaPiS_i ; -- Begin function _Z15last_digit_cudaPiS_i
	.globl	_Z15last_digit_cudaPiS_i
	.p2align	8
	.type	_Z15last_digit_cudaPiS_i,@function
_Z15last_digit_cudaPiS_i:               ; @_Z15last_digit_cudaPiS_i
; %bb.0:
	s_load_b32 s2, s[0:1], 0x10
	v_lshl_add_u32 v0, s15, 5, v0
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_i32_e32 vcc_lo, s2, v0
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB1_2
; %bb.1:
	s_load_b128 s[0:3], s[0:1], 0x0
	v_ashrrev_i32_e32 v1, 31, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v1, vcc_lo
	v_add_co_u32 v0, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	global_load_b32 v2, v[2:3], off
	s_waitcnt vmcnt(0)
	v_mul_hi_i32 v3, 0x66666667, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshrrev_b32_e32 v4, 31, v3
	v_ashrrev_i32_e32 v3, 2, v3
	v_add_nc_u32_e32 v3, v3, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v3, v3, 10
	v_sub_nc_u32_e32 v2, v2, v3
	global_store_b32 v[0:1], v2, off
.LBB1_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z15last_digit_cudaPiS_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 20
		.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 5
		.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_end1:
	.size	_Z15last_digit_cudaPiS_i, .Lfunc_end1-_Z15last_digit_cudaPiS_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 168
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; 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
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 24
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z9minA_cudaPiS_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z9minA_cudaPiS_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
  - .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:           _Z15last_digit_cudaPiS_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z15last_digit_cudaPiS_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
