/* Host-side code to perform counting sort 
 * 
 * Author: Naga Kandasamy
 * Date modified: March 2, 2021
 * 
 * Student name(s): Abishek S Kumar
 * Date modified: 03/08/2021
 * 
 * Compile as follows: make clean && make
 */

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

#include "counting_sort_kernel.cu"

/* Do not change the range value */
#define MIN_VALUE 0 
#define MAX_VALUE 255
#define THREAD_BLOCK_SIZE 256 
#define NUM_BLOCKS 32 
#define HISTOGRAM_SIZE 256 /* Histogram has 256 bins */

/* Uncomment to spit out debug info */
//#define DEBUG

extern "C" int counting_sort_gold(int *, int *, int, int);
int rand_int(int, int);
void print_array(int *, int);
void print_min_and_max_in_array(int *, int);
void print_histogram(int *, int, int);
void compute_on_device(int *, int *, int, int);
int check_if_sorted(int *, int);
int compare_results(int *, int *, int);

int main(int argc, char **argv)
{
    if (argc < 2) {
        printf("Usage: %s num-elements\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int num_elements = atoi(argv[1]);
    int range = MAX_VALUE - MIN_VALUE;
    int *input_array, *sorted_array_reference, *sorted_array_d;

    /* Populate input array with random integers between [0, RANGE] */
    printf("Generating input array with %d elements in the range 0 to %d\n", num_elements, range);
    input_array = (int *)malloc(num_elements * sizeof(int));
    if (input_array == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    
    srand(time(NULL));
    int i;
    for (i = 0; i < num_elements; i++)
        input_array[i] = rand_int (1, MAX_VALUE);

#ifdef DEBUG
    print_array(input_array, num_elements);
    print_min_and_max_in_array(input_array, num_elements);
#endif

    /* Sort elements in input array using reference implementation. 
     * The result is placed in sorted_array_reference. */
    printf("\nSorting array on CPU\n");
    int status;
    sorted_array_reference = (int *)malloc(num_elements * sizeof(int));
    if (sorted_array_reference == NULL) {
        perror("malloc"); 
        exit(EXIT_FAILURE);
    }
    memset(sorted_array_reference, 0, num_elements);
	struct timeval start, stop;	
	gettimeofday(&start, NULL);
    status = counting_sort_gold(input_array, sorted_array_reference, num_elements, range);
	gettimeofday(&stop, NULL);
	printf("Eexecution time = %f\n",stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/(float)1000000);
    if (status == -1) {
        exit(EXIT_FAILURE);
    }

    status = check_if_sorted(sorted_array_reference, num_elements);
    if (status == -1) {
        printf("Error sorting the input array using the reference code\n");
        exit(EXIT_FAILURE);
    }

    printf("Counting sort was successful on the CPU\n");

#ifdef DEBUG
    print_array(sorted_array_reference, num_elements);
#endif

    /* FIXME: Write function to sort elements in the array in parallel fashion. 
     * The result should be placed in sorted_array_mt. */
    printf("\nSorting array on GPU\n");
    sorted_array_d = (int *)malloc(num_elements * sizeof(int));
    if (sorted_array_d == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    memset(sorted_array_d, 0, num_elements);
    compute_on_device(input_array, sorted_array_d, num_elements, range);
#ifdef DEBUG
    print_array(sorted_array_d, num_elements);
#endif
    /* Check the two results for correctness */
    printf("\nComparing CPU and GPU results\n");
    status = compare_results(sorted_array_reference, sorted_array_d, num_elements);
    if (status == 0)
        printf("Test passed\n");
    else
        printf("Test failed\n");

    exit(EXIT_SUCCESS);
}


/* FIXME: Write the GPU implementation of counting sort */
void compute_on_device(int *input_array, int *sorted_array, int num_elements, int range)
{
	//print_array(input_array, num_elements);
	struct timeval start_a, stop_a;	
	gettimeofday(&start_a, NULL);
	    /* Allocate and initialize space to store histograms generated by the CPU and the GPU */
	int *histogram_on_cpu = (int *)malloc(sizeof(int) * HISTOGRAM_SIZE); 
    memset(histogram_on_cpu, 0, sizeof(int) * HISTOGRAM_SIZE); 

	int *input_on_cpu = (int *)malloc(sizeof(int) * num_elements); 
    memset(input_on_cpu, 0, sizeof(int) * num_elements);

    int *input_data_on_device = NULL;
	int *histogram_on_device = NULL;
	int *sorted_array_d = NULL;
	//For inclusive scan mem allocation
	int shared_mem_size = sizeof(int) * num_elements;
	
		/* Allocate space on GPU for input data */
	cudaMalloc((void**)&input_data_on_device, num_elements * sizeof(int));
	cudaMemcpy(input_data_on_device, input_array, num_elements * sizeof(int), cudaMemcpyHostToDevice);
	
		/* Allocate space on GPU for sorted output array and initialize contents to zero */
	cudaMalloc((void**)&sorted_array_d, num_elements * sizeof(int));
	//cudaMemcpy(sorted_array_d, sorted_array, num_elements * sizeof(int), cudaMemcpyHostToDevice);
	
	
	int num_bins = range + 1;
		/* Allocate space on GPU for histogram and initialize contents to zero */
	cudaMalloc((void**)&histogram_on_device, num_bins * sizeof(int));
	cudaMemset(histogram_on_device, 0, num_bins * sizeof(int)); 
	
	 	/* Set up the execution grid on GPU */
	dim3 thread_block(THREAD_BLOCK_SIZE, 1, 1);
	dim3 grid(NUM_BLOCKS,1);
	gettimeofday(&stop_a, NULL);
	printf("Data transfer time = %f\n",stop_a.tv_sec - start_a.tv_sec + (stop_a.tv_usec - start_a.tv_usec)/(float)1000000);
	
	
    struct timeval start, stop;	
	gettimeofday(&start, NULL);
	
	printf("Using shared memory to generate sorted array\n");
    gettimeofday(&start, NULL);
    //cudaMemset(sorted_array_d, 0, range * sizeof(int)); /* Reset output array */
    counting_sort_kernel<<<grid, thread_block, 2 * shared_mem_size>>>(input_data_on_device, histogram_on_device, sorted_array_d, num_elements, num_bins); 
	cudaDeviceSynchronize();
    gettimeofday(&stop, NULL);
	printf("Eexecution time = %f\n",stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/(float)1000000);
	    /* Copy result from device to host */
    cudaMemcpy(sorted_array, sorted_array_d, num_elements * sizeof(int), cudaMemcpyDeviceToHost);
	cudaMemcpy(input_on_cpu, input_data_on_device, num_elements * sizeof(int), cudaMemcpyDeviceToHost);
	//print_array(input_on_cpu, num_elements);
	cudaMemcpy(histogram_on_cpu, histogram_on_device, HISTOGRAM_SIZE * sizeof(int), cudaMemcpyDeviceToHost);
	//print_histogram(histogram_on_cpu, num_bins, num_elements);
	//check_for_error("KERNEL FAILURE");
	
		/* Free memory */
	cudaFree(input_data_on_device);
	cudaFree(histogram_on_device);
	cudaFree(sorted_array_d);
	
}

/* Check if array is sorted */
int check_if_sorted(int *array, int num_elements)
{
    int status = 0;
    int i;
    for (i = 1; i < num_elements; i++) {
        if (array[i - 1] > array[i]) {
            status = -1;
            break;
        }
    }

    return status;
}

/* Check if the arrays elements are identical */ 
int compare_results(int *array_1, int *array_2, int num_elements)
{
    int status = 0;
    int i;
    for (i = 0; i < num_elements; i++) {
        if (array_1[i] != array_2[i]) {
            status = -1;
            break;
        }
    }

    return status;
}

/* Return random integer between [min, max] */ 
int rand_int(int min, int max)
{
    float r = rand()/(float)RAND_MAX;
    return (int)floorf(min + (max - min) * r);
}

/* Print given array */
void print_array(int *this_array, int num_elements)
{
    printf("Array: ");
    int i;
    for (i = 0; i < num_elements; i++)
        printf("%d ", this_array[i]);
    
    printf("\n");
    return;
}

/* Return min and max values in given array */
void print_min_and_max_in_array(int *this_array, int num_elements)
{
    int i;

    int current_min = INT_MAX;
    for (i = 0; i < num_elements; i++)
        if (this_array[i] < current_min)
            current_min = this_array[i];

    int current_max = INT_MIN;
    for (i = 0; i < num_elements; i++)
        if (this_array[i] > current_max)
            current_max = this_array[i];

    printf("Minimum value in the array = %d\n", current_min);
    printf("Maximum value in the array = %d\n", current_max);
    return;
}



	.file	"tmpxft_00342a5c_00000000-6_counting_sort.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2036:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2036:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z15check_if_sortedPii
	.type	_Z15check_if_sortedPii, @function
_Z15check_if_sortedPii:
.LFB2029:
	.cfi_startproc
	endbr64
	movl	$1, %eax
.L3:
	cmpl	%eax, %esi
	jle	.L8
	movl	-4(%rdi,%rax,4), %edx
	incq	%rax
	cmpl	-4(%rdi,%rax,4), %edx
	jle	.L3
	orl	$-1, %eax
	ret
.L8:
	xorl	%eax, %eax
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z15check_if_sortedPii, .-_Z15check_if_sortedPii
	.globl	_Z15compare_resultsPiS_i
	.type	_Z15compare_resultsPiS_i, @function
_Z15compare_resultsPiS_i:
.LFB2030:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L10:
	cmpl	%eax, %edx
	jle	.L14
	movl	(%rdi,%rax,4), %ecx
	incq	%rax
	cmpl	-4(%rsi,%rax,4), %ecx
	je	.L10
	orl	$-1, %eax
	ret
.L14:
	xorl	%eax, %eax
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z15compare_resultsPiS_i, .-_Z15compare_resultsPiS_i
	.globl	_Z8rand_intii
	.type	_Z8rand_intii, @function
_Z8rand_intii:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%edi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	%esi, %ebx
	pushq	%rax
	.cfi_def_cfa_offset 32
	subl	%ebp, %ebx
	call	rand@PLT
	cvtsi2ssl	%ebx, %xmm0
	cvtsi2ssl	%eax, %xmm1
	mulss	.LC0(%rip), %xmm1
	mulss	%xmm1, %xmm0
	cvtsi2ssl	%ebp, %xmm1
	addss	%xmm1, %xmm0
	call	floorf@PLT
	popq	%rdx
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	cvttss2sil	%xmm0, %eax
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z8rand_intii, .-_Z8rand_intii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"Array: "
.LC2:
	.string	"%d "
.LC3:
	.string	"\n"
	.text
	.globl	_Z11print_arrayPii
	.type	_Z11print_arrayPii, @function
_Z11print_arrayPii:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	xorl	%eax, %eax
	leaq	.LC2(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	movl	$2, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%esi, %ebp
	leaq	.LC1(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rcx
	.cfi_def_cfa_offset 48
	call	__printf_chk@PLT
.L18:
	cmpl	%ebx, %ebp
	jle	.L21
	movl	(%r12,%rbx,4), %edx
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbx
	call	__printf_chk@PLT
	jmp	.L18
.L21:
	popq	%rdx
	.cfi_def_cfa_offset 40
	leaq	.LC3(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 32
	movl	$2, %edi
	popq	%rbp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2032:
	.size	_Z11print_arrayPii, .-_Z11print_arrayPii
	.section	.rodata.str1.1
.LC4:
	.string	"Minimum value in the array = %d\n"
.LC5:
	.string	"Maximum value in the array = %d\n"
	.text
	.globl	_Z26print_min_and_max_in_arrayPii
	.type	_Z26print_min_and_max_in_arrayPii, @function
_Z26print_min_and_max_in_arrayPii:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	xorl	%eax, %eax
	movl	$2147483647, %edx
.L23:
	cmpl	%eax, %esi
	jle	.L28
	movl	(%rdi,%rax,4), %ecx
	cmpl	%ecx, %edx
	cmovg	%ecx, %edx
	incq	%rax
	jmp	.L23
.L28:
	xorl	%eax, %eax
	movl	$-2147483648, %ebx
.L25:
	cmpl	%eax, %esi
	jle	.L29
	movl	(%rdi,%rax,4), %ecx
	cmpl	%ecx, %ebx
	cmovl	%ecx, %ebx
	incq	%rax
	jmp	.L25
.L29:
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebx, %edx
	leaq	.LC5(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2033:
	.size	_Z26print_min_and_max_in_arrayPii, .-_Z26print_min_and_max_in_arrayPii
	.globl	_Z46__device_stub__Z20counting_sort_kernelPiS_S_iiPiS_S_ii
	.type	_Z46__device_stub__Z20counting_sort_kernelPiS_S_iiPiS_S_ii, @function
_Z46__device_stub__Z20counting_sort_kernelPiS_S_iiPiS_S_ii:
.LFB2058:
	.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
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 4(%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, (%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	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%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	.L30
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z20counting_sort_kernelPiS_S_ii(%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
.L30:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
	call	__stack_chk_fail@PLT
.L32:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2058:
	.size	_Z46__device_stub__Z20counting_sort_kernelPiS_S_iiPiS_S_ii, .-_Z46__device_stub__Z20counting_sort_kernelPiS_S_iiPiS_S_ii
	.globl	_Z20counting_sort_kernelPiS_S_ii
	.type	_Z20counting_sort_kernelPiS_S_ii, @function
_Z20counting_sort_kernelPiS_S_ii:
.LFB2059:
	.cfi_startproc
	endbr64
	jmp	_Z46__device_stub__Z20counting_sort_kernelPiS_S_iiPiS_S_ii
	.cfi_endproc
.LFE2059:
	.size	_Z20counting_sort_kernelPiS_S_ii, .-_Z20counting_sort_kernelPiS_S_ii
	.section	.rodata.str1.1
.LC7:
	.string	"Data transfer time = %f\n"
.LC8:
	.string	"Using shared memory to generate sorted array\n"
.LC9:
	.string	"Eexecution time = %f\n"
	.text
	.globl	_Z17compute_on_devicePiS_ii
	.type	_Z17compute_on_devicePiS_ii, @function
_Z17compute_on_devicePiS_ii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rsi, %r14
	xorl	%esi, %esi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%edx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%ecx, %ebx
	incl	%ebx
	subq	$152, %rsp
	.cfi_def_cfa_offset 208
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	72(%rsp), %rdi
	call	gettimeofday@PLT
	movl	$1024, %edi
	call	malloc@PLT
	movl	$256, %ecx
	movq	%rax, %rbp
	xorl	%eax, %eax
	movq	%rbp, %rdi
	rep stosl
	movslq	%r12d, %rax
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movq	8(%rsp), %rcx
	xorl	%esi, %esi
	movq	%rax, %rdi
	movq	%rax, %r13
	movq	%rcx, %rdx
	call	__memset_chk@PLT
	movq	8(%rsp), %rsi
	xorl	%eax, %eax
	leaq	24(%rsp), %rdi
	movq	%rax, 24(%rsp)
	movq	%rax, 32(%rsp)
	movq	%rax, 40(%rsp)
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdx
	movq	24(%rsp), %rdi
	movq	%r15, %rsi
	movl	$1, %ecx
	movslq	%ebx, %r15
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rsi
	salq	$2, %r15
	leaq	40(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	%r15, %rsi
	leaq	32(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	32(%rsp), %rdi
	movq	%r15, %rdx
	xorl	%esi, %esi
	leaq	104(%rsp), %r15
	call	cudaMemset@PLT
	movl	$16777217, %eax
	xorl	%esi, %esi
	leaq	88(%rsp), %rdi
	salq	$8, %rax
	movl	$1, 56(%rsp)
	movq	%rax, 48(%rsp)
	subq	$224, %rax
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	gettimeofday@PLT
	movq	96(%rsp), %rax
	subq	80(%rsp), %rax
	leaq	.LC7(%rip), %rsi
	cvtsi2ssq	%rax, %xmm0
	movq	88(%rsp), %rax
	subq	72(%rsp), %rax
	divss	.LC6(%rip), %xmm0
	cvtsi2ssq	%rax, %xmm1
	movl	$2, %edi
	movb	$1, %al
	addss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	xorl	%esi, %esi
	movq	%r15, %rdi
	call	gettimeofday@PLT
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%esi, %esi
	movq	%r15, %rdi
	call	gettimeofday@PLT
	movl	56(%rsp), %ecx
	movl	68(%rsp), %esi
	xorl	%r9d, %r9d
	leal	0(,%r12,8), %r8d
	movq	48(%rsp), %rdx
	movq	60(%rsp), %rdi
	movslq	%r8d, %r8
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L36
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	movl	%ebx, %r8d
	movl	%r12d, %ecx
	movq	24(%rsp), %rdi
	call	_Z46__device_stub__Z20counting_sort_kernelPiS_S_iiPiS_S_ii
.L36:
	call	cudaDeviceSynchronize@PLT
	xorl	%esi, %esi
	leaq	120(%rsp), %rdi
	call	gettimeofday@PLT
	movq	128(%rsp), %rax
	subq	112(%rsp), %rax
	leaq	.LC9(%rip), %rsi
	cvtsi2ssq	%rax, %xmm0
	movq	120(%rsp), %rax
	subq	104(%rsp), %rax
	divss	.LC6(%rip), %xmm0
	cvtsi2ssq	%rax, %xmm1
	movl	$2, %edi
	movb	$1, %al
	addss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	8(%rsp), %rdx
	movq	%r14, %rdi
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdx
	movq	24(%rsp), %rsi
	movq	%r13, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	32(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbp, %rdi
	movl	$1024, %edx
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L37
	call	__stack_chk_fail@PLT
.L37:
	addq	$152, %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	_Z17compute_on_devicePiS_ii, .-_Z17compute_on_devicePiS_ii
	.section	.rodata.str1.1
.LC10:
	.string	"Usage: %s num-elements\n"
.LC11:
	.string	"Generating input array with %d elements in the range 0 to %d\n"
.LC12:
	.string	"malloc"
.LC13:
	.string	"\nSorting array on CPU\n"
.LC14:
	.string	"Error sorting the input array using the reference code\n"
.LC15:
	.string	"Counting sort was successful on the CPU\n"
.LC16:
	.string	"\nSorting array on GPU\n"
.LC17:
	.string	"\nComparing CPU and GPU results\n"
.LC18:
	.string	"Test passed\n"
.LC19:
	.string	"Test failed\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.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
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	decl	%edi
	jg	.L40
	movq	(%rsi), %rdx
	movl	$2, %edi
	leaq	.LC10(%rip), %rsi
	call	__printf_chk@PLT
	jmp	.L58
.L40:
	movq	8(%rsi), %rdi
	call	atoi@PLT
	movl	$255, %ecx
	movl	$2, %edi
	leaq	.LC11(%rip), %rsi
	movslq	%eax, %r14
	xorl	%eax, %eax
	movl	%r14d, %edx
	leaq	0(,%r14,4), %r13
	movq	%r14, %rbx
	call	__printf_chk@PLT
	movq	%r13, %rdi
	call	malloc@PLT
	movq	%rax, %r12
	testq	%rax, %rax
	jne	.L41
.L44:
	leaq	.LC12(%rip), %rdi
	call	perror@PLT
.L58:
	movl	$1, %edi
.L59:
	call	exit@PLT
.L41:
	xorl	%edi, %edi
	xorl	%ebp, %ebp
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
.L42:
	cmpl	%ebp, %ebx
	jle	.L60
	movl	$255, %esi
	movl	$1, %edi
	call	_Z8rand_intii
	movl	%eax, (%r12,%rbp,4)
	incq	%rbp
	jmp	.L42
.L60:
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r13, %rdi
	call	malloc@PLT
	movq	%rax, %rbp
	testq	%rax, %rax
	je	.L44
	movq	%r13, %rcx
	movq	%r14, %rdx
	xorl	%esi, %esi
	movq	%rax, %rdi
	call	__memset_chk@PLT
	xorl	%esi, %esi
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	movl	$255, %ecx
	movl	%ebx, %edx
	movq	%rbp, %rsi
	movq	%r12, %rdi
	call	counting_sort_gold@PLT
	xorl	%esi, %esi
	leaq	24(%rsp), %rdi
	movl	%eax, %r15d
	call	gettimeofday@PLT
	movq	32(%rsp), %rax
	subq	16(%rsp), %rax
	leaq	.LC9(%rip), %rsi
	cvtsi2ssq	%rax, %xmm0
	movq	24(%rsp), %rax
	subq	8(%rsp), %rax
	divss	.LC6(%rip), %xmm0
	cvtsi2ssq	%rax, %xmm1
	movl	$2, %edi
	movb	$1, %al
	addss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	incl	%r15d
	je	.L58
	movl	%ebx, %esi
	movq	%rbp, %rdi
	call	_Z15check_if_sortedPii
	incl	%eax
	jne	.L46
	leaq	.LC14(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L58
.L46:
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r13, %rdi
	call	malloc@PLT
	movq	%rax, %r15
	testq	%rax, %rax
	je	.L44
	movq	%r13, %rcx
	movq	%r14, %rdx
	xorl	%esi, %esi
	movq	%rax, %rdi
	call	__memset_chk@PLT
	movl	$255, %ecx
	movl	%ebx, %edx
	movq	%r15, %rsi
	movq	%r12, %rdi
	call	_Z17compute_on_devicePiS_ii
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r15, %rsi
	movq	%rbp, %rdi
	movl	%ebx, %edx
	call	_Z15compare_resultsPiS_i
	leaq	.LC18(%rip), %rsi
	movl	$2, %edi
	testl	%eax, %eax
	je	.L57
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
.L57:
	call	__printf_chk@PLT
	xorl	%edi, %edi
	jmp	.L59
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC20:
	.string	"_Z20counting_sort_kernelPiS_S_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2061:
	.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	.LC20(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z20counting_sort_kernelPiS_S_ii(%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
.LFE2061:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	805306368
	.align 4
.LC6:
	.long	1232348160
	.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:

/* Host-side code to perform counting sort 
 * 
 * Author: Naga Kandasamy
 * Date modified: March 2, 2021
 * 
 * Student name(s): Abishek S Kumar
 * Date modified: 03/08/2021
 * 
 * Compile as follows: make clean && make
 */


#include <hip/hip_runtime.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <math.h>
#include <limits.h>

#include "counting_sort_kernel.cu"

/* Do not change the range value */
#define MIN_VALUE 0 
#define MAX_VALUE 255
#define THREAD_BLOCK_SIZE 256 
#define NUM_BLOCKS 32 
#define HISTOGRAM_SIZE 256 /* Histogram has 256 bins */

/* Uncomment to spit out debug info */
//#define DEBUG

extern "C" int counting_sort_gold(int *, int *, int, int);
int rand_int(int, int);
void print_array(int *, int);
void print_min_and_max_in_array(int *, int);
void print_histogram(int *, int, int);
void compute_on_device(int *, int *, int, int);
int check_if_sorted(int *, int);
int compare_results(int *, int *, int);

int main(int argc, char **argv)
{
    if (argc < 2) {
        printf("Usage: %s num-elements\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int num_elements = atoi(argv[1]);
    int range = MAX_VALUE - MIN_VALUE;
    int *input_array, *sorted_array_reference, *sorted_array_d;

    /* Populate input array with random integers between [0, RANGE] */
    printf("Generating input array with %d elements in the range 0 to %d\n", num_elements, range);
    input_array = (int *)malloc(num_elements * sizeof(int));
    if (input_array == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    
    srand(time(NULL));
    int i;
    for (i = 0; i < num_elements; i++)
        input_array[i] = rand_int (1, MAX_VALUE);

#ifdef DEBUG
    print_array(input_array, num_elements);
    print_min_and_max_in_array(input_array, num_elements);
#endif

    /* Sort elements in input array using reference implementation. 
     * The result is placed in sorted_array_reference. */
    printf("\nSorting array on CPU\n");
    int status;
    sorted_array_reference = (int *)malloc(num_elements * sizeof(int));
    if (sorted_array_reference == NULL) {
        perror("malloc"); 
        exit(EXIT_FAILURE);
    }
    memset(sorted_array_reference, 0, num_elements);
	struct timeval start, stop;	
	gettimeofday(&start, NULL);
    status = counting_sort_gold(input_array, sorted_array_reference, num_elements, range);
	gettimeofday(&stop, NULL);
	printf("Eexecution time = %f\n",stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/(float)1000000);
    if (status == -1) {
        exit(EXIT_FAILURE);
    }

    status = check_if_sorted(sorted_array_reference, num_elements);
    if (status == -1) {
        printf("Error sorting the input array using the reference code\n");
        exit(EXIT_FAILURE);
    }

    printf("Counting sort was successful on the CPU\n");

#ifdef DEBUG
    print_array(sorted_array_reference, num_elements);
#endif

    /* FIXME: Write function to sort elements in the array in parallel fashion. 
     * The result should be placed in sorted_array_mt. */
    printf("\nSorting array on GPU\n");
    sorted_array_d = (int *)malloc(num_elements * sizeof(int));
    if (sorted_array_d == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    memset(sorted_array_d, 0, num_elements);
    compute_on_device(input_array, sorted_array_d, num_elements, range);
#ifdef DEBUG
    print_array(sorted_array_d, num_elements);
#endif
    /* Check the two results for correctness */
    printf("\nComparing CPU and GPU results\n");
    status = compare_results(sorted_array_reference, sorted_array_d, num_elements);
    if (status == 0)
        printf("Test passed\n");
    else
        printf("Test failed\n");

    exit(EXIT_SUCCESS);
}


/* FIXME: Write the GPU implementation of counting sort */
void compute_on_device(int *input_array, int *sorted_array, int num_elements, int range)
{
	//print_array(input_array, num_elements);
	struct timeval start_a, stop_a;	
	gettimeofday(&start_a, NULL);
	    /* Allocate and initialize space to store histograms generated by the CPU and the GPU */
	int *histogram_on_cpu = (int *)malloc(sizeof(int) * HISTOGRAM_SIZE); 
    memset(histogram_on_cpu, 0, sizeof(int) * HISTOGRAM_SIZE); 

	int *input_on_cpu = (int *)malloc(sizeof(int) * num_elements); 
    memset(input_on_cpu, 0, sizeof(int) * num_elements);

    int *input_data_on_device = NULL;
	int *histogram_on_device = NULL;
	int *sorted_array_d = NULL;
	//For inclusive scan mem allocation
	int shared_mem_size = sizeof(int) * num_elements;
	
		/* Allocate space on GPU for input data */
	hipMalloc((void**)&input_data_on_device, num_elements * sizeof(int));
	hipMemcpy(input_data_on_device, input_array, num_elements * sizeof(int), hipMemcpyHostToDevice);
	
		/* Allocate space on GPU for sorted output array and initialize contents to zero */
	hipMalloc((void**)&sorted_array_d, num_elements * sizeof(int));
	//cudaMemcpy(sorted_array_d, sorted_array, num_elements * sizeof(int), cudaMemcpyHostToDevice);
	
	
	int num_bins = range + 1;
		/* Allocate space on GPU for histogram and initialize contents to zero */
	hipMalloc((void**)&histogram_on_device, num_bins * sizeof(int));
	hipMemset(histogram_on_device, 0, num_bins * sizeof(int)); 
	
	 	/* Set up the execution grid on GPU */
	dim3 thread_block(THREAD_BLOCK_SIZE, 1, 1);
	dim3 grid(NUM_BLOCKS,1);
	gettimeofday(&stop_a, NULL);
	printf("Data transfer time = %f\n",stop_a.tv_sec - start_a.tv_sec + (stop_a.tv_usec - start_a.tv_usec)/(float)1000000);
	
	
    struct timeval start, stop;	
	gettimeofday(&start, NULL);
	
	printf("Using shared memory to generate sorted array\n");
    gettimeofday(&start, NULL);
    //cudaMemset(sorted_array_d, 0, range * sizeof(int)); /* Reset output array */
    counting_sort_kernel<<<grid, thread_block, 2 * shared_mem_size>>>(input_data_on_device, histogram_on_device, sorted_array_d, num_elements, num_bins); 
	hipDeviceSynchronize();
    gettimeofday(&stop, NULL);
	printf("Eexecution time = %f\n",stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/(float)1000000);
	    /* Copy result from device to host */
    hipMemcpy(sorted_array, sorted_array_d, num_elements * sizeof(int), hipMemcpyDeviceToHost);
	hipMemcpy(input_on_cpu, input_data_on_device, num_elements * sizeof(int), hipMemcpyDeviceToHost);
	//print_array(input_on_cpu, num_elements);
	hipMemcpy(histogram_on_cpu, histogram_on_device, HISTOGRAM_SIZE * sizeof(int), hipMemcpyDeviceToHost);
	//print_histogram(histogram_on_cpu, num_bins, num_elements);
	//check_for_error("KERNEL FAILURE");
	
		/* Free memory */
	hipFree(input_data_on_device);
	hipFree(histogram_on_device);
	hipFree(sorted_array_d);
	
}

/* Check if array is sorted */
int check_if_sorted(int *array, int num_elements)
{
    int status = 0;
    int i;
    for (i = 1; i < num_elements; i++) {
        if (array[i - 1] > array[i]) {
            status = -1;
            break;
        }
    }

    return status;
}

/* Check if the arrays elements are identical */ 
int compare_results(int *array_1, int *array_2, int num_elements)
{
    int status = 0;
    int i;
    for (i = 0; i < num_elements; i++) {
        if (array_1[i] != array_2[i]) {
            status = -1;
            break;
        }
    }

    return status;
}

/* Return random integer between [min, max] */ 
int rand_int(int min, int max)
{
    float r = rand()/(float)RAND_MAX;
    return (int)floorf(min + (max - min) * r);
}

/* Print given array */
void print_array(int *this_array, int num_elements)
{
    printf("Array: ");
    int i;
    for (i = 0; i < num_elements; i++)
        printf("%d ", this_array[i]);
    
    printf("\n");
    return;
}

/* Return min and max values in given array */
void print_min_and_max_in_array(int *this_array, int num_elements)
{
    int i;

    int current_min = INT_MAX;
    for (i = 0; i < num_elements; i++)
        if (this_array[i] < current_min)
            current_min = this_array[i];

    int current_max = INT_MIN;
    for (i = 0; i < num_elements; i++)
        if (this_array[i] > current_max)
            current_max = this_array[i];

    printf("Minimum value in the array = %d\n", current_min);
    printf("Maximum value in the array = %d\n", current_max);
    return;
}



	.text
	.file	"counting_sort.hip"
	.globl	_Z35__device_stub__counting_sort_kernelPiS_S_ii # -- Begin function _Z35__device_stub__counting_sort_kernelPiS_S_ii
	.type	_Z35__device_stub__counting_sort_kernelPiS_S_ii,@function
_Z35__device_stub__counting_sort_kernelPiS_S_ii: # @_Z35__device_stub__counting_sort_kernelPiS_S_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	$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	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	movq	%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	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z20counting_sort_kernelPiS_S_ii, %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	_Z35__device_stub__counting_sort_kernelPiS_S_ii, .Lfunc_end0-_Z35__device_stub__counting_sort_kernelPiS_S_ii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI1_0:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI1_1:
	.long	0x437e0000                      # float 254
.LCPI1_2:
	.long	0x3f800000                      # float 1
.LCPI1_3:
	.long	0x49742400                      # float 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$1, %edi
	jg	.LBB1_2
# %bb.1:
	movq	(%rsi), %rsi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB1_14
.LBB1_2:
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %ebx
	movl	$.L.str.1, %edi
	movl	%eax, %esi
	movl	$255, %edx
	xorl	%eax, %eax
	callq	printf
	movslq	%ebx, %r14
	leaq	(,%r14,4), %r12
	movq	%r12, %rdi
	callq	malloc
	testq	%rax, %rax
	je	.LBB1_3
# %bb.4:
	movq	%rax, %r15
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	testl	%ebx, %ebx
	jle	.LBB1_7
# %bb.5:                                # %.lr.ph.preheader
	movl	%ebx, %r13d
	xorl	%ebp, %ebp
.LBB1_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI1_0(%rip), %xmm0
	mulss	.LCPI1_1(%rip), %xmm0
	addss	.LCPI1_2(%rip), %xmm0
	callq	floorf@PLT
	cvttss2si	%xmm0, %eax
	movl	%eax, (%r15,%rbp,4)
	incq	%rbp
	cmpq	%rbp, %r13
	jne	.LBB1_6
.LBB1_7:                                # %._crit_edge
	movl	$.Lstr, %edi
	callq	puts@PLT
	movq	%r12, %rdi
	callq	malloc
	testq	%rax, %rax
	jne	.LBB1_8
.LBB1_3:
	movl	$.L.str.2, %edi
	callq	perror
.LBB1_14:
	movl	$1, %edi
	callq	exit
.LBB1_8:
	movq	%rax, %r13
	movq	%rax, %rdi
	xorl	%esi, %esi
	movq	%r14, %rdx
	callq	memset@PLT
	leaq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	%r15, %rdi
	movq	%r13, %rsi
	movl	%r14d, %edx
	movl	$255, %ecx
	callq	counting_sort_gold
	movl	%eax, 4(%rsp)                   # 4-byte Spill
	leaq	24(%rsp), %rbp
	movq	%rbp, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%rbp), %rax
	leaq	8(%rsp), %rcx
	subq	(%rcx), %rax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	movq	8(%rbp), %rax
	subq	8(%rcx), %rax
	cvtsi2ss	%rax, %xmm1
	divss	.LCPI1_3(%rip), %xmm1
	addss	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	cmpl	$-1, 4(%rsp)                    # 4-byte Folded Reload
	je	.LBB1_14
# %bb.9:
	cmpl	$2, %ebx
	jl	.LBB1_15
# %bb.10:                               # %.lr.ph.preheader.i
	movl	%ebx, %eax
	movl	$1, %ecx
.LBB1_12:                               # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movl	-4(%r13,%rcx,4), %edx
	cmpl	(%r13,%rcx,4), %edx
	jg	.LBB1_13
# %bb.11:                               #   in Loop: Header=BB1_12 Depth=1
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_12
.LBB1_15:                               # %.loopexit54
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movq	%r12, %rdi
	callq	malloc
	testq	%rax, %rax
	je	.LBB1_3
# %bb.16:
	movq	%rax, %r12
	movq	%rax, %rdi
	xorl	%esi, %esi
	movq	%r14, %rdx
	callq	memset@PLT
	movq	%r15, %rdi
	movq	%r12, %rsi
	movl	%r14d, %edx
	movl	$255, %ecx
	callq	_Z17compute_on_devicePiS_ii
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	$.Lstr.5, %edi
	testl	%ebx, %ebx
	jle	.LBB1_21
# %bb.17:                               # %.lr.ph.preheader.i46
	movl	%ebx, %eax
	xorl	%ecx, %ecx
.LBB1_19:                               # %.lr.ph.i48
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r13,%rcx,4), %edx
	cmpl	(%r12,%rcx,4), %edx
	jne	.LBB1_20
# %bb.18:                               #   in Loop: Header=BB1_19 Depth=1
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_19
	jmp	.LBB1_21
.LBB1_13:                               # %_Z15check_if_sortedPii.exit
	movl	$.Lstr.6, %edi
	callq	puts@PLT
	jmp	.LBB1_14
.LBB1_20:
	movl	$.Lstr.4, %edi
.LBB1_21:                               # %.loopexit
	callq	puts@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z8rand_intii
.LCPI2_0:
	.long	0x30000000                      # float 4.65661287E-10
	.text
	.globl	_Z8rand_intii
	.type	_Z8rand_intii,@function
_Z8rand_intii:                          # @_Z8rand_intii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebx
	movl	%edi, %ebp
	callq	rand
	cvtsi2ss	%eax, %xmm1
	cvtsi2ss	%ebp, %xmm2
	subl	%ebp, %ebx
	cvtsi2ss	%ebx, %xmm0
	mulss	.LCPI2_0(%rip), %xmm1
	mulss	%xmm1, %xmm0
	addss	%xmm2, %xmm0
	callq	floorf@PLT
	cvttss2si	%xmm0, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z8rand_intii, .Lfunc_end2-_Z8rand_intii
	.cfi_endproc
                                        # -- End function
	.globl	_Z15check_if_sortedPii          # -- Begin function _Z15check_if_sortedPii
	.type	_Z15check_if_sortedPii,@function
_Z15check_if_sortedPii:                 # @_Z15check_if_sortedPii
	.cfi_startproc
# %bb.0:
	xorl	%eax, %eax
	cmpl	$2, %esi
	jl	.LBB3_5
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %ecx
	movl	$1, %edx
.LBB3_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	-4(%rdi,%rdx,4), %esi
	cmpl	(%rdi,%rdx,4), %esi
	jg	.LBB3_4
# %bb.2:                                #   in Loop: Header=BB3_3 Depth=1
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB3_3
.LBB3_5:                                # %._crit_edge
	retq
.LBB3_4:
	movl	$-1, %eax
	retq
.Lfunc_end3:
	.size	_Z15check_if_sortedPii, .Lfunc_end3-_Z15check_if_sortedPii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z17compute_on_devicePiS_ii
.LCPI4_0:
	.long	0x49742400                      # float 1.0E+6
	.text
	.globl	_Z17compute_on_devicePiS_ii
	.type	_Z17compute_on_devicePiS_ii,@function
_Z17compute_on_devicePiS_ii:            # @_Z17compute_on_devicePiS_ii
	.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	$120, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%ecx, 28(%rsp)                  # 4-byte Spill
	movl	%edx, %ebp
	movq	%rsi, 40(%rsp)                  # 8-byte Spill
	movq	%rdi, %r14
	xorl	%ebx, %ebx
	leaq	72(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	$1, %edi
	movl	$1024, %esi                     # imm = 0x400
	callq	calloc@PLT
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	movl	%ebp, 24(%rsp)                  # 4-byte Spill
	movslq	%ebp, %r15
	shlq	$2, %r15
	movl	$1, %edi
	movq	%r15, %rsi
	callq	calloc@PLT
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	leaq	16(%rsp), %r13
	movq	%rbx, (%r13)
	leaq	8(%rsp), %r12
	movq	%rbx, (%r12)
	movq	%rsp, %rbp
	movq	%rbx, (%rbp)
	movq	%r13, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	movq	%r14, %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%rbp, %rdi
	movl	28(%rsp), %ebp                  # 4-byte Reload
	movq	%r15, %rsi
	callq	hipMalloc
	movslq	%ebp, %rax
	leaq	4(,%rax,4), %r14
	movq	%r12, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%r12), %rdi
	xorl	%esi, %esi
	movq	%r14, %rdx
	callq	hipMemset
	leaq	104(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%r14), %rax
	leaq	72(%rsp), %rcx
	subq	(%rcx), %rax
	cvtsi2ss	%rax, %xmm0
	movq	8(%r14), %rax
	subq	8(%rcx), %rax
	cvtsi2ss	%rax, %xmm1
	divss	.LCPI4_0(%rip), %xmm1
	addss	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm0
	movl	$.L.str.11, %edi
	movb	$1, %al
	callq	printf
	leaq	56(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	$.Lstr.7, %edi
	callq	puts@PLT
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leal	(%r15,%r15), %eax
	movslq	%eax, %r8
	movabsq	$4294967328, %rdi               # imm = 0x100000020
	leaq	224(%rdi), %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_2
# %bb.1:
	incl	%ebp
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdx
	movl	24(%rsp), %ecx                  # 4-byte Reload
	movl	%ebp, %r8d
	callq	_Z35__device_stub__counting_sort_kernelPiS_S_ii
.LBB4_2:
	callq	hipDeviceSynchronize
	leaq	88(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%rbx), %rax
	subq	56(%rsp), %rax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	movq	8(%rbx), %rax
	subq	64(%rsp), %rax
	xorps	%xmm1, %xmm1
	cvtsi2ss	%rax, %xmm1
	divss	.LCPI4_0(%rip), %xmm1
	addss	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movq	(%rsp), %rsi
	movq	40(%rsp), %rdi                  # 8-byte Reload
	movq	%r15, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rsi
	movq	32(%rsp), %rdi                  # 8-byte Reload
	movq	%r15, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rsi
	movl	$1024, %edx                     # imm = 0x400
	movq	48(%rsp), %rdi                  # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	addq	$120, %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
.Lfunc_end4:
	.size	_Z17compute_on_devicePiS_ii, .Lfunc_end4-_Z17compute_on_devicePiS_ii
	.cfi_endproc
                                        # -- End function
	.globl	_Z15compare_resultsPiS_i        # -- Begin function _Z15compare_resultsPiS_i
	.type	_Z15compare_resultsPiS_i,@function
_Z15compare_resultsPiS_i:               # @_Z15compare_resultsPiS_i
	.cfi_startproc
# %bb.0:
	testl	%edx, %edx
	jle	.LBB5_1
# %bb.3:                                # %.lr.ph.preheader
	movl	%edx, %eax
	xorl	%ecx, %ecx
.LBB5_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rdi,%rcx,4), %edx
	cmpl	(%rsi,%rcx,4), %edx
	jne	.LBB5_5
# %bb.2:                                #   in Loop: Header=BB5_4 Depth=1
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB5_4
.LBB5_1:
	xorl	%eax, %eax
	retq
.LBB5_5:
	movl	$-1, %eax
	retq
.Lfunc_end5:
	.size	_Z15compare_resultsPiS_i, .Lfunc_end5-_Z15compare_resultsPiS_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z11print_arrayPii              # -- Begin function _Z11print_arrayPii
	.type	_Z11print_arrayPii,@function
_Z11print_arrayPii:                     # @_Z11print_arrayPii
	.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	$.L.str.13, %edi
	xorl	%eax, %eax
	callq	printf
	testl	%ebp, %ebp
	jle	.LBB6_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%ebp, %r14d
	xorl	%r15d, %r15d
.LBB6_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%r15,4), %esi
	movl	$.L.str.14, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB6_2
.LBB6_3:                                # %._crit_edge
	movl	$10, %edi
	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
	jmp	putchar@PLT                     # TAILCALL
.Lfunc_end6:
	.size	_Z11print_arrayPii, .Lfunc_end6-_Z11print_arrayPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z26print_min_and_max_in_arrayPii # -- Begin function _Z26print_min_and_max_in_arrayPii
	.type	_Z26print_min_and_max_in_arrayPii,@function
_Z26print_min_and_max_in_arrayPii:      # @_Z26print_min_and_max_in_arrayPii
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	$-2147483648, %ebx              # imm = 0x80000000
	testl	%esi, %esi
	jle	.LBB7_1
# %bb.6:                                # %.lr.ph.preheader
	movl	%esi, %eax
	movl	%esi, %ecx
	movl	$2147483647, %esi               # imm = 0x7FFFFFFF
	xorl	%edx, %edx
.LBB7_7:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rdi,%rdx,4), %r8d
	cmpl	%esi, %r8d
	cmovll	%r8d, %esi
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB7_7
# %bb.3:                                # %.preheader
	testl	%eax, %eax
	jle	.LBB7_2
# %bb.4:                                # %.lr.ph29.preheader
	movl	$-2147483648, %ebx              # imm = 0x80000000
	xorl	%eax, %eax
.LBB7_5:                                # %.lr.ph29
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rdi,%rax,4), %edx
	cmpl	%ebx, %edx
	cmovgl	%edx, %ebx
	incq	%rax
	cmpq	%rax, %rcx
	jne	.LBB7_5
	jmp	.LBB7_2
.LBB7_1:
	movl	$2147483647, %esi               # imm = 0x7FFFFFFF
.LBB7_2:                                # %._crit_edge
	movl	$.L.str.16, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.17, %edi
	movl	%ebx, %esi
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	printf                          # TAILCALL
.Lfunc_end7:
	.size	_Z26print_min_and_max_in_arrayPii, .Lfunc_end7-_Z26print_min_and_max_in_arrayPii
	.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	.LBB8_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB8_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z20counting_sort_kernelPiS_S_ii, %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_end8:
	.size	__hip_module_ctor, .Lfunc_end8-__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	.LBB9_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
.LBB9_2:
	retq
.Lfunc_end9:
	.size	__hip_module_dtor, .Lfunc_end9-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z20counting_sort_kernelPiS_S_ii,@object # @_Z20counting_sort_kernelPiS_S_ii
	.section	.rodata,"a",@progbits
	.globl	_Z20counting_sort_kernelPiS_S_ii
	.p2align	3, 0x0
_Z20counting_sort_kernelPiS_S_ii:
	.quad	_Z35__device_stub__counting_sort_kernelPiS_S_ii
	.size	_Z20counting_sort_kernelPiS_S_ii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Usage: %s num-elements\n"
	.size	.L.str, 24

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Generating input array with %d elements in the range 0 to %d\n"
	.size	.L.str.1, 62

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Eexecution time = %f\n"
	.size	.L.str.4, 22

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"Data transfer time = %f\n"
	.size	.L.str.11, 25

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Array: "
	.size	.L.str.13, 8

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"%d "
	.size	.L.str.14, 4

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"Minimum value in the array = %d\n"
	.size	.L.str.16, 33

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"Maximum value in the array = %d\n"
	.size	.L.str.17, 33

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z20counting_sort_kernelPiS_S_ii"
	.size	.L__unnamed_1, 33

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

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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"\nSorting array on CPU"
	.size	.Lstr, 22

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Counting sort was successful on the CPU"
	.size	.Lstr.1, 40

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"\nSorting array on GPU"
	.size	.Lstr.2, 22

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"\nComparing CPU and GPU results"
	.size	.Lstr.3, 31

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"Test failed"
	.size	.Lstr.4, 12

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"Test passed"
	.size	.Lstr.5, 12

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"Error sorting the input array using the reference code"
	.size	.Lstr.6, 55

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"Using shared memory to generate sorted array"
	.size	.Lstr.7, 45

	.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 _Z35__device_stub__counting_sort_kernelPiS_S_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z20counting_sort_kernelPiS_S_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z20counting_sort_kernelPiS_S_ii ; -- Begin function _Z20counting_sort_kernelPiS_S_ii
	.globl	_Z20counting_sort_kernelPiS_S_ii
	.p2align	8
	.type	_Z20counting_sort_kernelPiS_S_ii,@function
_Z20counting_sort_kernelPiS_S_ii:       ; @_Z20counting_sort_kernelPiS_S_ii
; %bb.0:
	s_load_b64 s[8:9], s[0:1], 0x18
	v_lshlrev_b32_e32 v4, 2, v0
	s_waitcnt lgkmcnt(0)
	v_cmp_gt_u32_e32 vcc_lo, s9, v0
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	v_mov_b32_e32 v1, 0
	ds_store_b32 v4, v1
.LBB0_2:
	s_or_b32 exec_lo, exec_lo, s2
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_clause 0x2
	s_load_b32 s12, s[0:1], 0x2c
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[2:3], s[0:1], 0x10
	s_add_u32 s10, s0, 32
	s_addc_u32 s11, s1, 0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s1, s12, 0xffff
	s_mov_b32 s12, exec_lo
	v_mad_u64_u32 v[1:2], null, s15, s1, v[0:1]
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s8, v1
	s_cbranch_execz .LBB0_5
; %bb.3:                                ; %.lr.ph.preheader
	s_load_b32 s0, s[10:11], 0x0
	v_ashrrev_i32_e32 v2, 31, v1
	v_mov_b32_e32 v5, 1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s10, s0, s1
	v_add_co_u32 v2, s0, s4, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_add_co_ci_u32_e64 v3, s0, s5, v3, s0
	s_ashr_i32 s11, s10, 31
	s_lshl_b64 s[4:5], s[10:11], 2
	s_mov_b32 s11, 0
.LBB0_4:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	global_load_b32 v6, v[2:3], off
	v_add_nc_u32_e32 v1, s10, v1
	v_add_co_u32 v2, s1, v2, s4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e64 v3, s1, s5, v3, s1
	v_cmp_le_i32_e64 s0, s8, v1
	s_delay_alu instid0(VALU_DEP_1)
	s_or_b32 s11, s0, s11
	s_waitcnt vmcnt(0)
	v_lshlrev_b32_e32 v6, 2, v6
	ds_add_u32 v6, v5
	s_and_not1_b32 exec_lo, exec_lo, s11
	s_cbranch_execnz .LBB0_4
.LBB0_5:                                ; %Flow106
	s_or_b32 exec_lo, exec_lo, s12
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_and_saveexec_b32 s0, vcc_lo
	s_cbranch_execz .LBB0_7
; %bb.6:
	ds_load_b32 v1, v4
	s_waitcnt lgkmcnt(0)
	global_atomic_add_u32 v4, v1, s[6:7]
.LBB0_7:
	s_or_b32 exec_lo, exec_lo, s0
	v_mov_b32_e32 v2, 0
	s_mov_b32 s1, 0
	s_mov_b32 s0, exec_lo
	v_cmpx_ne_u32_e32 0, v0
	s_cbranch_execz .LBB0_9
; %bb.8:
	global_load_b32 v2, v4, s[6:7]
.LBB0_9:
	s_or_b32 exec_lo, exec_lo, s0
	v_lshl_add_u32 v1, v0, 2, 0x3fc
	s_cmp_gt_i32 s9, 1
	s_mov_b32 s4, 1
	s_cselect_b32 s0, -1, 0
	s_cmp_lt_i32 s9, 2
	s_waitcnt vmcnt(0)
	ds_store_b32 v1, v2
	s_cbranch_scc1 .LBB0_13
.LBB0_10:                               ; %.lr.ph59
                                        ; =>This Inner Loop Header: Depth=1
	s_mul_i32 s5, s1, s9
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	v_lshl_add_u32 v2, s5, 2, v1
	s_barrier
	buffer_gl0_inv
	s_sub_i32 s1, 1, s1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mul_i32 s8, s1, s9
	ds_load_b32 v2, v2
	v_lshl_add_u32 v3, s8, 2, v1
	s_mov_b32 s8, exec_lo
	s_waitcnt lgkmcnt(0)
	ds_store_b32 v3, v2
	v_cmpx_lt_u32_e64 s4, v0
	s_cbranch_execz .LBB0_12
; %bb.11:                               ;   in Loop: Header=BB0_10 Depth=1
	v_add_nc_u32_e32 v5, s5, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_subrev_nc_u32_e32 v5, s4, v5
	v_lshl_add_u32 v5, v5, 2, 0x3fc
	ds_load_b32 v5, v5
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v2, v5, v2
	ds_store_b32 v3, v2
.LBB0_12:                               ;   in Loop: Header=BB0_10 Depth=1
	s_or_b32 exec_lo, exec_lo, s8
	s_lshl_b32 s4, s4, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lt_i32 s4, s9
	s_cbranch_scc1 .LBB0_10
.LBB0_13:                               ; %._crit_edge60
	s_mul_i32 s1, s1, s9
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	v_lshl_add_u32 v0, s1, 2, v1
	s_barrier
	buffer_gl0_inv
	s_and_not1_b32 vcc_lo, exec_lo, s0
	ds_load_b32 v0, v0
	s_waitcnt lgkmcnt(0)
	global_store_b32 v4, v0, s[6:7]
	s_cbranch_vccnz .LBB0_19
; %bb.14:                               ; %.preheader.preheader
	v_mov_b32_e32 v0, 0
	s_mov_b32 s1, 0
	s_add_i32 s10, s9, -2
	s_mov_b32 s8, s1
	s_mov_b32 s0, s1
.LBB0_15:                               ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_17 Depth 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[4:5], s[0:1], 2
	s_add_u32 s4, s6, s4
	s_addc_u32 s5, s7, s5
	global_load_b32 v1, v0, s[4:5]
	s_waitcnt vmcnt(0)
	v_cmp_gt_i32_e32 vcc_lo, s8, v1
	v_readfirstlane_b32 s12, v1
	s_cbranch_vccnz .LBB0_18
; %bb.16:                               ; %.lr.ph63.preheader
                                        ;   in Loop: Header=BB0_15 Depth=1
	s_ashr_i32 s9, s8, 31
	v_mov_b32_e32 v1, s0
	s_lshl_b64 s[12:13], s[8:9], 2
	s_add_i32 s11, s8, -1
	s_add_u32 s8, s2, s12
	s_addc_u32 s9, s3, s13
.LBB0_17:                               ; %.lr.ph63
                                        ;   Parent Loop BB0_15 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	global_store_b32 v0, v1, s[8:9]
	global_load_b32 v2, v0, s[4:5]
	s_add_i32 s11, s11, 1
	s_add_u32 s8, s8, 4
	s_addc_u32 s9, s9, 0
	s_waitcnt vmcnt(0)
	v_cmp_lt_i32_e32 vcc_lo, s11, v2
	v_readfirstlane_b32 s12, v2
	s_cbranch_vccnz .LBB0_17
.LBB0_18:                               ; %._crit_edge64
                                        ;   in Loop: Header=BB0_15 Depth=1
	s_add_i32 s4, s0, 1
	s_cmp_lg_u32 s0, s10
	s_delay_alu instid0(VALU_DEP_1)
	s_mov_b32 s8, s12
	s_mov_b32 s0, s4
	s_cbranch_scc1 .LBB0_15
.LBB0_19:                               ; %._crit_edge68
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z20counting_sort_kernelPiS_S_ii
		.amdhsa_group_segment_fixed_size 1020
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 7
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z20counting_sort_kernelPiS_S_ii, .Lfunc_end0-_Z20counting_sort_kernelPiS_S_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 784
; NumSgprs: 18
; NumVgprs: 7
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 1020 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 7
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 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
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         28
        .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
      - .offset:         152
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .group_segment_fixed_size: 1020
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z20counting_sort_kernelPiS_S_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z20counting_sort_kernelPiS_S_ii.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
