#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <cuda.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#define VERBOSE 1
__global__ void checkMatchOnDevice(char *fileBuffer, char* searchString, int* matchArray, int numBytes,size_t searchSize,int* matchStartArray, int* matchEndArray)
{
  extern __shared__ int sdata[];
  int idx = blockIdx.x*blockDim.x + threadIdx.x;
  int rangeStart;
  int rangeEnd; 
  rangeStart = idx*numBytes;
  rangeEnd = rangeStart + numBytes;
  int i,j;
  int numMatches;
  int foundMatch;
  int firstMatch = 1;
  int firstMatchIndex = -1;
  int lastMatchIndex = -1;
  unsigned int tid = threadIdx.x; 

  for(numMatches=0, i = rangeStart; i < rangeEnd; i++)
  {
    foundMatch = 1;
    for(j = 0; j < searchSize; j++)
    {
      int index = i+j;
      if(fileBuffer[index] != searchString[j])
      {
        foundMatch = 0;
        break;
      }
    }
    if(foundMatch)
    {
      numMatches++;
      if(firstMatch)
      {
	firstMatchIndex = i;
        firstMatch = 0;
      }
      lastMatchIndex = i+searchSize;
      i+=searchSize-1;
    }
  }
  matchStartArray[idx] = firstMatchIndex;
  matchEndArray[idx] = lastMatchIndex;
  //matchArray[idx] = numMatches;
  sdata[tid] = numMatches;

  __syncthreads();
  for(unsigned int s=1;s<blockDim.x;s *= 2){
    int index = s*2*tid;
    if((index+s) <  blockDim.x){
	    sdata[index] += sdata[index + s];
    }
    __syncthreads();
  }
  if(tid==0) matchArray[blockIdx.x]=sdata[0];  
}
__global__ void cumulateOnDevice(int* matchArray, int noOfThreads, int* outArray)
{
  extern __shared__ int sdata2[];
  unsigned int tid = threadIdx.x;
  unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
  if(i<noOfThreads){
	sdata2[tid] = matchArray[i];
	//__syncthreads();

	for(unsigned int s=1;s<blockDim.x;s*=2){
	  int index = s*2*tid;
	
	__syncthreads();
	  if((index+s) < noOfThreads){
	    sdata2[index] += sdata2[index+s];
	  }
	// __syncthreads();
	}
	if(tid == 0) matchArray[blockIdx.x] = sdata2[0];
  }
}
int main(int argc, char *argv[])
{
  struct timeval cpuStart,cpuEnd;
  char* searchString = (char*)malloc(sizeof(char*)*80);
  char* fileBuffer = (char*)malloc(sizeof(char*)*10000000);
  int nBlocks;
  int threadsPerBlock;
  if(argc != 4)
  {
    printf("Usage: stringSearch \"Key\" numBlocks threadsPerBlock < inputFile\n");
    exit(0);
  }
  else
  {
    searchString = argv[1];
    nBlocks = atoi(argv[2]);
    threadsPerBlock = atoi(argv[3]);
    #ifdef VERBOSE
    printf("Search String: %s\n",searchString);
    #endif
    int ptr;
    for(ptr = 0; !feof(stdin);)
    {
      ptr+= fread(&(fileBuffer[ptr]),1,1,stdin);
    }
    char *deviceFileBuffer;                 // pointer to device memory
    char *deviceSearchBuffer;                 // pointer to device memory
    int *matchArray;
    int *outArray;
    int *hostMatchArray;
    int *hostMatchStartArray;
    int *hostMatchEndArray;
    int *matchStartArray;
    int *matchEndArray;
    int fileSize = ptr;
    //printf("FileSize: %d",strlen(fileBuffer));
    size_t searchSize = strlen(searchString);
    cudaEvent_t start, stop; 
    float time; 
    cudaEventCreate(&start); 
    cudaEventCreate(&stop);
    cudaThreadSynchronize();
    gettimeofday(&cpuStart, NULL);
    // allocate file buffer space on device 
    cudaMalloc((void **) &deviceFileBuffer, fileSize);
    // allocate search string space on device 
    cudaMalloc((void **) &deviceSearchBuffer, searchSize);
    // copy data from host to device
    cudaMemcpy(deviceFileBuffer, fileBuffer, fileSize, cudaMemcpyHostToDevice);
    cudaMemcpy(deviceSearchBuffer, searchString, searchSize, cudaMemcpyHostToDevice);
    // do calculation on device:
    // Part 1 of 2. Compute execution configuration
    size_t numThreads = nBlocks*threadsPerBlock;
    int numBytesPerThread = fileSize/numThreads; 
    //Allocate match array space on device
    cudaMalloc((void **) &matchArray, sizeof(int)*numThreads);
    cudaMalloc((void **) &outArray, sizeof(int)*numThreads);
    hostMatchArray = (int*)malloc(sizeof(int)*numThreads);
    cudaMalloc((void **) &matchStartArray, sizeof(int)*numThreads);
    hostMatchStartArray = (int*)malloc(sizeof(int)*numThreads);
    cudaMalloc((void **) &matchEndArray, sizeof(int)*numThreads);
    hostMatchEndArray = (int*)malloc(sizeof(int)*numThreads);
    //Init array to 0
    int i;
    for(i = 0; i < numThreads; i++)
    {
      hostMatchArray[i] = 0; 
      hostMatchStartArray[i] = -1; 
      hostMatchEndArray[i] = -1; 
    }
    cudaMemcpy(matchArray, hostMatchArray, numThreads, cudaMemcpyHostToDevice);
    cudaMemcpy(matchStartArray, hostMatchStartArray, numThreads, cudaMemcpyHostToDevice);
    cudaMemcpy(matchEndArray, hostMatchEndArray, numThreads, cudaMemcpyHostToDevice);
    //printf("Number of threads:%d, Number of blocks:%d, Num Threads Per Block:%d, Num Bytes Per Thread:%d\n",numThreads,nBlocks,threadsPerBlock,numBytesPerThread);
    // Part 2 of 2. Call call checkMatchOnDevice kernel
    cudaEventRecord( start, 0 );
    checkMatchOnDevice <<< nBlocks, threadsPerBlock , threadsPerBlock*sizeof(int)>>> (deviceFileBuffer, deviceSearchBuffer, matchArray,numBytesPerThread,searchSize,matchStartArray,matchEndArray);
    int newNBlocks=nBlocks,newNThreads;//printf("\nNew Blocks:%d",nBlocks);
    cudaThreadSynchronize();
	while(newNBlocks > 1){
	newNThreads = newNBlocks;
	newNBlocks = (newNBlocks/threadsPerBlock)+1;
    cumulateOnDevice <<< newNBlocks, threadsPerBlock ,threadsPerBlock * sizeof(int)>>> (matchArray,newNThreads,outArray);
	}
    cudaEventRecord( stop, 0 ); 
    cudaEventSynchronize( stop ); 
    cudaEventElapsedTime( &time, start, stop ); 
    cudaEventDestroy( start ); 
    cudaEventDestroy( stop ); 
    cudaThreadSynchronize();
    // Retrieve result from device and store in host array
    cudaMemcpy(hostMatchArray, matchArray, sizeof(int)*numThreads, cudaMemcpyDeviceToHost);
    cudaMemcpy(hostMatchStartArray, matchStartArray, sizeof(int)*numThreads, cudaMemcpyDeviceToHost);
    cudaMemcpy(hostMatchEndArray, matchEndArray, sizeof(int)*numThreads, cudaMemcpyDeviceToHost);
    int total = 0;
    //for(i = 0; i < numThreads; i++)
    //{
	//total += hostMatchArray[i];
       //printf("%d)%d\n",i,hostMatchArray[i]);
    //}
	total = hostMatchArray [0];
    //Overlap check, commented out for hw2  
/*  for(i = 0; i < numThreads; i++)
    {
	if(hostMatchEndArray[i] != -1 && hostMatchStartArray[i+1] != -1)
	{
		if(hostMatchEndArray[i] - hostMatchStartArray[i+1] < 0)
			total--;	
	}
	//printf("%d)%d\n",i,hostMatchStartArray[i]);
	//printf("start:%d,end:%d\n",hostMatchStartArray[i],hostMatchEndArray[i]);
    }*/
    
    gettimeofday(&cpuEnd, NULL);
   // float totalTime = (cpuEnd - cpuStart);
    //printf("Number of threads:%d, Number of blocks:%d, Num Threads Per Block:%d, Num Bytes Per Thread:%d\n",numThreads,nBlocks,threadsPerBlock,numBytesPerThread);
    //printf("numOfThread: %4d matchCount: %4d CPUrunningTime: %8ld\n", blocksize, thread_num, sum, q.tv_usec - p.tv_usec + (q.tv_sec-p.tv_sec)*1000000);
    #ifdef VERBOSE
    printf("Completed Successfully! Number of blocks:%d Number of threads per block:%d Num Threads: %d Matches:%d CPU Time:%8ld GPU Time:%f\n\n",nBlocks,threadsPerBlock,numThreads,total,cpuEnd.tv_usec - cpuStart.tv_usec + (cpuEnd.tv_sec - cpuStart.tv_sec ),time);
    #else
    printf("%d %f\n\n",numThreads,time);
    //printf("%d %8ld\n\n",numThreads,cpuEnd.tv_usec - cpuStart.tv_usec + (cpuEnd.tv_usec - cpuStart.tv_usec) );
    #endif
    cudaFree(matchArray);
    cudaFree(outArray);
  }
  
}

	.file	"tmpxft_002f3b03_00000000-6_p4.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z50__device_stub__Z18checkMatchOnDevicePcS_PiimS0_S0_PcS_PiimS0_S0_
	.type	_Z50__device_stub__Z18checkMatchOnDevicePcS_PiimS0_S0_PcS_PiimS0_S0_, @function
_Z50__device_stub__Z18checkMatchOnDevicePcS_PiimS0_S0_PcS_PiimS0_S0_:
.LFB2052:
	.cfi_startproc
	endbr64
	subq	$200, %rsp
	.cfi_def_cfa_offset 208
	movq	208(%rsp), %rax
	movq	%rdi, 56(%rsp)
	leaq	80(%rsp), %rdi
	movq	%rsi, 48(%rsp)
	leaq	92(%rsp), %rsi
	movq	%rdx, 40(%rsp)
	leaq	64(%rsp), %rdx
	movl	%ecx, 36(%rsp)
	leaq	72(%rsp), %rcx
	movq	%r8, 24(%rsp)
	movq	%r9, 16(%rsp)
	movq	%rax, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 184(%rsp)
	xorl	%eax, %eax
	leaq	56(%rsp), %rax
	movl	$1, 88(%rsp)
	movq	%rax, 128(%rsp)
	leaq	48(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	40(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	36(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 176(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 80(%rsp)
	movq	%rax, 92(%rsp)
	movl	$1, 100(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	72(%rsp)
	.cfi_def_cfa_offset 216
	leaq	_Z18checkMatchOnDevicePcS_PiimS0_S0_(%rip), %rdi
	pushq	72(%rsp)
	.cfi_def_cfa_offset 224
	movq	108(%rsp), %rcx
	movl	116(%rsp), %r8d
	movq	96(%rsp), %rsi
	movl	104(%rsp), %edx
	leaq	144(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 216
	popq	%rdx
	.cfi_def_cfa_offset 208
.L2:
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$200, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2052:
	.size	_Z50__device_stub__Z18checkMatchOnDevicePcS_PiimS0_S0_PcS_PiimS0_S0_, .-_Z50__device_stub__Z18checkMatchOnDevicePcS_PiimS0_S0_PcS_PiimS0_S0_
	.globl	_Z18checkMatchOnDevicePcS_PiimS0_S0_
	.type	_Z18checkMatchOnDevicePcS_PiimS0_S0_, @function
_Z18checkMatchOnDevicePcS_PiimS0_S0_:
.LFB2053:
	.cfi_startproc
	endbr64
	jmp	_Z50__device_stub__Z18checkMatchOnDevicePcS_PiimS0_S0_PcS_PiimS0_S0_
	.cfi_endproc
.LFE2053:
	.size	_Z18checkMatchOnDevicePcS_PiimS0_S0_, .-_Z18checkMatchOnDevicePcS_PiimS0_S0_
	.globl	_Z39__device_stub__Z16cumulateOnDevicePiiS_PiiS_
	.type	_Z39__device_stub__Z16cumulateOnDevicePiiS_PiiS_, @function
_Z39__device_stub__Z16cumulateOnDevicePiiS_PiiS_:
.LFB2054:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movl	%esi, 20(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L8
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z16cumulateOnDevicePiiS_(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L8:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2054:
	.size	_Z39__device_stub__Z16cumulateOnDevicePiiS_PiiS_, .-_Z39__device_stub__Z16cumulateOnDevicePiiS_PiiS_
	.globl	_Z16cumulateOnDevicePiiS_
	.type	_Z16cumulateOnDevicePiiS_, @function
_Z16cumulateOnDevicePiiS_:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_Z39__device_stub__Z16cumulateOnDevicePiiS_PiiS_
	.cfi_endproc
.LFE2055:
	.size	_Z16cumulateOnDevicePiiS_, .-_Z16cumulateOnDevicePiiS_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Usage: stringSearch \"Key\" numBlocks threadsPerBlock < inputFile\n"
.LC1:
	.string	"Search String: %s\n"
.LC2:
	.string	"Completed Successfully! Number of blocks:%d Number of threads per block:%d Num Threads: %d Matches:%d CPU Time:%8ld GPU Time:%f\n\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%edi, %r12d
	movl	$80000000, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	subq	$184, %rsp
	.cfi_def_cfa_offset 240
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	cmpl	$4, %r12d
	je	.L14
	movl	$2, %edi
	leaq	.LC0(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L14:
	movq	16(%rbx), %rdi
	movq	%rax, %rbp
	movq	8(%rbx), %r14
	xorl	%r15d, %r15d
	call	atoi@PLT
	movq	24(%rbx), %rdi
	movl	%eax, %r13d
	call	atoi@PLT
	movq	%r14, %rdx
	movl	$2, %edi
	leaq	.LC1(%rip), %rsi
	movl	%eax, %r12d
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L15:
	movq	stdin(%rip), %rdi
	movslq	%r15d, %rbx
	call	feof@PLT
	testl	%eax, %eax
	jne	.L25
	movl	$80000000, %esi
	leaq	0(%rbp,%rbx), %rdi
	movl	$1, %ecx
	movq	stdin(%rip), %r8
	cmpq	%rsi, %rbx
	movl	$1, %edx
	cmovnb	%rbx, %rsi
	subq	%rbx, %rsi
	call	__fread_chk@PLT
	addl	%eax, %r15d
	jmp	.L15
.L25:
	movq	%r14, %rdi
	call	strlen@PLT
	leaq	104(%rsp), %rdi
	movq	%rax, %r15
	call	cudaEventCreate@PLT
	leaq	112(%rsp), %rdi
	call	cudaEventCreate@PLT
	call	cudaThreadSynchronize@PLT
	leaq	136(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
	leaq	56(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	64(%rsp), %rdi
	movq	%r15, %rsi
	call	cudaMalloc@PLT
	movq	%rbp, %rsi
	movl	%r13d, %ebp
	movq	56(%rsp), %rdi
	imull	%r12d, %ebp
	movl	$1, %ecx
	movq	%rbx, %rdx
	call	cudaMemcpy@PLT
	movq	64(%rsp), %rdi
	movq	%r15, %rdx
	movq	%r14, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movslq	%ebp, %rbp
	leaq	72(%rsp), %rdi
	leaq	0(,%rbp,4), %rax
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	leaq	80(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	call	malloc@PLT
	movq	8(%rsp), %rsi
	leaq	88(%rsp), %rdi
	movq	%rax, %r14
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	call	malloc@PLT
	movq	8(%rsp), %rsi
	leaq	96(%rsp), %rdi
	movq	%rax, 16(%rsp)
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	call	malloc@PLT
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
.L17:
	cmpq	%rbp, %rax
	jnb	.L26
	movq	16(%rsp), %rdx
	movq	24(%rsp), %rcx
	xorl	%r9d, %r9d
	movl	%r9d, (%r14,%rax,4)
	movl	$-1, (%rdx,%rax,4)
	movl	$-1, (%rcx,%rax,4)
	incq	%rax
	jmp	.L17
.L26:
	movq	72(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rdx
	movq	%r14, %rsi
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rsi
	movq	88(%rsp), %rdi
	movq	%rbp, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rsi
	movq	96(%rsp), %rdi
	movq	%rbp, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	104(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movslq	%r12d, %rax
	movl	%r13d, 124(%rsp)
	xorl	%r9d, %r9d
	salq	$2, %rax
	movl	%r12d, 152(%rsp)
	movq	%rax, 32(%rsp)
	movq	32(%rsp), %r8
	movabsq	$4294967297, %rax
	movq	%rax, 156(%rsp)
	movl	160(%rsp), %ecx
	movq	%rax, 128(%rsp)
	movq	152(%rsp), %rdx
	movq	124(%rsp), %rdi
	movl	132(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L19
	movq	%rbx, %rax
	xorl	%edx, %edx
	pushq	%rsi
	.cfi_def_cfa_offset 248
	movq	%r15, %r8
	divq	%rbp
	pushq	104(%rsp)
	.cfi_def_cfa_offset 256
	movq	104(%rsp), %r9
	movq	88(%rsp), %rdx
	movq	80(%rsp), %rsi
	movq	72(%rsp), %rdi
	movq	%rax, %rcx
	call	_Z50__device_stub__Z18checkMatchOnDevicePcS_PiimS0_S0_PcS_PiimS0_S0_
	popq	%rdi
	.cfi_def_cfa_offset 248
	popq	%r8
	.cfi_def_cfa_offset 240
.L19:
	call	cudaThreadSynchronize@PLT
	movl	%r13d, %r10d
	movabsq	$4294967297, %r15
.L20:
	cmpl	$1, %r10d
	jle	.L27
	movl	%r10d, %eax
	movq	32(%rsp), %r8
	xorl	%r9d, %r9d
	movl	%r10d, 44(%rsp)
	cltd
	movq	%r15, 156(%rsp)
	movl	160(%rsp), %ecx
	idivl	%r12d
	movq	%r15, 128(%rsp)
	movl	132(%rsp), %esi
	movl	%r12d, 152(%rsp)
	movq	152(%rsp), %rdx
	leal	1(%rax), %ebx
	movl	%ebx, 124(%rsp)
	movq	124(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	movl	44(%rsp), %r10d
	testl	%eax, %eax
	jne	.L21
	movq	80(%rsp), %rdx
	movq	72(%rsp), %rdi
	movl	%r10d, %esi
	call	_Z39__device_stub__Z16cumulateOnDevicePiiS_PiiS_
.L21:
	movl	%ebx, %r10d
	jmp	.L20
.L27:
	movq	112(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	112(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	112(%rsp), %rdx
	movq	104(%rsp), %rsi
	leaq	124(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	104(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	112(%rsp), %rdi
	call	cudaEventDestroy@PLT
	call	cudaThreadSynchronize@PLT
	movq	8(%rsp), %rdx
	movq	72(%rsp), %rsi
	movq	%r14, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdx
	movq	88(%rsp), %rsi
	movl	$2, %ecx
	movq	16(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdx
	movq	96(%rsp), %rsi
	movl	$2, %ecx
	movq	24(%rsp), %rdi
	call	cudaMemcpy@PLT
	movl	(%r14), %r9d
	leaq	152(%rsp), %rdi
	xorl	%esi, %esi
	movl	%r9d, 8(%rsp)
	call	gettimeofday@PLT
	movq	%rbp, %r8
	movl	%r12d, %ecx
	movl	%r13d, %edx
	cvtss2sd	124(%rsp), %xmm0
	pushq	%rax
	.cfi_def_cfa_offset 248
	movl	$2, %edi
	movq	168(%rsp), %rax
	subq	152(%rsp), %rax
	addq	160(%rsp), %rax
	leaq	.LC2(%rip), %rsi
	subq	144(%rsp), %rax
	pushq	%rax
	.cfi_def_cfa_offset 256
	movl	24(%rsp), %r9d
	movb	$1, %al
	call	__printf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 248
	popq	%rcx
	.cfi_def_cfa_offset 240
	movq	72(%rsp), %rdi
	call	cudaFree@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	addq	$184, %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
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC3:
	.string	"_Z16cumulateOnDevicePiiS_"
.LC4:
	.string	"_Z18checkMatchOnDevicePcS_PiimS0_S0_"
	.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	.LC3(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z16cumulateOnDevicePiiS_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC4(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z18checkMatchOnDevicePcS_PiimS0_S0_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2057:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <hip/hip_runtime.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#define VERBOSE 1
__global__ void checkMatchOnDevice(char *fileBuffer, char* searchString, int* matchArray, int numBytes,size_t searchSize,int* matchStartArray, int* matchEndArray)
{
  extern __shared__ int sdata[];
  int idx = blockIdx.x*blockDim.x + threadIdx.x;
  int rangeStart;
  int rangeEnd; 
  rangeStart = idx*numBytes;
  rangeEnd = rangeStart + numBytes;
  int i,j;
  int numMatches;
  int foundMatch;
  int firstMatch = 1;
  int firstMatchIndex = -1;
  int lastMatchIndex = -1;
  unsigned int tid = threadIdx.x; 

  for(numMatches=0, i = rangeStart; i < rangeEnd; i++)
  {
    foundMatch = 1;
    for(j = 0; j < searchSize; j++)
    {
      int index = i+j;
      if(fileBuffer[index] != searchString[j])
      {
        foundMatch = 0;
        break;
      }
    }
    if(foundMatch)
    {
      numMatches++;
      if(firstMatch)
      {
	firstMatchIndex = i;
        firstMatch = 0;
      }
      lastMatchIndex = i+searchSize;
      i+=searchSize-1;
    }
  }
  matchStartArray[idx] = firstMatchIndex;
  matchEndArray[idx] = lastMatchIndex;
  //matchArray[idx] = numMatches;
  sdata[tid] = numMatches;

  __syncthreads();
  for(unsigned int s=1;s<blockDim.x;s *= 2){
    int index = s*2*tid;
    if((index+s) <  blockDim.x){
	    sdata[index] += sdata[index + s];
    }
    __syncthreads();
  }
  if(tid==0) matchArray[blockIdx.x]=sdata[0];  
}
__global__ void cumulateOnDevice(int* matchArray, int noOfThreads, int* outArray)
{
  extern __shared__ int sdata2[];
  unsigned int tid = threadIdx.x;
  unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
  if(i<noOfThreads){
	sdata2[tid] = matchArray[i];
	//__syncthreads();

	for(unsigned int s=1;s<blockDim.x;s*=2){
	  int index = s*2*tid;
	
	__syncthreads();
	  if((index+s) < noOfThreads){
	    sdata2[index] += sdata2[index+s];
	  }
	// __syncthreads();
	}
	if(tid == 0) matchArray[blockIdx.x] = sdata2[0];
  }
}
int main(int argc, char *argv[])
{
  struct timeval cpuStart,cpuEnd;
  char* searchString = (char*)malloc(sizeof(char*)*80);
  char* fileBuffer = (char*)malloc(sizeof(char*)*10000000);
  int nBlocks;
  int threadsPerBlock;
  if(argc != 4)
  {
    printf("Usage: stringSearch \"Key\" numBlocks threadsPerBlock < inputFile\n");
    exit(0);
  }
  else
  {
    searchString = argv[1];
    nBlocks = atoi(argv[2]);
    threadsPerBlock = atoi(argv[3]);
    #ifdef VERBOSE
    printf("Search String: %s\n",searchString);
    #endif
    int ptr;
    for(ptr = 0; !feof(stdin);)
    {
      ptr+= fread(&(fileBuffer[ptr]),1,1,stdin);
    }
    char *deviceFileBuffer;                 // pointer to device memory
    char *deviceSearchBuffer;                 // pointer to device memory
    int *matchArray;
    int *outArray;
    int *hostMatchArray;
    int *hostMatchStartArray;
    int *hostMatchEndArray;
    int *matchStartArray;
    int *matchEndArray;
    int fileSize = ptr;
    //printf("FileSize: %d",strlen(fileBuffer));
    size_t searchSize = strlen(searchString);
    hipEvent_t start, stop; 
    float time; 
    hipEventCreate(&start); 
    hipEventCreate(&stop);
    hipDeviceSynchronize();
    gettimeofday(&cpuStart, NULL);
    // allocate file buffer space on device 
    hipMalloc((void **) &deviceFileBuffer, fileSize);
    // allocate search string space on device 
    hipMalloc((void **) &deviceSearchBuffer, searchSize);
    // copy data from host to device
    hipMemcpy(deviceFileBuffer, fileBuffer, fileSize, hipMemcpyHostToDevice);
    hipMemcpy(deviceSearchBuffer, searchString, searchSize, hipMemcpyHostToDevice);
    // do calculation on device:
    // Part 1 of 2. Compute execution configuration
    size_t numThreads = nBlocks*threadsPerBlock;
    int numBytesPerThread = fileSize/numThreads; 
    //Allocate match array space on device
    hipMalloc((void **) &matchArray, sizeof(int)*numThreads);
    hipMalloc((void **) &outArray, sizeof(int)*numThreads);
    hostMatchArray = (int*)malloc(sizeof(int)*numThreads);
    hipMalloc((void **) &matchStartArray, sizeof(int)*numThreads);
    hostMatchStartArray = (int*)malloc(sizeof(int)*numThreads);
    hipMalloc((void **) &matchEndArray, sizeof(int)*numThreads);
    hostMatchEndArray = (int*)malloc(sizeof(int)*numThreads);
    //Init array to 0
    int i;
    for(i = 0; i < numThreads; i++)
    {
      hostMatchArray[i] = 0; 
      hostMatchStartArray[i] = -1; 
      hostMatchEndArray[i] = -1; 
    }
    hipMemcpy(matchArray, hostMatchArray, numThreads, hipMemcpyHostToDevice);
    hipMemcpy(matchStartArray, hostMatchStartArray, numThreads, hipMemcpyHostToDevice);
    hipMemcpy(matchEndArray, hostMatchEndArray, numThreads, hipMemcpyHostToDevice);
    //printf("Number of threads:%d, Number of blocks:%d, Num Threads Per Block:%d, Num Bytes Per Thread:%d\n",numThreads,nBlocks,threadsPerBlock,numBytesPerThread);
    // Part 2 of 2. Call call checkMatchOnDevice kernel
    hipEventRecord( start, 0 );
    checkMatchOnDevice <<< nBlocks, threadsPerBlock , threadsPerBlock*sizeof(int)>>> (deviceFileBuffer, deviceSearchBuffer, matchArray,numBytesPerThread,searchSize,matchStartArray,matchEndArray);
    int newNBlocks=nBlocks,newNThreads;//printf("\nNew Blocks:%d",nBlocks);
    hipDeviceSynchronize();
	while(newNBlocks > 1){
	newNThreads = newNBlocks;
	newNBlocks = (newNBlocks/threadsPerBlock)+1;
    cumulateOnDevice <<< newNBlocks, threadsPerBlock ,threadsPerBlock * sizeof(int)>>> (matchArray,newNThreads,outArray);
	}
    hipEventRecord( stop, 0 ); 
    hipEventSynchronize( stop ); 
    hipEventElapsedTime( &time, start, stop ); 
    hipEventDestroy( start ); 
    hipEventDestroy( stop ); 
    hipDeviceSynchronize();
    // Retrieve result from device and store in host array
    hipMemcpy(hostMatchArray, matchArray, sizeof(int)*numThreads, hipMemcpyDeviceToHost);
    hipMemcpy(hostMatchStartArray, matchStartArray, sizeof(int)*numThreads, hipMemcpyDeviceToHost);
    hipMemcpy(hostMatchEndArray, matchEndArray, sizeof(int)*numThreads, hipMemcpyDeviceToHost);
    int total = 0;
    //for(i = 0; i < numThreads; i++)
    //{
	//total += hostMatchArray[i];
       //printf("%d)%d\n",i,hostMatchArray[i]);
    //}
	total = hostMatchArray [0];
    //Overlap check, commented out for hw2  
/*  for(i = 0; i < numThreads; i++)
    {
	if(hostMatchEndArray[i] != -1 && hostMatchStartArray[i+1] != -1)
	{
		if(hostMatchEndArray[i] - hostMatchStartArray[i+1] < 0)
			total--;	
	}
	//printf("%d)%d\n",i,hostMatchStartArray[i]);
	//printf("start:%d,end:%d\n",hostMatchStartArray[i],hostMatchEndArray[i]);
    }*/
    
    gettimeofday(&cpuEnd, NULL);
   // float totalTime = (cpuEnd - cpuStart);
    //printf("Number of threads:%d, Number of blocks:%d, Num Threads Per Block:%d, Num Bytes Per Thread:%d\n",numThreads,nBlocks,threadsPerBlock,numBytesPerThread);
    //printf("numOfThread: %4d matchCount: %4d CPUrunningTime: %8ld\n", blocksize, thread_num, sum, q.tv_usec - p.tv_usec + (q.tv_sec-p.tv_sec)*1000000);
    #ifdef VERBOSE
    printf("Completed Successfully! Number of blocks:%d Number of threads per block:%d Num Threads: %d Matches:%d CPU Time:%8ld GPU Time:%f\n\n",nBlocks,threadsPerBlock,numThreads,total,cpuEnd.tv_usec - cpuStart.tv_usec + (cpuEnd.tv_sec - cpuStart.tv_sec ),time);
    #else
    printf("%d %f\n\n",numThreads,time);
    //printf("%d %8ld\n\n",numThreads,cpuEnd.tv_usec - cpuStart.tv_usec + (cpuEnd.tv_usec - cpuStart.tv_usec) );
    #endif
    hipFree(matchArray);
    hipFree(outArray);
  }
  
}

	.text
	.file	"p4.hip"
	.globl	_Z33__device_stub__checkMatchOnDevicePcS_PiimS0_S0_ # -- Begin function _Z33__device_stub__checkMatchOnDevicePcS_PiimS0_S0_
	.type	_Z33__device_stub__checkMatchOnDevicePcS_PiimS0_S0_,@function
_Z33__device_stub__checkMatchOnDevicePcS_PiimS0_S0_: # @_Z33__device_stub__checkMatchOnDevicePcS_PiimS0_S0_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$160, %rsp
	.cfi_def_cfa_offset 208
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	32(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	24(%rsp), %r8
	movq	%r9, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	208(%rsp), %rax
	movq	%rax, 48(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%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	$_Z18checkMatchOnDevicePcS_PiimS0_S0_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$176, %rsp
	.cfi_adjust_cfa_offset -176
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z33__device_stub__checkMatchOnDevicePcS_PiimS0_S0_, .Lfunc_end0-_Z33__device_stub__checkMatchOnDevicePcS_PiimS0_S0_
	.cfi_endproc
                                        # -- End function
	.globl	_Z31__device_stub__cumulateOnDevicePiiS_ # -- Begin function _Z31__device_stub__cumulateOnDevicePiiS_
	.type	_Z31__device_stub__cumulateOnDevicePiiS_,@function
_Z31__device_stub__cumulateOnDevicePiiS_: # @_Z31__device_stub__cumulateOnDevicePiiS_
	.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	12(%rsp), %rcx
	movl	%esi, (%rcx)
	leaq	32(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z16cumulateOnDevicePiiS_, %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	_Z31__device_stub__cumulateOnDevicePiiS_, .Lfunc_end1-_Z31__device_stub__cumulateOnDevicePiiS_
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$168, %rsp
	.cfi_def_cfa_offset 224
	.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, %r15
	movl	%edi, %ebx
	movl	$80000000, %edi                 # imm = 0x4C4B400
	callq	malloc
	cmpl	$4, %ebx
	jne	.LBB2_14
# %bb.1:
	movq	%rax, %r14
	movq	8(%r15), %rbx
	movq	16(%r15), %rdi
	callq	atoi
	movl	%eax, %ebp
	movq	24(%r15), %rdi
	callq	atoi
	movl	%eax, 16(%rsp)                  # 4-byte Spill
	xorl	%r15d, %r15d
	movl	$.L.str.1, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	stdin(%rip), %rdi
	callq	feof
	testl	%eax, %eax
	movl	%ebp, 20(%rsp)                  # 4-byte Spill
	jne	.LBB2_4
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movslq	%r15d, %r15
	leaq	(%r14,%r15), %rdi
	movq	stdin(%rip), %rcx
	movl	$1, %esi
	movl	$1, %edx
	callq	fread
	addl	%eax, %r15d
	movq	stdin(%rip), %rdi
	callq	feof
	testl	%eax, %eax
	je	.LBB2_2
# %bb.3:                                # %._crit_edge.loopexit
	movslq	%r15d, %r15
.LBB2_4:                                # %._crit_edge
	movq	%rbx, %rdi
	callq	strlen
	movq	%rax, %rbp
	leaq	56(%rsp), %rdi
	callq	hipEventCreate
	leaq	32(%rsp), %rdi
	callq	hipEventCreate
	callq	hipDeviceSynchronize
	leaq	136(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	120(%rsp), %r12
	movq	%r12, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	leaq	112(%rsp), %r13
	movq	%r13, %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	movq	(%r12), %rdi
	movq	%r14, %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r13), %rdi
	movq	%rbx, %rsi
	movq	%rbp, 48(%rsp)                  # 8-byte Spill
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	16(%rsp), %ebx                  # 4-byte Reload
	movl	20(%rsp), %ebp                  # 4-byte Reload
	imull	%ebp, %ebx
	movslq	%ebx, %r14
	movq	%r15, %rax
	xorl	%edx, %edx
	divq	%r14
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	leaq	(,%r14,4), %r15
	leaq	24(%rsp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	leaq	80(%rsp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r12
	leaq	72(%rsp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r13
	leaq	64(%rsp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	%r15, 104(%rsp)                 # 8-byte Spill
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r15
	testl	%r14d, %r14d
	je	.LBB2_6
# %bb.5:                                # %.lr.ph94.preheader
	movl	%ebx, %ebx
	shlq	$2, %rbx
	movq	%r12, %rdi
	xorl	%esi, %esi
	movq	%rbx, %rdx
	callq	memset@PLT
	movq	%r13, %rdi
	movl	$255, %esi
	movq	%rbx, %rdx
	callq	memset@PLT
	movq	%r15, %rdi
	movl	$255, %esi
	movq	%rbx, %rdx
	callq	memset@PLT
.LBB2_6:                                # %._crit_edge95
	movq	24(%rsp), %rdi
	movq	%r12, 96(%rsp)                  # 8-byte Spill
	movq	%r12, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	72(%rsp), %rdi
	movq	%r13, 88(%rsp)                  # 8-byte Spill
	movq	%r13, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	64(%rsp), %rdi
	movq	%r15, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	16(%rsp), %eax                  # 4-byte Reload
	movslq	%eax, %r13
	shlq	$2, %r13
	movl	%ebp, %edi
	btsq	$32, %rdi
	movl	%eax, %ebx
	btsq	$32, %rbx
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	movq	%r13, %r8
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_8
# %bb.7:
	movq	120(%rsp), %rdi
	movq	112(%rsp), %rsi
	movq	24(%rsp), %rdx
	movq	72(%rsp), %r9
	movq	64(%rsp), %rax
	movq	%rax, (%rsp)
	movq	40(%rsp), %rcx                  # 8-byte Reload
                                        # kill: def $ecx killed $ecx killed $rcx
	movq	48(%rsp), %r8                   # 8-byte Reload
	callq	_Z33__device_stub__checkMatchOnDevicePcS_PiimS0_S0_
.LBB2_8:
	movq	%r15, 40(%rsp)                  # 8-byte Spill
	movq	%r14, 48(%rsp)                  # 8-byte Spill
	callq	hipDeviceSynchronize
	cmpl	$2, %ebp
	jl	.LBB2_13
# %bb.9:                                # %.lr.ph98.preheader
	movabsq	$4294967296, %r15               # imm = 0x100000000
	movl	20(%rsp), %r12d                 # 4-byte Reload
.LBB2_10:                               # %.lr.ph98
                                        # =>This Inner Loop Header: Depth=1
	movl	%r12d, %eax
	cltd
	idivl	16(%rsp)                        # 4-byte Folded Reload
	movl	%eax, %r14d
	leal	1(%r14), %ebp
	movq	%rbp, %rdi
	orq	%r15, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	movq	%r13, %r8
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_12
# %bb.11:                               #   in Loop: Header=BB2_10 Depth=1
	movq	24(%rsp), %rdi
	movq	80(%rsp), %rdx
	movl	%r12d, %esi
	callq	_Z31__device_stub__cumulateOnDevicePiiS_
.LBB2_12:                               #   in Loop: Header=BB2_10 Depth=1
	movl	%ebp, %r12d
	testl	%r14d, %r14d
	jg	.LBB2_10
.LBB2_13:                               # %._crit_edge99
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	32(%rsp), %rdi
	callq	hipEventSynchronize
	movq	56(%rsp), %rsi
	movq	32(%rsp), %rdx
	leaq	132(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	movq	56(%rsp), %rdi
	callq	hipEventDestroy
	movq	32(%rsp), %rdi
	callq	hipEventDestroy
	callq	hipDeviceSynchronize
	movq	24(%rsp), %rsi
	movq	96(%rsp), %r15                  # 8-byte Reload
	movq	%r15, %rdi
	movq	104(%rsp), %r14                 # 8-byte Reload
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	72(%rsp), %rsi
	movq	88(%rsp), %rdi                  # 8-byte Reload
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	64(%rsp), %rsi
	movq	40(%rsp), %rdi                  # 8-byte Reload
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	(%r15), %r14d
	leaq	152(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	8(%r15), %r9
	movq	144(%rsp), %rax
	addq	(%r15), %r9
	addq	136(%rsp), %rax
	subq	%rax, %r9
	cvtss2sd	(%rbx), %xmm0
	movl	$.L.str.2, %edi
	movl	20(%rsp), %esi                  # 4-byte Reload
	movl	16(%rsp), %edx                  # 4-byte Reload
	movq	48(%rsp), %rcx                  # 8-byte Reload
	movl	%r14d, %r8d
	movb	$1, %al
	callq	printf
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	80(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$168, %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
.LBB2_14:
	.cfi_def_cfa_offset 224
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%edi, %edi
	callq	exit
.Lfunc_end2:
	.size	main, .Lfunc_end2-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	.LBB3_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB3_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z18checkMatchOnDevicePcS_PiimS0_S0_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z16cumulateOnDevicePiiS_, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_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	_Z18checkMatchOnDevicePcS_PiimS0_S0_,@object # @_Z18checkMatchOnDevicePcS_PiimS0_S0_
	.section	.rodata,"a",@progbits
	.globl	_Z18checkMatchOnDevicePcS_PiimS0_S0_
	.p2align	3, 0x0
_Z18checkMatchOnDevicePcS_PiimS0_S0_:
	.quad	_Z33__device_stub__checkMatchOnDevicePcS_PiimS0_S0_
	.size	_Z18checkMatchOnDevicePcS_PiimS0_S0_, 8

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

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"Search String: %s\n"
	.size	.L.str.1, 19

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Completed Successfully! Number of blocks:%d Number of threads per block:%d Num Threads: %d Matches:%d CPU Time:%8ld GPU Time:%f\n\n"
	.size	.L.str.2, 130

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z18checkMatchOnDevicePcS_PiimS0_S0_"
	.size	.L__unnamed_1, 37

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z16cumulateOnDevicePiiS_"
	.size	.L__unnamed_2, 26

	.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	"Usage: stringSearch \"Key\" numBlocks threadsPerBlock < inputFile"
	.size	.Lstr, 64

	.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 _Z33__device_stub__checkMatchOnDevicePcS_PiimS0_S0_
	.addrsig_sym _Z31__device_stub__cumulateOnDevicePiiS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z18checkMatchOnDevicePcS_PiimS0_S0_
	.addrsig_sym _Z16cumulateOnDevicePiiS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z18checkMatchOnDevicePcS_PiimS0_S0_ ; -- Begin function _Z18checkMatchOnDevicePcS_PiimS0_S0_
	.globl	_Z18checkMatchOnDevicePcS_PiimS0_S0_
	.p2align	8
	.type	_Z18checkMatchOnDevicePcS_PiimS0_S0_,@function
_Z18checkMatchOnDevicePcS_PiimS0_S0_:   ; @_Z18checkMatchOnDevicePcS_PiimS0_S0_
; %bb.0:
	s_clause 0x5
	s_load_b32 s9, s[0:1], 0x44
	s_load_b64 s[12:13], s[0:1], 0x30
	s_load_b32 s14, s[0:1], 0x18
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[10:11], s[0:1], 0x10
	s_load_b128 s[0:3], s[0:1], 0x20
	s_mov_b32 s8, s15
	v_mov_b32_e32 v4, -1
	s_waitcnt lgkmcnt(0)
	s_and_b32 s9, s9, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mad_u64_u32 v[1:2], null, s8, s9, v[0:1]
	s_cmp_lt_i32 s14, 1
	s_cbranch_scc1 .LBB0_13
; %bb.1:                                ; %.preheader.lr.ph
	s_delay_alu instid0(VALU_DEP_1)
	v_mul_lo_u32 v2, v1, s14
	v_dual_mov_b32 v3, 0 :: v_dual_mov_b32 v4, -1
	v_dual_mov_b32 v7, 1 :: v_dual_mov_b32 v6, 0
	s_cmp_lg_u64 s[0:1], 0
	s_mov_b32 s17, 0
	s_cselect_b32 s16, -1, 0
	s_delay_alu instid0(VALU_DEP_3)
	v_dual_mov_b32 v5, -1 :: v_dual_add_nc_u32 v8, s14, v2
	s_add_i32 s18, s0, -1
.LBB0_2:                                ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
	s_and_not1_b32 vcc_lo, exec_lo, s16
	s_cbranch_vccnz .LBB0_8
; %bb.3:                                ; %.lr.ph.preheader
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_mov_b64 s[14:15], 0
	s_mov_b32 s20, 0
                                        ; implicit-def: $sgpr19
                                        ; implicit-def: $sgpr22
                                        ; implicit-def: $sgpr21
.LBB0_4:                                ; %.lr.ph
                                        ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	v_add_nc_u32_e32 v9, s14, v2
	s_add_u32 s24, s6, s14
	s_addc_u32 s25, s7, s15
	s_or_b32 s21, s21, exec_lo
	s_or_b32 s22, s22, exec_lo
	v_ashrrev_i32_e32 v10, 31, v9
	v_add_co_u32 v9, vcc_lo, s4, v9
	s_mov_b32 s23, exec_lo
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v10, vcc_lo, s5, v10, vcc_lo
	global_load_u8 v9, v[9:10], off
	global_load_u8 v10, v6, s[24:25]
	s_waitcnt vmcnt(0)
	v_cmpx_eq_u16_e64 v9, v10
; %bb.5:                                ;   in Loop: Header=BB0_4 Depth=2
	s_add_u32 s14, s14, 1
	s_addc_u32 s15, s15, 0
	s_and_not1_b32 s22, s22, exec_lo
	v_cmp_ge_u64_e64 s24, s[14:15], s[0:1]
	s_and_not1_b32 s21, s21, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s24, s24, exec_lo
	s_or_b32 s22, s22, s24
; %bb.6:                                ; %Flow102
                                        ;   in Loop: Header=BB0_4 Depth=2
	s_or_b32 exec_lo, exec_lo, s23
	s_xor_b32 s23, s21, -1
	s_and_b32 s24, exec_lo, s22
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	s_or_b32 s20, s24, s20
	s_and_not1_b32 s19, s19, exec_lo
	s_and_b32 s23, s23, exec_lo
	s_or_b32 s19, s19, s23
	s_and_not1_b32 exec_lo, exec_lo, s20
	s_cbranch_execnz .LBB0_4
; %bb.7:                                ; %loop.exit.guard
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s20
	s_branch .LBB0_9
.LBB0_8:                                ;   in Loop: Header=BB0_2 Depth=1
	s_mov_b32 s19, -1
                                        ; implicit-def: $vgpr4
.LBB0_9:                                ; %Flow103
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s14, s19
; %bb.10:                               ; %.critedge.loopexit
                                        ;   in Loop: Header=BB0_2 Depth=1
	v_add_nc_u32_e32 v9, s18, v2
	v_cmp_eq_u32_e32 vcc_lo, 0, v7
	v_add_nc_u32_e32 v4, s0, v2
	v_add_nc_u32_e32 v3, 1, v3
	v_mov_b32_e32 v7, 0
	v_dual_cndmask_b32 v5, v2, v5 :: v_dual_mov_b32 v2, v9
; %bb.11:                               ; %.loopexit
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s14
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v2, 1, v2
	v_cmp_ge_i32_e32 vcc_lo, v2, v8
	s_or_b32 s17, vcc_lo, s17
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s17
	s_cbranch_execnz .LBB0_2
; %bb.12:                               ; %Flow105
	s_or_b32 exec_lo, exec_lo, s17
	s_branch .LBB0_14
.LBB0_13:
	v_mov_b32_e32 v3, 0
	v_mov_b32_e32 v5, -1
.LBB0_14:                               ; %._crit_edge
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v2, 31, v1
	v_lshl_add_u32 v8, v0, 2, 0
	s_cmp_lt_u32 s9, 2
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	ds_store_b32 v8, v3
	v_add_co_u32 v6, vcc_lo, s2, v1
	v_add_co_ci_u32_e32 v7, vcc_lo, s3, v2, vcc_lo
	v_add_co_u32 v1, vcc_lo, s12, v1
	v_add_co_ci_u32_e32 v2, vcc_lo, s13, v2, vcc_lo
	global_store_b32 v[6:7], v5, off
	global_store_b32 v[1:2], v4, off
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_19
; %bb.15:                               ; %.lr.ph78.preheader
	s_mov_b32 s1, 1
.LBB0_16:                               ; %.lr.ph78
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	s_lshl_b32 s0, s1, 1
	s_mov_b32 s2, exec_lo
	v_mul_lo_u32 v1, s0, v0
	v_add_nc_u32_e32 v2, s1, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_u32_e64 s9, v2
	s_cbranch_execz .LBB0_18
; %bb.17:                               ;   in Loop: Header=BB0_16 Depth=1
	v_lshl_add_u32 v1, v1, 2, 0
	s_delay_alu instid0(VALU_DEP_1)
	v_lshl_add_u32 v2, s1, 2, v1
	ds_load_b32 v2, v2
	ds_load_b32 v3, v1
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v2, v3, v2
	ds_store_b32 v1, v2
.LBB0_18:                               ;   in Loop: Header=BB0_16 Depth=1
	s_or_b32 exec_lo, exec_lo, s2
	s_cmp_ge_u32 s0, s9
	s_mov_b32 s1, s0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_16
.LBB0_19:                               ; %._crit_edge79
	s_mov_b32 s0, exec_lo
	v_cmpx_eq_u32_e32 0, v0
	s_cbranch_execz .LBB0_21
; %bb.20:
	v_mov_b32_e32 v0, 0
	s_mov_b32 s9, 0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[0:1], s[8:9], 2
	s_add_u32 s0, s10, s0
	ds_load_b32 v1, v0
	s_addc_u32 s1, s11, s1
	s_waitcnt lgkmcnt(0)
	global_store_b32 v0, v1, s[0:1]
.LBB0_21:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z18checkMatchOnDevicePcS_PiimS0_S0_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 312
		.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 11
		.amdhsa_next_free_sgpr 26
		.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	_Z18checkMatchOnDevicePcS_PiimS0_S0_, .Lfunc_end0-_Z18checkMatchOnDevicePcS_PiimS0_S0_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 704
; NumSgprs: 28
; NumVgprs: 11
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 3
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 28
; NumVGPRsForWavesPerEU: 11
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.protected	_Z16cumulateOnDevicePiiS_ ; -- Begin function _Z16cumulateOnDevicePiiS_
	.globl	_Z16cumulateOnDevicePiiS_
	.p2align	8
	.type	_Z16cumulateOnDevicePiiS_,@function
_Z16cumulateOnDevicePiiS_:              ; @_Z16cumulateOnDevicePiiS_
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x24
	s_load_b32 s3, s[0:1], 0x8
	s_mov_b32 s2, s15
	s_mov_b32 s5, exec_lo
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s2, s4, v[0:1]
	v_cmpx_gt_u32_e64 s3, v1
	s_cbranch_execz .LBB1_8
; %bb.1:
	s_load_b64 s[0:1], s[0:1], 0x0
	v_mov_b32_e32 v2, 0
	s_cmp_lt_u32 s4, 2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v1, vcc_lo, s0, v1
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v2, vcc_lo, s1, v2, vcc_lo
	global_load_b32 v1, v[1:2], off
	v_lshl_add_u32 v2, v0, 2, 0
	s_waitcnt vmcnt(0)
	ds_store_b32 v2, v1
	s_cbranch_scc1 .LBB1_6
; %bb.2:                                ; %.lr.ph.preheader
	s_mov_b32 s6, 1
.LBB1_3:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_lshl_b32 s5, s6, 1
	s_mov_b32 s7, exec_lo
	v_mul_lo_u32 v1, s5, v0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_add_nc_u32_e32 v2, s6, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_u32_e64 s3, v2
	s_cbranch_execz .LBB1_5
; %bb.4:                                ;   in Loop: Header=BB1_3 Depth=1
	v_lshl_add_u32 v1, v1, 2, 0
	s_delay_alu instid0(VALU_DEP_1)
	v_lshl_add_u32 v2, s6, 2, v1
	ds_load_b32 v2, v2
	ds_load_b32 v3, v1
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v2, v3, v2
	ds_store_b32 v1, v2
.LBB1_5:                                ;   in Loop: Header=BB1_3 Depth=1
	s_or_b32 exec_lo, exec_lo, s7
	s_cmp_ge_u32 s5, s4
	s_mov_b32 s6, s5
	s_cbranch_scc0 .LBB1_3
.LBB1_6:                                ; %._crit_edge
	v_cmp_eq_u32_e32 vcc_lo, 0, v0
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB1_8
; %bb.7:
	v_mov_b32_e32 v0, 0
	s_mov_b32 s3, 0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[2:3], s[2:3], 2
	s_add_u32 s0, s0, s2
	ds_load_b32 v1, v0
	s_addc_u32 s1, s1, s3
	s_waitcnt lgkmcnt(0)
	global_store_b32 v0, v1, s[0:1]
.LBB1_8:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z16cumulateOnDevicePiiS_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 4
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end1:
	.size	_Z16cumulateOnDevicePiiS_, .Lfunc_end1-_Z16cumulateOnDevicePiiS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 340
; NumSgprs: 18
; NumVgprs: 4
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 4
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

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

	.end_amdgpu_metadata
