/*****************************************************************************
*
* String Pattern Matching - Serial Implementation
* 
* Reference: http://people.maths.ox.ac.uk/~gilesm/cuda/
*
*****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctime>

// Includes CUDA
#include <cuda_runtime.h>

#define LINEWIDTH 20

// citation: https://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api
#define checkCudaErrors(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
   if (code != cudaSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

void matchPattern_CPU(unsigned int *text, unsigned int *words, int *matches, int nwords, int length)
{
	unsigned int word;

	for (int l=0; l<length; l++)
	{
		for (int offset=0; offset<4; offset++)
		{
			if (offset==0)
				word = text[l];
			else
				word = (text[l]>>(8*offset)) + (text[l+1]<<(32-8*offset)); 

			for (int w=0; w<nwords; w++){
				matches[w] += (word==words[w]);
			} 
				
		}
	}
}

__global__ void matchPattern_gpu_1(unsigned int *text, unsigned int *words, int *matches, int nwords, int length)
{
	unsigned int word;

	int idx = blockIdx.x * blockDim.x + threadIdx.x;

	if(idx < length)
    {
		for (int offset=0; offset<4; offset++)
		{
			if (offset==0)
				word = text[idx];
			else
				word = (text[idx]>>(8*offset)) + (text[idx+1]<<(32-8*offset)); 

			for (int w=0; w<nwords; w++){
				if (word==words[w]){
					atomicAdd(&matches[w],1);
				}
			} 	
		}
	}
}


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

	int length, len, nwords=5, matches[nwords];
	char *ctext, keywords[nwords][LINEWIDTH], *line;
	line = (char*) malloc(sizeof(char)*LINEWIDTH);
	unsigned int  *text,  *words;
	memset(matches, 0, sizeof(matches));


	// read in text and keywords for processing
	FILE *fp, *wfile;
	wfile = fopen("./data/keywords.txt","r");
	if (!wfile)
	{	printf("keywords.txt: File not found.\n");	exit(0);}

	int k=0, cnt = nwords;
	size_t read, linelen = LINEWIDTH;
	while((read = getline(&line, &linelen, wfile)) != -1 && cnt--)
	{
		strncpy(keywords[k], line, sizeof(line));
		keywords[k][4] = '\0';
		k++;
	}
	fclose(wfile);



	fp = fopen("./data/small.txt","r");
	if (!fp)
	{	printf("Unable to open the file.\n");	exit(0);}

	length = 0;
	while (getc(fp) != EOF) length++;
	ctext = (char *) malloc(length+4);

	rewind(fp);

	for (int l=0; l<length; l++) ctext[l] = getc(fp);
	for (int l=length; l<length+4; l++) ctext[l] = ' ';

	fclose(fp);

	printf("Length : %d\n", length );
	// define number of words of text, and set pointers
	len  = length/4;
	text = (unsigned int *) ctext;

	// define words for matching
	words = (unsigned int *) malloc(nwords*sizeof(unsigned int));

	for (int w=0; w<nwords; w++)
	{
		words[w] = ((unsigned int) keywords[w][0])
             + ((unsigned int) keywords[w][1])*(1<<8)
             + ((unsigned int) keywords[w][2])*(1<<16)
             + ((unsigned int) keywords[w][3])*(1<<24);

	}
	// CPU execution
	const clock_t begin_time = clock();
	matchPattern_CPU(text, words, matches, nwords, len);
	float runTime = (float)( clock() - begin_time ) /  CLOCKS_PER_SEC;
	printf("Time for matching keywords: %fs\n\n", runTime);

	printf("Printing Matches:\n");
	printf("Word\t  |\tNumber of Matches\n===================================\n");
	for (int i = 0; i < nwords; ++i)
		printf("%s\t  |\t%d\n", keywords[i], matches[i]);


	// GPU execution
	unsigned int *d_text; unsigned int *d_words; int *d_matches;
	int *h_matches;
	h_matches = (int *)malloc(nwords*sizeof(int));

	cudaMalloc((void**)&d_words, nwords*sizeof(unsigned int));
	cudaMalloc((void**)&d_matches, nwords*sizeof(int));
	cudaMalloc((void**)&d_text, sizeof(char)*strlen(ctext));


	cudaMemcpy(d_text, text, sizeof(char)*strlen(ctext), cudaMemcpyHostToDevice);
	cudaMemcpy(d_words, words, nwords*sizeof(unsigned int), cudaMemcpyHostToDevice);

	matchPattern_gpu_1<<<len/32,32>>>(d_text, d_words, d_matches, nwords, len);
	checkCudaErrors(cudaPeekAtLastError());
	checkCudaErrors(cudaMemcpy(h_matches, d_matches, nwords*sizeof(int), cudaMemcpyDeviceToHost));

	for(int i = 0; i<nwords; i++) {
		if(matches[i] != h_matches[i]) {
			printf("WRONG OUTPUT:\t %s\t|\t%d\n",  keywords[i], h_matches[i]);
		}
	}
	
	free(ctext);
	free(words);
	cudaFree(d_words);
	cudaFree(d_matches);
	cudaFree(d_text);
	
}

	.file	"tmpxft_0032f661_00000000-6_pattern_CPU.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._Z9gpuAssert9cudaErrorPKcib.str1.1,"aMS",@progbits,1
.LC0:
	.string	"GPUassert: %s %s %d\n"
	.section	.text._Z9gpuAssert9cudaErrorPKcib,"axG",@progbits,_Z9gpuAssert9cudaErrorPKcib,comdat
	.weak	_Z9gpuAssert9cudaErrorPKcib
	.type	_Z9gpuAssert9cudaErrorPKcib, @function
_Z9gpuAssert9cudaErrorPKcib:
.LFB2027:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L8
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	%edi, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movl	%edx, 12(%rsp)
	movq	%rsi, (%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r9d
	movq	(%rsp), %r8
	leaq	.LC0(%rip), %rdx
	movq	stderr(%rip), %rdi
	movq	%rax, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	testb	%bpl, %bpl
	je	.L2
	movl	%ebx, %edi
	call	exit@PLT
.L2:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
.L8:
	.cfi_restore 3
	.cfi_restore 6
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9gpuAssert9cudaErrorPKcib, .-_Z9gpuAssert9cudaErrorPKcib
	.text
	.globl	_Z16matchPattern_CPUPjS_Piii
	.type	_Z16matchPattern_CPUPjS_Piii, @function
_Z16matchPattern_CPUPjS_Piii:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movq	%rsi, %r10
	movl	%ecx, %r11d
	movq	%rdx, %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	xorl	%eax, %eax
	movl	$32, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
.L13:
	cmpl	%eax, %r8d
	jle	.L20
	movl	(%rdi,%rax,4), %edx
	xorl	%r9d, %r9d
	xorl	%ebx, %ebx
.L17:
	xorl	%ecx, %ecx
.L14:
	cmpl	%ecx, %r11d
	jle	.L21
	xorl	%r12d, %r12d
	cmpl	%edx, (%r10,%rcx,4)
	sete	%r12b
	addl	%r12d, (%rsi,%rcx,4)
	incq	%rcx
	jmp	.L14
.L21:
	incl	%ebx
	addl	$8, %r9d
	cmpl	$4, %ebx
	je	.L16
	movl	4(%rdi,%rax,4), %edx
	movl	%ebp, %ecx
	movl	(%rdi,%rax,4), %r12d
	subl	%r9d, %ecx
	sall	%cl, %edx
	movl	%r9d, %ecx
	shrl	%cl, %r12d
	addl	%r12d, %edx
	jmp	.L17
.L16:
	incq	%rax
	jmp	.L13
.L20:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z16matchPattern_CPUPjS_Piii, .-_Z16matchPattern_CPUPjS_Piii
	.globl	_Z44__device_stub__Z18matchPattern_gpu_1PjS_PiiiPjS_Piii
	.type	_Z44__device_stub__Z18matchPattern_gpu_1PjS_PiiiPjS_Piii, @function
_Z44__device_stub__Z18matchPattern_gpu_1PjS_PiiiPjS_Piii:
.LFB2054:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movq	%rdx, 8(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 4(%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%rsp, %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L22
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z18matchPattern_gpu_1PjS_Piii(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L22:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L24
	call	__stack_chk_fail@PLT
.L24:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2054:
	.size	_Z44__device_stub__Z18matchPattern_gpu_1PjS_PiiiPjS_Piii, .-_Z44__device_stub__Z18matchPattern_gpu_1PjS_PiiiPjS_Piii
	.globl	_Z18matchPattern_gpu_1PjS_Piii
	.type	_Z18matchPattern_gpu_1PjS_Piii, @function
_Z18matchPattern_gpu_1PjS_Piii:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_Z44__device_stub__Z18matchPattern_gpu_1PjS_PiiiPjS_Piii
	.cfi_endproc
.LFE2055:
	.size	_Z18matchPattern_gpu_1PjS_Piii, .-_Z18matchPattern_gpu_1PjS_Piii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"r"
.LC2:
	.string	"./data/keywords.txt"
.LC3:
	.string	"keywords.txt: File not found.\n"
.LC4:
	.string	"./data/small.txt"
.LC5:
	.string	"Unable to open the file.\n"
.LC6:
	.string	"Length : %d\n"
.LC8:
	.string	"Time for matching keywords: %fs\n\n"
.LC9:
	.string	"Printing Matches:\n"
.LC10:
	.string	"Word\t  |\tNumber of Matches\n===================================\n"
.LC11:
	.string	"%s\t  |\t%d\n"
.LC12:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/sedflix/cuda_pattern_matching/master/pattern_CPU.cu"
.LC13:
	.string	"WRONG OUTPUT:\t %s\t|\t%d\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
	movl	$20, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	leaq	.LC1(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$216, %rsp
	.cfi_def_cfa_offset 272
	movq	%fs:40, %rax
	movq	%rax, 200(%rsp)
	xorl	%eax, %eax
	leaq	80(%rsp), %r12
	call	malloc@PLT
	movl	$5, %ecx
	movq	%r12, %rdi
	movq	%r13, %rsi
	movq	%rax, 16(%rsp)
	xorl	%eax, %eax
	rep stosl
	leaq	.LC2(%rip), %rdi
	call	fopen@PLT
	leaq	.LC3(%rip), %rsi
	testq	%rax, %rax
	je	.L53
	movq	$20, 24(%rsp)
	movq	%rax, %rbp
	xorl	%ebx, %ebx
	leaq	24(%rsp), %r14
.L30:
	leaq	16(%rsp), %rdi
	movq	%rbp, %rdx
	movq	%r14, %rsi
	call	getline@PLT
	incq	%rax
	je	.L29
	cmpq	$100, %rbx
	je	.L29
	movq	16(%rsp), %rsi
	movl	$100, %ecx
	leaq	100(%rsp,%rbx), %rdi
	movl	$8, %edx
	subq	%rbx, %rcx
	call	__strncpy_chk@PLT
	movb	$0, 104(%rsp,%rbx)
	addq	$20, %rbx
	jmp	.L30
.L29:
	movq	%rbp, %rdi
	xorl	%ebx, %ebx
	call	fclose@PLT
	movq	%r13, %rsi
	leaq	.LC4(%rip), %rdi
	call	fopen@PLT
	movq	%rax, %r13
	testq	%rax, %rax
	jne	.L31
	leaq	.LC5(%rip), %rsi
.L53:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%edi, %edi
	call	exit@PLT
.L31:
	movq	%r13, %rdi
	movl	%ebx, %ebp
	leal	1(%rbx), %ebx
	call	getc@PLT
	incl	%eax
	jne	.L31
	leal	4(%rbp), %edi
	movslq	%ebp, %r15
	movslq	%edi, %rdi
	call	malloc@PLT
	movq	%r13, %rdi
	movq	%rax, %rbx
	call	rewind@PLT
	movq	%rbx, %r14
	leaq	(%r15,%rbx), %rdx
.L32:
	cmpq	%rdx, %r14
	je	.L54
	movq	%r13, %rdi
	movq	%rdx, (%rsp)
	incq	%r14
	call	getc@PLT
	movq	(%rsp), %rdx
	movb	%al, -1(%r14)
	jmp	.L32
.L54:
	movl	$538976288, (%rbx,%r15)
	movq	%r13, %rdi
	leaq	100(%rsp), %r14
	call	fclose@PLT
	movl	%ebp, %edx
	leaq	.LC6(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	%ebp, %eax
	movl	$20, %edi
	sarl	$2, %eax
	movl	%eax, 12(%rsp)
	call	malloc@PLT
	movq	%r14, %rdx
	xorl	%ecx, %ecx
	movq	%rax, (%rsp)
.L34:
	movsbl	3(%rdx), %eax
	movsbl	2(%rdx), %esi
	addq	$20, %rdx
	sall	$8, %eax
	addl	%esi, %eax
	movsbl	-19(%rdx), %esi
	sall	$8, %eax
	addl	%esi, %eax
	movsbl	-20(%rdx), %esi
	sall	$8, %eax
	addl	%esi, %eax
	movq	(%rsp), %rsi
	movl	%eax, (%rsi,%rcx,4)
	incq	%rcx
	cmpq	$5, %rcx
	jne	.L34
	call	clock@PLT
	movl	12(%rsp), %r8d
	movq	%r12, %rdx
	movq	%rbx, %rdi
	movq	(%rsp), %rsi
	movl	$5, %ecx
	movq	%rax, %r13
	xorl	%r15d, %r15d
	call	_Z16matchPattern_CPUPjS_Piii
	call	clock@PLT
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	subq	%r13, %rax
	movq	%r14, %r13
	cvtsi2ssq	%rax, %xmm0
	movb	$1, %al
	divss	.LC7(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L35:
	movl	(%r12,%r15,4), %ecx
	movq	%r13, %rdx
	leaq	.LC11(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	incq	%r15
	addq	$20, %r13
	call	__printf_chk@PLT
	cmpq	$5, %r15
	jne	.L35
	movl	$20, %edi
	sarl	$7, %ebp
	call	malloc@PLT
	leaq	40(%rsp), %rdi
	movl	$20, %esi
	movq	%rax, %r13
	call	cudaMalloc@PLT
	movl	$20, %esi
	leaq	48(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	%rbx, %rdi
	call	strlen@PLT
	leaq	32(%rsp), %rdi
	movq	%rax, %rsi
	call	cudaMalloc@PLT
	movq	%rbx, %rdi
	call	strlen@PLT
	movq	32(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movq	%rax, %rdx
	call	cudaMemcpy@PLT
	movq	(%rsp), %rsi
	movq	40(%rsp), %rdi
	movl	$1, %ecx
	movl	$20, %edx
	call	cudaMemcpy@PLT
	movl	%ebp, 56(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movl	$134217729, %edx
	movl	$1, %ecx
	movl	$1, 76(%rsp)
	movq	%rax, 60(%rsp)
	movq	56(%rsp), %rdi
	salq	$5, %rdx
	movl	64(%rsp), %esi
	movq	%rdx, 68(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L36
	movl	12(%rsp), %r8d
	movq	48(%rsp), %rdx
	movl	$5, %ecx
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	call	_Z44__device_stub__Z18matchPattern_gpu_1PjS_PiiiPjS_Piii
.L36:
	call	cudaPeekAtLastError@PLT
	leaq	.LC12(%rip), %rbp
	movl	$1, %ecx
	xorl	%r15d, %r15d
	movl	%eax, %edi
	movq	%rbp, %rsi
	movl	$162, %edx
	call	_Z9gpuAssert9cudaErrorPKcib
	movq	48(%rsp), %rsi
	movl	$2, %ecx
	movq	%r13, %rdi
	movl	$20, %edx
	call	cudaMemcpy@PLT
	movq	%rbp, %rsi
	movl	$1, %ecx
	movl	$163, %edx
	movl	%eax, %edi
	leaq	.LC13(%rip), %rbp
	call	_Z9gpuAssert9cudaErrorPKcib
.L38:
	movl	0(%r13,%r15), %ecx
	cmpl	%ecx, (%r12,%r15)
	je	.L37
	leaq	(%r15,%r15,4), %rdx
	movq	%rbp, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	addq	%r14, %rdx
	call	__printf_chk@PLT
.L37:
	addq	$4, %r15
	cmpq	$20, %r15
	jne	.L38
	movq	%rbx, %rdi
	call	free@PLT
	movq	(%rsp), %rdi
	call	free@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	200(%rsp), %rax
	subq	%fs:40, %rax
	je	.L39
	call	__stack_chk_fail@PLT
.L39:
	addq	$216, %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
.LC14:
	.string	"_Z18matchPattern_gpu_1PjS_Piii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC14(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z18matchPattern_gpu_1PjS_Piii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2057:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC7:
	.long	1232348160
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

/*****************************************************************************
*
* String Pattern Matching - Serial Implementation
* 
* Reference: http://people.maths.ox.ac.uk/~gilesm/cuda/
*
*****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctime>

// Includes CUDA
#include <hip/hip_runtime.h>

#define LINEWIDTH 20

// citation: https://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api
#define checkCudaErrors(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(hipError_t code, const char *file, int line, bool abort=true)
{
   if (code != hipSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", hipGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

void matchPattern_CPU(unsigned int *text, unsigned int *words, int *matches, int nwords, int length)
{
	unsigned int word;

	for (int l=0; l<length; l++)
	{
		for (int offset=0; offset<4; offset++)
		{
			if (offset==0)
				word = text[l];
			else
				word = (text[l]>>(8*offset)) + (text[l+1]<<(32-8*offset)); 

			for (int w=0; w<nwords; w++){
				matches[w] += (word==words[w]);
			} 
				
		}
	}
}

__global__ void matchPattern_gpu_1(unsigned int *text, unsigned int *words, int *matches, int nwords, int length)
{
	unsigned int word;

	int idx = blockIdx.x * blockDim.x + threadIdx.x;

	if(idx < length)
    {
		for (int offset=0; offset<4; offset++)
		{
			if (offset==0)
				word = text[idx];
			else
				word = (text[idx]>>(8*offset)) + (text[idx+1]<<(32-8*offset)); 

			for (int w=0; w<nwords; w++){
				if (word==words[w]){
					atomicAdd(&matches[w],1);
				}
			} 	
		}
	}
}


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

	int length, len, nwords=5, matches[nwords];
	char *ctext, keywords[nwords][LINEWIDTH], *line;
	line = (char*) malloc(sizeof(char)*LINEWIDTH);
	unsigned int  *text,  *words;
	memset(matches, 0, sizeof(matches));


	// read in text and keywords for processing
	FILE *fp, *wfile;
	wfile = fopen("./data/keywords.txt","r");
	if (!wfile)
	{	printf("keywords.txt: File not found.\n");	exit(0);}

	int k=0, cnt = nwords;
	size_t read, linelen = LINEWIDTH;
	while((read = getline(&line, &linelen, wfile)) != -1 && cnt--)
	{
		strncpy(keywords[k], line, sizeof(line));
		keywords[k][4] = '\0';
		k++;
	}
	fclose(wfile);



	fp = fopen("./data/small.txt","r");
	if (!fp)
	{	printf("Unable to open the file.\n");	exit(0);}

	length = 0;
	while (getc(fp) != EOF) length++;
	ctext = (char *) malloc(length+4);

	rewind(fp);

	for (int l=0; l<length; l++) ctext[l] = getc(fp);
	for (int l=length; l<length+4; l++) ctext[l] = ' ';

	fclose(fp);

	printf("Length : %d\n", length );
	// define number of words of text, and set pointers
	len  = length/4;
	text = (unsigned int *) ctext;

	// define words for matching
	words = (unsigned int *) malloc(nwords*sizeof(unsigned int));

	for (int w=0; w<nwords; w++)
	{
		words[w] = ((unsigned int) keywords[w][0])
             + ((unsigned int) keywords[w][1])*(1<<8)
             + ((unsigned int) keywords[w][2])*(1<<16)
             + ((unsigned int) keywords[w][3])*(1<<24);

	}
	// CPU execution
	const clock_t begin_time = clock();
	matchPattern_CPU(text, words, matches, nwords, len);
	float runTime = (float)( clock() - begin_time ) /  CLOCKS_PER_SEC;
	printf("Time for matching keywords: %fs\n\n", runTime);

	printf("Printing Matches:\n");
	printf("Word\t  |\tNumber of Matches\n===================================\n");
	for (int i = 0; i < nwords; ++i)
		printf("%s\t  |\t%d\n", keywords[i], matches[i]);


	// GPU execution
	unsigned int *d_text; unsigned int *d_words; int *d_matches;
	int *h_matches;
	h_matches = (int *)malloc(nwords*sizeof(int));

	hipMalloc((void**)&d_words, nwords*sizeof(unsigned int));
	hipMalloc((void**)&d_matches, nwords*sizeof(int));
	hipMalloc((void**)&d_text, sizeof(char)*strlen(ctext));


	hipMemcpy(d_text, text, sizeof(char)*strlen(ctext), hipMemcpyHostToDevice);
	hipMemcpy(d_words, words, nwords*sizeof(unsigned int), hipMemcpyHostToDevice);

	matchPattern_gpu_1<<<len/32,32>>>(d_text, d_words, d_matches, nwords, len);
	checkCudaErrors(hipPeekAtLastError());
	checkCudaErrors(hipMemcpy(h_matches, d_matches, nwords*sizeof(int), hipMemcpyDeviceToHost));

	for(int i = 0; i<nwords; i++) {
		if(matches[i] != h_matches[i]) {
			printf("WRONG OUTPUT:\t %s\t|\t%d\n",  keywords[i], h_matches[i]);
		}
	}
	
	free(ctext);
	free(words);
	hipFree(d_words);
	hipFree(d_matches);
	hipFree(d_text);
	
}

	.text
	.file	"pattern_CPU.hip"
	.globl	_Z16matchPattern_CPUPjS_Piii    # -- Begin function _Z16matchPattern_CPUPjS_Piii
	.type	_Z16matchPattern_CPUPjS_Piii,@function
_Z16matchPattern_CPUPjS_Piii:           # @_Z16matchPattern_CPUPjS_Piii
	.cfi_startproc
# %bb.0:
	testl	%r8d, %r8d
	jle	.LBB0_11
# %bb.1:                                # %.preheader.lr.ph
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset %rbx, -24
	.cfi_offset %rbp, -16
	movl	%ecx, %eax
	movl	%r8d, %r8d
	movl	%ecx, %r9d
	xorl	%r10d, %r10d
.LBB0_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_3 Depth 2
                                        #       Child Loop BB0_7 Depth 3
	xorl	%r11d, %r11d
.LBB0_3:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB0_7 Depth 3
	movl	(%rdi,%r10,4), %ebx
	testl	%r11d, %r11d
	je	.LBB0_5
# %bb.4:                                #   in Loop: Header=BB0_3 Depth=2
	leal	(,%r11,8), %ecx
	shrl	%cl, %ebx
	movl	4(%rdi,%r10,4), %ebp
	negb	%cl
                                        # kill: def $cl killed $cl killed $ecx
	shll	%cl, %ebp
	addl	%ebp, %ebx
.LBB0_5:                                #   in Loop: Header=BB0_3 Depth=2
	testl	%eax, %eax
	jle	.LBB0_8
# %bb.6:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB0_3 Depth=2
	xorl	%ecx, %ecx
.LBB0_7:                                # %.lr.ph
                                        #   Parent Loop BB0_2 Depth=1
                                        #     Parent Loop BB0_3 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	xorl	%ebp, %ebp
	cmpl	(%rsi,%rcx,4), %ebx
	sete	%bpl
	addl	%ebp, (%rdx,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %r9
	jne	.LBB0_7
.LBB0_8:                                # %._crit_edge
                                        #   in Loop: Header=BB0_3 Depth=2
	incl	%r11d
	cmpl	$4, %r11d
	jne	.LBB0_3
# %bb.9:                                #   in Loop: Header=BB0_2 Depth=1
	incq	%r10
	cmpq	%r8, %r10
	jne	.LBB0_2
# %bb.10:
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %rbp
.LBB0_11:                               # %._crit_edge27
	retq
.Lfunc_end0:
	.size	_Z16matchPattern_CPUPjS_Piii, .Lfunc_end0-_Z16matchPattern_CPUPjS_Piii
	.cfi_endproc
                                        # -- End function
	.globl	_Z33__device_stub__matchPattern_gpu_1PjS_Piii # -- Begin function _Z33__device_stub__matchPattern_gpu_1PjS_Piii
	.type	_Z33__device_stub__matchPattern_gpu_1PjS_Piii,@function
_Z33__device_stub__matchPattern_gpu_1PjS_Piii: # @_Z33__device_stub__matchPattern_gpu_1PjS_Piii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$128, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	24(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	4(%rsp), %rdx
	movl	%ecx, (%rdx)
	movq	%rsp, %rcx
	movl	%r8d, (%rcx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z18matchPattern_gpu_1PjS_Piii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z33__device_stub__matchPattern_gpu_1PjS_Piii, .Lfunc_end1-_Z33__device_stub__matchPattern_gpu_1PjS_Piii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI2_0:
	.long	0x49742400                      # float 1.0E+6
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$184, %rsp
	.cfi_def_cfa_offset 240
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$20, %edi
	callq	malloc
	movq	%rax, 16(%rsp)
	xorps	%xmm0, %xmm0
	movaps	%xmm0, 48(%rsp)
	movl	$0, 64(%rsp)
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB2_1
# %bb.3:
	movq	%rax, %rbx
	leaq	72(%rsp), %rsi
	movq	$20, (%rsi)
	leaq	16(%rsp), %rdi
	movq	%rax, %rdx
	callq	getline
	cmpq	$-1, %rax
	je	.LBB2_7
# %bb.4:                                # %.lr.ph.preheader
	xorl	%r13d, %r13d
	leaq	16(%rsp), %r14
	leaq	72(%rsp), %r15
.LBB2_5:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	leaq	(%rsp,%r13), %r12
	addq	$80, %r12
	movq	16(%rsp), %rsi
	movl	$8, %edx
	movq	%r12, %rdi
	callq	strncpy
	movb	$0, 4(%r12)
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%rbx, %rdx
	callq	getline
	cmpq	$-1, %rax
	je	.LBB2_7
# %bb.6:                                # %.lr.ph
                                        #   in Loop: Header=BB2_5 Depth=1
	leaq	20(%r13), %rax
	cmpl	$80, %r13d
	movq	%rax, %r13
	jne	.LBB2_5
.LBB2_7:                                # %.critedge
	movq	%rbx, %rdi
	callq	fclose
	movl	$.L.str.3, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB2_29
# %bb.8:                                # %.preheader103.preheader
	movq	%rax, %r14
	movq	$-1, %r15
.LBB2_9:                                # %.preheader103
                                        # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	callq	getc
	incq	%r15
	cmpl	$-1, %eax
	jne	.LBB2_9
# %bb.10:
	leaq	4(%r15), %rdi
	callq	malloc
	movq	%rax, %rbx
	movq	%r14, %rdi
	callq	rewind
	testl	%r15d, %r15d
	je	.LBB2_13
# %bb.11:                               # %.lr.ph108.preheader
	xorl	%r12d, %r12d
.LBB2_12:                               # %.lr.ph108
                                        # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	callq	getc
	movb	%al, (%rbx,%r12)
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB2_12
.LBB2_13:                               # %.preheader
	movl	$538976288, (%rbx,%r15)         # imm = 0x20202020
	movq	%r14, %rdi
	callq	fclose
	xorl	%r12d, %r12d
	movl	$.L.str.5, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$20, %edi
	callq	malloc
	movq	%rax, %r14
	leaq	83(%rsp), %rax
.LBB2_14:                               # =>This Inner Loop Header: Depth=1
	movsbl	-3(%rax), %ecx
	movsbl	-2(%rax), %edx
	shll	$8, %edx
	addl	%ecx, %edx
	movsbl	-1(%rax), %ecx
	shll	$16, %ecx
	movzbl	(%rax), %esi
	shll	$24, %esi
	addl	%ecx, %esi
	addl	%edx, %esi
	movl	%esi, (%r14,%r12,4)
	incq	%r12
	addq	$20, %rax
	cmpq	$5, %r12
	jne	.LBB2_14
# %bb.15:
	movl	%r15d, %ebp
	shrl	$2, %ebp
	callq	clock
	movq	%rax, %r12
	leaq	48(%rsp), %rdx
	movq	%rbx, %rdi
	movq	%r14, %rsi
	movl	$5, %ecx
	movl	%ebp, 28(%rsp)                  # 4-byte Spill
	movl	%ebp, %r8d
	callq	_Z16matchPattern_CPUPjS_Piii
	callq	clock
	subq	%r12, %rax
	cvtsi2ss	%rax, %xmm0
	divss	.LCPI2_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	leaq	80(%rsp), %r12
	xorl	%r13d, %r13d
.LBB2_16:                               # =>This Inner Loop Header: Depth=1
	movl	48(%rsp,%r13,4), %edx
	movl	$.L.str.9, %edi
	movq	%r12, %rsi
	xorl	%eax, %eax
	callq	printf
	incq	%r13
	addq	$20, %r12
	cmpq	$5, %r13
	jne	.LBB2_16
# %bb.17:
	movl	$20, %edi
	callq	malloc
	movq	%rax, %r12
	leaq	32(%rsp), %r13
	movl	$20, %esi
	movq	%r13, %rdi
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movl	$20, %esi
	callq	hipMalloc
	movq	%rbx, %rdi
	callq	strlen
	leaq	40(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%rax, %rsi
	callq	hipMalloc
	movq	(%rbp), %rbp
	movq	%rbx, %rdi
	callq	strlen
	movq	%rbp, %rdi
	movq	%rbx, %rsi
	movq	%rax, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r13), %rdi
	movl	$20, %edx
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	shrl	$7, %r15d
	btsq	$32, %r15
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$32, %rdx
	movq	%r15, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_19
# %bb.18:
	movq	40(%rsp), %rdi
	movq	32(%rsp), %rsi
	movq	8(%rsp), %rdx
	movl	$5, %ecx
	movl	28(%rsp), %r8d                  # 4-byte Reload
	callq	_Z33__device_stub__matchPattern_gpu_1PjS_Piii
.LBB2_19:
	callq	hipPeekAtLastError
	testl	%eax, %eax
	jne	.LBB2_20
# %bb.22:                               # %_Z9gpuAssert10hipError_tPKcib.exit
	movq	8(%rsp), %rsi
	movl	$20, %edx
	movq	%r12, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB2_28
# %bb.23:                               # %_Z9gpuAssert10hipError_tPKcib.exit99.preheader.preheader
	leaq	80(%rsp), %r15
	xorl	%r13d, %r13d
.LBB2_24:                               # %_Z9gpuAssert10hipError_tPKcib.exit99.preheader
                                        # =>This Inner Loop Header: Depth=1
	movl	(%r12,%r13,4), %edx
	cmpl	%edx, 48(%rsp,%r13,4)
	je	.LBB2_26
# %bb.25:                               #   in Loop: Header=BB2_24 Depth=1
	movl	$.L.str.11, %edi
	movq	%r15, %rsi
	xorl	%eax, %eax
	callq	printf
.LBB2_26:                               # %_Z9gpuAssert10hipError_tPKcib.exit99
                                        #   in Loop: Header=BB2_24 Depth=1
	incq	%r13
	addq	$20, %r15
	cmpq	$5, %r13
	jne	.LBB2_24
# %bb.27:
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$184, %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_1:
	.cfi_def_cfa_offset 240
	movl	$.Lstr, %edi
	jmp	.LBB2_2
.LBB2_29:
	movl	$.Lstr.1, %edi
.LBB2_2:
	callq	puts@PLT
	xorl	%edi, %edi
	callq	exit
.LBB2_20:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.12, %esi
	movl	$.L.str.10, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$162, %r8d
	jmp	.LBB2_21
.LBB2_28:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.12, %esi
	movl	$.L.str.10, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$163, %r8d
.LBB2_21:
	xorl	%eax, %eax
	callq	fprintf
	movl	%ebp, %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:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB3_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB3_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z18matchPattern_gpu_1PjS_Piii, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end3:
	.size	__hip_module_ctor, .Lfunc_end3-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB4_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB4_2:
	retq
.Lfunc_end4:
	.size	__hip_module_dtor, .Lfunc_end4-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z18matchPattern_gpu_1PjS_Piii,@object # @_Z18matchPattern_gpu_1PjS_Piii
	.section	.rodata,"a",@progbits
	.globl	_Z18matchPattern_gpu_1PjS_Piii
	.p2align	3, 0x0
_Z18matchPattern_gpu_1PjS_Piii:
	.quad	_Z33__device_stub__matchPattern_gpu_1PjS_Piii
	.size	_Z18matchPattern_gpu_1PjS_Piii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"./data/keywords.txt"
	.size	.L.str, 20

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"r"
	.size	.L.str.1, 2

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"./data/small.txt"
	.size	.L.str.3, 17

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Length : %d\n"
	.size	.L.str.5, 13

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Time for matching keywords: %fs\n\n"
	.size	.L.str.6, 34

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"%s\t  |\t%d\n"
	.size	.L.str.9, 11

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/sedflix/cuda_pattern_matching/master/pattern_CPU.hip"
	.size	.L.str.10, 115

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"WRONG OUTPUT:\t %s\t|\t%d\n"
	.size	.L.str.11, 24

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"GPUassert: %s %s %d\n"
	.size	.L.str.12, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z18matchPattern_gpu_1PjS_Piii"
	.size	.L__unnamed_1, 31

	.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	"keywords.txt: File not found."
	.size	.Lstr, 30

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Unable to open the file."
	.size	.Lstr.1, 25

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"Printing Matches:"
	.size	.Lstr.2, 18

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"Word\t  |\tNumber of Matches\n==================================="
	.size	.Lstr.3, 63

	.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__matchPattern_gpu_1PjS_Piii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z18matchPattern_gpu_1PjS_Piii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z18matchPattern_gpu_1PjS_Piii ; -- Begin function _Z18matchPattern_gpu_1PjS_Piii
	.globl	_Z18matchPattern_gpu_1PjS_Piii
	.p2align	8
	.type	_Z18matchPattern_gpu_1PjS_Piii,@function
_Z18matchPattern_gpu_1PjS_Piii:         ; @_Z18matchPattern_gpu_1PjS_Piii
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x2c
	s_load_b64 s[2:3], s[0:1], 0x18
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s4, v[0:1]
	v_cmp_gt_i32_e32 vcc_lo, s3, v1
	s_and_saveexec_b32 s3, vcc_lo
	s_cbranch_execz .LBB0_10
; %bb.1:                                ; %.preheader
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[0:1], s[0:1], 0x10
	v_ashrrev_i32_e32 v2, 31, v1
	s_cmp_gt_i32 s2, 0
	s_mov_b32 s10, 0
	s_cselect_b32 s3, -1, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	v_mov_b32_e32 v2, 0
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s4, v0
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v1, vcc_lo
.LBB0_2:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_5 Depth 2
	global_load_b32 v3, v[0:1], off
	s_cmp_eq_u32 s10, 0
	s_cbranch_scc1 .LBB0_4
; %bb.3:                                ;   in Loop: Header=BB0_2 Depth=1
	global_load_b32 v4, v[0:1], off offset:4
	s_lshl_b32 s4, s10, 3
	s_waitcnt vmcnt(1)
	v_lshrrev_b32_e32 v3, s4, v3
	s_sub_i32 s4, 32, s4
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1)
	v_lshl_add_u32 v3, v4, s4, v3
.LBB0_4:                                ;   in Loop: Header=BB0_2 Depth=1
	s_mov_b64 s[4:5], s[0:1]
	s_and_not1_b32 vcc_lo, exec_lo, s3
	s_mov_b64 s[8:9], s[6:7]
	s_mov_b32 s11, s2
	s_cbranch_vccnz .LBB0_9
.LBB0_5:                                ; %.lr.ph
                                        ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	global_load_b32 v4, v2, s[8:9]
	s_mov_b32 s12, exec_lo
	s_waitcnt vmcnt(0)
	v_cmpx_eq_u32_e64 v3, v4
	s_cbranch_execz .LBB0_8
; %bb.6:                                ;   in Loop: Header=BB0_5 Depth=2
	s_mov_b32 s13, exec_lo
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mbcnt_lo_u32_b32 v4, s13, 0
	v_cmp_eq_u32_e32 vcc_lo, 0, v4
	s_and_b32 s14, exec_lo, vcc_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mov_b32 exec_lo, s14
	s_cbranch_execz .LBB0_8
; %bb.7:                                ;   in Loop: Header=BB0_5 Depth=2
	s_bcnt1_i32_b32 s13, s13
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mov_b32_e32 v4, s13
	global_atomic_add_u32 v2, v4, s[4:5]
.LBB0_8:                                ;   in Loop: Header=BB0_5 Depth=2
	s_or_b32 exec_lo, exec_lo, s12
	s_add_i32 s11, s11, -1
	s_add_u32 s8, s8, 4
	s_addc_u32 s9, s9, 0
	s_add_u32 s4, s4, 4
	s_addc_u32 s5, s5, 0
	s_cmp_eq_u32 s11, 0
	s_cbranch_scc0 .LBB0_5
.LBB0_9:                                ; %._crit_edge
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_add_i32 s10, s10, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s10, 4
	s_cbranch_scc0 .LBB0_2
.LBB0_10:                               ; %.loopexit
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z18matchPattern_gpu_1PjS_Piii
		.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 5
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z18matchPattern_gpu_1PjS_Piii, .Lfunc_end0-_Z18matchPattern_gpu_1PjS_Piii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 348
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         28
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .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:           _Z18matchPattern_gpu_1PjS_Piii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z18matchPattern_gpu_1PjS_Piii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
