#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <math.h>
#include <string.h>
#include <sys/time.h>


// Radix sort kernel
__global__ void radix_sort_kernel(const int* d_number_array, int* d_digit_array, int n, int divisor, int lock){
    __shared__ int shared_d_sub_number_array[1024];

    shared_d_sub_number_array[threadIdx.y * 32 + threadIdx.x] = 0;

    int index = blockIdx.x * 1024 + threadIdx.y * 32 + threadIdx.x;
    if (index < n){
        shared_d_sub_number_array[index % 1024] = d_number_array[index];
    }

    __shared__ int shared_d_digit_array[10];
    if (threadIdx.y == 0 && threadIdx.x < 10){
        shared_d_digit_array[threadIdx.x] = 0;
    }
    __syncthreads();

    if (threadIdx.y == 0 && threadIdx.x == 0){
        for (int i = 0; i < 1024; i++){
            if ((blockIdx.x * 1024 + i) >= n){
                break;
            }
            shared_d_digit_array[(shared_d_sub_number_array[i] / divisor) % 10]++;
        }

        for (int i = 1; i < 10; i++){
            shared_d_digit_array[i] += shared_d_digit_array[i-1];
        }

    }
    __syncthreads();

    for (int selected_block = 0; selected_block < gridDim.x; selected_block++){
        if (blockIdx.x == selected_block){
            lock = 1;
            if (threadIdx.y == 0 && threadIdx.x < 10){
                d_digit_array[threadIdx.x] += shared_d_digit_array[threadIdx.x];
            }
            lock = 0;
        } else {
            clock_t start = clock64();
            clock_t now;
            for (;;){
                now = clock64();
                clock_t cycles = now > start ? now - start : now + (0xffffffff - start);
                if (cycles >= 625000){
                    break;
                }
            }

            for(;;){
                if (!lock){
                    break;
                }
            }
        }

        __syncthreads();
    }

}
__host__ void write_output(char* filename, int* h_number_array, int n){
    FILE * foutput = fopen(filename, "w");
    for (int i = 0; i < n; i++){
        fprintf(foutput, "%d\n", h_number_array[i]);
    }

    fclose(foutput);
}

__host__ void rng(int* arr, int n){
    int seed = 13516118;
    srand(seed);
    for (int i = 0; i < n; i++){
        arr[i] = (int) rand();
    }
}

int main(int argc, char** argv) {
    if (argc != 3){
        printf("Usage: %s number_of_elements file_output_name\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int n = atoi(argv[1]);
    if (!(n)){
        fprintf(stderr,"parameter is zero or not a number.");
        exit(EXIT_FAILURE);
    }

    int* h_number_array, *h_changed_number_array, *d_number_array, *h_digit_array, *d_digit_array;
    h_number_array = (int*) malloc (n * sizeof(int));
    if (h_number_array == NULL){
        fprintf(stderr, "Cannot allocate host memory.");
        exit(EXIT_FAILURE);
    }
    h_changed_number_array = (int *) malloc (n * sizeof(int));
    if (h_changed_number_array == NULL){
        fprintf(stderr, "Cannot allocate host memory.");
        exit(EXIT_FAILURE);
    }
    cudaMalloc((void **) &d_number_array, n * sizeof(int));
    if (d_number_array == NULL){
        fprintf(stderr, "Cannot allocate device memory.");
        exit(EXIT_FAILURE);
    }
    cudaMemset(d_number_array, 0, n * sizeof(int));
    cudaMalloc((void **) &d_digit_array, 10 * sizeof(int));
    if (d_digit_array == NULL){
        fprintf(stderr, "Cannot allocate device memory.");
        exit(EXIT_FAILURE);
    }
    cudaMemset(d_digit_array, 0, 10 * sizeof(int));
    h_digit_array = (int *) malloc (10 * sizeof(int));
    if (h_digit_array == NULL){
        fprintf(stderr, "Cannot allocate device memory.");
        exit(EXIT_FAILURE);
    }

    rng(h_number_array,n);
    cudaMemcpy(d_number_array, h_number_array, n * sizeof(int), cudaMemcpyHostToDevice);

    dim3 grid, block;
    block.x = 32;
    block.y = 32;
    grid.x = n / (block.x * block.y);
    if (n % (block.x * block.y) != 0)
        grid.x++;

    int lock = 0;
	struct timeval timestart, timeend;
	int elapsed;
	
	gettimeofday(&timestart, NULL);
	int max = INT_MIN;
    for (int i = 0; i < n; i++){
        if (max < h_number_array[i]){
            max = h_number_array[i];
        }
    }

    for (int divisor = 1; max/divisor > 0; divisor *= 10){
        radix_sort_kernel<<<grid, block>>>(d_number_array, d_digit_array, n, divisor, lock);
        cudaDeviceSynchronize();

        cudaMemcpy(h_digit_array, d_digit_array, 10 * sizeof(int), cudaMemcpyDeviceToHost);
        for (int i = n - 1; i >= 0; i--){
            h_changed_number_array[h_digit_array[(h_number_array[i] / divisor) % 10] - 1] = h_number_array[i];

            h_digit_array[(h_number_array[i] / divisor) % 10]--;
        }

        memcpy(h_number_array, h_changed_number_array, n * sizeof(int));
        cudaMemcpy(d_number_array, h_changed_number_array, n * sizeof(int), cudaMemcpyHostToDevice);
        cudaMemset(d_digit_array, 0, 10 * sizeof(int));
    }
	gettimeofday(&timeend, NULL);
	elapsed = ((timeend.tv_sec - timestart.tv_sec) * 1000000) + (timeend.tv_usec - timestart.tv_usec);
	printf("Sorting time: %d microseconds\n", elapsed);

    write_output(argv[2], h_number_array, n);

    free(h_number_array);
    free(h_changed_number_array);
    free(h_digit_array);
    cudaFree(d_number_array);
    cudaFree(d_digit_array);
    return 0;
}

	.file	"tmpxft_0020818d_00000000-6_main.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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"w"
.LC1:
	.string	"%d\n"
	.text
	.globl	_Z12write_outputPcPii
	.type	_Z12write_outputPcPii, @function
_Z12write_outputPcPii:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	.LC1(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rsi, %r13
	leaq	.LC0(%rip), %rsi
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movl	%edx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	call	fopen@PLT
	movq	%rax, %rbx
.L3:
	cmpl	%ebp, %r12d
	jle	.L7
	movl	0(%r13,%rbp,4), %ecx
	movq	%r14, %rdx
	movl	$2, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	incq	%rbp
	call	__fprintf_chk@PLT
	jmp	.L3
.L7:
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z12write_outputPcPii, .-_Z12write_outputPcPii
	.globl	_Z3rngPii
	.type	_Z3rngPii, @function
_Z3rngPii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movq	%rdi, %r12
	movl	$13516118, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%ebx, %ebx
	call	srand@PLT
.L9:
	cmpl	%ebx, %ebp
	jle	.L12
	call	rand@PLT
	movl	%eax, (%r12,%rbx,4)
	incq	%rbx
	jmp	.L9
.L12:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z3rngPii, .-_Z3rngPii
	.globl	_Z43__device_stub__Z17radix_sort_kernelPKiPiiiiPKiPiiii
	.type	_Z43__device_stub__Z17radix_sort_kernelPKiPiiiiPKiPiiii, @function
_Z43__device_stub__Z17radix_sort_kernelPKiPiiiiPKiPiiii:
.LFB2054:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, 4(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%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)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L13
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z17radix_sort_kernelPKiPiiii(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	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 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L13:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2054:
	.size	_Z43__device_stub__Z17radix_sort_kernelPKiPiiiiPKiPiiii, .-_Z43__device_stub__Z17radix_sort_kernelPKiPiiiiPKiPiiii
	.globl	_Z17radix_sort_kernelPKiPiiii
	.type	_Z17radix_sort_kernelPKiPiiii, @function
_Z17radix_sort_kernelPKiPiiii:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_Z43__device_stub__Z17radix_sort_kernelPKiPiiiiPKiPiiii
	.cfi_endproc
.LFE2055:
	.size	_Z17radix_sort_kernelPKiPiiii, .-_Z17radix_sort_kernelPKiPiiii
	.section	.rodata.str1.1
.LC2:
	.string	"Usage: %s number_of_elements file_output_name\n"
.LC3:
	.string	"parameter is zero or not a number."
.LC4:
	.string	"Cannot allocate host memory."
.LC5:
	.string	"Cannot allocate device memory."
.LC6:
	.string	"Sorting time: %d microseconds\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
	movq	%rsi, %r14
	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
	subq	$120, %rsp
	.cfi_def_cfa_offset 176
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	cmpl	$3, %edi
	je	.L19
	movq	(%rsi), %rdx
	movl	$2, %edi
	leaq	.LC2(%rip), %rsi
	call	__printf_chk@PLT
	jmp	.L38
.L19:
	movq	8(%rsi), %rdi
	call	atoi@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.L20
	leaq	.LC3(%rip), %rdx
.L39:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
.L40:
	call	__fprintf_chk@PLT
.L38:
	movl	$1, %edi
	call	exit@PLT
.L20:
	cltq
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movq	%rax, %rbp
	testq	%rax, %rax
	je	.L41
	movq	8(%rsp), %rdi
	call	malloc@PLT
	movq	%rax, %r12
	testq	%rax, %rax
	jne	.L22
.L41:
	leaq	.LC4(%rip), %rdx
	jmp	.L39
.L22:
	movq	8(%rsp), %rsi
	leaq	32(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	32(%rsp), %rdi
	testq	%rdi, %rdi
	je	.L42
	movq	8(%rsp), %rdx
	xorl	%esi, %esi
	call	cudaMemset@PLT
	leaq	40(%rsp), %rdi
	movl	$40, %esi
	call	cudaMalloc@PLT
	movq	40(%rsp), %rdi
	testq	%rdi, %rdi
	jne	.L24
.L42:
	leaq	.LC5(%rip), %rdx
	jmp	.L39
.L24:
	movl	$40, %edx
	xorl	%esi, %esi
	call	cudaMemset@PLT
	movl	$40, %edi
	call	malloc@PLT
	movq	%rax, 16(%rsp)
	testq	%rax, %rax
	jne	.L25
	movq	stderr(%rip), %rdi
	leaq	.LC5(%rip), %rdx
	movl	$2, %esi
	jmp	.L40
.L25:
	movl	%ebx, %esi
	movq	%rbp, %rdi
	movl	$-2147483648, %r15d
	call	_Z3rngPii
	movq	8(%rsp), %rdx
	movq	32(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	72(%rsp), %rdi
	movabsq	$4294967297, %rax
	movl	$1, 68(%rsp)
	movq	%rax, 52(%rsp)
	movl	%ebx, %eax
	shrl	$10, %eax
	movl	%eax, 28(%rsp)
	movl	%ebx, %eax
	andl	$1023, %eax
	cmpl	$1, %eax
	sbbl	$-1, 28(%rsp)
	xorl	%esi, %esi
	call	gettimeofday@PLT
	xorl	%eax, %eax
.L27:
	cmpl	%eax, %ebx
	jle	.L43
	movl	0(%rbp,%rax,4), %edx
	cmpl	%edx, %r15d
	cmovl	%edx, %r15d
	incq	%rax
	jmp	.L27
.L43:
	movl	$1, %r13d
.L29:
	movl	%r15d, %eax
	cltd
	idivl	%r13d
	testl	%eax, %eax
	jle	.L44
	movl	28(%rsp), %eax
	movl	68(%rsp), %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$137438953504, %rdx
	movl	56(%rsp), %esi
	movl	%eax, 48(%rsp)
	movq	48(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L30
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	xorl	%r8d, %r8d
	movl	%r13d, %ecx
	movl	%ebx, %edx
	call	_Z43__device_stub__Z17radix_sort_kernelPKiPiiiiPKiPiiii
.L30:
	call	cudaDeviceSynchronize@PLT
	movq	16(%rsp), %rdi
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	movl	$40, %edx
	call	cudaMemcpy@PLT
	leal	-1(%rbx), %ecx
	movl	$10, %edi
	movslq	%ecx, %rcx
.L31:
	testl	%ecx, %ecx
	js	.L45
	movl	0(%rbp,%rcx,4), %esi
	decq	%rcx
	movl	%esi, %eax
	cltd
	idivl	%r13d
	cltd
	idivl	%edi
	movq	16(%rsp), %rax
	movslq	%edx, %rdx
	leaq	(%rax,%rdx,4), %rdx
	movslq	(%rdx), %r8
	movq	%r8, %rax
	movl	%esi, -4(%r12,%r8,4)
	decl	%eax
	movl	%eax, (%rdx)
	jmp	.L31
.L45:
	movq	8(%rsp), %rcx
	movq	%r12, %rsi
	imull	$10, %r13d, %r13d
	movq	%rbp, %rdi
	movq	%rcx, %rdx
	call	__memcpy_chk@PLT
	movq	8(%rsp), %rdx
	movq	32(%rsp), %rdi
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	40(%rsp), %rdi
	movl	$40, %edx
	xorl	%esi, %esi
	call	cudaMemset@PLT
	jmp	.L29
.L44:
	xorl	%esi, %esi
	leaq	88(%rsp), %rdi
	call	gettimeofday@PLT
	movq	88(%rsp), %rdx
	subq	72(%rsp), %rdx
	xorl	%eax, %eax
	imull	$1000000, %edx, %edx
	leaq	.LC6(%rip), %rsi
	subl	80(%rsp), %edx
	movl	$2, %edi
	addl	96(%rsp), %edx
	call	__printf_chk@PLT
	movq	16(%r14), %rdi
	movl	%ebx, %edx
	movq	%rbp, %rsi
	call	_Z12write_outputPcPii
	movq	%rbp, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	16(%rsp), %rdi
	call	free@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L34
	call	__stack_chk_fail@PLT
.L34:
	addq	$120, %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
.LC7:
	.string	"_Z17radix_sort_kernelPKiPiiii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.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	.LC7(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z17radix_sort_kernelPKiPiiii(%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
.LFE2057:
	.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>

#include <math.h>
#include <string.h>
#include <sys/time.h>


// Radix sort kernel
__global__ void radix_sort_kernel(const int* d_number_array, int* d_digit_array, int n, int divisor, int lock){
    __shared__ int shared_d_sub_number_array[1024];

    shared_d_sub_number_array[threadIdx.y * 32 + threadIdx.x] = 0;

    int index = blockIdx.x * 1024 + threadIdx.y * 32 + threadIdx.x;
    if (index < n){
        shared_d_sub_number_array[index % 1024] = d_number_array[index];
    }

    __shared__ int shared_d_digit_array[10];
    if (threadIdx.y == 0 && threadIdx.x < 10){
        shared_d_digit_array[threadIdx.x] = 0;
    }
    __syncthreads();

    if (threadIdx.y == 0 && threadIdx.x == 0){
        for (int i = 0; i < 1024; i++){
            if ((blockIdx.x * 1024 + i) >= n){
                break;
            }
            shared_d_digit_array[(shared_d_sub_number_array[i] / divisor) % 10]++;
        }

        for (int i = 1; i < 10; i++){
            shared_d_digit_array[i] += shared_d_digit_array[i-1];
        }

    }
    __syncthreads();

    for (int selected_block = 0; selected_block < gridDim.x; selected_block++){
        if (blockIdx.x == selected_block){
            lock = 1;
            if (threadIdx.y == 0 && threadIdx.x < 10){
                d_digit_array[threadIdx.x] += shared_d_digit_array[threadIdx.x];
            }
            lock = 0;
        } else {
            clock_t start = clock64();
            clock_t now;
            for (;;){
                now = clock64();
                clock_t cycles = now > start ? now - start : now + (0xffffffff - start);
                if (cycles >= 625000){
                    break;
                }
            }

            for(;;){
                if (!lock){
                    break;
                }
            }
        }

        __syncthreads();
    }

}
__host__ void write_output(char* filename, int* h_number_array, int n){
    FILE * foutput = fopen(filename, "w");
    for (int i = 0; i < n; i++){
        fprintf(foutput, "%d\n", h_number_array[i]);
    }

    fclose(foutput);
}

__host__ void rng(int* arr, int n){
    int seed = 13516118;
    srand(seed);
    for (int i = 0; i < n; i++){
        arr[i] = (int) rand();
    }
}

int main(int argc, char** argv) {
    if (argc != 3){
        printf("Usage: %s number_of_elements file_output_name\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int n = atoi(argv[1]);
    if (!(n)){
        fprintf(stderr,"parameter is zero or not a number.");
        exit(EXIT_FAILURE);
    }

    int* h_number_array, *h_changed_number_array, *d_number_array, *h_digit_array, *d_digit_array;
    h_number_array = (int*) malloc (n * sizeof(int));
    if (h_number_array == NULL){
        fprintf(stderr, "Cannot allocate host memory.");
        exit(EXIT_FAILURE);
    }
    h_changed_number_array = (int *) malloc (n * sizeof(int));
    if (h_changed_number_array == NULL){
        fprintf(stderr, "Cannot allocate host memory.");
        exit(EXIT_FAILURE);
    }
    hipMalloc((void **) &d_number_array, n * sizeof(int));
    if (d_number_array == NULL){
        fprintf(stderr, "Cannot allocate device memory.");
        exit(EXIT_FAILURE);
    }
    hipMemset(d_number_array, 0, n * sizeof(int));
    hipMalloc((void **) &d_digit_array, 10 * sizeof(int));
    if (d_digit_array == NULL){
        fprintf(stderr, "Cannot allocate device memory.");
        exit(EXIT_FAILURE);
    }
    hipMemset(d_digit_array, 0, 10 * sizeof(int));
    h_digit_array = (int *) malloc (10 * sizeof(int));
    if (h_digit_array == NULL){
        fprintf(stderr, "Cannot allocate device memory.");
        exit(EXIT_FAILURE);
    }

    rng(h_number_array,n);
    hipMemcpy(d_number_array, h_number_array, n * sizeof(int), hipMemcpyHostToDevice);

    dim3 grid, block;
    block.x = 32;
    block.y = 32;
    grid.x = n / (block.x * block.y);
    if (n % (block.x * block.y) != 0)
        grid.x++;

    int lock = 0;
	struct timeval timestart, timeend;
	int elapsed;
	
	gettimeofday(&timestart, NULL);
	int max = INT_MIN;
    for (int i = 0; i < n; i++){
        if (max < h_number_array[i]){
            max = h_number_array[i];
        }
    }

    for (int divisor = 1; max/divisor > 0; divisor *= 10){
        radix_sort_kernel<<<grid, block>>>(d_number_array, d_digit_array, n, divisor, lock);
        hipDeviceSynchronize();

        hipMemcpy(h_digit_array, d_digit_array, 10 * sizeof(int), hipMemcpyDeviceToHost);
        for (int i = n - 1; i >= 0; i--){
            h_changed_number_array[h_digit_array[(h_number_array[i] / divisor) % 10] - 1] = h_number_array[i];

            h_digit_array[(h_number_array[i] / divisor) % 10]--;
        }

        memcpy(h_number_array, h_changed_number_array, n * sizeof(int));
        hipMemcpy(d_number_array, h_changed_number_array, n * sizeof(int), hipMemcpyHostToDevice);
        hipMemset(d_digit_array, 0, 10 * sizeof(int));
    }
	gettimeofday(&timeend, NULL);
	elapsed = ((timeend.tv_sec - timestart.tv_sec) * 1000000) + (timeend.tv_usec - timestart.tv_usec);
	printf("Sorting time: %d microseconds\n", elapsed);

    write_output(argv[2], h_number_array, n);

    free(h_number_array);
    free(h_changed_number_array);
    free(h_digit_array);
    hipFree(d_number_array);
    hipFree(d_digit_array);
    return 0;
}

	.text
	.file	"main.hip"
	.globl	_Z32__device_stub__radix_sort_kernelPKiPiiii # -- Begin function _Z32__device_stub__radix_sort_kernelPKiPiiii
	.type	_Z32__device_stub__radix_sort_kernelPKiPiiii,@function
_Z32__device_stub__radix_sort_kernelPKiPiiii: # @_Z32__device_stub__radix_sort_kernelPKiPiiii
	.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	$128, %rsp
	.cfi_def_cfa_offset 176
	.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	4(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%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	$_Z17radix_sort_kernelPKiPiiii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	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	_Z32__device_stub__radix_sort_kernelPKiPiiii, .Lfunc_end0-_Z32__device_stub__radix_sort_kernelPKiPiiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z12write_outputPcPii           # -- Begin function _Z12write_outputPcPii
	.type	_Z12write_outputPcPii,@function
_Z12write_outputPcPii:                  # @_Z12write_outputPcPii
	.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	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%edx, %ebp
	movq	%rsi, %rbx
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, %r14
	testl	%ebp, %ebp
	jle	.LBB1_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%ebp, %r15d
	xorl	%r12d, %r12d
.LBB1_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%r12,4), %edx
	movl	$.L.str.1, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB1_2
.LBB1_3:                                # %._crit_edge
	movq	%r14, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.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
	jmp	fclose                          # TAILCALL
.Lfunc_end1:
	.size	_Z12write_outputPcPii, .Lfunc_end1-_Z12write_outputPcPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z3rngPii                       # -- Begin function _Z3rngPii
	.type	_Z3rngPii,@function
_Z3rngPii:                              # @_Z3rngPii
	.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	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movq	%rdi, %rbx
	movl	$13516118, %edi                 # imm = 0xCE3D56
	callq	srand
	testl	%ebp, %ebp
	jle	.LBB2_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%ebp, %r14d
	xorl	%r15d, %r15d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	movl	%eax, (%rbx,%r15,4)
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB2_2
.LBB2_3:                                # %._crit_edge
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.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	_Z3rngPii, .Lfunc_end2-_Z3rngPii
	.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	$88, %rsp
	.cfi_def_cfa_offset 144
	.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, %r12
	cmpl	$3, %edi
	jne	.LBB3_20
# %bb.1:
	movq	8(%r12), %rdi
	callq	atoi
	testl	%eax, %eax
	je	.LBB3_21
# %bb.2:
	movl	%eax, %ebp
	movslq	%eax, %r13
	shlq	$2, %r13
	movq	%r13, %rdi
	callq	malloc
	testq	%rax, %rax
	je	.LBB3_19
# %bb.3:
	movq	%rax, %r14
	movq	%r13, %rdi
	callq	malloc
	testq	%rax, %rax
	je	.LBB3_19
# %bb.4:
	movq	%rax, %r15
	leaq	16(%rsp), %rbx
	movq	%rbx, %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	movq	(%rbx), %rdi
	testq	%rdi, %rdi
	je	.LBB3_18
# %bb.5:
	movq	%r12, 40(%rsp)                  # 8-byte Spill
	xorl	%esi, %esi
	movq	%r13, 32(%rsp)                  # 8-byte Spill
	movq	%r13, %rdx
	callq	hipMemset
	leaq	8(%rsp), %rbx
	movl	$40, %esi
	movq	%rbx, %rdi
	callq	hipMalloc
	movq	(%rbx), %rdi
	testq	%rdi, %rdi
	je	.LBB3_18
# %bb.6:
	movl	$40, %edx
	xorl	%esi, %esi
	callq	hipMemset
	movl	$40, %edi
	callq	malloc
	testq	%rax, %rax
	je	.LBB3_18
# %bb.7:
	movq	%rax, %r13
	movq	%r14, %rdi
	movl	%ebp, %esi
	callq	_Z3rngPii
	movq	16(%rsp), %rdi
	movq	%r14, %rsi
	movq	32(%rsp), %rdx                  # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%ebp, %ecx
	shrl	$10, %ecx
	movl	%ebp, %eax
	andl	$1023, %eax                     # imm = 0x3FF
	cmpl	$1, %eax
	sbbl	$-1, %ecx
	movq	%rcx, 24(%rsp)                  # 8-byte Spill
	leaq	56(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	%ebp, 4(%rsp)                   # 4-byte Spill
	testl	%ebp, %ebp
	jle	.LBB3_17
# %bb.8:                                # %.lr.ph.preheader
	btsq	$32, 24(%rsp)                   # 8-byte Folded Spill
	movl	4(%rsp), %r12d                  # 4-byte Reload
	movl	$-2147483648, %ebp              # imm = 0x80000000
	xorl	%eax, %eax
.LBB3_9:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%ebp, %ecx
	movl	(%r14,%rax,4), %ebp
	cmpl	%ebp, %ecx
	cmovgl	%ecx, %ebp
	incq	%rax
	cmpq	%rax, %r12
	jne	.LBB3_9
# %bb.10:                               # %.preheader
	testl	%ebp, %ebp
	jle	.LBB3_17
# %bb.11:                               # %.lr.ph93
	incq	%r12
	movl	$1, %ebx
	movq	%r12, 48(%rsp)                  # 8-byte Spill
.LBB3_12:                               # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_15 Depth 2
	movq	24(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_14
# %bb.13:                               #   in Loop: Header=BB3_12 Depth=1
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	4(%rsp), %edx                   # 4-byte Reload
	movl	%ebx, %ecx
	xorl	%r8d, %r8d
	callq	_Z32__device_stub__radix_sort_kernelPKiPiiii
.LBB3_14:                               #   in Loop: Header=BB3_12 Depth=1
	callq	hipDeviceSynchronize
	movq	8(%rsp), %rsi
	movl	$40, %edx
	movq	%r13, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%r12, %rsi
	cmpl	$0, 4(%rsp)                     # 4-byte Folded Reload
	jle	.LBB3_16
.LBB3_15:                               # %.lr.ph91
                                        #   Parent Loop BB3_12 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	-8(%r14,%rsi,4), %ecx
	movl	%ecx, %eax
	cltd
	idivl	%ebx
	cltq
	imulq	$1717986919, %rax, %rdx         # imm = 0x66666667
	movq	%rdx, %rdi
	shrq	$63, %rdi
	sarq	$34, %rdx
	addl	%edi, %edx
	addl	%edx, %edx
	leal	(%rdx,%rdx,4), %edx
	subl	%edx, %eax
	cltq
	movslq	(%r13,%rax,4), %rdx
	movl	%ecx, -4(%r15,%rdx,4)
	leal	-1(%rdx), %ecx
	movl	%ecx, (%r13,%rax,4)
	decq	%rsi
	cmpq	$1, %rsi
	jg	.LBB3_15
.LBB3_16:                               # %._crit_edge
                                        #   in Loop: Header=BB3_12 Depth=1
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	32(%rsp), %r12                  # 8-byte Reload
	movq	%r12, %rdx
	callq	memcpy@PLT
	movq	16(%rsp), %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rdi
	movl	$40, %edx
	xorl	%esi, %esi
	callq	hipMemset
	addl	%ebx, %ebx
	leal	(%rbx,%rbx,4), %ebx
	movl	%ebp, %eax
	xorl	%edx, %edx
	divl	%ebx
	testl	%eax, %eax
	movq	48(%rsp), %r12                  # 8-byte Reload
	jg	.LBB3_12
.LBB3_17:                               # %._crit_edge94
	leaq	72(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	(%rbx), %eax
	movl	8(%rbx), %esi
	subl	56(%rsp), %eax
	imull	$1000000, %eax, %eax            # imm = 0xF4240
	subl	64(%rsp), %esi
	addl	%eax, %esi
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	movq	40(%rsp), %rax                  # 8-byte Reload
	movq	16(%rax), %rdi
	movq	%r14, %rsi
	movl	4(%rsp), %edx                   # 4-byte Reload
	callq	_Z12write_outputPcPii
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$88, %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
.LBB3_18:
	.cfi_def_cfa_offset 144
	movq	stderr(%rip), %rcx
	movl	$.L.str.5, %edi
	movl	$30, %esi
	jmp	.LBB3_22
.LBB3_19:
	movq	stderr(%rip), %rcx
	movl	$.L.str.4, %edi
	movl	$28, %esi
	jmp	.LBB3_22
.LBB3_20:
	movq	(%r12), %rsi
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB3_23
.LBB3_21:
	movq	stderr(%rip), %rcx
	movl	$.L.str.3, %edi
	movl	$34, %esi
.LBB3_22:
	movl	$1, %edx
	callq	fwrite@PLT
.LBB3_23:
	movl	$1, %edi
	callq	exit
.Lfunc_end3:
	.size	main, .Lfunc_end3-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	.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	$_Z17radix_sort_kernelPKiPiiii, %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	_Z17radix_sort_kernelPKiPiiii,@object # @_Z17radix_sort_kernelPKiPiiii
	.section	.rodata,"a",@progbits
	.globl	_Z17radix_sort_kernelPKiPiiii
	.p2align	3, 0x0
_Z17radix_sort_kernelPKiPiiii:
	.quad	_Z32__device_stub__radix_sort_kernelPKiPiiii
	.size	_Z17radix_sort_kernelPKiPiiii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"w"
	.size	.L.str, 2

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"%d\n"
	.size	.L.str.1, 4

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Usage: %s number_of_elements file_output_name\n"
	.size	.L.str.2, 47

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"parameter is zero or not a number."
	.size	.L.str.3, 35

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Cannot allocate host memory."
	.size	.L.str.4, 29

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Cannot allocate device memory."
	.size	.L.str.5, 31

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Sorting time: %d microseconds\n"
	.size	.L.str.6, 31

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z17radix_sort_kernelPKiPiiii"
	.size	.L__unnamed_1, 30

	.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 _Z32__device_stub__radix_sort_kernelPKiPiiii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z17radix_sort_kernelPKiPiiii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z17radix_sort_kernelPKiPiiii ; -- Begin function _Z17radix_sort_kernelPKiPiiii
	.globl	_Z17radix_sort_kernelPKiPiiii
	.p2align	8
	.type	_Z17radix_sort_kernelPKiPiiii,@function
_Z17radix_sort_kernelPKiPiiii:          ; @_Z17radix_sort_kernelPKiPiiii
; %bb.0:
	s_clause 0x1
	s_load_b64 s[8:9], s[0:1], 0x10
	s_load_b128 s[4:7], s[0:1], 0x0
	v_bfe_u32 v3, v0, 10, 10
	v_and_b32_e32 v4, 0x3ff, v0
	s_lshl_b32 s3, s15, 10
	s_mov_b32 s2, exec_lo
	v_mov_b32_e32 v2, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshl_add_u32 v1, v3, 5, v4
	v_add_nc_u32_e32 v0, s3, v1
	v_lshlrev_b32_e32 v1, 2, v1
	ds_store_b32 v1, v2
	s_waitcnt lgkmcnt(0)
	v_cmpx_gt_i32_e64 s8, v0
	s_cbranch_execz .LBB0_2
; %bb.1:
	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[5:6], 2, v[0:1]
	v_lshrrev_b32_e32 v1, 22, v1
	v_add_nc_u32_e32 v1, v0, v1
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v5, vcc_lo, s4, v5
	v_add_co_ci_u32_e32 v6, vcc_lo, s5, v6, vcc_lo
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_and_b32_e32 v1, 0x3ffffc00, v1
	global_load_b32 v2, v[5:6], off
	v_sub_nc_u32_e32 v0, v0, v1
	v_lshlrev_b32_e32 v0, 2, v0
	s_waitcnt vmcnt(0)
	ds_store_b32 v0, v2
.LBB0_2:
	s_or_b32 exec_lo, exec_lo, s2
	v_cmp_eq_u32_e32 vcc_lo, 0, v3
	v_cmp_gt_u32_e64 s2, 10, v4
	v_lshlrev_b32_e32 v2, 2, v4
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s4, vcc_lo, s2
	s_and_saveexec_b32 s2, s4
	s_cbranch_execz .LBB0_4
; %bb.3:
	v_mov_b32_e32 v0, 0
	ds_store_b32 v2, v0 offset:4096
.LBB0_4:
	s_or_b32 exec_lo, exec_lo, s2
	v_or_b32_e32 v0, v3, v4
	s_mov_b32 s2, exec_lo
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_10
; %bb.5:                                ; %.preheader44
	s_cmp_ge_u32 s3, s8
	s_cbranch_scc1 .LBB0_8
; %bb.6:                                ; %.lr.ph.preheader
	s_ashr_i32 s5, s9, 31
	s_add_i32 s3, s3, 1
	s_add_i32 s9, s9, s5
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_xor_b32 s9, s9, s5
	v_cvt_f32_u32_e32 v0, s9
	s_sub_i32 s10, 0, s9
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_iflag_f32_e32 v0, v0
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v0, 0x4f7ffffe, v0
	v_cvt_u32_f32_e32 v0, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_readfirstlane_b32 s11, v0
	s_mul_i32 s12, s10, s11
	s_mov_b32 s10, 0
	s_mul_hi_u32 s12, s11, s12
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_i32 s11, s11, s12
	s_mov_b32 s12, 0
.LBB0_7:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	v_mov_b32_e32 v0, s10
	ds_load_b32 v0, v0
	s_waitcnt lgkmcnt(0)
	v_readfirstlane_b32 s13, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_ashr_i32 s14, s13, 31
	s_add_i32 s13, s13, s14
	s_xor_b32 s16, s14, s5
	s_xor_b32 s13, s13, s14
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_hi_u32 s14, s13, s11
	s_mul_i32 s17, s14, s9
	s_add_i32 s18, s14, 1
	s_sub_i32 s13, s13, s17
	s_delay_alu instid0(SALU_CYCLE_1)
	s_sub_i32 s17, s13, s9
	s_cmp_ge_u32 s13, s9
	s_cselect_b32 s14, s18, s14
	s_cselect_b32 s13, s17, s13
	s_add_i32 s17, s14, 1
	s_cmp_ge_u32 s13, s9
	s_cselect_b32 s13, s17, s14
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_xor_b32 s13, s13, s16
	s_sub_i32 s13, s13, s16
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_hi_i32 s14, s13, 0x66666667
	s_lshr_b32 s16, s14, 31
	s_lshr_b32 s14, s14, 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s14, s14, s16
	s_mul_i32 s14, s14, 10
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_sub_i32 s13, s13, s14
	s_lshl_b32 s13, s13, 2
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mov_b32_e32 v0, s13
	s_add_i32 s13, s12, 1
	s_cmpk_lt_u32 s12, 0x3ff
	s_cselect_b32 s14, -1, 0
	ds_load_b32 v1, v0 offset:4096
	s_add_i32 s12, s3, s12
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lt_u32 s12, s8
	s_cselect_b32 s12, -1, 0
	s_add_i32 s10, s10, 4
	s_and_b32 s14, s14, s12
	s_mov_b32 s12, s13
	s_and_not1_b32 vcc_lo, exec_lo, s14
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v1, 1, v1
	ds_store_b32 v0, v1 offset:4096
	s_cbranch_vccz .LBB0_7
.LBB0_8:                                ; %.preheader42
	v_mov_b32_e32 v0, 0
	s_mov_b32 s3, 0
	ds_load_b32 v0, v0 offset:4096
.LBB0_9:                                ; =>This Inner Loop Header: Depth=1
	v_mov_b32_e32 v1, s3
	s_add_i32 s3, s3, 4
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lg_u32 s3, 36
	ds_load_b32 v3, v1 offset:4100
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v0, v3, v0
	ds_store_b32 v1, v0 offset:4100
	s_cbranch_scc1 .LBB0_9
.LBB0_10:                               ; %Flow76
	s_or_b32 exec_lo, exec_lo, s2
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_load_b32 s5, s[0:1], 0x20
	s_mov_b32 s1, 0
	s_waitcnt lgkmcnt(0)
	s_cmp_eq_u32 s5, 0
	s_cbranch_scc1 .LBB0_21
; %bb.11:                               ; %.lr.ph50
	v_add_co_u32 v0, s0, s6, v2
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_ci_u32_e64 v1, null, s7, 0, s0
	v_or_b32_e32 v2, 0x1000, v2
	s_mov_b32 s6, 0
.LBB0_12:                               ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_14 Depth 2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lg_u32 s15, s6
	s_mov_b32 s0, -1
	s_cbranch_scc0 .LBB0_16
; %bb.13:                               ;   in Loop: Header=BB0_12 Depth=1
	s_getreg_b32 s2, hwreg(HW_REG_SHADER_CYCLES, 0, 20)
	s_mov_b32 s3, s1
.LBB0_14:                               ;   Parent Loop BB0_12 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_getreg_b32 s0, hwreg(HW_REG_SHADER_CYCLES, 0, 20)
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_cmp_gt_i64_e64 s7, s[0:1], s[2:3]
	s_sub_u32 s0, s0, s2
	s_subb_u32 s8, 0, 0
	s_add_u32 s10, s0, -1
	s_addc_u32 s9, s8, 0
	s_and_b32 s7, s7, exec_lo
	s_cselect_b32 s9, s8, s9
	s_cselect_b32 s8, s0, s10
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_gt_i64_e64 s0, 0x98968, s[8:9]
	s_and_b32 vcc_lo, exec_lo, s0
	s_cbranch_vccnz .LBB0_14
; %bb.15:                               ; %Flow
                                        ;   in Loop: Header=BB0_12 Depth=1
	s_mov_b32 s0, 0
.LBB0_16:                               ; %Flow70
                                        ;   in Loop: Header=BB0_12 Depth=1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_b32 vcc_lo, exec_lo, s0
	s_cbranch_vccz .LBB0_20
; %bb.17:                               ;   in Loop: Header=BB0_12 Depth=1
	s_and_saveexec_b32 s0, s4
	s_cbranch_execz .LBB0_19
; %bb.18:                               ;   in Loop: Header=BB0_12 Depth=1
	global_load_b32 v3, v[0:1], off
	ds_load_b32 v4, v2
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_add_nc_u32_e32 v3, v3, v4
	global_store_b32 v[0:1], v3, off
.LBB0_19:                               ; %Flow69
                                        ;   in Loop: Header=BB0_12 Depth=1
	s_or_b32 exec_lo, exec_lo, s0
.LBB0_20:                               ; %.loopexit
                                        ;   in Loop: Header=BB0_12 Depth=1
	s_add_i32 s6, s6, 1
	s_waitcnt_vscnt null, 0x0
	s_cmp_eq_u32 s6, s5
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_12
.LBB0_21:                               ; %._crit_edge
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z17radix_sort_kernelPKiPiiii
		.amdhsa_group_segment_fixed_size 4136
		.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 1
		.amdhsa_next_free_vgpr 7
		.amdhsa_next_free_sgpr 19
		.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	_Z17radix_sort_kernelPKiPiiii, .Lfunc_end0-_Z17radix_sort_kernelPKiPiiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 896
; NumSgprs: 21
; NumVgprs: 7
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 4136 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 21
; NumVGPRsForWavesPerEU: 7
; 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: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

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

	.end_amdgpu_metadata
