#include <assert.h> 
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <cuda_profiler_api.h>
// #include <cutil_inline.h>

// Convenience function for checking CUDA runtime API results
// can be wrapped around any runtime API call. No-op in release builds.
inline
cudaError_t checkCuda(cudaError_t result)
{
#if defined(DEBUG) || defined(_DEBUG)
  if (result != cudaSuccess) {
    fprintf(stderr, "CUDA Runtime Error: %s\n", cudaGetErrorString(result));
    assert(result == cudaSuccess);
  }
#endif
  return result;
}

 
 #define MAX_THREADS    128 

 int*   r_values;
 int*   d_values;

 // initialize data set
 void Init(int* values, int i, int N) {
    srand( time(NULL) );
        printf("\n------------------------------\n");

        if (i == 0) {
        // Uniform distribution
                printf("Data set distribution: Uniform\n");
                for (int x = 0; x < N; ++x) {
                        values[x] = rand() % 100;
                        //printf("%d ", values[x]);
                }
        }
        else if (i == 1) {
        // Gaussian distribution
        #define MEAN    100
        #define STD_DEV 5
                printf("Data set distribution: Gaussian\n");
                float r;
                for (int x = 0; x < N; ++x) {
                        r  = (rand()%3 - 1) + (rand()%3 - 1) + (rand()%3 - 1);
                        values[x] = int( round(r * STD_DEV + MEAN) );
                        //printf("%d ", values[x]);
                }
        }
        else if (i == 2) {
        // Bucket distribution
                printf("Data set distribution: Bucket\n");
                int j = 0;
                for (int x = 0; x < N; ++x, ++j) {
                        if (j / 20 < 1)
                                values[x] = rand() % 20;
                        else if (j / 20 < 2)
                                values[x] = rand() % 20 + 20;
                        else if (j / 20 < 3)
                                values[x] = rand() % 20 + 40;
                        else if (j / 20 < 4)
                                values[x] = rand() % 20 + 60;
                        else if (j / 20 < 5)
                                values[x] = rand() % 20 + 80;
                        if (j == 100)
                                j = 0;
                        //printf("%d ", values[x]);
                }
        }
        else if (i == 3) {
        // Sorted distribution
                printf("Data set distribution: Sorted\n");
                /*for (int x = 0; x < N; ++x)
                        printf("%d ", values[x]);
        */
        }
    else if (i == 4) {
        // Zero distribution
                printf("Data set distribution: Zero\n");
                int r = rand() % 100;
                for (int x = 0; x < N; ++x) {
                        values[x] = r;
                        //printf("%d ", values[x]);
                }
        }
        printf("\n");
}

 // Kernel function
 __global__ static void quicksort(int* values, int N) {
 #define MAX_LEVELS 300

    int pivot, L, R;
    int idx =  threadIdx.x + blockIdx.x * blockDim.x;
    int start[MAX_LEVELS];
    int end[MAX_LEVELS];

    start[idx] = idx;
    end[idx] = N - 1;
    while (idx >= 0) {
        L = start[idx];
        R = end[idx];
        if (L < R) {
            pivot = values[L];
            while (L < R) {
                while (values[R] >= pivot && L < R)
                    R--;
                if(L < R)
                    values[L++] = values[R];
                while (values[L] < pivot && L < R)
                    L++;
                if (L < R)
                    values[R--] = values[L];
            }
            values[L] = pivot;
            start[idx + 1] = L + 1;
            end[idx + 1] = end[idx];
            end[idx++] = L;
            if (end[idx] - start[idx] > end[idx - 1] - start[idx - 1]) {
                            // swap start[idx] and start[idx-1]
                            int tmp = start[idx];
                            start[idx] = start[idx - 1];
                            start[idx - 1] = tmp;

                            // swap end[idx] and end[idx-1]
                            tmp = end[idx];
                            end[idx] = end[idx - 1];
                            end[idx - 1] = tmp;
                    }

        }
        else
            idx--;
    }
}
 
 // program main
 int main(int argc, char **argv) {

    if (argc != 3 ) {
        fprintf(stderr, "Syntax: %s <Vector size Width>  <CacheConfL1> \n", argv[0]);
            return EXIT_FAILURE;
    }
    
    int N = atoi(argv[1]);
    int CacheConfL1 = atoi(argv[2]);
    size_t size = N * sizeof(long int);
    
    printf("./bitonic_sort starting with %d numbers...\n", N);
    srand( time(NULL) );

    int devId = 0;
    checkCuda( cudaSetDevice(devId) );
        cudaDeviceReset();
    
    cudaDeviceProp prop;
    checkCuda( cudaGetDeviceProperties(&prop, devId) );
    printf("Device: %s\n", prop.name);

    
    // allocate host memory
    r_values = (int*)malloc(size);
    
    // allocate device memory
        cudaMalloc((void**)&d_values, size);

    // allocate threads per block
        const unsigned int cThreadsPerBlock = 128;
                
    /* Types of data sets to be sorted:
         *      1. Normal distribution
         *      2. Gaussian distribution
         *      3. Bucket distribution
         *      4. Sorted Distribution
         *      5. Zero Distribution
         */

    for (int i = 0; i < 1; ++i) {
                // initialize data set
                Init(r_values, i, N);

        // copy data to device  
        cudaMemcpy(d_values, r_values, size, cudaMemcpyHostToDevice) ;

        printf("Beginning kernel execution...\n");

        if (CacheConfL1 == 1){
            cudaFuncSetCacheConfig(quicksort, cudaFuncCachePreferShared);
        }
        else if (CacheConfL1 == 2){
            cudaFuncSetCacheConfig(quicksort, cudaFuncCachePreferEqual);
        }
        else if (CacheConfL1 == 3){
            cudaFuncSetCacheConfig(quicksort, cudaFuncCachePreferL1);
        }
        else {
            cudaFuncSetCacheConfig(quicksort, cudaFuncCachePreferNone);
        }
        
         cudaThreadSynchronize() ;  
    
        // execute kernel
        cudaProfilerStart(); 
        quicksort <<< MAX_THREADS / cThreadsPerBlock, MAX_THREADS / cThreadsPerBlock, cThreadsPerBlock >>> (d_values, N);
        cudaProfilerStop();   

        cudaThreadSynchronize() ;
            
        // copy data back to host
        cudaMemcpy(r_values, d_values, size, cudaMemcpyDeviceToHost) ;
    
        // test
        printf("Assertion started\n");
        for (int x = 0; x < N - 1; x++) 
            assert(r_values[x] <= r_values[x + 1]);
        printf("Assertion Finished");
    }
    
    // free memory
    cudaFree(d_values);
    free(r_values);
    
    //cutilExit(argc, argv);
    cudaThreadExit();
}

	.file	"tmpxft_0024d6f2_00000000-6_quickSort.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.type	_ZL29__device_stub__Z9quicksortPiiPii, @function
_ZL29__device_stub__Z9quicksortPiiPii:
.LFB2054:
	.cfi_startproc
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movl	%esi, 4(%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_ZL9quicksortPii(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L2:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2054:
	.size	_ZL29__device_stub__Z9quicksortPiiPii, .-_ZL29__device_stub__Z9quicksortPiiPii
	.type	_ZL9quicksortPii, @function
_ZL9quicksortPii:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_ZL29__device_stub__Z9quicksortPiiPii
	.cfi_endproc
.LFE2055:
	.size	_ZL9quicksortPii, .-_ZL9quicksortPii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"\n------------------------------\n"
.LC1:
	.string	"Data set distribution: Uniform\n"
.LC2:
	.string	"Data set distribution: Gaussian\n"
.LC5:
	.string	"Data set distribution: Bucket\n"
.LC6:
	.string	"Data set distribution: Sorted\n"
.LC7:
	.string	"Data set distribution: Zero\n"
.LC8:
	.string	"\n"
	.text
	.globl	_Z4InitPiii
	.type	_Z4InitPiii, @function
_Z4InitPiii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movl	%edx, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movq	%rdi, %rbx
	xorl	%edi, %edi
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
	xorl	%eax, %eax
	movl	$2, %edi
	leaq	.LC0(%rip), %rsi
	call	__printf_chk@PLT
	cmpl	$4, %ebp
	ja	.L9
	leaq	.L11(%rip), %rdx
	movslq	(%rdx,%rbp,4), %rax
	addq	%rdx, %rax
	notrack jmp	*%rax
	.section	.rodata
	.align 4
	.align 4
.L11:
	.long	.L15-.L11
	.long	.L14-.L11
	.long	.L13-.L11
	.long	.L12-.L11
	.long	.L10-.L11
	.text
.L15:
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%ebp, %ebp
	call	__printf_chk@PLT
	movl	$100, %r12d
.L16:
	cmpl	%ebp, %r13d
	jle	.L9
	call	rand@PLT
	cltd
	idivl	%r12d
	movl	%edx, (%rbx,%rbp,4)
	incq	%rbp
	jmp	.L16
.L14:
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%r14d, %r14d
	call	__printf_chk@PLT
.L18:
	cmpl	%r14d, %r13d
	jle	.L9
	call	rand@PLT
	movl	$3, %ebp
	movl	%eax, %r12d
	call	rand@PLT
	movl	%eax, %ecx
	movl	%r12d, %eax
	cltd
	idivl	%ebp
	movl	%ecx, %eax
	movl	%edx, %esi
	cltd
	idivl	%ebp
	leal	-2(%rsi,%rdx), %r12d
	call	rand@PLT
	cltd
	idivl	%ebp
	leal	-1(%r12,%rdx), %eax
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC3(%rip), %xmm0
	addss	.LC4(%rip), %xmm0
	cvttss2sil	%xmm0, %eax
	movl	%eax, (%rbx,%r14,4)
	incq	%r14
	jmp	.L18
.L13:
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%ebp, %ebp
	call	__printf_chk@PLT
	xorl	%r12d, %r12d
	movl	$20, %r14d
.L21:
	cmpl	%r12d, %r13d
	jle	.L9
	cmpl	$19, %ebp
	jg	.L22
	call	rand@PLT
	cltd
	idivl	%r14d
	jmp	.L34
.L22:
	cmpl	$39, %ebp
	jg	.L24
	call	rand@PLT
	cltd
	idivl	%r14d
	addl	$20, %edx
	jmp	.L34
.L24:
	cmpl	$59, %ebp
	jg	.L25
	call	rand@PLT
	cltd
	idivl	%r14d
	addl	$40, %edx
	jmp	.L34
.L25:
	cmpl	$79, %ebp
	jg	.L26
	call	rand@PLT
	movl	$20, %ecx
	cltd
	idivl	%ecx
	addl	$60, %edx
	jmp	.L34
.L26:
	cmpl	$99, %ebp
	jg	.L27
	call	rand@PLT
	movl	$20, %ecx
	cltd
	idivl	%ecx
	addl	$80, %edx
.L34:
	movl	%edx, (%rbx,%r12,4)
	jmp	.L28
.L27:
	cmpl	$100, %ebp
	jne	.L28
	xorl	%ebp, %ebp
.L28:
	incl	%ebp
	incq	%r12
	jmp	.L21
.L12:
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L9
.L10:
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	call	rand@PLT
	movl	$100, %ecx
	cltd
	idivl	%ecx
	xorl	%eax, %eax
.L30:
	cmpl	%eax, %r13d
	jle	.L9
	movl	%edx, (%rbx,%rax,4)
	incq	%rax
	jmp	.L30
.L9:
	popq	%rbx
	.cfi_def_cfa_offset 40
	leaq	.LC8(%rip), %rsi
	popq	%rbp
	.cfi_def_cfa_offset 32
	movl	$2, %edi
	popq	%r12
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z4InitPiii, .-_Z4InitPiii
	.section	.rodata.str1.1
.LC9:
	.string	"Syntax: %s <Vector size Width>  <CacheConfL1> \n"
.LC10:
	.string	"./bitonic_sort starting with %d numbers...\n"
.LC11:
	.string	"Device: %s\n"
.LC12:
	.string	"Beginning kernel execution...\n"
.LC13:
	.string	"Assertion started\n"
.LC14:
	.string	"Assertion Finished"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rsi, %rbx
	subq	$1080, %rsp
	.cfi_def_cfa_offset 1120
	movq	%fs:40, %rax
	movq	%rax, 1064(%rsp)
	xorl	%eax, %eax
	cmpl	$3, %edi
	je	.L36
	movq	(%rsi), %rcx
	movq	stderr(%rip), %rdi
	leaq	.LC9(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %eax
	jmp	.L35
.L36:
	movq	8(%rsi), %rdi
	leaq	32(%rsp), %r13
	call	atoi@PLT
	movq	16(%rbx), %rdi
	movl	%eax, %ebp
	call	atoi@PLT
	movl	%ebp, %edx
	movl	$2, %edi
	movslq	%ebp, %rbx
	leaq	.LC10(%rip), %rsi
	movl	%eax, %r12d
	xorl	%eax, %eax
	salq	$3, %rbx
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
	xorl	%edi, %edi
	call	cudaSetDevice@PLT
	call	cudaDeviceReset@PLT
	xorl	%esi, %esi
	movq	%r13, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movq	%r13, %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC11(%rip), %rsi
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	call	malloc@PLT
	movq	%rbx, %rsi
	leaq	d_values(%rip), %rdi
	movq	%rax, r_values(%rip)
	call	cudaMalloc@PLT
	movq	r_values(%rip), %rdi
	xorl	%esi, %esi
	movl	%ebp, %edx
	call	_Z4InitPiii
	movq	r_values(%rip), %rsi
	movl	$1, %ecx
	movq	%rbx, %rdx
	movq	d_values(%rip), %rdi
	call	cudaMemcpy@PLT
	leaq	.LC12(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	cmpl	$1, %r12d
	movl	$1, %esi
	leaq	_ZL9quicksortPii(%rip), %rdi
	je	.L45
	movl	$3, %esi
	cmpl	$2, %r12d
	je	.L45
	movl	$2, %esi
	cmpl	$3, %r12d
	je	.L45
	xorl	%esi, %esi
.L45:
	call	cudaFuncSetCacheConfig@PLT
	call	cudaThreadSynchronize@PLT
	call	cudaProfilerStart@PLT
	xorl	%r9d, %r9d
	movl	$128, %r8d
	movabsq	$4294967297, %rdi
	movq	%rdi, %rdx
	movl	$1, %ecx
	movl	$1, %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L42
	movq	d_values(%rip), %rdi
	movl	%ebp, %esi
	call	_ZL29__device_stub__Z9quicksortPiiPii
.L42:
	call	cudaProfilerStop@PLT
	call	cudaThreadSynchronize@PLT
	movq	d_values(%rip), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdx
	movq	r_values(%rip), %rdi
	call	cudaMemcpy@PLT
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC14(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	d_values(%rip), %rdi
	call	cudaFree@PLT
	movq	r_values(%rip), %rdi
	call	free@PLT
	call	cudaThreadExit@PLT
	xorl	%eax, %eax
.L35:
	movq	1064(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L43
	call	__stack_chk_fail@PLT
.L43:
	addq	$1080, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	main, .-main
	.section	.rodata.str1.1
.LC15:
	.string	"_Z9quicksortPii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC15(%rip), %rdx
	movq	%rax, %rdi
	leaq	_ZL9quicksortPii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2057:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	d_values
	.bss
	.align 8
	.type	d_values, @object
	.size	d_values, 8
d_values:
	.zero	8
	.globl	r_values
	.align 8
	.type	r_values, @object
	.size	r_values, 8
r_values:
	.zero	8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC3:
	.long	1084227584
	.align 4
.LC4:
	.long	1120403456
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <assert.h> 
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <hip/hip_runtime_api.h>
// #include <cutil_inline.h>

// Convenience function for checking CUDA runtime API results
// can be wrapped around any runtime API call. No-op in release builds.
inline
hipError_t checkCuda(hipError_t result)
{
#if defined(DEBUG) || defined(_DEBUG)
  if (result != hipSuccess) {
    fprintf(stderr, "CUDA Runtime Error: %s\n", hipGetErrorString(result));
    assert(result == hipSuccess);
  }
#endif
  return result;
}

 
 #define MAX_THREADS    128 

 int*   r_values;
 int*   d_values;

 // initialize data set
 void Init(int* values, int i, int N) {
    srand( time(NULL) );
        printf("\n------------------------------\n");

        if (i == 0) {
        // Uniform distribution
                printf("Data set distribution: Uniform\n");
                for (int x = 0; x < N; ++x) {
                        values[x] = rand() % 100;
                        //printf("%d ", values[x]);
                }
        }
        else if (i == 1) {
        // Gaussian distribution
        #define MEAN    100
        #define STD_DEV 5
                printf("Data set distribution: Gaussian\n");
                float r;
                for (int x = 0; x < N; ++x) {
                        r  = (rand()%3 - 1) + (rand()%3 - 1) + (rand()%3 - 1);
                        values[x] = int( round(r * STD_DEV + MEAN) );
                        //printf("%d ", values[x]);
                }
        }
        else if (i == 2) {
        // Bucket distribution
                printf("Data set distribution: Bucket\n");
                int j = 0;
                for (int x = 0; x < N; ++x, ++j) {
                        if (j / 20 < 1)
                                values[x] = rand() % 20;
                        else if (j / 20 < 2)
                                values[x] = rand() % 20 + 20;
                        else if (j / 20 < 3)
                                values[x] = rand() % 20 + 40;
                        else if (j / 20 < 4)
                                values[x] = rand() % 20 + 60;
                        else if (j / 20 < 5)
                                values[x] = rand() % 20 + 80;
                        if (j == 100)
                                j = 0;
                        //printf("%d ", values[x]);
                }
        }
        else if (i == 3) {
        // Sorted distribution
                printf("Data set distribution: Sorted\n");
                /*for (int x = 0; x < N; ++x)
                        printf("%d ", values[x]);
        */
        }
    else if (i == 4) {
        // Zero distribution
                printf("Data set distribution: Zero\n");
                int r = rand() % 100;
                for (int x = 0; x < N; ++x) {
                        values[x] = r;
                        //printf("%d ", values[x]);
                }
        }
        printf("\n");
}

 // Kernel function
 __global__ static void quicksort(int* values, int N) {
 #define MAX_LEVELS 300

    int pivot, L, R;
    int idx =  threadIdx.x + blockIdx.x * blockDim.x;
    int start[MAX_LEVELS];
    int end[MAX_LEVELS];

    start[idx] = idx;
    end[idx] = N - 1;
    while (idx >= 0) {
        L = start[idx];
        R = end[idx];
        if (L < R) {
            pivot = values[L];
            while (L < R) {
                while (values[R] >= pivot && L < R)
                    R--;
                if(L < R)
                    values[L++] = values[R];
                while (values[L] < pivot && L < R)
                    L++;
                if (L < R)
                    values[R--] = values[L];
            }
            values[L] = pivot;
            start[idx + 1] = L + 1;
            end[idx + 1] = end[idx];
            end[idx++] = L;
            if (end[idx] - start[idx] > end[idx - 1] - start[idx - 1]) {
                            // swap start[idx] and start[idx-1]
                            int tmp = start[idx];
                            start[idx] = start[idx - 1];
                            start[idx - 1] = tmp;

                            // swap end[idx] and end[idx-1]
                            tmp = end[idx];
                            end[idx] = end[idx - 1];
                            end[idx - 1] = tmp;
                    }

        }
        else
            idx--;
    }
}
 
 // program main
 int main(int argc, char **argv) {

    if (argc != 3 ) {
        fprintf(stderr, "Syntax: %s <Vector size Width>  <CacheConfL1> \n", argv[0]);
            return EXIT_FAILURE;
    }
    
    int N = atoi(argv[1]);
    int CacheConfL1 = atoi(argv[2]);
    size_t size = N * sizeof(long int);
    
    printf("./bitonic_sort starting with %d numbers...\n", N);
    srand( time(NULL) );

    int devId = 0;
    checkCuda( hipSetDevice(devId) );
        hipDeviceReset();
    
    hipDeviceProp_t prop;
    checkCuda( hipGetDeviceProperties(&prop, devId) );
    printf("Device: %s\n", prop.name);

    
    // allocate host memory
    r_values = (int*)malloc(size);
    
    // allocate device memory
        hipMalloc((void**)&d_values, size);

    // allocate threads per block
        const unsigned int cThreadsPerBlock = 128;
                
    /* Types of data sets to be sorted:
         *      1. Normal distribution
         *      2. Gaussian distribution
         *      3. Bucket distribution
         *      4. Sorted Distribution
         *      5. Zero Distribution
         */

    for (int i = 0; i < 1; ++i) {
                // initialize data set
                Init(r_values, i, N);

        // copy data to device  
        hipMemcpy(d_values, r_values, size, hipMemcpyHostToDevice) ;

        printf("Beginning kernel execution...\n");

        if (CacheConfL1 == 1){
            hipFuncSetCacheConfig(reinterpret_cast<const void*>(quicksort), hipFuncCachePreferShared);
        }
        else if (CacheConfL1 == 2){
            hipFuncSetCacheConfig(reinterpret_cast<const void*>(quicksort), hipFuncCachePreferEqual);
        }
        else if (CacheConfL1 == 3){
            hipFuncSetCacheConfig(reinterpret_cast<const void*>(quicksort), hipFuncCachePreferL1);
        }
        else {
            hipFuncSetCacheConfig(reinterpret_cast<const void*>(quicksort), hipFuncCachePreferNone);
        }
        
         hipDeviceSynchronize() ;  
    
        // execute kernel
        hipProfilerStart(); 
        quicksort <<< MAX_THREADS / cThreadsPerBlock, MAX_THREADS / cThreadsPerBlock, cThreadsPerBlock >>> (d_values, N);
        hipProfilerStop();   

        hipDeviceSynchronize() ;
            
        // copy data back to host
        hipMemcpy(r_values, d_values, size, hipMemcpyDeviceToHost) ;
    
        // test
        printf("Assertion started\n");
        for (int x = 0; x < N - 1; x++) 
            assert(r_values[x] <= r_values[x + 1]);
        printf("Assertion Finished");
    }
    
    // free memory
    hipFree(d_values);
    free(r_values);
    
    //cutilExit(argc, argv);
    hipDeviceReset();
}

	.text
	.file	"quickSort.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z4InitPiii
.LCPI0_0:
	.long	0x40a00000                      # float 5
.LCPI0_1:
	.long	0x42c80000                      # float 100
	.text
	.globl	_Z4InitPiii
	.type	_Z4InitPiii,@function
_Z4InitPiii:                            # @_Z4InitPiii
	.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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.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	%edx, %ebp
	movl	%esi, %r14d
	movq	%rdi, %rbx
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	movl	$.Lstr, %edi
	callq	puts@PLT
	cmpl	$4, %r14d
	ja	.LBB0_26
# %bb.1:
	movl	%r14d, %eax
	jmpq	*.LJTI0_0(,%rax,8)
.LBB0_2:
	movl	$.Lstr.5, %edi
	callq	puts@PLT
	testl	%ebp, %ebp
	jle	.LBB0_26
# %bb.3:                                # %.lr.ph62.preheader
	movl	%ebp, %r14d
	xorl	%r15d, %r15d
.LBB0_4:                                # %.lr.ph62
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	cltq
	imulq	$1374389535, %rax, %rcx         # imm = 0x51EB851F
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$37, %rcx
	addl	%edx, %ecx
	imull	$100, %ecx, %ecx
	subl	%ecx, %eax
	movl	%eax, (%rbx,%r15,4)
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB0_4
	jmp	.LBB0_26
.LBB0_23:
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	callq	rand
	testl	%ebp, %ebp
	jle	.LBB0_26
# %bb.24:                               # %.lr.ph.preheader
	cltq
	imulq	$1374389535, %rax, %rcx         # imm = 0x51EB851F
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$37, %rcx
	addl	%edx, %ecx
	imull	$100, %ecx, %ecx
	subl	%ecx, %eax
	movl	%ebp, %ecx
	xorl	%edx, %edx
.LBB0_25:                               # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%eax, (%rbx,%rdx,4)
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB0_25
	jmp	.LBB0_26
.LBB0_8:
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	testl	%ebp, %ebp
	jle	.LBB0_26
# %bb.9:                                # %.lr.ph58.preheader
	movl	%ebp, %r14d
	xorl	%r15d, %r15d
	movl	$1, %ebp
	xorl	%r12d, %r12d
.LBB0_10:                               # %.lr.ph58
                                        # =>This Inner Loop Header: Depth=1
	cmpl	$19, %r12d
	jg	.LBB0_12
# %bb.11:                               #   in Loop: Header=BB0_10 Depth=1
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$35, %rcx
	addl	%edx, %ecx
	shll	$2, %ecx
	leal	(%rcx,%rcx,4), %ecx
	subl	%ecx, %eax
	jmp	.LBB0_20
.LBB0_12:                               #   in Loop: Header=BB0_10 Depth=1
	cmpl	$39, %r12d
	ja	.LBB0_14
# %bb.13:                               #   in Loop: Header=BB0_10 Depth=1
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$35, %rcx
	addl	%edx, %ecx
	shll	$2, %ecx
	leal	(%rcx,%rcx,4), %ecx
	negl	%ecx
	addl	%ecx, %eax
	addl	$20, %eax
	jmp	.LBB0_20
.LBB0_14:                               #   in Loop: Header=BB0_10 Depth=1
	cmpl	$59, %r12d
	ja	.LBB0_16
# %bb.15:                               #   in Loop: Header=BB0_10 Depth=1
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$35, %rcx
	addl	%edx, %ecx
	shll	$2, %ecx
	leal	(%rcx,%rcx,4), %ecx
	negl	%ecx
	addl	%ecx, %eax
	addl	$40, %eax
	jmp	.LBB0_20
.LBB0_16:                               #   in Loop: Header=BB0_10 Depth=1
	cmpl	$79, %r12d
	ja	.LBB0_18
# %bb.17:                               #   in Loop: Header=BB0_10 Depth=1
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$35, %rcx
	addl	%edx, %ecx
	shll	$2, %ecx
	leal	(%rcx,%rcx,4), %ecx
	negl	%ecx
	addl	%ecx, %eax
	addl	$60, %eax
	jmp	.LBB0_20
.LBB0_18:                               #   in Loop: Header=BB0_10 Depth=1
	cmpl	$99, %r12d
	ja	.LBB0_21
# %bb.19:                               #   in Loop: Header=BB0_10 Depth=1
	callq	rand
	cltq
	imulq	$1717986919, %rax, %rcx         # imm = 0x66666667
	movq	%rcx, %rdx
	shrq	$63, %rdx
	sarq	$35, %rcx
	addl	%edx, %ecx
	shll	$2, %ecx
	leal	(%rcx,%rcx,4), %ecx
	negl	%ecx
	addl	%ecx, %eax
	addl	$80, %eax
.LBB0_20:                               # %.sink.split
                                        #   in Loop: Header=BB0_10 Depth=1
	movl	%eax, (%rbx,%r15,4)
.LBB0_21:                               #   in Loop: Header=BB0_10 Depth=1
	incq	%r15
	cmpl	$100, %r12d
	leal	1(%r12), %eax
	cmovel	%ebp, %eax
	movl	%eax, %r12d
	cmpq	%r15, %r14
	jne	.LBB0_10
	jmp	.LBB0_26
.LBB0_22:
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	jmp	.LBB0_26
.LBB0_5:
	movl	$.Lstr.4, %edi
	callq	puts@PLT
	testl	%ebp, %ebp
	jle	.LBB0_26
# %bb.6:                                # %.lr.ph60.preheader
	movl	%ebp, %r14d
	xorl	%r15d, %r15d
.LBB0_7:                                # %.lr.ph60
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	movslq	%eax, %r13
	imulq	$1431655766, %r13, %rax         # imm = 0x55555556
	movq	%rax, %rcx
	shrq	$63, %rcx
	shrq	$32, %rax
	addl	%ecx, %eax
	leal	(%rax,%rax,2), %eax
	subl	%eax, %r13d
	callq	rand
	movslq	%eax, %r12
	imulq	$1431655766, %r12, %rax         # imm = 0x55555556
	movq	%rax, %rcx
	shrq	$63, %rcx
	shrq	$32, %rax
	addl	%ecx, %eax
	leal	(%rax,%rax,2), %eax
	subl	%eax, %r12d
	addl	%r13d, %r12d
	callq	rand
	cltq
	imulq	$1431655766, %rax, %rcx         # imm = 0x55555556
	movq	%rcx, %rdx
	shrq	$63, %rdx
	shrq	$32, %rcx
	addl	%edx, %ecx
	leal	(%rcx,%rcx,2), %ecx
	subl	%ecx, %eax
	addl	%r12d, %eax
	addl	$-3, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI0_0(%rip), %xmm0
	addss	.LCPI0_1(%rip), %xmm0
	callq	roundf@PLT
	cvttss2si	%xmm0, %eax
	movl	%eax, (%rbx,%r15,4)
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB0_7
.LBB0_26:                               # %.loopexit
	movl	$10, %edi
	addq	$8, %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
	jmp	putchar@PLT                     # TAILCALL
.Lfunc_end0:
	.size	_Z4InitPiii, .Lfunc_end0-_Z4InitPiii
	.cfi_endproc
	.section	.rodata,"a",@progbits
	.p2align	3, 0x0
.LJTI0_0:
	.quad	.LBB0_2
	.quad	.LBB0_5
	.quad	.LBB0_8
	.quad	.LBB0_22
	.quad	.LBB0_23
                                        # -- End function
	.text
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$1472, %rsp                     # imm = 0x5C0
	.cfi_def_cfa_offset 1520
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	cmpl	$3, %edi
	jne	.LBB1_1
# %bb.2:                                # %.critedge
	movq	8(%rbx), %rdi
	callq	atoi
	movl	%eax, %ebp
	movq	16(%rbx), %rdi
	callq	atoi
	movl	%eax, %r14d
	movslq	%ebp, %rbx
	shlq	$3, %rbx
	xorl	%r15d, %r15d
	movl	$.L.str.8, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	xorl	%edi, %edi
	callq	hipSetDevice
	callq	hipDeviceReset
	movq	%rsp, %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movl	$.L.str.9, %edi
	movq	%r12, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, r_values(%rip)
	movl	$d_values, %edi
	movq	%rbx, %rsi
	callq	hipMalloc
	movq	r_values(%rip), %rdi
	xorl	%esi, %esi
	movl	%ebp, %edx
	callq	_Z4InitPiii
	movq	d_values(%rip), %rdi
	movq	r_values(%rip), %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$.Lstr.6, %edi
	callq	puts@PLT
	decl	%r14d
	cmpl	$2, %r14d
	ja	.LBB1_4
# %bb.3:                                # %switch.lookup
	movl	%r14d, %eax
	movl	.Lswitch.table.main(,%rax,4), %r15d
.LBB1_4:
	movl	$_ZL9quicksortPii, %edi
	movl	%r15d, %esi
	callq	hipFuncSetCacheConfig
	callq	hipDeviceSynchronize
	callq	hipProfilerStart
	xorl	%r14d, %r14d
	movabsq	$4294967297, %rdi               # imm = 0x100000001
	movl	$128, %r8d
	movl	$1, %esi
	movq	%rdi, %rdx
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_6
# %bb.5:
	movq	d_values(%rip), %rdi
	movl	%ebp, %esi
	callq	_ZL24__device_stub__quicksortPii
.LBB1_6:
	callq	hipProfilerStop
	callq	hipDeviceSynchronize
	movq	r_values(%rip), %rdi
	movq	d_values(%rip), %rsi
	movq	%rbx, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	$.Lstr.7, %edi
	callq	puts@PLT
	movl	$.L.str.12, %edi
	xorl	%eax, %eax
	callq	printf
	movq	d_values(%rip), %rdi
	callq	hipFree
	movq	r_values(%rip), %rdi
	callq	free
	callq	hipDeviceReset
.LBB1_7:
	movl	%r14d, %eax
	addq	$1472, %rsp                     # imm = 0x5C0
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB1_1:
	.cfi_def_cfa_offset 1520
	movq	stderr(%rip), %rdi
	movq	(%rbx), %rdx
	movl	$.L.str.7, %esi
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %r14d
	jmp	.LBB1_7
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	_ZL24__device_stub__quicksortPii,@function # -- Begin function _ZL24__device_stub__quicksortPii
_ZL24__device_stub__quicksortPii:       # @_ZL24__device_stub__quicksortPii
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rcx
	movl	%esi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%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	$_ZL9quicksortPii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	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_end2:
	.size	_ZL24__device_stub__quicksortPii, .Lfunc_end2-_ZL24__device_stub__quicksortPii
	.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	.LBB3_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB3_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_ZL9quicksortPii, %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_end3:
	.size	__hip_module_ctor, .Lfunc_end3-__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	.LBB4_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
.LBB4_2:
	retq
.Lfunc_end4:
	.size	__hip_module_dtor, .Lfunc_end4-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	r_values,@object                # @r_values
	.bss
	.globl	r_values
	.p2align	3, 0x0
r_values:
	.quad	0
	.size	r_values, 8

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

	.type	.L.str.7,@object                # @.str.7
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.7:
	.asciz	"Syntax: %s <Vector size Width>  <CacheConfL1> \n"
	.size	.L.str.7, 48

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"./bitonic_sort starting with %d numbers...\n"
	.size	.L.str.8, 44

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Device: %s\n"
	.size	.L.str.9, 12

	.type	_ZL9quicksortPii,@object        # @_ZL9quicksortPii
	.section	.rodata,"a",@progbits
	.p2align	3, 0x0
_ZL9quicksortPii:
	.quad	_ZL24__device_stub__quicksortPii
	.size	_ZL9quicksortPii, 8

	.type	.L.str.12,@object               # @.str.12
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.12:
	.asciz	"Assertion Finished"
	.size	.L.str.12, 19

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_ZL9quicksortPii"
	.size	.L__unnamed_1, 17

	.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	"\n------------------------------"
	.size	.Lstr, 32

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Data set distribution: Zero"
	.size	.Lstr.1, 28

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Data set distribution: Sorted"
	.size	.Lstr.2, 30

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"Data set distribution: Bucket"
	.size	.Lstr.3, 30

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"Data set distribution: Gaussian"
	.size	.Lstr.4, 32

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"Data set distribution: Uniform"
	.size	.Lstr.5, 31

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"Beginning kernel execution..."
	.size	.Lstr.6, 30

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"Assertion started"
	.size	.Lstr.7, 18

	.type	.Lswitch.table.main,@object     # @switch.table.main
	.section	.rodata,"a",@progbits
	.p2align	2, 0x0
.Lswitch.table.main:
	.long	1                               # 0x1
	.long	3                               # 0x3
	.long	2                               # 0x2
	.size	.Lswitch.table.main, 12

	.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 _ZL24__device_stub__quicksortPii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym d_values
	.addrsig_sym _ZL9quicksortPii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.section	.text._ZL9quicksortPii,"axG",@progbits,_ZL9quicksortPii,comdat
	.globl	_ZL9quicksortPii                ; -- Begin function _ZL9quicksortPii
	.p2align	8
	.type	_ZL9quicksortPii,@function
_ZL9quicksortPii:                       ; @_ZL9quicksortPii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x1c
	s_load_b32 s3, s[0:1], 0x8
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	s_add_i32 s2, s3, -1
	v_lshlrev_b32_e32 v0, 2, v1
	v_mov_b32_e32 v2, s2
	s_mov_b32 s2, exec_lo
	s_delay_alu instid0(VALU_DEP_2)
	v_add_nc_u32_e32 v3, 16, v0
	v_add_nc_u32_e32 v0, 0x4c0, v0
	scratch_store_b32 v3, v1, off
	scratch_store_b32 v0, v2, off
	v_cmpx_lt_i32_e32 -1, v1
	s_cbranch_execz .LBB0_19
; %bb.1:                                ; %.lr.ph.preheader
	s_load_b64 s[2:3], s[0:1], 0x0
	s_mov_b32 s8, 0
	s_waitcnt lgkmcnt(0)
	s_add_u32 s4, s2, 4
	s_addc_u32 s5, s3, 0
	s_add_u32 s6, s2, -4
	s_addc_u32 s7, s3, -1
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_6 Depth 2
                                        ;       Child Loop BB0_7 Depth 3
                                        ;       Child Loop BB0_11 Depth 3
	v_lshlrev_b32_e32 v2, 2, v1
	s_mov_b32 s0, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_add_nc_u32_e32 v0, 16, v2
	v_add_nc_u32_e32 v12, 0x4c0, v2
	scratch_load_b32 v3, v0, off
	scratch_load_b32 v2, v12, off
	s_waitcnt vmcnt(0)
	v_cmpx_ge_i32_e64 v3, v2
	s_xor_b32 s0, exec_lo, s0
; %bb.3:                                ;   in Loop: Header=BB0_2 Depth=1
	v_add_nc_u32_e32 v1, -1, v1
                                        ; implicit-def: $vgpr0
                                        ; implicit-def: $vgpr2
                                        ; implicit-def: $vgpr12
                                        ; implicit-def: $vgpr3
; %bb.4:                                ; %Flow130
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_and_not1_saveexec_b32 s9, s0
	s_cbranch_execz .LBB0_18
; %bb.5:                                ;   in Loop: Header=BB0_2 Depth=1
	v_ashrrev_i32_e32 v4, 31, v3
	v_mov_b32_e32 v6, v2
	s_mov_b32 s10, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[4:5], 2, v[3:4]
	v_add_co_u32 v4, vcc_lo, s2, v4
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v5, vcc_lo, s3, v5, vcc_lo
	global_load_b32 v13, v[4:5], off
	v_mov_b32_e32 v4, v3
.LBB0_6:                                ; %.preheader
                                        ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Loop Header: Depth=2
                                        ;       Child Loop BB0_7 Depth 3
                                        ;       Child Loop BB0_11 Depth 3
	v_ashrrev_i32_e32 v7, 31, v6
	v_mov_b32_e32 v14, v6
	s_mov_b32 s12, 0
                                        ; implicit-def: $sgpr11
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[7:8], 2, v[6:7]
	v_add_co_u32 v7, vcc_lo, s4, v7
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v8, vcc_lo, s5, v8, vcc_lo
.LBB0_7:                                ;   Parent Loop BB0_2 Depth=1
                                        ;     Parent Loop BB0_6 Depth=2
                                        ; =>    This Inner Loop Header: Depth=3
	global_load_b32 v9, v[7:8], off offset:-4
	v_mov_b32_e32 v6, v14
	v_add_co_u32 v7, s0, v7, -4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e64 v8, s0, -1, v8, s0
	v_cmp_ge_i32_e32 vcc_lo, v4, v6
	v_cmp_lt_i32_e64 s0, v4, v6
	v_add_nc_u32_e32 v14, -1, v6
	s_waitcnt vmcnt(0)
	v_cmp_lt_i32_e64 s1, v9, v13
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s1, vcc_lo, s1
	s_and_b32 s1, exec_lo, s1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	s_or_b32 s12, s1, s12
	s_and_not1_b32 s1, s11, exec_lo
	s_and_b32 s0, s0, exec_lo
	s_or_b32 s11, s1, s0
	s_and_not1_b32 exec_lo, exec_lo, s12
	s_cbranch_execnz .LBB0_7
; %bb.8:                                ;   in Loop: Header=BB0_6 Depth=2
	s_or_b32 exec_lo, exec_lo, s12
	s_and_saveexec_b32 s0, s11
	s_cbranch_execz .LBB0_10
; %bb.9:                                ;   in Loop: Header=BB0_6 Depth=2
	v_ashrrev_i32_e32 v5, 31, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[10:11], 2, v[4:5]
	v_add_nc_u32_e32 v4, 1, v4
	v_add_co_u32 v10, vcc_lo, s2, v10
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v11, vcc_lo, s3, v11, vcc_lo
	global_store_b32 v[10:11], v9, off
.LBB0_10:                               ; %.preheader93
                                        ;   in Loop: Header=BB0_6 Depth=2
	s_or_b32 exec_lo, exec_lo, s0
	v_ashrrev_i32_e32 v5, 31, v4
	v_mov_b32_e32 v9, v4
	s_mov_b32 s12, 0
                                        ; implicit-def: $sgpr11
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[10:11], 2, v[4:5]
	v_add_co_u32 v10, vcc_lo, s6, v10
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v11, vcc_lo, s7, v11, vcc_lo
.LBB0_11:                               ;   Parent Loop BB0_2 Depth=1
                                        ;     Parent Loop BB0_6 Depth=2
                                        ; =>    This Inner Loop Header: Depth=3
	global_load_b32 v5, v[10:11], off offset:4
	v_mov_b32_e32 v4, v9
	v_add_co_u32 v10, s0, v10, 4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e64 v11, s0, 0, v11, s0
	v_cmp_ge_i32_e32 vcc_lo, v4, v6
	v_cmp_lt_i32_e64 s0, v4, v6
	v_add_nc_u32_e32 v9, 1, v4
	s_waitcnt vmcnt(0)
	v_cmp_ge_i32_e64 s1, v5, v13
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s1, vcc_lo, s1
	s_and_b32 s1, exec_lo, s1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	s_or_b32 s12, s1, s12
	s_and_not1_b32 s1, s11, exec_lo
	s_and_b32 s0, s0, exec_lo
	s_or_b32 s11, s1, s0
	s_and_not1_b32 exec_lo, exec_lo, s12
	s_cbranch_execnz .LBB0_11
; %bb.12:                               ;   in Loop: Header=BB0_6 Depth=2
	s_or_b32 exec_lo, exec_lo, s12
	s_and_saveexec_b32 s0, s11
	s_cbranch_execz .LBB0_14
; %bb.13:                               ;   in Loop: Header=BB0_6 Depth=2
	v_mov_b32_e32 v6, v14
	global_store_b32 v[7:8], v5, off
.LBB0_14:                               ;   in Loop: Header=BB0_6 Depth=2
	s_or_b32 exec_lo, exec_lo, s0
	v_cmp_ge_i32_e32 vcc_lo, v4, v6
	s_or_b32 s10, vcc_lo, s10
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s10
	s_cbranch_execnz .LBB0_6
; %bb.15:                               ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s10
	v_sub_nc_u32_e32 v6, v2, v9
	v_sub_nc_u32_e32 v7, v4, v3
	v_mov_b32_e32 v5, v2
	s_mov_b32 s0, exec_lo
	global_store_b32 v[10:11], v13, off
	scratch_store_b32 v0, v9, off offset:4
	scratch_store_b64 v12, v[4:5], off
	v_cmpx_gt_i32_e64 v6, v7
	s_cbranch_execz .LBB0_17
; %bb.16:                               ;   in Loop: Header=BB0_2 Depth=1
	v_dual_mov_b32 v10, v3 :: v_dual_mov_b32 v3, v4
	scratch_store_b64 v0, v[9:10], off
	scratch_store_b64 v12, v[2:3], off
.LBB0_17:                               ; %Flow
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s0
	v_add_nc_u32_e32 v1, 1, v1
.LBB0_18:                               ; %Flow131
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s9
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_cmp_gt_i32_e32 vcc_lo, 0, v1
	s_or_b32 s8, vcc_lo, s8
	s_and_not1_b32 exec_lo, exec_lo, s8
	s_cbranch_execnz .LBB0_2
.LBB0_19:                               ; %._crit_edge
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _ZL9quicksortPii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 2416
		.amdhsa_kernarg_size 272
		.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 1
		.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 15
		.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
	.section	.text._ZL9quicksortPii,"axG",@progbits,_ZL9quicksortPii,comdat
.Lfunc_end0:
	.size	_ZL9quicksortPii, .Lfunc_end0-_ZL9quicksortPii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 756
; NumSgprs: 18
; NumVgprs: 15
; ScratchSize: 2416
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 15
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 1
; 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:         16
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _ZL9quicksortPii
    .private_segment_fixed_size: 2416
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _ZL9quicksortPii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     15
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
