/*****************************************
Project 3
James Albu, Rebecca Johnson, Jacob Manfre
GPU Radix Sort Algorithm
*******************************************/

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

#define MAX 2147483647;								//largest 32bit signed integer
 //#define MAX 99;

unsigned int * valuesList;							//holds values for parallel radix sort
unsigned int * valuesList2;							//array holds values for sequential radix sort
unsigned int* d_valuesList;							//holds values for device

struct timezone Idunno;
struct timeval startTime, endTime;

float totalRunningTime = 0.00000;
unsigned int totalNumbers;							//number of data values in array
int histogramSize;
int digit = 1000000000;								//largest possible place value for 32bit signed integers

//calculates running time of the radix sort algorithm
float report_running_time() {
	long sec_diff, usec_diff;
	gettimeofday(&endTime, &Idunno);
	sec_diff = endTime.tv_sec - startTime.tv_sec;
	usec_diff= endTime.tv_usec-startTime.tv_usec;
	if(usec_diff < 0) {
		sec_diff --;
		usec_diff += 1000000;
	}

	return (float)(sec_diff*1.0 + usec_diff/1000000.0);
}

//sequentially sorts the radix sort algorithm on the CPU in order to compare its running time to GPU
void seqSort(unsigned int * array, int size){
  int i;
  long long semiSorted[size];
  int significantDigit = 1;
  int largestNum = 1000000000;
  // Loop until we reach the largest significant digit
  while (largestNum / significantDigit > 0)
  {  
    long long bucket[10] = { 0 };
    // Counts the number of "keys" or digits that will go into each bucket
    for (i = 0; i < size; i++)
      bucket[(array[i] / significantDigit) % 10]++;
    for (i = 1; i < 10; i++)
      bucket[i] += bucket[i - 1];   
    // Use the bucket to fill a "semiSorted" array
    for (i = size - 1; i >= 0; i--)
      semiSorted[--bucket[(array[i] / significantDigit) % 10]] = array[i];
    for (i = 0; i < size; i++)
      array[i] = semiSorted[i];
    // Move to next significant digit
    significantDigit *= 10;
    
  }
}

//function to print out arrays
void printArray(int * array, int size) {	
	printf("[ ");
  	for (int i = 0; i < size; i++) {
    	printf("%d ", array[i]);}
  	printf("]\n");
}

void printArrayU(unsigned int * array, int size) {	
	printf("[ ");
  	for (int i = 0; i < size; i++) {
    	printf("%d ", array[i]);
	}
  	printf("]\n");
}

//main GPU kernel
//counts the number of instances for a place value and stores in a histogram
__global__ void radix_Sort(unsigned int* valuesList, int digitMax, int digitCurrent, int startPos, int arraySize, int* histogram, int* mainOffset, int* mainOffsetChanged) {

	 int tid = threadIdx.x + blockIdx.x * blockDim.x;
	 tid += startPos;
	// take element in values at this instanced thread and find the digit 
	// we're looking for thats passed in and increment the corresponding element 
	// in the histogram
	int tempDigitMax = digitMax;
	int tempDigitCurrent = digitCurrent;
	if (tid < startPos + arraySize) {
		int num = valuesList[tid];
		while (tempDigitMax != tempDigitCurrent) {
			num = valuesList[tid] / tempDigitMax;
			num *= tempDigitMax;

			tempDigitMax /= 10;
			num = valuesList[tid] - num;
		}

		atomicAdd(&histogram[num/digitCurrent], 1);
	}
	__syncthreads();

	// find offset before values
	mainOffset[0] = histogram[0];
	mainOffsetChanged[0] = histogram[0];
	for (int i = 1; i < 10; i++) {
		mainOffsetChanged[i] = mainOffsetChanged[i-1] + histogram[i];
		mainOffset[i] = mainOffset[i-1] + histogram[i];
	}


	__syncthreads();

	return;

}

//rearragnes the array elements to correspond to the bucket they are placed in
__global__ void moveElements(unsigned int *valuesList, unsigned int *indexList, int startPos, int arraySize) {
	int tid = threadIdx.x + blockIdx.x * blockDim.x;
	tid += startPos;

	if (tid < startPos + arraySize) {
		int val = valuesList[tid];
		int index = indexList[tid] + startPos;

		__syncthreads();
		valuesList[index] = val;
	}
	__syncthreads();

	return;

}

//initializing the radix sort values and memory allocation functions
void sortArray(int dig, int totalNums, int minIndex, int prevMin) {
	int * histogram;
	int * offset;
	int * offsetAfter;

	int* d_histogram;
	int* d_offset;
	int* d_offsetAfter;

	histogram = (int*)malloc(sizeof(int)*histogramSize);
	offset = (int*)malloc(sizeof(int)*histogramSize);
	offsetAfter = (int*)malloc(sizeof(int)*histogramSize);

	// fill histogram and offset arrays with 0's
	for (int i = 0; i < histogramSize; i++) {
		histogram[i] = 0;
		offset[i] = 0;
		offsetAfter[i] = 0;
	}

	cudaMalloc((void **) &d_valuesList, sizeof(unsigned int)*totalNumbers);
	cudaMalloc((void**) &d_histogram, sizeof(int)*histogramSize);
	cudaMalloc((void**) &d_offset, sizeof(int)*histogramSize);
	cudaMalloc((void**) &d_offsetAfter, sizeof(int)*histogramSize);

	cudaMemcpy(d_valuesList, valuesList, sizeof(unsigned int)*totalNumbers, cudaMemcpyHostToDevice);
	cudaMemcpy(d_histogram, histogram, sizeof(int)*histogramSize, cudaMemcpyHostToDevice);
	cudaMemcpy(d_offset, offset, sizeof(int)*histogramSize, cudaMemcpyHostToDevice);
	cudaMemcpy(d_offsetAfter, offsetAfter, sizeof(int)*histogramSize, cudaMemcpyHostToDevice);
        
        gettimeofday(&startTime, &Idunno);
	radix_Sort<<<(totalNums+255)/256, 256>>>(d_valuesList, digit, dig, minIndex, totalNums, d_histogram, d_offset, d_offsetAfter);
	totalRunningTime = totalRunningTime + report_running_time();
	
	// copy data back to host from the device
	cudaMemcpy(valuesList, d_valuesList, sizeof(unsigned int)*totalNumbers, cudaMemcpyDeviceToHost);
	cudaMemcpy(histogram, d_histogram, sizeof(int)*histogramSize, cudaMemcpyDeviceToHost);
	cudaMemcpy(offset, d_offset, sizeof(int)*histogramSize, cudaMemcpyDeviceToHost);
	cudaMemcpy(offsetAfter, d_offsetAfter, sizeof(int)*histogramSize, cudaMemcpyDeviceToHost);
	// free memory on device
	cudaFree(d_valuesList);
	cudaFree(d_histogram);
	cudaFree(d_offset);
	cudaFree(d_offsetAfter);

	// find offset after values
	unsigned int *indexArray = (unsigned int*)malloc(sizeof(unsigned int)*totalNumbers);
	unsigned int *d_indexArray;
	for (int i = minIndex; i < minIndex + totalNums; i++) {
		// find the digit to sort by
		int num = valuesList[i];
		int tempDigit = digit;
		while (tempDigit != dig) {
			num = valuesList[i] / tempDigit;
			num *= tempDigit;

			tempDigit /= 10;
			num = valuesList[i] - num;
		}

		indexArray[i] = (offsetAfter[num/dig] - 1);
		offsetAfter[num/dig]--;
	}

	// copy main array and index array to device to rearrange values
	cudaMalloc((void **) &d_valuesList, sizeof(unsigned int)*totalNumbers);
	cudaMalloc((void **) &d_indexArray, sizeof(unsigned int)*totalNumbers);

	cudaMemcpy(d_valuesList, valuesList, sizeof(unsigned int)*totalNumbers, cudaMemcpyHostToDevice);
	cudaMemcpy(d_indexArray, indexArray, sizeof(unsigned int)*totalNumbers, cudaMemcpyHostToDevice);

	// printf("MIN INDEX: %d\n", minIndex);
	// printf("SIZE: %d\n", totalNums);
	// printf("DIGIT: %d\n", dig);
	// printArrayU(indexArray, totalNumbers);
 	
	gettimeofday(&startTime, &Idunno);
	// kernel call to rearrange the numbers in valuesList
	moveElements<<<(totalNums+255)/256,256>>>(d_valuesList, d_indexArray, minIndex, totalNums);
	totalRunningTime = totalRunningTime + report_running_time();

	// copy data back to host from the device
	cudaMemcpy(valuesList, d_valuesList, sizeof(unsigned int)*totalNumbers, cudaMemcpyDeviceToHost);
	cudaMemcpy(indexArray, d_indexArray, sizeof(unsigned int)*totalNumbers, cudaMemcpyDeviceToHost);
	// free memory
	cudaFree(d_valuesList);
	cudaFree(d_indexArray);

	// printf("HISTOGRAM:\n");
	// printArray(histogram, histogramSize);

	// printf("OFFSET BEFORE:\n");
	// printArray(offset, histogramSize);

	// printf("OFFSET AFTER:\n");
	// printArray(offsetAfter, histogramSize);

	// printf("VALUES AFTER:\n");
	// printArrayU(valuesList, totalNumbers);

	// call sortArray on each index of the histogram if that index value is greater than 1
	for (int i = 0; i < 10; i++) {
		if (histogram[i] > 1 && dig != 1) {
			int minInd;
			if (i == 0) {
				minInd = 0;
			}
			else{
				minInd = offset[i-1];
			} 

			// recursion
			sortArray(dig/10, offset[i]-minInd, minInd+prevMin, minInd+prevMin);
		}
	}
	
	return;
}

int main(int argc, char **argv) {

	totalNumbers = atoi(argv[1]);
	histogramSize = 10;

	valuesList = (unsigned int *)malloc(sizeof(unsigned int)*totalNumbers);
	valuesList2 = (unsigned int *)malloc(sizeof(unsigned int)*totalNumbers);

	srand(1);	
	// generate totalNumbers random numbers for valuesList
	for (int i = 0; i < totalNumbers; i++) {
		valuesList[i] = (int) rand()%MAX;
	}
	for (int i = 0; i < totalNumbers; i++)
		valuesList2[i] = valuesList[i];

//	printf("VALUES BEFORE:\n");
//	printArrayU(valuesList, totalNumbers);
	printf("GPU running time: \n");
	sortArray(digit, totalNumbers, 0, 0);
        printf("%f \n", totalRunningTime);
  
        printf("CPU running time:\n");
  	gettimeofday(&startTime, &Idunno);
  	seqSort(valuesList2, totalNumbers);
  	printf("%f \n", report_running_time());

//  printf("SeqSort: \n");
//  printArrayU(&valuesList2[0], totalNumbers);
  
//	printf("VALUES AFTER:\n");
//	printArrayU(valuesList, totalNumbers);

	return 0;
}

	.file	"tmpxft_002ab2ab_00000000-6_radix_sort.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2035:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z19report_running_timev
	.type	_Z19report_running_timev, @function
_Z19report_running_timev:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rdx
	.cfi_def_cfa_offset 16
	leaq	Idunno(%rip), %rsi
	leaq	endTime(%rip), %rdi
	call	gettimeofday@PLT
	movq	endTime(%rip), %rdx
	movq	8+endTime(%rip), %rax
	subq	startTime(%rip), %rdx
	subq	8+startTime(%rip), %rax
	jns	.L3
	decq	%rdx
	addq	$1000000, %rax
.L3:
	cvtsi2sdq	%rdx, %xmm1
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC0(%rip), %xmm0
	popq	%rax
	.cfi_def_cfa_offset 8
	addsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z19report_running_timev, .-_Z19report_running_timev
	.globl	_Z7seqSortPji
	.type	_Z7seqSortPji, @function
_Z7seqSortPji:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rdi, %r9
	movl	%esi, %r8d
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	subq	$104, %rsp
	.cfi_offset 15, -24
	.cfi_offset 14, -32
	.cfi_offset 13, -40
	.cfi_offset 12, -48
	.cfi_offset 3, -56
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	xorl	%eax, %eax
	movslq	%esi, %rax
	movq	%rsp, %rcx
	leaq	15(,%rax,8), %rdx
	movq	%rdx, %rax
	andq	$-4096, %rdx
	andq	$-16, %rax
	subq	%rdx, %rcx
.L7:
	cmpq	%rcx, %rsp
	je	.L8
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L7
.L8:
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L9
	orq	$0, -8(%rsp,%rax)
.L9:
	movq	%rsp, %rbx
	movl	$10, %r11d
	movl	$1, %esi
	xorl	%r13d, %r13d
	movl	$10, %r10d
	leaq	-136(%rbp), %r15
	leaq	-64(%rbp), %r12
	leal	-1(%r8), %r14d
.L17:
	leaq	-136(%rbp), %rdi
	movl	$20, %ecx
	movl	%r13d, %eax
	rep stosl
.L10:
	cmpl	%ecx, %r8d
	jle	.L25
	movl	(%r9,%rcx,4), %eax
	xorl	%edx, %edx
	incq	%rcx
	divl	%esi
	xorl	%edx, %edx
	divl	%r10d
	movl	%edx, %edx
	incq	-136(%rbp,%rdx,8)
	jmp	.L10
.L25:
	movq	%r15, %rax
.L12:
	movq	(%rax), %rdx
	addq	%rdx, 8(%rax)
	addq	$8, %rax
	cmpq	%r12, %rax
	jne	.L12
	movslq	%r14d, %rcx
.L13:
	testl	%ecx, %ecx
	js	.L26
	movl	(%r9,%rcx,4), %eax
	xorl	%edx, %edx
	movl	(%r9,%rcx,4), %edi
	decq	%rcx
	divl	%esi
	xorl	%edx, %edx
	divl	%r10d
	movl	%edx, %edx
	movq	-136(%rbp,%rdx,8), %rax
	decq	%rax
	movq	%rax, -136(%rbp,%rdx,8)
	movq	%rdi, (%rbx,%rax,8)
	jmp	.L13
.L26:
	xorl	%eax, %eax
.L15:
	cmpl	%eax, %r8d
	jle	.L27
	movq	(%rbx,%rax,8), %rdx
	movl	%edx, (%r9,%rax,4)
	incq	%rax
	jmp	.L15
.L27:
	imull	$10, %esi, %esi
	decl	%r11d
	jne	.L17
	movq	-56(%rbp), %rax
	subq	%fs:40, %rax
	je	.L18
	call	__stack_chk_fail@PLT
.L18:
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z7seqSortPji, .-_Z7seqSortPji
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"[ "
.LC2:
	.string	"%d "
.LC3:
	.string	"]\n"
	.text
	.globl	_Z10printArrayPii
	.type	_Z10printArrayPii, @function
_Z10printArrayPii:
.LFB2029:
	.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
.L29:
	cmpl	%ebx, %ebp
	jle	.L32
	movl	(%r12,%rbx,4), %edx
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbx
	call	__printf_chk@PLT
	jmp	.L29
.L32:
	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
.LFE2029:
	.size	_Z10printArrayPii, .-_Z10printArrayPii
	.globl	_Z11printArrayUPji
	.type	_Z11printArrayUPji, @function
_Z11printArrayUPji:
.LFB2030:
	.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
.L34:
	cmpl	%ebx, %ebp
	jle	.L37
	movl	(%r12,%rbx,4), %edx
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbx
	call	__printf_chk@PLT
	jmp	.L34
.L37:
	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
.LFE2030:
	.size	_Z11printArrayUPji, .-_Z11printArrayUPji
	.globl	_Z42__device_stub__Z10radix_SortPjiiiiPiS0_S0_PjiiiiPiS0_S0_
	.type	_Z42__device_stub__Z10radix_SortPjiiiiPiS0_S0_PjiiiiPiS0_S0_, @function
_Z42__device_stub__Z10radix_SortPjiiiiPiS0_S0_PjiiiiPiS0_S0_:
.LFB2057:
	.cfi_startproc
	endbr64
	subq	$200, %rsp
	.cfi_def_cfa_offset 208
	movq	208(%rsp), %rax
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movl	%esi, 36(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rax, 8(%rsp)
	movq	216(%rsp), %rax
	movl	%edx, 32(%rsp)
	leaq	56(%rsp), %rdx
	movl	%ecx, 28(%rsp)
	leaq	64(%rsp), %rcx
	movl	%r8d, 24(%rsp)
	movq	%r9, 16(%rsp)
	movq	%rax, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 184(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	36(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	28(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 168(%rsp)
	movq	%rsp, %rax
	movq	%rax, 176(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L38
	pushq	64(%rsp)
	.cfi_def_cfa_offset 216
	leaq	_Z10radix_SortPjiiiiPiS0_S0_(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 224
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 216
	popq	%rdx
	.cfi_def_cfa_offset 208
.L38:
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L40
	call	__stack_chk_fail@PLT
.L40:
	addq	$200, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2057:
	.size	_Z42__device_stub__Z10radix_SortPjiiiiPiS0_S0_PjiiiiPiS0_S0_, .-_Z42__device_stub__Z10radix_SortPjiiiiPiS0_S0_PjiiiiPiS0_S0_
	.globl	_Z10radix_SortPjiiiiPiS0_S0_
	.type	_Z10radix_SortPjiiiiPiS0_S0_, @function
_Z10radix_SortPjiiiiPiS0_S0_:
.LFB2058:
	.cfi_startproc
	endbr64
	jmp	_Z42__device_stub__Z10radix_SortPjiiiiPiS0_S0_PjiiiiPiS0_S0_
	.cfi_endproc
.LFE2058:
	.size	_Z10radix_SortPjiiiiPiS0_S0_, .-_Z10radix_SortPjiiiiPiS0_S0_
	.globl	_Z36__device_stub__Z12moveElementsPjS_iiPjS_ii
	.type	_Z36__device_stub__Z12moveElementsPjS_iiPjS_ii, @function
_Z36__device_stub__Z12moveElementsPjS_iiPjS_ii:
.LFB2059:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L43
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z12moveElementsPjS_ii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L43:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L45
	call	__stack_chk_fail@PLT
.L45:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2059:
	.size	_Z36__device_stub__Z12moveElementsPjS_iiPjS_ii, .-_Z36__device_stub__Z12moveElementsPjS_iiPjS_ii
	.globl	_Z12moveElementsPjS_ii
	.type	_Z12moveElementsPjS_ii, @function
_Z12moveElementsPjS_ii:
.LFB2060:
	.cfi_startproc
	endbr64
	jmp	_Z36__device_stub__Z12moveElementsPjS_iiPjS_ii
	.cfi_endproc
.LFE2060:
	.size	_Z12moveElementsPjS_ii, .-_Z12moveElementsPjS_ii
	.globl	_Z9sortArrayiiii
	.type	_Z9sortArrayiiii, @function
_Z9sortArrayiiii:
.LFB2031:
	.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
	movl	%esi, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%edx, %r13d
	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
	movl	%edi, %ebx
	subq	$104, %rsp
	.cfi_def_cfa_offset 160
	movslq	histogramSize(%rip), %r15
	movl	%ecx, 28(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	movl	%r15d, 8(%rsp)
	salq	$2, %r15
	movq	%r15, %rdi
	call	malloc@PLT
	movq	%r15, %rdi
	movq	%rax, %r12
	call	malloc@PLT
	movq	%r15, %rdi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	8(%rsp), %edx
	movq	%rax, %r15
	xorl	%eax, %eax
.L49:
	cmpl	%eax, %edx
	jle	.L70
	xorl	%ecx, %ecx
	movl	%ecx, (%r12,%rax,4)
	movl	%ecx, 0(%rbp,%rax,4)
	movl	%ecx, (%r15,%rax,4)
	incq	%rax
	jmp	.L49
.L70:
	movl	totalNumbers(%rip), %esi
	leaq	d_valuesList(%rip), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	histogramSize(%rip), %rsi
	leaq	32(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	histogramSize(%rip), %rsi
	leaq	40(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movslq	histogramSize(%rip), %rsi
	leaq	48(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movl	totalNumbers(%rip), %edx
	movl	$1, %ecx
	movq	valuesList(%rip), %rsi
	movq	d_valuesList(%rip), %rdi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	histogramSize(%rip), %rdx
	movq	32(%rsp), %rdi
	movq	%r12, %rsi
	movl	$1, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	histogramSize(%rip), %rdx
	movq	40(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	histogramSize(%rip), %rdx
	movq	48(%rsp), %rdi
	movq	%r15, %rsi
	movl	$1, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	leaq	Idunno(%rip), %rsi
	leaq	startTime(%rip), %rdi
	call	gettimeofday@PLT
	movl	$256, %esi
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	leal	255(%r14), %eax
	movl	$16777217, %ecx
	movl	$1, 84(%rsp)
	cltd
	salq	$8, %rcx
	idivl	%esi
	movq	%rcx, 76(%rsp)
	movq	%rcx, %rdx
	movl	$1, %ecx
	movl	%eax, 64(%rsp)
	movl	%eax, 24(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 68(%rsp)
	movq	64(%rsp), %rdi
	movl	72(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L51
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	movl	digit(%rip), %esi
	movl	%ebx, %edx
	movl	%r14d, %r8d
	movq	d_valuesList(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movl	%r13d, %ecx
	movq	48(%rsp), %r9
	call	_Z42__device_stub__Z10radix_SortPjiiiiPiS0_S0_PjiiiiPiS0_S0_
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L51:
	call	_Z19report_running_timev
	movl	totalNumbers(%rip), %edx
	addss	totalRunningTime(%rip), %xmm0
	movl	$2, %ecx
	movq	d_valuesList(%rip), %rsi
	movq	valuesList(%rip), %rdi
	salq	$2, %rdx
	movss	%xmm0, totalRunningTime(%rip)
	call	cudaMemcpy@PLT
	movslq	histogramSize(%rip), %rdx
	movq	32(%rsp), %rsi
	movq	%r12, %rdi
	movl	$2, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	histogramSize(%rip), %rdx
	movq	40(%rsp), %rsi
	movq	%rbp, %rdi
	movl	$2, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movslq	histogramSize(%rip), %rdx
	movq	48(%rsp), %rsi
	movq	%r15, %rdi
	movl	$2, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movq	d_valuesList(%rip), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movl	totalNumbers(%rip), %eax
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 16(%rsp)
	call	malloc@PLT
	movq	valuesList(%rip), %r10
	movslq	%r13d, %rsi
	leal	(%r14,%r13), %r9d
	movq	%rax, 8(%rsp)
	movl	digit(%rip), %r11d
.L52:
	cmpl	%esi, %r9d
	jle	.L71
	movl	(%r10,%rsi,4), %edi
	movl	%r11d, %ecx
	movl	%edi, %eax
.L53:
	cmpl	%ebx, %ecx
	je	.L72
	movl	%edi, %eax
	xorl	%edx, %edx
	divl	%ecx
	imull	%ecx, %eax
	movl	%eax, %r8d
	movl	%ecx, %eax
	movl	$10, %ecx
	cltd
	idivl	%ecx
	movl	%eax, %ecx
	movl	%edi, %eax
	subl	%r8d, %eax
	jmp	.L53
.L72:
	cltd
	movq	8(%rsp), %rcx
	idivl	%ebx
	cltq
	leaq	(%r15,%rax,4), %rdx
	movl	(%rdx), %eax
	decl	%eax
	movl	%eax, (%rcx,%rsi,4)
	incq	%rsi
	movl	%eax, (%rdx)
	jmp	.L52
.L71:
	movq	16(%rsp), %rsi
	leaq	d_valuesList(%rip), %rdi
	call	cudaMalloc@PLT
	movl	totalNumbers(%rip), %esi
	leaq	56(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movl	totalNumbers(%rip), %edx
	movl	$1, %ecx
	movq	valuesList(%rip), %rsi
	movq	d_valuesList(%rip), %rdi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movl	totalNumbers(%rip), %edx
	movq	8(%rsp), %rsi
	movl	$1, %ecx
	movq	56(%rsp), %rdi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	leaq	Idunno(%rip), %rsi
	leaq	startTime(%rip), %rdi
	call	gettimeofday@PLT
	movl	24(%rsp), %eax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$16777217, %edx
	movl	$1, %ecx
	movl	$1, 84(%rsp)
	movl	%eax, 64(%rsp)
	salq	$8, %rdx
	movabsq	$4294967297, %rax
	movq	%rax, 68(%rsp)
	movq	64(%rsp), %rdi
	movl	72(%rsp), %esi
	movq	%rdx, 76(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L56
	movq	56(%rsp), %rsi
	movq	d_valuesList(%rip), %rdi
	movl	%r14d, %ecx
	movl	%r13d, %edx
	call	_Z36__device_stub__Z12moveElementsPjS_iiPjS_ii
.L56:
	call	_Z19report_running_timev
	movl	totalNumbers(%rip), %edx
	xorl	%r13d, %r13d
	addss	totalRunningTime(%rip), %xmm0
	movq	d_valuesList(%rip), %rsi
	movq	valuesList(%rip), %rdi
	movl	$2, %ecx
	salq	$2, %rdx
	movss	%xmm0, totalRunningTime(%rip)
	call	cudaMemcpy@PLT
	movl	totalNumbers(%rip), %edx
	movq	56(%rsp), %rsi
	movl	$2, %ecx
	movq	8(%rsp), %rdi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movq	d_valuesList(%rip), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
.L59:
	cmpl	$1, (%r12,%r13,4)
	jle	.L57
	cmpl	$1, %ebx
	je	.L57
	xorl	%eax, %eax
	testq	%r13, %r13
	je	.L58
	movl	-4(%rbp,%r13,4), %eax
.L58:
	movl	28(%rsp), %ecx
	movl	0(%rbp,%r13,4), %esi
	movl	$10, %edi
	addl	%eax, %ecx
	subl	%eax, %esi
	movl	%ebx, %eax
	cltd
	idivl	%edi
	movl	%ecx, %edx
	movl	%eax, %edi
	call	_Z9sortArrayiiii
.L57:
	incq	%r13
	cmpq	$10, %r13
	jne	.L59
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L60
	call	__stack_chk_fail@PLT
.L60:
	addq	$104, %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
.LFE2031:
	.size	_Z9sortArrayiiii, .-_Z9sortArrayiiii
	.section	.rodata.str1.1
.LC4:
	.string	"GPU running time: \n"
.LC5:
	.string	"%f \n"
.LC6:
	.string	"CPU running time:\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	$2147483647, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	pushq	%rcx
	.cfi_def_cfa_offset 32
	movq	8(%rsi), %rdi
	call	atoi@PLT
	movl	$10, histogramSize(%rip)
	movl	%eax, totalNumbers(%rip)
	movl	%eax, %eax
	leaq	0(,%rax,4), %rbx
	movq	%rbx, %rdi
	call	malloc@PLT
	movq	%rbx, %rdi
	xorl	%ebx, %ebx
	movq	%rax, valuesList(%rip)
	call	malloc@PLT
	movl	$1, %edi
	movq	%rax, valuesList2(%rip)
	call	srand@PLT
.L74:
	cmpl	totalNumbers(%rip), %ebx
	jnb	.L79
	call	rand@PLT
	cltd
	idivl	%ebp
	movq	valuesList(%rip), %rax
	movl	%edx, (%rax,%rbx,4)
	incq	%rbx
	jmp	.L74
.L79:
	movq	valuesList(%rip), %rdx
	movq	valuesList2(%rip), %rcx
	xorl	%eax, %eax
.L76:
	cmpl	totalNumbers(%rip), %eax
	jnb	.L80
	movl	(%rdx,%rax,4), %esi
	movl	%esi, (%rcx,%rax,4)
	incq	%rax
	jmp	.L76
.L80:
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	totalNumbers(%rip), %esi
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	movl	digit(%rip), %edi
	leaq	.LC5(%rip), %rbx
	call	_Z9sortArrayiiii
	movq	%rbx, %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	totalRunningTime(%rip), %xmm0
	call	__printf_chk@PLT
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	Idunno(%rip), %rsi
	leaq	startTime(%rip), %rdi
	call	gettimeofday@PLT
	movl	totalNumbers(%rip), %esi
	movq	valuesList2(%rip), %rdi
	call	_Z7seqSortPji
	call	_Z19report_running_timev
	movq	%rbx, %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	main, .-main
	.section	.rodata.str1.1
.LC7:
	.string	"_Z12moveElementsPjS_ii"
.LC8:
	.string	"_Z10radix_SortPjiiiiPiS0_S0_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2062:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC7(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z12moveElementsPjS_ii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC8(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z10radix_SortPjiiiiPiS0_S0_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2062:
	.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
	.globl	digit
	.data
	.align 4
	.type	digit, @object
	.size	digit, 4
digit:
	.long	1000000000
	.globl	histogramSize
	.bss
	.align 4
	.type	histogramSize, @object
	.size	histogramSize, 4
histogramSize:
	.zero	4
	.globl	totalNumbers
	.align 4
	.type	totalNumbers, @object
	.size	totalNumbers, 4
totalNumbers:
	.zero	4
	.globl	totalRunningTime
	.align 4
	.type	totalRunningTime, @object
	.size	totalRunningTime, 4
totalRunningTime:
	.zero	4
	.globl	endTime
	.align 16
	.type	endTime, @object
	.size	endTime, 16
endTime:
	.zero	16
	.globl	startTime
	.align 16
	.type	startTime, @object
	.size	startTime, 16
startTime:
	.zero	16
	.globl	Idunno
	.align 8
	.type	Idunno, @object
	.size	Idunno, 8
Idunno:
	.zero	8
	.globl	d_valuesList
	.align 8
	.type	d_valuesList, @object
	.size	d_valuesList, 8
d_valuesList:
	.zero	8
	.globl	valuesList2
	.align 8
	.type	valuesList2, @object
	.size	valuesList2, 8
valuesList2:
	.zero	8
	.globl	valuesList
	.align 8
	.type	valuesList, @object
	.size	valuesList, 8
valuesList:
	.zero	8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	0
	.long	1093567616
	.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:

/*****************************************
Project 3
James Albu, Rebecca Johnson, Jacob Manfre
GPU Radix Sort Algorithm
*******************************************/


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

#define MAX 2147483647;								//largest 32bit signed integer
 //#define MAX 99;

unsigned int * valuesList;							//holds values for parallel radix sort
unsigned int * valuesList2;							//array holds values for sequential radix sort
unsigned int* d_valuesList;							//holds values for device

struct timezone Idunno;
struct timeval startTime, endTime;

float totalRunningTime = 0.00000;
unsigned int totalNumbers;							//number of data values in array
int histogramSize;
int digit = 1000000000;								//largest possible place value for 32bit signed integers

//calculates running time of the radix sort algorithm
float report_running_time() {
	long sec_diff, usec_diff;
	gettimeofday(&endTime, &Idunno);
	sec_diff = endTime.tv_sec - startTime.tv_sec;
	usec_diff= endTime.tv_usec-startTime.tv_usec;
	if(usec_diff < 0) {
		sec_diff --;
		usec_diff += 1000000;
	}

	return (float)(sec_diff*1.0 + usec_diff/1000000.0);
}

//sequentially sorts the radix sort algorithm on the CPU in order to compare its running time to GPU
void seqSort(unsigned int * array, int size){
  int i;
  long long semiSorted[size];
  int significantDigit = 1;
  int largestNum = 1000000000;
  // Loop until we reach the largest significant digit
  while (largestNum / significantDigit > 0)
  {  
    long long bucket[10] = { 0 };
    // Counts the number of "keys" or digits that will go into each bucket
    for (i = 0; i < size; i++)
      bucket[(array[i] / significantDigit) % 10]++;
    for (i = 1; i < 10; i++)
      bucket[i] += bucket[i - 1];   
    // Use the bucket to fill a "semiSorted" array
    for (i = size - 1; i >= 0; i--)
      semiSorted[--bucket[(array[i] / significantDigit) % 10]] = array[i];
    for (i = 0; i < size; i++)
      array[i] = semiSorted[i];
    // Move to next significant digit
    significantDigit *= 10;
    
  }
}

//function to print out arrays
void printArray(int * array, int size) {	
	printf("[ ");
  	for (int i = 0; i < size; i++) {
    	printf("%d ", array[i]);}
  	printf("]\n");
}

void printArrayU(unsigned int * array, int size) {	
	printf("[ ");
  	for (int i = 0; i < size; i++) {
    	printf("%d ", array[i]);
	}
  	printf("]\n");
}

//main GPU kernel
//counts the number of instances for a place value and stores in a histogram
__global__ void radix_Sort(unsigned int* valuesList, int digitMax, int digitCurrent, int startPos, int arraySize, int* histogram, int* mainOffset, int* mainOffsetChanged) {

	 int tid = threadIdx.x + blockIdx.x * blockDim.x;
	 tid += startPos;
	// take element in values at this instanced thread and find the digit 
	// we're looking for thats passed in and increment the corresponding element 
	// in the histogram
	int tempDigitMax = digitMax;
	int tempDigitCurrent = digitCurrent;
	if (tid < startPos + arraySize) {
		int num = valuesList[tid];
		while (tempDigitMax != tempDigitCurrent) {
			num = valuesList[tid] / tempDigitMax;
			num *= tempDigitMax;

			tempDigitMax /= 10;
			num = valuesList[tid] - num;
		}

		atomicAdd(&histogram[num/digitCurrent], 1);
	}
	__syncthreads();

	// find offset before values
	mainOffset[0] = histogram[0];
	mainOffsetChanged[0] = histogram[0];
	for (int i = 1; i < 10; i++) {
		mainOffsetChanged[i] = mainOffsetChanged[i-1] + histogram[i];
		mainOffset[i] = mainOffset[i-1] + histogram[i];
	}


	__syncthreads();

	return;

}

//rearragnes the array elements to correspond to the bucket they are placed in
__global__ void moveElements(unsigned int *valuesList, unsigned int *indexList, int startPos, int arraySize) {
	int tid = threadIdx.x + blockIdx.x * blockDim.x;
	tid += startPos;

	if (tid < startPos + arraySize) {
		int val = valuesList[tid];
		int index = indexList[tid] + startPos;

		__syncthreads();
		valuesList[index] = val;
	}
	__syncthreads();

	return;

}

//initializing the radix sort values and memory allocation functions
void sortArray(int dig, int totalNums, int minIndex, int prevMin) {
	int * histogram;
	int * offset;
	int * offsetAfter;

	int* d_histogram;
	int* d_offset;
	int* d_offsetAfter;

	histogram = (int*)malloc(sizeof(int)*histogramSize);
	offset = (int*)malloc(sizeof(int)*histogramSize);
	offsetAfter = (int*)malloc(sizeof(int)*histogramSize);

	// fill histogram and offset arrays with 0's
	for (int i = 0; i < histogramSize; i++) {
		histogram[i] = 0;
		offset[i] = 0;
		offsetAfter[i] = 0;
	}

	hipMalloc((void **) &d_valuesList, sizeof(unsigned int)*totalNumbers);
	hipMalloc((void**) &d_histogram, sizeof(int)*histogramSize);
	hipMalloc((void**) &d_offset, sizeof(int)*histogramSize);
	hipMalloc((void**) &d_offsetAfter, sizeof(int)*histogramSize);

	hipMemcpy(d_valuesList, valuesList, sizeof(unsigned int)*totalNumbers, hipMemcpyHostToDevice);
	hipMemcpy(d_histogram, histogram, sizeof(int)*histogramSize, hipMemcpyHostToDevice);
	hipMemcpy(d_offset, offset, sizeof(int)*histogramSize, hipMemcpyHostToDevice);
	hipMemcpy(d_offsetAfter, offsetAfter, sizeof(int)*histogramSize, hipMemcpyHostToDevice);
        
        gettimeofday(&startTime, &Idunno);
	radix_Sort<<<(totalNums+255)/256, 256>>>(d_valuesList, digit, dig, minIndex, totalNums, d_histogram, d_offset, d_offsetAfter);
	totalRunningTime = totalRunningTime + report_running_time();
	
	// copy data back to host from the device
	hipMemcpy(valuesList, d_valuesList, sizeof(unsigned int)*totalNumbers, hipMemcpyDeviceToHost);
	hipMemcpy(histogram, d_histogram, sizeof(int)*histogramSize, hipMemcpyDeviceToHost);
	hipMemcpy(offset, d_offset, sizeof(int)*histogramSize, hipMemcpyDeviceToHost);
	hipMemcpy(offsetAfter, d_offsetAfter, sizeof(int)*histogramSize, hipMemcpyDeviceToHost);
	// free memory on device
	hipFree(d_valuesList);
	hipFree(d_histogram);
	hipFree(d_offset);
	hipFree(d_offsetAfter);

	// find offset after values
	unsigned int *indexArray = (unsigned int*)malloc(sizeof(unsigned int)*totalNumbers);
	unsigned int *d_indexArray;
	for (int i = minIndex; i < minIndex + totalNums; i++) {
		// find the digit to sort by
		int num = valuesList[i];
		int tempDigit = digit;
		while (tempDigit != dig) {
			num = valuesList[i] / tempDigit;
			num *= tempDigit;

			tempDigit /= 10;
			num = valuesList[i] - num;
		}

		indexArray[i] = (offsetAfter[num/dig] - 1);
		offsetAfter[num/dig]--;
	}

	// copy main array and index array to device to rearrange values
	hipMalloc((void **) &d_valuesList, sizeof(unsigned int)*totalNumbers);
	hipMalloc((void **) &d_indexArray, sizeof(unsigned int)*totalNumbers);

	hipMemcpy(d_valuesList, valuesList, sizeof(unsigned int)*totalNumbers, hipMemcpyHostToDevice);
	hipMemcpy(d_indexArray, indexArray, sizeof(unsigned int)*totalNumbers, hipMemcpyHostToDevice);

	// printf("MIN INDEX: %d\n", minIndex);
	// printf("SIZE: %d\n", totalNums);
	// printf("DIGIT: %d\n", dig);
	// printArrayU(indexArray, totalNumbers);
 	
	gettimeofday(&startTime, &Idunno);
	// kernel call to rearrange the numbers in valuesList
	moveElements<<<(totalNums+255)/256,256>>>(d_valuesList, d_indexArray, minIndex, totalNums);
	totalRunningTime = totalRunningTime + report_running_time();

	// copy data back to host from the device
	hipMemcpy(valuesList, d_valuesList, sizeof(unsigned int)*totalNumbers, hipMemcpyDeviceToHost);
	hipMemcpy(indexArray, d_indexArray, sizeof(unsigned int)*totalNumbers, hipMemcpyDeviceToHost);
	// free memory
	hipFree(d_valuesList);
	hipFree(d_indexArray);

	// printf("HISTOGRAM:\n");
	// printArray(histogram, histogramSize);

	// printf("OFFSET BEFORE:\n");
	// printArray(offset, histogramSize);

	// printf("OFFSET AFTER:\n");
	// printArray(offsetAfter, histogramSize);

	// printf("VALUES AFTER:\n");
	// printArrayU(valuesList, totalNumbers);

	// call sortArray on each index of the histogram if that index value is greater than 1
	for (int i = 0; i < 10; i++) {
		if (histogram[i] > 1 && dig != 1) {
			int minInd;
			if (i == 0) {
				minInd = 0;
			}
			else{
				minInd = offset[i-1];
			} 

			// recursion
			sortArray(dig/10, offset[i]-minInd, minInd+prevMin, minInd+prevMin);
		}
	}
	
	return;
}

int main(int argc, char **argv) {

	totalNumbers = atoi(argv[1]);
	histogramSize = 10;

	valuesList = (unsigned int *)malloc(sizeof(unsigned int)*totalNumbers);
	valuesList2 = (unsigned int *)malloc(sizeof(unsigned int)*totalNumbers);

	srand(1);	
	// generate totalNumbers random numbers for valuesList
	for (int i = 0; i < totalNumbers; i++) {
		valuesList[i] = (int) rand()%MAX;
	}
	for (int i = 0; i < totalNumbers; i++)
		valuesList2[i] = valuesList[i];

//	printf("VALUES BEFORE:\n");
//	printArrayU(valuesList, totalNumbers);
	printf("GPU running time: \n");
	sortArray(digit, totalNumbers, 0, 0);
        printf("%f \n", totalRunningTime);
  
        printf("CPU running time:\n");
  	gettimeofday(&startTime, &Idunno);
  	seqSort(valuesList2, totalNumbers);
  	printf("%f \n", report_running_time());

//  printf("SeqSort: \n");
//  printArrayU(&valuesList2[0], totalNumbers);
  
//	printf("VALUES AFTER:\n");
//	printArrayU(valuesList, totalNumbers);

	return 0;
}

	.text
	.file	"radix_sort.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z19report_running_timev
.LCPI0_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	_Z19report_running_timev
	.type	_Z19report_running_timev,@function
_Z19report_running_timev:               # @_Z19report_running_timev
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	$endTime, %edi
	movl	$Idunno, %esi
	callq	gettimeofday
	movq	endTime(%rip), %rax
	subq	startTime(%rip), %rax
	movq	endTime+8(%rip), %rcx
	subq	startTime+8(%rip), %rcx
	leaq	1000000(%rcx), %rdx
	movq	%rcx, %rsi
	sarq	$63, %rsi
	addq	%rax, %rsi
	testq	%rcx, %rcx
	cmovnsq	%rcx, %rdx
	cvtsi2sd	%rsi, %xmm0
	cvtsi2sd	%rdx, %xmm1
	divsd	.LCPI0_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm1, %xmm0
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z19report_running_timev, .Lfunc_end0-_Z19report_running_timev
	.cfi_endproc
                                        # -- End function
	.globl	_Z7seqSortPji                   # -- Begin function _Z7seqSortPji
	.type	_Z7seqSortPji,@function
_Z7seqSortPji:                          # @_Z7seqSortPji
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	pushq	%r15
	pushq	%r14
	pushq	%rbx
	subq	$88, %rsp
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	movl	%esi, %r8d
	movq	%rsp, %r9
	leaq	15(,%r8,8), %rax
	andq	$-16, %rax
	subq	%rax, %r9
	movq	%r9, %rsp
	leaq	1(%r8), %r10
	movl	$1, %r11d
	xorps	%xmm0, %xmm0
	movl	$3435973837, %ebx               # imm = 0xCCCCCCCD
.LBB1_1:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_13 Depth 2
                                        #     Child Loop BB1_4 Depth 2
                                        #     Child Loop BB1_6 Depth 2
                                        #     Child Loop BB1_9 Depth 2
	movaps	%xmm0, -48(%rbp)
	movaps	%xmm0, -64(%rbp)
	movaps	%xmm0, -80(%rbp)
	movaps	%xmm0, -96(%rbp)
	movaps	%xmm0, -112(%rbp)
	testl	%esi, %esi
	jle	.LBB1_2
# %bb.12:                               # %.lr.ph.preheader
                                        #   in Loop: Header=BB1_1 Depth=1
	xorl	%ecx, %ecx
.LBB1_13:                               # %.lr.ph
                                        #   Parent Loop BB1_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%rdi,%rcx,4), %eax
	xorl	%edx, %edx
	divl	%r11d
                                        # kill: def $eax killed $eax def $rax
	movq	%rax, %rdx
	imulq	%rbx, %rdx
	shrq	$35, %rdx
	addl	%edx, %edx
	leal	(%rdx,%rdx,4), %edx
	subl	%edx, %eax
	incq	-112(%rbp,%rax,8)
	incq	%rcx
	cmpq	%rcx, %r8
	jne	.LBB1_13
# %bb.14:                               # %.preheader31.loopexit
                                        #   in Loop: Header=BB1_1 Depth=1
	movq	-112(%rbp), %rax
	jmp	.LBB1_3
.LBB1_2:                                #   in Loop: Header=BB1_1 Depth=1
	xorl	%eax, %eax
.LBB1_3:                                # %.preheader31.preheader
                                        #   in Loop: Header=BB1_1 Depth=1
	movl	$1, %ecx
.LBB1_4:                                # %.preheader31
                                        #   Parent Loop BB1_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	addq	-112(%rbp,%rcx,8), %rax
	movq	%rax, -112(%rbp,%rcx,8)
	incq	%rcx
	cmpq	$10, %rcx
	jne	.LBB1_4
# %bb.5:                                # %.preheader30
                                        #   in Loop: Header=BB1_1 Depth=1
	movq	%r10, %r14
	testl	%esi, %esi
	jle	.LBB1_10
.LBB1_6:                                # %.lr.ph35
                                        #   Parent Loop BB1_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	-8(%rdi,%r14,4), %ecx
	movl	%ecx, %eax
	xorl	%edx, %edx
	divl	%r11d
                                        # kill: def $eax killed $eax def $rax
	movq	%rax, %rdx
	imulq	%rbx, %rdx
	shrq	$35, %rdx
	addl	%edx, %edx
	leal	(%rdx,%rdx,4), %edx
	subl	%edx, %eax
	movq	-112(%rbp,%rax,8), %rdx
	leaq	-1(%rdx), %r15
	movq	%r15, -112(%rbp,%rax,8)
	movq	%rcx, -8(%r9,%rdx,8)
	decq	%r14
	cmpq	$1, %r14
	jg	.LBB1_6
# %bb.7:                                # %.preheader
                                        #   in Loop: Header=BB1_1 Depth=1
	testl	%esi, %esi
	jle	.LBB1_10
# %bb.8:                                # %.lr.ph37.preheader
                                        #   in Loop: Header=BB1_1 Depth=1
	xorl	%eax, %eax
.LBB1_9:                                # %.lr.ph37
                                        #   Parent Loop BB1_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%r9,%rax,8), %ecx
	movl	%ecx, (%rdi,%rax,4)
	incq	%rax
	cmpq	%rax, %r8
	jne	.LBB1_9
.LBB1_10:                               # %._crit_edge
                                        #   in Loop: Header=BB1_1 Depth=1
	addl	%r11d, %r11d
	leal	(%r11,%r11,4), %r11d
	cmpl	$1000000001, %r11d              # imm = 0x3B9ACA01
	jb	.LBB1_1
# %bb.11:
	leaq	-24(%rbp), %rsp
	popq	%rbx
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa %rsp, 8
	retq
.Lfunc_end1:
	.size	_Z7seqSortPji, .Lfunc_end1-_Z7seqSortPji
	.cfi_endproc
                                        # -- End function
	.globl	_Z10printArrayPii               # -- Begin function _Z10printArrayPii
	.type	_Z10printArrayPii,@function
_Z10printArrayPii:                      # @_Z10printArrayPii
	.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, %edi
	xorl	%eax, %eax
	callq	printf
	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
	movl	(%rbx,%r15,4), %esi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB2_2
.LBB2_3:                                # %._crit_edge
	movl	$.Lstr.1, %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	puts@PLT                        # TAILCALL
.Lfunc_end2:
	.size	_Z10printArrayPii, .Lfunc_end2-_Z10printArrayPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z11printArrayUPji              # -- Begin function _Z11printArrayUPji
	.type	_Z11printArrayUPji,@function
_Z11printArrayUPji:                     # @_Z11printArrayUPji
	.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, %edi
	xorl	%eax, %eax
	callq	printf
	testl	%ebp, %ebp
	jle	.LBB3_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%ebp, %r14d
	xorl	%r15d, %r15d
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%r15,4), %esi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB3_2
.LBB3_3:                                # %._crit_edge
	movl	$.Lstr.1, %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	puts@PLT                        # TAILCALL
.Lfunc_end3:
	.size	_Z11printArrayUPji, .Lfunc_end3-_Z11printArrayUPji
	.cfi_endproc
                                        # -- End function
	.globl	_Z25__device_stub__radix_SortPjiiiiPiS0_S0_ # -- Begin function _Z25__device_stub__radix_SortPjiiiiPiS0_S0_
	.type	_Z25__device_stub__radix_SortPjiiiiPiS0_S0_,@function
_Z25__device_stub__radix_SortPjiiiiPiS0_S0_: # @_Z25__device_stub__radix_SortPjiiiiPiS0_S0_
	.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	$144, %rsp
	.cfi_def_cfa_offset 192
	.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	12(%rsp), %rdi
	movl	%esi, (%rdi)
	leaq	8(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	movq	%rsp, %rcx
	movl	%r8d, (%rcx)
	leaq	32(%rsp), %r8
	movq	%r9, (%r8)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	192(%rsp), %rax
	movq	%rax, 48(%rbx)
	leaq	200(%rsp), %rax
	movq	%rax, 56(%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	$_Z10radix_SortPjiiiiPiS0_S0_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	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_end4:
	.size	_Z25__device_stub__radix_SortPjiiiiPiS0_S0_, .Lfunc_end4-_Z25__device_stub__radix_SortPjiiiiPiS0_S0_
	.cfi_endproc
                                        # -- End function
	.globl	_Z27__device_stub__moveElementsPjS_ii # -- Begin function _Z27__device_stub__moveElementsPjS_ii
	.type	_Z27__device_stub__moveElementsPjS_ii,@function
_Z27__device_stub__moveElementsPjS_ii:  # @_Z27__device_stub__moveElementsPjS_ii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z12moveElementsPjS_ii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	_Z27__device_stub__moveElementsPjS_ii, .Lfunc_end5-_Z27__device_stub__moveElementsPjS_ii
	.cfi_endproc
                                        # -- End function
	.globl	_Z9sortArrayiiii                # -- Begin function _Z9sortArrayiiii
	.type	_Z9sortArrayiiii,@function
_Z9sortArrayiiii:                       # @_Z9sortArrayiiii
	.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	$72, %rsp
	.cfi_def_cfa_offset 128
	.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, 60(%rsp)                  # 4-byte Spill
                                        # kill: def $edx killed $edx def $rdx
	movq	%rdx, 24(%rsp)                  # 8-byte Spill
                                        # kill: def $esi killed $esi def $rsi
	movq	%rsi, (%rsp)                    # 8-byte Spill
	movl	%edi, %ebp
	movslq	histogramSize(%rip), %r12
	leaq	(,%r12,4), %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r14
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r15
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r13
	testq	%r12, %r12
	jle	.LBB6_2
# %bb.1:                                # %.lr.ph.preheader
	movl	%r12d, %ebx
	shlq	$2, %rbx
	movq	%r14, %rdi
	xorl	%esi, %esi
	movq	%rbx, %rdx
	callq	memset@PLT
	movq	%r15, %rdi
	xorl	%esi, %esi
	movq	%rbx, %rdx
	callq	memset@PLT
	movq	%r13, %rdi
	xorl	%esi, %esi
	movq	%rbx, %rdx
	callq	memset@PLT
.LBB6_2:                                # %._crit_edge
	movl	totalNumbers(%rip), %esi
	shlq	$2, %rsi
	movl	$d_valuesList, %edi
	callq	hipMalloc
	movslq	histogramSize(%rip), %rsi
	shlq	$2, %rsi
	leaq	48(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipMalloc
	movslq	histogramSize(%rip), %rsi
	shlq	$2, %rsi
	leaq	40(%rsp), %r12
	movq	%r12, %rdi
	callq	hipMalloc
	movslq	histogramSize(%rip), %rsi
	shlq	$2, %rsi
	leaq	8(%rsp), %rdi
	callq	hipMalloc
	movq	d_valuesList(%rip), %rdi
	movq	valuesList(%rip), %rsi
	movl	totalNumbers(%rip), %edx
	shlq	$2, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rbx), %rdi
	movslq	histogramSize(%rip), %rdx
	shlq	$2, %rdx
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r12), %rdi
	movslq	histogramSize(%rip), %rdx
	shlq	$2, %rdx
	movq	%r15, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	8(%rsp), %rax
	movq	(%rax), %rdi
	movslq	histogramSize(%rip), %rdx
	shlq	$2, %rdx
	movq	%r13, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$startTime, %edi
	movl	$Idunno, %esi
	callq	gettimeofday
	movq	(%rsp), %rbx                    # 8-byte Reload
	leal	255(%rbx), %eax
	leal	510(%rbx), %ecx
	testl	%eax, %eax
	cmovnsl	%eax, %ecx
	sarl	$8, %ecx
	movabsq	$4294967552, %rdx               # imm = 0x100000100
	leaq	(%rcx,%rdx), %rdi
	addq	$-256, %rdi
	movq	%rdi, 64(%rsp)                  # 8-byte Spill
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB6_4
# %bb.3:
	movq	d_valuesList(%rip), %rdi
	movl	digit(%rip), %esi
	movq	48(%rsp), %r9
	movl	%ebp, %edx
	movq	24(%rsp), %rcx                  # 8-byte Reload
                                        # kill: def $ecx killed $ecx killed $rcx
	movl	%ebx, %r8d
	pushq	8(%rsp)
	.cfi_adjust_cfa_offset 8
	pushq	48(%rsp)
	.cfi_adjust_cfa_offset 8
	callq	_Z25__device_stub__radix_SortPjiiiiPiS0_S0_
	addq	$16, %rsp
	.cfi_adjust_cfa_offset -16
.LBB6_4:
	movss	totalRunningTime(%rip), %xmm0   # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, 16(%rsp)                 # 4-byte Spill
	callq	_Z19report_running_timev
	addss	16(%rsp), %xmm0                 # 4-byte Folded Reload
	movss	%xmm0, totalRunningTime(%rip)
	movq	valuesList(%rip), %rdi
	movq	d_valuesList(%rip), %rsi
	movl	totalNumbers(%rip), %edx
	shlq	$2, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	48(%rsp), %rsi
	movslq	histogramSize(%rip), %rdx
	shlq	$2, %rdx
	movq	%r14, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	40(%rsp), %rsi
	movslq	histogramSize(%rip), %rdx
	shlq	$2, %rdx
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rsi
	movslq	histogramSize(%rip), %rdx
	shlq	$2, %rdx
	movq	%r13, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	d_valuesList(%rip), %rdi
	callq	hipFree
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movl	totalNumbers(%rip), %edi
	shlq	$2, %rdi
	movq	%rdi, 16(%rsp)                  # 8-byte Spill
	callq	malloc
	movq	%rax, %r12
	testl	%ebx, %ebx
	jle	.LBB6_11
# %bb.5:                                # %.lr.ph93
	movq	24(%rsp), %rdx                  # 8-byte Reload
	leal	(%rdx,%rbx), %eax
	movq	valuesList(%rip), %rcx
	movl	digit(%rip), %esi
	movslq	%edx, %rdi
	movslq	%eax, %r8
.LBB6_6:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB6_8 Depth 2
	movl	(%rcx,%rdi,4), %eax
	cmpl	%ebp, %esi
	je	.LBB6_10
# %bb.7:                                # %.lr.ph89.preheader
                                        #   in Loop: Header=BB6_6 Depth=1
	movl	%esi, %edx
.LBB6_8:                                # %.lr.ph89
                                        #   Parent Loop BB6_6 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%edx, %r9d
	movslq	%edx, %rdx
	imulq	$1717986919, %rdx, %rdx         # imm = 0x66666667
	movq	%rdx, %r10
	shrq	$63, %r10
	sarq	$34, %rdx
	addl	%r10d, %edx
	cmpl	%ebp, %edx
	jne	.LBB6_8
# %bb.9:                                # %._crit_edge90
                                        #   in Loop: Header=BB6_6 Depth=1
	xorl	%edx, %edx
	divl	%r9d
	movl	%edx, %eax
.LBB6_10:                               #   in Loop: Header=BB6_6 Depth=1
	cltd
	idivl	%ebp
	cltq
	movl	(%r13,%rax,4), %edx
	decl	%edx
	movl	%edx, (%r12,%rdi,4)
	movl	%edx, (%r13,%rax,4)
	incq	%rdi
	cmpq	%r8, %rdi
	jl	.LBB6_6
.LBB6_11:                               # %._crit_edge94
	movl	$d_valuesList, %edi
	movq	16(%rsp), %rsi                  # 8-byte Reload
	callq	hipMalloc
	movl	totalNumbers(%rip), %esi
	shlq	$2, %rsi
	leaq	32(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipMalloc
	movq	d_valuesList(%rip), %rdi
	movq	valuesList(%rip), %rsi
	movl	totalNumbers(%rip), %edx
	shlq	$2, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rbx), %rdi
	movl	totalNumbers(%rip), %edx
	shlq	$2, %rdx
	movq	%r12, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$startTime, %edi
	movl	$Idunno, %esi
	callq	gettimeofday
	movq	64(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	movabsq	$4294967552, %rdx               # imm = 0x100000100
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB6_13
# %bb.12:
	movq	d_valuesList(%rip), %rdi
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdx                  # 8-byte Reload
                                        # kill: def $edx killed $edx killed $rdx
	movq	(%rsp), %rcx                    # 8-byte Reload
                                        # kill: def $ecx killed $ecx killed $rcx
	callq	_Z27__device_stub__moveElementsPjS_ii
.LBB6_13:
	movss	totalRunningTime(%rip), %xmm0   # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%rsp)                   # 4-byte Spill
	callq	_Z19report_running_timev
	addss	(%rsp), %xmm0                   # 4-byte Folded Reload
	movss	%xmm0, totalRunningTime(%rip)
	movq	valuesList(%rip), %rdi
	movq	d_valuesList(%rip), %rsi
	movl	totalNumbers(%rip), %edx
	shlq	$2, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	32(%rsp), %rsi
	movl	totalNumbers(%rip), %edx
	shlq	$2, %rdx
	movq	%r12, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	d_valuesList(%rip), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	movslq	%ebp, %rax
	imulq	$1717986919, %rax, %rbx         # imm = 0x66666667
	movq	%rbx, %rax
	shrq	$63, %rax
	sarq	$34, %rbx
	addl	%eax, %ebx
	xorl	%r12d, %r12d
	movl	60(%rsp), %r13d                 # 4-byte Reload
.LBB6_14:                               # =>This Inner Loop Header: Depth=1
	cmpl	$1, %ebp
	je	.LBB6_20
# %bb.15:                               #   in Loop: Header=BB6_14 Depth=1
	cmpl	$2, (%r14,%r12,4)
	jl	.LBB6_20
# %bb.16:                               #   in Loop: Header=BB6_14 Depth=1
	testq	%r12, %r12
	je	.LBB6_17
# %bb.18:                               #   in Loop: Header=BB6_14 Depth=1
	movl	-4(%r15,%r12,4), %ecx
	jmp	.LBB6_19
.LBB6_17:                               #   in Loop: Header=BB6_14 Depth=1
	xorl	%ecx, %ecx
.LBB6_19:                               #   in Loop: Header=BB6_14 Depth=1
	movl	(%r15,%r12,4), %esi
	subl	%ecx, %esi
	addl	%r13d, %ecx
	movl	%ebx, %edi
	movl	%ecx, %edx
	callq	_Z9sortArrayiiii
.LBB6_20:                               #   in Loop: Header=BB6_14 Depth=1
	incq	%r12
	cmpq	$10, %r12
	jne	.LBB6_14
# %bb.21:
	addq	$72, %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_end6:
	.size	_Z9sortArrayiiii, .Lfunc_end6-_Z9sortArrayiiii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, totalNumbers(%rip)
	movl	$10, histogramSize(%rip)
	movl	%eax, %ebx
	shlq	$2, %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, valuesList(%rip)
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, valuesList2(%rip)
	movl	$1, %edi
	callq	srand
	cmpl	$0, totalNumbers(%rip)
	je	.LBB7_6
# %bb.1:                                # %.lr.ph.preheader
	xorl	%ebx, %ebx
.LBB7_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	movq	%rax, %rcx
	shlq	$30, %rcx
	addq	%rax, %rcx
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$61, %rcx
	addl	%edx, %ecx
	addl	%ecx, %eax
	shll	$31, %ecx
	addl	%eax, %ecx
	movq	valuesList(%rip), %rax
	movl	%ecx, (%rax,%rbx,4)
	incq	%rbx
	movl	totalNumbers(%rip), %eax
	cmpq	%rax, %rbx
	jb	.LBB7_2
# %bb.3:                                # %.preheader
	testl	%eax, %eax
	je	.LBB7_6
# %bb.4:                                # %.lr.ph11
	movq	valuesList(%rip), %rax
	movq	valuesList2(%rip), %rcx
	xorl	%edx, %edx
.LBB7_5:                                # =>This Inner Loop Header: Depth=1
	movl	(%rax,%rdx,4), %esi
	movl	%esi, (%rcx,%rdx,4)
	incq	%rdx
	movl	totalNumbers(%rip), %esi
	cmpq	%rsi, %rdx
	jb	.LBB7_5
.LBB7_6:                                # %._crit_edge
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movl	digit(%rip), %edi
	movl	totalNumbers(%rip), %esi
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	callq	_Z9sortArrayiiii
	cvtss2sd	totalRunningTime(%rip), %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	$startTime, %edi
	movl	$Idunno, %esi
	callq	gettimeofday
	movq	valuesList2(%rip), %rdi
	movl	totalNumbers(%rip), %esi
	callq	_Z7seqSortPji
	callq	_Z19report_running_timev
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end7:
	.size	main, .Lfunc_end7-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB8_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB8_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z10radix_SortPjiiiiPiS0_S0_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z12moveElementsPjS_ii, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_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	valuesList,@object              # @valuesList
	.bss
	.globl	valuesList
	.p2align	3, 0x0
valuesList:
	.quad	0
	.size	valuesList, 8

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

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

	.type	Idunno,@object                  # @Idunno
	.globl	Idunno
	.p2align	2, 0x0
Idunno:
	.zero	8
	.size	Idunno, 8

	.type	startTime,@object               # @startTime
	.globl	startTime
	.p2align	3, 0x0
startTime:
	.zero	16
	.size	startTime, 16

	.type	endTime,@object                 # @endTime
	.globl	endTime
	.p2align	3, 0x0
endTime:
	.zero	16
	.size	endTime, 16

	.type	totalRunningTime,@object        # @totalRunningTime
	.globl	totalRunningTime
	.p2align	2, 0x0
totalRunningTime:
	.long	0x00000000                      # float 0
	.size	totalRunningTime, 4

	.type	totalNumbers,@object            # @totalNumbers
	.globl	totalNumbers
	.p2align	2, 0x0
totalNumbers:
	.long	0                               # 0x0
	.size	totalNumbers, 4

	.type	histogramSize,@object           # @histogramSize
	.globl	histogramSize
	.p2align	2, 0x0
histogramSize:
	.long	0                               # 0x0
	.size	histogramSize, 4

	.type	digit,@object                   # @digit
	.data
	.globl	digit
	.p2align	2, 0x0
digit:
	.long	1000000000                      # 0x3b9aca00
	.size	digit, 4

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

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

	.type	_Z10radix_SortPjiiiiPiS0_S0_,@object # @_Z10radix_SortPjiiiiPiS0_S0_
	.section	.rodata,"a",@progbits
	.globl	_Z10radix_SortPjiiiiPiS0_S0_
	.p2align	3, 0x0
_Z10radix_SortPjiiiiPiS0_S0_:
	.quad	_Z25__device_stub__radix_SortPjiiiiPiS0_S0_
	.size	_Z10radix_SortPjiiiiPiS0_S0_, 8

	.type	_Z12moveElementsPjS_ii,@object  # @_Z12moveElementsPjS_ii
	.globl	_Z12moveElementsPjS_ii
	.p2align	3, 0x0
_Z12moveElementsPjS_ii:
	.quad	_Z27__device_stub__moveElementsPjS_ii
	.size	_Z12moveElementsPjS_ii, 8

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

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z10radix_SortPjiiiiPiS0_S0_"
	.size	.L__unnamed_1, 29

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z12moveElementsPjS_ii"
	.size	.L__unnamed_2, 23

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

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

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

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"GPU running time: "
	.size	.Lstr.2, 19

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"CPU running time:"
	.size	.Lstr.3, 18

	.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 _Z25__device_stub__radix_SortPjiiiiPiS0_S0_
	.addrsig_sym _Z27__device_stub__moveElementsPjS_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym d_valuesList
	.addrsig_sym Idunno
	.addrsig_sym startTime
	.addrsig_sym endTime
	.addrsig_sym _Z10radix_SortPjiiiiPiS0_S0_
	.addrsig_sym _Z12moveElementsPjS_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z10radix_SortPjiiiiPiS0_S0_ ; -- Begin function _Z10radix_SortPjiiiiPiS0_S0_
	.globl	_Z10radix_SortPjiiiiPiS0_S0_
	.p2align	8
	.type	_Z10radix_SortPjiiiiPiS0_S0_,@function
_Z10radix_SortPjiiiiPiS0_S0_:           ; @_Z10radix_SortPjiiiiPiS0_S0_
; %bb.0:
	s_clause 0x2
	s_load_b32 s12, s[0:1], 0x3c
	s_load_b256 s[4:11], s[0:1], 0x8
	s_load_b64 s[2:3], s[0:1], 0x28
	s_waitcnt lgkmcnt(0)
	s_and_b32 s12, s12, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s12, v[0:1]
	v_cmp_gt_i32_e32 vcc_lo, s7, v1
	s_and_saveexec_b32 s7, vcc_lo
	s_cbranch_execz .LBB0_5
; %bb.1:
	s_load_b64 s[0:1], s[0:1], 0x0
	v_add_nc_u32_e32 v0, s6, v1
	s_cmp_eq_u32 s4, s5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v1, 31, v0
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s0, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_load_b32 v0, v[0:1], off
	s_cbranch_scc1 .LBB0_4
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_mul_hi_i32 s1, s4, 0x66666667
	s_mov_b32 s0, s4
	s_lshr_b32 s4, s1, 31
	s_ashr_i32 s1, s1, 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s4, s1, s4
	s_cmp_lg_u32 s4, s5
	s_cbranch_scc1 .LBB0_2
; %bb.3:                                ; %._crit_edge
	v_cvt_f32_u32_e32 v1, s0
	s_sub_i32 s1, 0, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	v_cvt_u32_f32_e32 v1, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v2, s1, v1
	v_mul_hi_u32 v2, v1, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v1, v1, v2
	s_waitcnt vmcnt(0)
	v_mul_hi_u32 v1, v0, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v1, v1, s0
	v_sub_nc_u32_e32 v0, v0, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_subrev_nc_u32_e32 v1, s0, v0
	v_cmp_le_u32_e32 vcc_lo, s0, v0
	v_cndmask_b32_e32 v0, v0, v1, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_subrev_nc_u32_e32 v1, s0, v0
	v_cmp_le_u32_e32 vcc_lo, s0, v0
	v_cndmask_b32_e32 v0, v0, v1, vcc_lo
.LBB0_4:
	s_ashr_i32 s0, s5, 31
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_ashrrev_i32_e32 v3, 31, v0
	s_add_i32 s1, s5, s0
	s_xor_b32 s1, s1, s0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_cvt_f32_u32_e32 v1, s1
	s_sub_i32 s4, 0, s1
	v_add_nc_u32_e32 v0, v0, v3
	v_rcp_iflag_f32_e32 v1, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_xor_b32_e32 v0, v0, v3
	v_xor_b32_e32 v3, s0, v3
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	v_cvt_u32_f32_e32 v1, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v2, s4, v1
	v_mul_hi_u32 v2, v1, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v1, v1, v2
	v_mul_hi_u32 v1, v0, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v2, v1, s1
	v_sub_nc_u32_e32 v0, v0, v2
	v_add_nc_u32_e32 v2, 1, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_subrev_nc_u32_e32 v4, s1, v0
	v_cmp_le_u32_e32 vcc_lo, s1, v0
	v_dual_cndmask_b32 v0, v0, v4 :: v_dual_cndmask_b32 v1, v1, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_le_u32_e32 vcc_lo, s1, v0
	v_add_nc_u32_e32 v2, 1, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cndmask_b32_e32 v0, v1, v2, vcc_lo
	v_mov_b32_e32 v2, 1
	v_xor_b32_e32 v0, v0, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_nc_u32_e32 v0, v0, v3
	v_ashrrev_i32_e32 v1, 31, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	v_add_co_u32 v0, vcc_lo, s8, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s9, v1, vcc_lo
	global_atomic_add_u32 v[0:1], v2, off
.LBB0_5:                                ; %Flow81
	s_or_b32 exec_lo, exec_lo, s7
	v_mov_b32_e32 v0, 0
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_add_u32 s4, s8, 4
	global_load_b32 v1, v0, s[8:9]
	s_mov_b64 s[0:1], 0
	s_addc_u32 s5, s9, 0
	s_waitcnt vmcnt(0)
	s_clause 0x1
	global_store_b32 v0, v1, s[10:11]
	global_store_b32 v0, v1, s[2:3]
.LBB0_6:                                ; =>This Inner Loop Header: Depth=1
	s_add_u32 s6, s2, s0
	s_addc_u32 s7, s3, s1
	s_add_u32 s8, s4, s0
	s_addc_u32 s9, s5, s1
	s_clause 0x1
	global_load_b32 v1, v0, s[6:7]
	global_load_b32 v2, v0, s[8:9]
	s_add_u32 s12, s10, s0
	s_addc_u32 s13, s11, s1
	s_add_u32 s0, s0, 4
	s_addc_u32 s1, s1, 0
	s_cmp_eq_u32 s0, 36
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v1, v2, v1
	global_store_b32 v0, v1, s[6:7] offset:4
	s_clause 0x1
	global_load_b32 v1, v0, s[12:13]
	global_load_b32 v2, v0, s[8:9]
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v1, v2, v1
	global_store_b32 v0, v1, s[12:13] offset:4
	s_cbranch_scc0 .LBB0_6
; %bb.7:
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z10radix_SortPjiiiiPiS0_S0_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 304
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 5
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z10radix_SortPjiiiiPiS0_S0_, .Lfunc_end0-_Z10radix_SortPjiiiiPiS0_S0_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 708
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.protected	_Z12moveElementsPjS_ii  ; -- Begin function _Z12moveElementsPjS_ii
	.globl	_Z12moveElementsPjS_ii
	.p2align	8
	.type	_Z12moveElementsPjS_ii,@function
_Z12moveElementsPjS_ii:                 ; @_Z12moveElementsPjS_ii
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x24
	s_load_b64 s[2:3], s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s4, v[0:1]
	v_cmp_gt_i32_e32 vcc_lo, s3, v1
	s_and_saveexec_b32 s3, vcc_lo
	s_cbranch_execz .LBB1_2
; %bb.1:
	s_load_b128 s[4:7], s[0:1], 0x0
	v_add_nc_u32_e32 v0, s2, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v1, 31, v0
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v2, vcc_lo, s6, v0
	v_add_co_ci_u32_e32 v3, vcc_lo, s7, v1, vcc_lo
	v_add_co_u32 v0, vcc_lo, s4, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v1, vcc_lo
	global_load_b32 v2, v[2:3], off
	global_load_b32 v3, v[0:1], off
	s_waitcnt vmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_add_nc_u32_e32 v0, s2, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v1, 31, v0
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s4, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v1, vcc_lo
	global_store_b32 v[0:1], v3, off
.LBB1_2:
	s_or_b32 exec_lo, exec_lo, s3
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z12moveElementsPjS_ii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.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 4
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end1:
	.size	_Z12moveElementsPjS_ii, .Lfunc_end1-_Z12moveElementsPjS_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 216
; NumSgprs: 18
; NumVgprs: 4
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 4
; Occupancy: 16
; WaveLimiterHint : 1
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         32
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         40
        .size:           8
        .value_kind:     global_buffer
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z10radix_SortPjiiiiPiS0_S0_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z10radix_SortPjiiiiPiS0_S0_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z12moveElementsPjS_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z12moveElementsPjS_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     4
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
