#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 50000
 void printArr( int arr[], int n )
{
    int i;
    for ( i = 0; i < n; ++i )
        printf( "%d ", arr[i] );
}
__device__ int d_size;

__global__ void partition (int *arr, int *arr_l, int *arr_h, int n)
{
    int z = blockIdx.x*blockDim.x+threadIdx.x;
    d_size = 0;
    __syncthreads();
    if (z<n)
      {
        int h = arr_h[z];
        int l = arr_l[z];
        int x = arr[h];
        int i = (l - 1);
        int temp;
        for (int j = l; j <= h- 1; j++)
          {
            if (arr[j] <= x)
              {
                i++;
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
              }
          }
        temp = arr[i+1];
        arr[i+1] = arr[h];
        arr[h] = temp;
        int p = (i + 1);
        if (p-1 > l)
          {
            int ind = atomicAdd(&d_size, 1);
            arr_l[ind] = l;
            arr_h[ind] = p-1;  
          }
        if ( p+1 < h )
          {
            int ind = atomicAdd(&d_size, 1);
            arr_l[ind] = p+1;
            arr_h[ind] = h; 
          }
      }
}
 
void quickSortIterative (int arr[], int l, int h)
{
    int lstack[ h - l + 1 ], hstack[ h - l + 1];
 
    int top = -1, *d_d, *d_l, *d_h;
 
    lstack[ ++top ] = l;
    hstack[ top ] = h;

    cudaMalloc(&d_d, (h-l+1)*sizeof(int));
    cudaMemcpy(d_d, arr,(h-l+1)*sizeof(int),cudaMemcpyHostToDevice);

    cudaMalloc(&d_l, (h-l+1)*sizeof(int));
    cudaMemcpy(d_l, lstack,(h-l+1)*sizeof(int),cudaMemcpyHostToDevice);
    cudaMalloc(&d_h, (h-l+1)*sizeof(int));
    cudaMemcpy(d_h, hstack,(h-l+1)*sizeof(int),cudaMemcpyHostToDevice);
    int n_t = 1;
    int n_b = 1;
    int n_i = 1; 
    while ( n_i > 0 )
    {
        partition<<<n_b,n_t>>>( d_d, d_l, d_h, n_i);
        int answer;
        cudaMemcpyFromSymbol(&answer, d_size, sizeof(int), 0, cudaMemcpyDeviceToHost); 
        if (answer < 1024)
          {
            n_t = answer;
          }
        else
          {
            n_t = 1024;
            n_b = answer/n_t + (answer%n_t==0?0:1);
          }
        n_i = answer;
        cudaMemcpy(arr, d_d,(h-l+1)*sizeof(int),cudaMemcpyDeviceToHost);
    }
}
 

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

      
       int *arr;
       int numElements; 
    
    FILE *inp1 = fopen(argv[1], "r");
    fscanf(inp1, "%d", &numElements);
    
    printf("\nInput length = %d\n",numElements);
    arr= new int[numElements];
    for(int i = 0; i < numElements; ++i){
	fscanf(inp1, "%d", &arr[i]);
	
    }
    
   /* printf("\nInput\n");    
    for(int i=0;i<numElements;i++){
    	printf("%d ",arr[i]);
    }
*/
    int start_s=clock();
    quickSortIterative( arr, 0, numElements);
    int stop_s=clock();
    
    
  
    
    
    
    FILE *op = fopen(argv[2], "r");
    fscanf(op, "%d", &numElements);
    int *output;
    output=new int[numElements];
    for(int i = 0; i < numElements; ++i){
	fscanf(op, "%d", &output[i]);
    }
    int flag=0;
    printf("\n");
    for(int i=0;i<numElements;i++){
    	if(output[i]!=arr[i+1]){
    		printf("\nSolution wrong Expecting : %d but got : %d\n",output[i],arr[i]);
    		flag=1;
    	}
    }
    if(flag==0){
    printf("\nSolution is Correct !!!\n");
    printf("\nTime :  %f s \n",(stop_s-start_s)/double(CLOCKS_PER_SEC));
    }
    
    fclose(op);
    fclose(inp1);
    
    //printf("\nOutput\n");
    //printArr( arr, numElements);

    return 0;
}

	.file	"tmpxft_0038ddb4_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
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d "
	.text
	.globl	_Z8printArrPii
	.type	_Z8printArrPii, @function
_Z8printArrPii:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC0(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L3:
	cmpl	%ebx, %ebp
	jle	.L7
	movl	(%r12,%rbx,4), %edx
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbx
	call	__printf_chk@PLT
	jmp	.L3
.L7:
	popq	%rax
	.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
.LFE2027:
	.size	_Z8printArrPii, .-_Z8printArrPii
	.globl	_Z33__device_stub__Z9partitionPiS_S_iPiS_S_i
	.type	_Z33__device_stub__Z9partitionPiS_S_iPiS_S_i, @function
_Z33__device_stub__Z9partitionPiS_S_iPiS_S_i:
.LFB2054:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 4(%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	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%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	.L8
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z9partitionPiS_S_i(%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
.L8:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2054:
	.size	_Z33__device_stub__Z9partitionPiS_S_iPiS_S_i, .-_Z33__device_stub__Z9partitionPiS_S_iPiS_S_i
	.globl	_Z9partitionPiS_S_i
	.type	_Z9partitionPiS_S_i, @function
_Z9partitionPiS_S_i:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_Z33__device_stub__Z9partitionPiS_S_iPiS_S_i
	.cfi_endproc
.LFE2055:
	.size	_Z9partitionPiS_S_i, .-_Z9partitionPiS_S_i
	.globl	_Z18quickSortIterativePiii
	.type	_Z18quickSortIterativePiii, @function
_Z18quickSortIterativePiii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%esi, %ecx
	movl	%edx, %esi
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	pushq	%r15
	pushq	%r14
	pushq	%r13
	pushq	%r12
	pushq	%rbx
	.cfi_offset 15, -24
	.cfi_offset 14, -32
	.cfi_offset 13, -40
	.cfi_offset 12, -48
	.cfi_offset 3, -56
	movl	%edx, %ebx
	subl	%ecx, %ebx
	incl	%ebx
	subq	$88, %rsp
	movq	%rdi, -120(%rbp)
	movslq	%ebx, %rbx
	salq	$2, %rbx
	movq	%rsp, %rdi
	movq	%fs:40, %rax
	movq	%rax, -56(%rbp)
	xorl	%eax, %eax
	leaq	15(%rbx), %rax
	movq	%rax, %r8
	movq	%rax, %rdx
	andq	$-4096, %r8
	andq	$-16, %rdx
	subq	%r8, %rdi
.L14:
	cmpq	%rdi, %rsp
	je	.L15
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L14
.L15:
	andl	$4095, %edx
	subq	%rdx, %rsp
	testq	%rdx, %rdx
	je	.L16
	orq	$0, -8(%rsp,%rdx)
.L16:
	leaq	3(%rsp), %r12
	movq	%rax, %rdx
	movq	%rsp, %r8
	andq	$-4096, %rax
	movq	%r12, %rdi
	andq	$-16, %rdx
	andq	$-4, %r12
	subq	%rax, %r8
	shrq	$2, %rdi
.L17:
	cmpq	%r8, %rsp
	je	.L18
	subq	$4096, %rsp
	orq	$0, 4088(%rsp)
	jmp	.L17
.L18:
	movq	%rdx, %rax
	andl	$4095, %eax
	subq	%rax, %rsp
	testq	%rax, %rax
	je	.L19
	orq	$0, -8(%rsp,%rax)
.L19:
	leaq	3(%rsp), %r14
	movl	$1, %r13d
	movl	%ecx, 0(,%rdi,4)
	leaq	-104(%rbp), %rdi
	movabsq	$4294967297, %r15
	movq	%r14, %rax
	andq	$-4, %r14
	shrq	$2, %rax
	movl	%esi, 0(,%rax,4)
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	-120(%rbp), %rsi
	movq	-104(%rbp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	-96(%rbp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	-96(%rbp), %rdi
	movq	%rbx, %rdx
	movq	%r12, %rsi
	movl	$1, %ecx
	movl	$1, %r12d
	call	cudaMemcpy@PLT
	leaq	-88(%rbp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	-88(%rbp), %rdi
	movq	%r14, %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	movl	$1, %r14d
	call	cudaMemcpy@PLT
.L22:
	movq	%r15, -64(%rbp)
	movl	-60(%rbp), %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%r15, -76(%rbp)
	movl	-72(%rbp), %esi
	movl	%r13d, -68(%rbp)
	movq	-68(%rbp), %rdx
	movl	%r14d, -80(%rbp)
	movq	-80(%rbp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L20
	movq	-88(%rbp), %rdx
	movq	-96(%rbp), %rsi
	movl	%r12d, %ecx
	movq	-104(%rbp), %rdi
	call	_Z33__device_stub__Z9partitionPiS_S_iPiS_S_i
.L20:
	leaq	-68(%rbp), %rdi
	movl	$2, %r8d
	xorl	%ecx, %ecx
	movl	$4, %edx
	leaq	_ZL6d_size(%rip), %rsi
	call	cudaMemcpyFromSymbol@PLT
	movl	-68(%rbp), %r12d
	movl	%r12d, %r13d
	cmpl	$1023, %r12d
	jle	.L21
	xorl	%r14d, %r14d
	movl	%r12d, %eax
	testl	$1023, %r12d
	movl	$1024, %r13d
	setne	%r14b
	sarl	$10, %eax
	addl	%eax, %r14d
.L21:
	movq	-104(%rbp), %rsi
	movq	-120(%rbp), %rdi
	movl	$2, %ecx
	movq	%rbx, %rdx
	call	cudaMemcpy@PLT
	testl	%r12d, %r12d
	jg	.L22
	movq	-56(%rbp), %rax
	subq	%fs:40, %rax
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	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	_Z18quickSortIterativePiii, .-_Z18quickSortIterativePiii
	.section	.rodata.str1.1
.LC1:
	.string	"r"
.LC2:
	.string	"%d"
.LC3:
	.string	"\nInput length = %d\n"
.LC4:
	.string	"\n"
.LC5:
	.string	"\nSolution wrong Expecting : %d but got : %d\n"
.LC6:
	.string	"\nSolution is Correct !!!\n"
.LC8:
	.string	"\nTime :  %f s \n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC1(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movq	8(%rsi), %rdi
	movq	%r15, %rsi
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	20(%rsp), %r14
	call	fopen@PLT
	movq	%r14, %rdx
	leaq	.LC2(%rip), %rsi
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movl	20(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC3(%rip), %rsi
	call	__printf_chk@PLT
	movslq	20(%rsp), %rdi
	movabsq	$2305843009213693950, %rax
	cmpq	%rdi, %rax
	jb	.L34
	salq	$2, %rdi
	xorl	%ebp, %ebp
	call	_Znam@PLT
	movq	%rax, %rbx
	movq	%rax, %r13
	jmp	.L35
.L34:
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L46
	call	__cxa_throw_bad_array_new_length@PLT
.L35:
	cmpl	%ebp, 20(%rsp)
	jle	.L47
	movq	(%rsp), %rdi
	movq	%r13, %rdx
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	incl	%ebp
	addq	$4, %r13
	call	__isoc23_fscanf@PLT
	jmp	.L35
.L47:
	call	clock@PLT
	movl	20(%rsp), %edx
	xorl	%esi, %esi
	movq	%rbx, %rdi
	movq	%rax, %r13
	call	_Z18quickSortIterativePiii
	call	clock@PLT
	movq	16(%r12), %rdi
	movq	%r15, %rsi
	movq	%rax, %rbp
	call	fopen@PLT
	movq	%r14, %rdx
	leaq	.LC2(%rip), %rsi
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movslq	20(%rsp), %rdi
	movabsq	$2305843009213693950, %rax
	cmpq	%rdi, %rax
	jb	.L34
	salq	$2, %rdi
	xorl	%r15d, %r15d
	call	_Znam@PLT
	movq	%rax, %r14
	movq	%rax, %r12
.L38:
	cmpl	%r15d, 20(%rsp)
	jle	.L48
	movq	8(%rsp), %rdi
	movq	%r12, %rdx
	xorl	%eax, %eax
	incl	%r15d
	leaq	.LC2(%rip), %rsi
	addq	$4, %r12
	call	__isoc23_fscanf@PLT
	jmp	.L38
.L48:
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%r12d, %r12d
	call	__printf_chk@PLT
	leaq	.LC5(%rip), %r15
	xorl	%eax, %eax
.L40:
	cmpl	%r12d, 20(%rsp)
	jle	.L49
	movl	(%r14,%r12,4), %edx
	cmpl	4(%rbx,%r12,4), %edx
	je	.L41
	movl	(%rbx,%r12,4), %ecx
	movq	%r15, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %eax
.L41:
	incq	%r12
	jmp	.L40
.L49:
	testl	%eax, %eax
	jne	.L43
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	subl	%r13d, %ebp
	call	__printf_chk@PLT
	movl	$2, %edi
	cvtsi2sdl	%ebp, %xmm0
	movb	$1, %al
	leaq	.LC8(%rip), %rsi
	divsd	.LC7(%rip), %xmm0
	call	__printf_chk@PLT
.L43:
	movq	8(%rsp), %rdi
	call	fclose@PLT
	movq	(%rsp), %rdi
	call	fclose@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L44
.L46:
	call	__stack_chk_fail@PLT
.L44:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	main, .-main
	.section	.rodata.str1.1
.LC9:
	.string	"_Z9partitionPiS_S_i"
.LC10:
	.string	"d_size"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC9(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z9partitionPiS_S_i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC10(%rip), %rdx
	movl	$4, %r9d
	leaq	_ZL6d_size(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.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
.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
	.local	_ZL6d_size
	.comm	_ZL6d_size,4,4
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC7:
	.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:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 50000
 void printArr( int arr[], int n )
{
    int i;
    for ( i = 0; i < n; ++i )
        printf( "%d ", arr[i] );
}
__device__ int d_size;

__global__ void partition (int *arr, int *arr_l, int *arr_h, int n)
{
    int z = blockIdx.x*blockDim.x+threadIdx.x;
    d_size = 0;
    __syncthreads();
    if (z<n)
      {
        int h = arr_h[z];
        int l = arr_l[z];
        int x = arr[h];
        int i = (l - 1);
        int temp;
        for (int j = l; j <= h- 1; j++)
          {
            if (arr[j] <= x)
              {
                i++;
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
              }
          }
        temp = arr[i+1];
        arr[i+1] = arr[h];
        arr[h] = temp;
        int p = (i + 1);
        if (p-1 > l)
          {
            int ind = atomicAdd(&d_size, 1);
            arr_l[ind] = l;
            arr_h[ind] = p-1;  
          }
        if ( p+1 < h )
          {
            int ind = atomicAdd(&d_size, 1);
            arr_l[ind] = p+1;
            arr_h[ind] = h; 
          }
      }
}
 
void quickSortIterative (int arr[], int l, int h)
{
    int lstack[ h - l + 1 ], hstack[ h - l + 1];
 
    int top = -1, *d_d, *d_l, *d_h;
 
    lstack[ ++top ] = l;
    hstack[ top ] = h;

    hipMalloc(&d_d, (h-l+1)*sizeof(int));
    hipMemcpy(d_d, arr,(h-l+1)*sizeof(int),hipMemcpyHostToDevice);

    hipMalloc(&d_l, (h-l+1)*sizeof(int));
    hipMemcpy(d_l, lstack,(h-l+1)*sizeof(int),hipMemcpyHostToDevice);
    hipMalloc(&d_h, (h-l+1)*sizeof(int));
    hipMemcpy(d_h, hstack,(h-l+1)*sizeof(int),hipMemcpyHostToDevice);
    int n_t = 1;
    int n_b = 1;
    int n_i = 1; 
    while ( n_i > 0 )
    {
        partition<<<n_b,n_t>>>( d_d, d_l, d_h, n_i);
        int answer;
        hipMemcpyFromSymbol(&answer, HIP_SYMBOL(d_size), sizeof(int), 0, hipMemcpyDeviceToHost); 
        if (answer < 1024)
          {
            n_t = answer;
          }
        else
          {
            n_t = 1024;
            n_b = answer/n_t + (answer%n_t==0?0:1);
          }
        n_i = answer;
        hipMemcpy(arr, d_d,(h-l+1)*sizeof(int),hipMemcpyDeviceToHost);
    }
}
 

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

      
       int *arr;
       int numElements; 
    
    FILE *inp1 = fopen(argv[1], "r");
    fscanf(inp1, "%d", &numElements);
    
    printf("\nInput length = %d\n",numElements);
    arr= new int[numElements];
    for(int i = 0; i < numElements; ++i){
	fscanf(inp1, "%d", &arr[i]);
	
    }
    
   /* printf("\nInput\n");    
    for(int i=0;i<numElements;i++){
    	printf("%d ",arr[i]);
    }
*/
    int start_s=clock();
    quickSortIterative( arr, 0, numElements);
    int stop_s=clock();
    
    
  
    
    
    
    FILE *op = fopen(argv[2], "r");
    fscanf(op, "%d", &numElements);
    int *output;
    output=new int[numElements];
    for(int i = 0; i < numElements; ++i){
	fscanf(op, "%d", &output[i]);
    }
    int flag=0;
    printf("\n");
    for(int i=0;i<numElements;i++){
    	if(output[i]!=arr[i+1]){
    		printf("\nSolution wrong Expecting : %d but got : %d\n",output[i],arr[i]);
    		flag=1;
    	}
    }
    if(flag==0){
    printf("\nSolution is Correct !!!\n");
    printf("\nTime :  %f s \n",(stop_s-start_s)/double(CLOCKS_PER_SEC));
    }
    
    fclose(op);
    fclose(inp1);
    
    //printf("\nOutput\n");
    //printArr( arr, numElements);

    return 0;
}

	.text
	.file	"quicksort.hip"
	.globl	_Z8printArrPii                  # -- Begin function _Z8printArrPii
	.type	_Z8printArrPii,@function
_Z8printArrPii:                         # @_Z8printArrPii
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB0_4
# %bb.1:                                # %.lr.ph.preheader
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	%esi, %r14d
	xorl	%r15d, %r15d
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%r15,4), %esi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB0_2
# %bb.3:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB0_4:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z8printArrPii, .Lfunc_end0-_Z8printArrPii
	.cfi_endproc
                                        # -- End function
	.globl	_Z24__device_stub__partitionPiS_S_i # -- Begin function _Z24__device_stub__partitionPiS_S_i
	.type	_Z24__device_stub__partitionPiS_S_i,@function
_Z24__device_stub__partitionPiS_S_i:    # @_Z24__device_stub__partitionPiS_S_i
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%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	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	$_Z9partitionPiS_S_i, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z24__device_stub__partitionPiS_S_i, .Lfunc_end1-_Z24__device_stub__partitionPiS_S_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z18quickSortIterativePiii      # -- Begin function _Z18quickSortIterativePiii
	.type	_Z18quickSortIterativePiii,@function
_Z18quickSortIterativePiii:             # @_Z18quickSortIterativePiii
	.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	%r13
	pushq	%r12
	pushq	%rbx
	subq	$40, %rsp
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	movq	%rdi, %rbx
	movl	%edx, %eax
	subl	%esi, %eax
	leal	1(%rax), %ecx
	leaq	15(,%rcx,4), %rcx
	andq	$-16, %rcx
	movq	%rsp, %r12
	subq	%rcx, %r12
	movq	%r12, %rsp
	movq	%rsp, %r15
	subq	%rcx, %r15
	movq	%r15, %rsp
	movl	%esi, (%r12)
	movl	%edx, (%r15)
	cltq
	leaq	4(,%rax,4), %r14
	leaq	-64(%rbp), %r13
	movq	%r13, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	movl	$1, %r13d
	movq	%rbx, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	-56(%rbp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	-56(%rbp), %rax
	movq	(%rax), %rdi
	movq	%r12, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	-72(%rbp), %r12
	movq	%r12, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%r12), %rdi
	movq	%r15, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$1, %r15d
	movl	$1, %r12d
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	movl	%r15d, %edi
	movabsq	$4294967296, %rax               # imm = 0x100000000
	orq	%rax, %rdi
	movl	%r13d, %edx
	orq	%rax, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_3
# %bb.2:                                #   in Loop: Header=BB2_1 Depth=1
	movq	-64(%rbp), %rdi
	movq	-56(%rbp), %rsi
	movq	-72(%rbp), %rdx
	movl	%r12d, %ecx
	callq	_Z24__device_stub__partitionPiS_S_i
.LBB2_3:                                #   in Loop: Header=BB2_1 Depth=1
	movl	$d_size, %esi
	movl	$4, %edx
	leaq	-44(%rbp), %rdi
	xorl	%ecx, %ecx
	movl	$2, %r8d
	callq	hipMemcpyFromSymbol
	movl	-44(%rbp), %r12d
	movl	%r12d, %r13d
	cmpl	$1024, %r12d                    # imm = 0x400
	jl	.LBB2_5
# %bb.4:                                #   in Loop: Header=BB2_1 Depth=1
	movl	%r12d, %r15d
	shrl	$10, %r15d
	movl	%r12d, %eax
	andl	$1023, %eax                     # imm = 0x3FF
	cmpl	$1, %eax
	sbbl	$-1, %r15d
	movl	$1024, %r13d                    # imm = 0x400
.LBB2_5:                                #   in Loop: Header=BB2_1 Depth=1
	movq	-64(%rbp), %rsi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%r12d, %r12d
	jg	.LBB2_1
# %bb.6:
	leaq	-40(%rbp), %rsp
	popq	%rbx
	popq	%r12
	popq	%r13
	popq	%r14
	popq	%r15
	popq	%rbp
	.cfi_def_cfa %rsp, 8
	retq
.Lfunc_end2:
	.size	_Z18quickSortIterativePiii, .Lfunc_end2-_Z18quickSortIterativePiii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI3_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %r12
	movq	8(%rsi), %rdi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %rbx
	leaq	4(%rsp), %r14
	movl	$.L.str.2, %esi
	movq	%rax, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%r14), %esi
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	movslq	(%r14), %r15
	leaq	(,%r15,4), %rdi
	testq	%r15, %r15
	movq	$-1, %rax
	cmovsq	%rax, %rdi
	callq	_Znam
	movq	%rax, %r14
	testq	%r15, %r15
	jle	.LBB3_3
# %bb.1:                                # %.lr.ph.preheader
	movq	%r14, %r15
	xorl	%r13d, %r13d
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.2, %esi
	movq	%rbx, %rdi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r13
	movslq	4(%rsp), %rax
	addq	$4, %r15
	cmpq	%rax, %r13
	jl	.LBB3_2
.LBB3_3:                                # %._crit_edge
	callq	clock
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	leaq	4(%rsp), %rbp
	movl	(%rbp), %edx
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	_Z18quickSortIterativePiii
	callq	clock
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	movq	16(%r12), %rdi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %r12
	movl	$.L.str.2, %esi
	movq	%rax, %rdi
	movq	%rbp, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movslq	(%rbp), %r13
	leaq	(,%r13,4), %rdi
	testq	%r13, %r13
	movq	$-1, %rax
	cmovsq	%rax, %rdi
	callq	_Znam
	movq	%rax, %r15
	testq	%r13, %r13
	jle	.LBB3_6
# %bb.4:                                # %.lr.ph36.preheader
	movq	%r15, %rbp
	xorl	%r13d, %r13d
.LBB3_5:                                # %.lr.ph36
                                        # =>This Inner Loop Header: Depth=1
	movl	$.L.str.2, %esi
	movq	%r12, %rdi
	movq	%rbp, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%r13
	movslq	4(%rsp), %rax
	addq	$4, %rbp
	cmpq	%rax, %r13
	jl	.LBB3_5
.LBB3_6:                                # %._crit_edge37
	movl	$10, %edi
	callq	putchar@PLT
	movl	4(%rsp), %eax
	testl	%eax, %eax
	jle	.LBB3_12
# %bb.7:                                # %.lr.ph41.outer.preheader
	movb	$1, %r13b
	xorl	%ebp, %ebp
.LBB3_8:                                # %.lr.ph41.outer
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_9 Depth 2
	cltq
.LBB3_9:                                # %.lr.ph41
                                        #   Parent Loop BB3_8 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%r15,%rbp,4), %esi
	cmpl	4(%r14,%rbp,4), %esi
	jne	.LBB3_14
# %bb.10:                               #   in Loop: Header=BB3_9 Depth=2
	incq	%rbp
	cmpq	%rax, %rbp
	jl	.LBB3_9
	jmp	.LBB3_11
.LBB3_14:                               # %.thread
                                        #   in Loop: Header=BB3_8 Depth=1
	movl	(%r14,%rbp,4), %edx
	xorl	%r13d, %r13d
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	movslq	4(%rsp), %rax
	incq	%rbp
	cmpq	%rax, %rbp
	jl	.LBB3_8
	jmp	.LBB3_13
.LBB3_11:                               # %._crit_edge42
	testb	$1, %r13b
	je	.LBB3_13
.LBB3_12:                               # %._crit_edge42.thread
	movl	$.Lstr, %edi
	callq	puts@PLT
	movq	8(%rsp), %rax                   # 8-byte Reload
	subl	16(%rsp), %eax                  # 4-byte Folded Reload
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI3_0(%rip), %xmm0
	movl	$.L.str.7, %edi
	movb	$1, %al
	callq	printf
.LBB3_13:                               # %._crit_edge42.thread53
	movq	%r12, %rdi
	callq	fclose
	movq	%rbx, %rdi
	callq	fclose
	xorl	%eax, %eax
	addq	$24, %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_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	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	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z9partitionPiS_S_i, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$0, 8(%rsp)
	movl	$0, (%rsp)
	movl	$d_size, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movl	$4, %r9d
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	callq	__hipRegisterVar
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB5_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%d "
	.size	.L.str, 4

	.type	d_size,@object                  # @d_size
	.local	d_size
	.comm	d_size,4,4
	.type	_Z9partitionPiS_S_i,@object     # @_Z9partitionPiS_S_i
	.section	.rodata,"a",@progbits
	.globl	_Z9partitionPiS_S_i
	.p2align	3, 0x0
_Z9partitionPiS_S_i:
	.quad	_Z24__device_stub__partitionPiS_S_i
	.size	_Z9partitionPiS_S_i, 8

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

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

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"\nInput length = %d\n"
	.size	.L.str.3, 20

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"\nSolution wrong Expecting : %d but got : %d\n"
	.size	.L.str.5, 45

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"\nTime :  %f s \n"
	.size	.L.str.7, 16

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z9partitionPiS_S_i"
	.size	.L__unnamed_1, 20

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"d_size"
	.size	.L__unnamed_2, 7

	.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	"\nSolution is Correct !!!"
	.size	.Lstr, 25

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z24__device_stub__partitionPiS_S_i
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym d_size
	.addrsig_sym _Z9partitionPiS_S_i
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z9partitionPiS_S_i     ; -- Begin function _Z9partitionPiS_S_i
	.globl	_Z9partitionPiS_S_i
	.p2align	8
	.type	_Z9partitionPiS_S_i,@function
_Z9partitionPiS_S_i:                    ; @_Z9partitionPiS_S_i
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x2c
	s_load_b32 s4, s[0:1], 0x18
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	v_mov_b32_e32 v0, 0
	s_getpc_b64 s[2:3]
	s_add_u32 s2, s2, d_size@rel32@lo+4
	s_addc_u32 s3, s3, d_size@rel32@hi+12
	global_store_b32 v0, v0, s[2:3]
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_i32_e64 s4, v1
	s_cbranch_execz .LBB0_15
; %bb.1:
	s_clause 0x1
	s_load_b64 s[2:3], s[0:1], 0x10
	s_load_b128 s[4:7], s[0:1], 0x0
	v_ashrrev_i32_e32 v2, 31, v1
	s_mov_b32 s1, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s2, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v3, vcc_lo
	v_add_co_u32 v2, vcc_lo, s6, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s7, v3, vcc_lo
	global_load_b32 v0, v[0:1], off
	s_waitcnt vmcnt(0)
	v_ashrrev_i32_e32 v1, 31, v0
	s_delay_alu instid0(VALU_DEP_1)
	v_lshlrev_b64 v[5:6], 2, v[0:1]
	global_load_b32 v1, v[2:3], off
	v_add_co_u32 v5, vcc_lo, s4, v5
	v_add_co_ci_u32_e32 v6, vcc_lo, s5, v6, vcc_lo
	global_load_b32 v9, v[5:6], off
	s_waitcnt vmcnt(1)
	v_add_nc_u32_e32 v3, -1, v1
	v_cmpx_lt_i32_e64 v1, v0
	s_cbranch_execz .LBB0_7
; %bb.2:                                ; %.lr.ph.preheader
	v_ashrrev_i32_e32 v2, 31, v1
	s_add_u32 s8, s4, 4
	s_addc_u32 s10, s5, 0
	s_mov_b32 s9, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[7:8], 2, v[1:2]
	v_mov_b32_e32 v2, v1
	v_add_co_u32 v7, vcc_lo, s4, v7
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v8, vcc_lo, s5, v8, vcc_lo
.LBB0_3:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	global_load_b32 v10, v[7:8], off
	s_mov_b32 s0, exec_lo
	s_waitcnt vmcnt(0)
	v_cmpx_le_i32_e64 v10, v9
	s_cbranch_execz .LBB0_5
; %bb.4:                                ;   in Loop: Header=BB0_3 Depth=1
	v_ashrrev_i32_e32 v4, 31, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[11:12], 2, v[3:4]
	v_add_nc_u32_e32 v3, 1, v3
	v_add_co_u32 v11, vcc_lo, s8, v11
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v12, vcc_lo, s10, v12, vcc_lo
	global_load_b32 v4, v[11:12], off
	global_store_b32 v[11:12], v10, off
	s_waitcnt vmcnt(0)
	global_store_b32 v[7:8], v4, off
.LBB0_5:                                ;   in Loop: Header=BB0_3 Depth=1
	s_or_b32 exec_lo, exec_lo, s0
	v_add_nc_u32_e32 v2, 1, v2
	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, 0, v8, s0
	v_cmp_ge_i32_e32 vcc_lo, v2, v0
	s_or_b32 s9, vcc_lo, s9
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s9
	s_cbranch_execnz .LBB0_3
; %bb.6:                                ; %._crit_edge.loopexit
	s_or_b32 exec_lo, exec_lo, s9
	global_load_b32 v9, v[5:6], off
.LBB0_7:                                ; %Flow75
	s_or_b32 exec_lo, exec_lo, s1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v4, 31, v3
	s_mov_b32 s0, exec_lo
	v_lshlrev_b64 v[7:8], 2, v[3:4]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v7, vcc_lo, s4, v7
	v_add_co_ci_u32_e32 v8, vcc_lo, s5, v8, vcc_lo
	global_load_b32 v2, v[7:8], off offset:4
	s_waitcnt vmcnt(1)
	global_store_b32 v[7:8], v9, off offset:4
	s_waitcnt vmcnt(0)
	global_store_b32 v[5:6], v2, off
	v_cmpx_gt_i32_e64 v3, v1
	s_cbranch_execz .LBB0_11
; %bb.8:
	s_mov_b32 s4, exec_lo
	s_mov_b32 s1, exec_lo
	v_mbcnt_lo_u32_b32 v2, s4, 0
                                        ; implicit-def: $vgpr4
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_eq_u32_e32 0, v2
	s_cbranch_execz .LBB0_10
; %bb.9:
	s_bcnt1_i32_b32 s8, s4
	s_delay_alu instid0(SALU_CYCLE_1)
	v_dual_mov_b32 v4, 0 :: v_dual_mov_b32 v5, s8
	s_getpc_b64 s[4:5]
	s_add_u32 s4, s4, d_size@rel32@lo+4
	s_addc_u32 s5, s5, d_size@rel32@hi+12
	global_atomic_add_u32 v4, v4, v5, s[4:5] glc
.LBB0_10:
	s_or_b32 exec_lo, exec_lo, s1
	s_waitcnt vmcnt(0)
	v_readfirstlane_b32 s1, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v4, s1, v2
	v_ashrrev_i32_e32 v5, 31, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[4:5], 2, v[4:5]
	v_add_co_u32 v6, vcc_lo, s6, v4
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v7, vcc_lo, s7, v5, vcc_lo
	v_add_co_u32 v4, vcc_lo, s2, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s3, v5, vcc_lo
	global_store_b32 v[6:7], v1, off
	global_store_b32 v[4:5], v3, off
.LBB0_11:                               ; %Flow74
	s_or_b32 exec_lo, exec_lo, s0
	v_add_nc_u32_e32 v1, 2, v3
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_lt_i32_e32 vcc_lo, v1, v0
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_15
; %bb.12:
	s_mov_b32 s1, exec_lo
	s_mov_b32 s0, exec_lo
	v_mbcnt_lo_u32_b32 v2, s1, 0
                                        ; implicit-def: $vgpr3
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_eq_u32_e32 0, v2
	s_cbranch_execz .LBB0_14
; %bb.13:
	s_bcnt1_i32_b32 s1, s1
	s_delay_alu instid0(SALU_CYCLE_1)
	v_dual_mov_b32 v3, 0 :: v_dual_mov_b32 v4, s1
	s_getpc_b64 s[4:5]
	s_add_u32 s4, s4, d_size@rel32@lo+4
	s_addc_u32 s5, s5, d_size@rel32@hi+12
	global_atomic_add_u32 v3, v3, v4, s[4:5] glc
.LBB0_14:
	s_or_b32 exec_lo, exec_lo, s0
	s_waitcnt vmcnt(0)
	v_readfirstlane_b32 s0, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v2, s0, v2
	v_ashrrev_i32_e32 v3, 31, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[2:3]
	v_add_co_u32 v4, vcc_lo, s6, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v5, vcc_lo, s7, v3, vcc_lo
	v_add_co_u32 v2, vcc_lo, s2, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s3, v3, vcc_lo
	global_store_b32 v[4:5], v1, off
	global_store_b32 v[2:3], v0, off
.LBB0_15:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z9partitionPiS_S_i
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 13
		.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	_Z9partitionPiS_S_i, .Lfunc_end0-_Z9partitionPiS_S_i
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 880
; NumSgprs: 18
; NumVgprs: 13
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 13
; 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
	.protected	d_size                  ; @d_size
	.type	d_size,@object
	.section	.bss,"aw",@nobits
	.globl	d_size
	.p2align	2, 0x0
d_size:
	.long	0                               ; 0x0
	.size	d_size, 4

	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.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 d_size
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z9partitionPiS_S_i
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z9partitionPiS_S_i.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     13
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
