/* Program : To find the histogram of N count words in a text file
 * Author : Anant Shah
 * Date : 17-9-2018
 * Roll Number : EE16B105
 **/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<ctype.h>

#define MAXWORDS 20000
#define MAX_WORD_LEN 20
#define ERROR_HANDLER(error_msg,line) error_handler(error_msg,line)
#define SHARED_BIN_SIZE 512
#define NUM_TASKS 8
#define NUM_THREADS 256

void error_handler(cudaError_t error_msg,int line){
	/* Function to detect an error in a cuda command */
	if( error_msg != cudaSuccess){
		printf("%s in %s at %d",cudaGetErrorString(error_msg),__FILE__,line);
		exit(EXIT_FAILURE);
	}
}

__host__ int str_len(char *str){
	/* Function to find the length of a string */
	int len=0;
	while( *(str+len) != '\0'){
		len++;
	}
	return len;
}

__host__ __device__ void initialize_bins(int *bin,unsigned size){
	/* Function to initialize the array of bins */
	for(int i=0;i<size;i++){
		bin[i] = 0;
	}
}

void checkWord(char *curWord,int *count,int *words_len){
        /* Function to modify the words so as to remove the punctuations */
        int     length = str_len(curWord); /* Length of the string */
        int     i=0;
        int     let_found=0; /* Letter found (used to test if apostrophe occurs in the middle of the word) */
        char    letter;
        int     size=0;
        char    *new_curWord;
        new_curWord = (char *)malloc(MAX_WORD_LEN*sizeof(char));

        while(i<length){
                letter = curWord[i];
                if(letter==45){
                        words_len[(*count)]=size;
                        (*count)++;
                        int k=i+1;
                        while(k<length){
                                new_curWord[k-i-1] = curWord[k];
                                k++;
                        }
                        checkWord(new_curWord,count,words_len);
                        return;
                }
                else if(isalnum(letter)==0){
                        if(let_found!=0){
                                /* When a punctuation exists but is not the last element */
                                char    let_inc;
                                let_inc = curWord[i+1];
                                if(isalnum(let_inc)!=0) size++;
                        }
                        i++;
                }
                else{
                        i++;
                        size++;
                        let_found=1;
                }
        }
        words_len[(*count)] = size;
}

__host__ int store_words(char *filename,int *words_len){
	/* Function to read all the words from a file and store them */
	FILE	*ipf = fopen(filename,"r");
	int	*totalWordCount;
	char	curWord[40]; /* Reading in the current word from the file */

	totalWordCount = (int *)malloc(sizeof(int));
	(*totalWordCount)=0;

	while(fscanf(ipf,"%s",curWord)!=EOF && *totalWordCount<MAXWORDS){
		checkWord(curWord,totalWordCount,words_len);	
		(*totalWordCount)++;
	}
	return *totalWordCount;
}

__host__ __device__ int power(int base,int exp){
	int power = 1;
	for(int i=0;i<exp;i++){
		power=power*base;
	}
	return power;
}

__global__ void n_count(int *words_len,int *bins,int N,int num_words){
	/* Kernel function to count the n-count grams
	 * Parameters : words_len - Array consisting of the lengths of the words
	 *		bins - Array consisting of the bins of the histogram
	 *		N - value of N in n-count 
	 *		start_bin - Start bin value to be subtracted to obtain the bin value
	 */
	
	extern __shared__ int	s_words_len[]; /* Storing the world lengths in the shared memory */
	__shared__ int	s_hist[SHARED_BIN_SIZE]; /* Pointer in the shared memory to the privatized histogram */
	__shared__ int	s_hist_ind; /* Pointer in the shared memory which maps to the bin indices in the global memory(starting index) */
	__shared__ int	ps_hist_ind; /* keep a track of the previous pointer which maps the global memory bins to the shared memory  */
	__shared__ int	s_hist_hit; /* Number of times the shared memory histogram is hit */
	int  	*global_ind = (int *)&s_words_len[NUM_TASKS*NUM_THREADS+N-1];
	int	bdim = blockDim.x; /* Number of threads in the block in the x-direction */
	int 	bx = blockIdx.x; /* Block ID  */
	int 	tid = threadIdx.x; /* Thread ID in a block */
	int	bin; /* Bin of the set of words read by a thread */

	/* Initialize the private histogram with 0's */
	for(int i=0;i<(SHARED_BIN_SIZE+bdim-1)/bdim;i++){
		if( (i*bdim+tid) < (SHARED_BIN_SIZE)){
			s_hist[i*bdim+tid] = 0;
			s_hist_ind = 0;
			s_hist_hit = 0;
		}
	}
	if(tid < power(20,N)/SHARED_BIN_SIZE)		global_ind[tid] = 0;
	__syncthreads(); 

	/* Load the words from the global memory into the shared memory */
	for(int i=0;i<=NUM_TASKS;i++){
		/* Only 1 warp will have control divergence */
		if( (bx*NUM_TASKS*bdim+i*bdim+tid) < num_words){
			if( (i*bdim+tid) < (NUM_TASKS*bdim+N-1))		s_words_len[i*bdim+tid] = words_len[bx*NUM_TASKS*bdim+i*bdim+tid];
		}
	}
	__syncthreads();

	/* Read the lengths of the corresponding N words */
	/* Keep a condition so that it dosent read invalid words */
	for(int i=0;i<NUM_TASKS;i++){
		if( (bx*NUM_TASKS*bdim+i*bdim+tid) <= (num_words-N)){
			bin = 0;
			for(int j=0;j<N;j++){
				bin+=(s_words_len[i*bdim+j+tid]-1)*power(20,N-1-j); 
			}
			/* Check which bin they lie in and add accordingly */
			if( (bin>=s_hist_ind) && (bin<(s_hist_ind+SHARED_BIN_SIZE)) ){
				atomicAdd(&(s_hist[bin-s_hist_ind]),1);
				atomicAdd(&s_hist_hit,1);
			}
			else {								
				atomicAdd(&(bins[bin]),1);
				atomicAdd(&global_ind[bin/SHARED_BIN_SIZE],1);
			}
		}
		__syncthreads();
		if(i==NUM_TASKS/4){
			if(tid==0){
				/* Only 1 thread is required to perform this computation */
				ps_hist_ind = s_hist_ind;
				for(int j=0;j<power(20,N)/SHARED_BIN_SIZE;j++){
					if(global_ind[j]>s_hist_hit)	s_hist_ind = SHARED_BIN_SIZE*j;
				}
				s_hist_hit = 0;
			}
			__syncthreads();

		/* Now I synchronously need to load the existing histogram into the global memory and move the other histogram into the shared memory */
			if(ps_hist_ind != s_hist_ind){
				for(int j=0;j<(SHARED_BIN_SIZE+bdim-1)/bdim;j++){
					if( (j*bdim+tid) < (SHARED_BIN_SIZE)){
						if(ps_hist_ind+j*bdim+tid<power(20,N))		atomicAdd(&(bins[ps_hist_ind+j*bdim+tid]),s_hist[j*bdim+tid]);
						s_hist[j*bdim+tid] = 0;
					}
				}
				__syncthreads();
			}
		}
	}
	__syncthreads();

	/* Copy the privatized histogram back to the global memory */
	for(int i=0;i<(SHARED_BIN_SIZE+bdim-1)/bdim;i++){
		if( (i*bdim+tid) < (SHARED_BIN_SIZE)){
			if(s_hist_ind+i*bdim+tid<power(20,N))		atomicAdd(&(bins[s_hist_ind+i*bdim+tid]),s_hist[i*bdim+tid]);
		}
	}
}

void print_hist_to_file(int *hist,int N){
	FILE *fp;
	const char *file = "ee16b105_out.txt";
	fp = fopen(file,"w");

	for(int i=0;i<power(MAX_WORD_LEN,N);i++){
                int val = i;
		if(hist[i]!=0){
                	for(int j=0;j<N;j++){
                        	fprintf(fp,"%d ",(val/power(20,N-1-j))+1);
                        	val = val%power(20,N-1-j);
                	}
                	fprintf(fp,"%d\n",hist[i]);
		}
        }
	fclose(fp);
}

int main(int argc,char **argv){
	if(argc!=3){
		printf("error : Invalid number of arguments\n");
		exit(EXIT_FAILURE);
	}

	int 		N = atoi(argv[1]); /* N-grams to be found */
	char 		*filename = argv[2]; /* File which contains the text */

	int		*h_words_len; /* Store all the lengths of the words */
	int		*d_words_len; /* Store all the lengths of the words on the device */
	int		*h_bins; /* An array to store the bins. Each index will map to one bin. This will reside on the host */	
	int		*h_hist; /* Output histogram on the host */
	int		*d_bins; /* An array to store the bins on the device */
	size_t		size_bins; /* Size of the bin array */
	size_t		size_words_len; /* Size required to store all the words length */
	size_t		size_sharedmem; /* Size of the shared memory required by a block */
	int		total_count; /* Total number of words in the text file */

	/**************************** Allocating memory to the bins on the host ****************************/
	size_bins = sizeof(int)*power(MAX_WORD_LEN,N); /* There will be MAX_WORD_LEN^N combinations of N-count grams */
	size_words_len = sizeof(int)*MAXWORDS;

	h_bins = (int *)malloc(size_bins); /* Allocate memory to the bins on the device */
	h_words_len = (int *)malloc(size_words_len);
	h_hist = (int *)malloc(size_bins);	

	/********************************* Initialize the bins ********************************************/

	initialize_bins(h_bins,pow(MAX_WORD_LEN,N));
	total_count = store_words(filename,h_words_len);

	/********************************  Allocate memory on the device ***********************************/

	ERROR_HANDLER(cudaMalloc((void **)&d_words_len,size_words_len),__LINE__);
	ERROR_HANDLER(cudaMalloc((void **)&d_bins,size_bins),__LINE__);
	
	/******************************** Copy Data from the host to the device ****************************/

	ERROR_HANDLER(cudaMemcpy(d_bins,h_bins,size_bins,cudaMemcpyHostToDevice),__LINE__);
	ERROR_HANDLER(cudaMemcpy(d_words_len,h_words_len,size_words_len,cudaMemcpyHostToDevice),__LINE__);

	/******************************* Invoke the kernel to find the histogram ***************************/

	int	blocks = (total_count+NUM_THREADS*NUM_TASKS-1)/(NUM_THREADS*NUM_TASKS);
	size_sharedmem = sizeof(int)*(NUM_TASKS*NUM_THREADS+N-1)+sizeof(int)*(power(20,N)/SHARED_BIN_SIZE);	

	n_count<<<blocks,NUM_THREADS,size_sharedmem>>>(d_words_len,d_bins,N,total_count);

	ERROR_HANDLER(cudaMemcpy(h_hist,d_bins,size_bins,cudaMemcpyDeviceToHost),__LINE__);

	print_hist_to_file(h_hist,N);
	/******************************** Free the allocated memory ***************************************/

	cudaFree(d_bins);
	cudaFree(d_words_len);
	free(h_hist);
	free(h_bins);
	free(h_words_len);
}

	.file	"tmpxft_00388a9c_00000000-6_ee16b105.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2037:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2037:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/anantshah200/CS6023/master/Assignment3/ee16b105.cu"
.LC1:
	.string	"%s in %s at %d"
	.text
	.globl	_Z13error_handler9cudaErrori
	.type	_Z13error_handler9cudaErrori, @function
_Z13error_handler9cudaErrori:
.LFB2027:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L2
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	%esi, 12(%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r8d
	movl	$2, %edi
	leaq	.LC0(%rip), %rcx
	movq	%rax, %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z13error_handler9cudaErrori, .-_Z13error_handler9cudaErrori
	.globl	_Z7str_lenPc
	.type	_Z7str_lenPc, @function
_Z7str_lenPc:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	strlen@PLT
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z7str_lenPc, .-_Z7str_lenPc
	.globl	_Z15initialize_binsPij
	.type	_Z15initialize_binsPij, @function
_Z15initialize_binsPij:
.LFB2029:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L11:
	cmpl	%esi, %eax
	jnb	.L13
	xorl	%edx, %edx
	movl	%edx, (%rdi,%rax,4)
	incq	%rax
	jmp	.L11
.L13:
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z15initialize_binsPij, .-_Z15initialize_binsPij
	.globl	_Z9checkWordPcPiS0_
	.type	_Z9checkWordPcPiS0_, @function
_Z9checkWordPcPiS0_:
.LFB2030:
	.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
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	%rdi, (%rsp)
.L19:
	movq	(%rsp), %rdi
	xorl	%ebx, %ebx
	xorl	%r13d, %r13d
	xorl	%r15d, %r15d
	call	_Z7str_lenPc
	movl	$20, %edi
	movq	(%rsp), %r14
	movl	%eax, 12(%rsp)
	call	malloc@PLT
	movq	%rax, (%rsp)
.L15:
	cmpl	%ebx, 12(%rsp)
	jle	.L30
	movsbl	(%r14,%rbx), %edi
	cmpb	$45, %dil
	jne	.L16
	movslq	0(%rbp), %rax
	movq	(%rsp), %rcx
	movslq	%ebx, %rdx
	movl	%r13d, (%r12,%rax,4)
	leal	1(%rbx), %eax
	subq	%rdx, %rcx
	incl	0(%rbp)
	cltq
.L17:
	cmpl	%eax, 12(%rsp)
	jle	.L19
	movb	(%r14,%rax), %dl
	movb	%dl, -1(%rcx,%rax)
	incq	%rax
	jmp	.L17
.L16:
	call	isalnum@PLT
	testl	%eax, %eax
	jne	.L20
	testl	%r15d, %r15d
	je	.L22
	movsbl	1(%r14,%rbx), %edi
	call	isalnum@PLT
	cmpl	$1, %eax
	sbbl	$-1, %r13d
	jmp	.L22
.L20:
	incl	%r13d
	movl	$1, %r15d
.L22:
	incq	%rbx
	jmp	.L15
.L30:
	movslq	0(%rbp), %rax
	movl	%r13d, (%r12,%rax,4)
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z9checkWordPcPiS0_, .-_Z9checkWordPcPiS0_
	.section	.rodata.str1.1
.LC2:
	.string	"r"
.LC3:
	.string	"%s"
	.text
	.globl	_Z11store_wordsPcPi
	.type	_Z11store_wordsPcPi, @function
_Z11store_wordsPcPi:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC3(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rsi, %r12
	leaq	.LC2(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movl	$4, %edi
	movq	%rax, %r13
	call	malloc@PLT
	movq	%rax, %rbp
.L33:
	movl	%ebx, 0(%rbp)
	movq	%rsp, %r14
	xorl	%eax, %eax
	movq	%r15, %rsi
	movq	%r14, %rdx
	movq	%r13, %rdi
	call	__isoc23_fscanf@PLT
	incl	%eax
	je	.L31
	cmpl	$19999, %ebx
	jg	.L31
	movq	%r12, %rdx
	movq	%rbp, %rsi
	movq	%r14, %rdi
	call	_Z9checkWordPcPiS0_
	movl	0(%rbp), %eax
	leal	1(%rax), %ebx
	jmp	.L33
.L31:
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L34
	call	__stack_chk_fail@PLT
.L34:
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	movl	%ebx, %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
.LFE2031:
	.size	_Z11store_wordsPcPi, .-_Z11store_wordsPcPi
	.globl	_Z5powerii
	.type	_Z5powerii, @function
_Z5powerii:
.LFB2032:
	.cfi_startproc
	endbr64
	xorl	%edx, %edx
	movl	$1, %eax
.L40:
	cmpl	%esi, %edx
	jge	.L42
	imull	%edi, %eax
	incl	%edx
	jmp	.L40
.L42:
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z5powerii, .-_Z5powerii
	.section	.rodata.str1.1
.LC4:
	.string	"w"
.LC5:
	.string	"ee16b105_out.txt"
.LC6:
	.string	"%d "
.LC7:
	.string	"%d\n"
	.text
	.globl	_Z18print_hist_to_filePii
	.type	_Z18print_hist_to_filePii, @function
_Z18print_hist_to_filePii:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC6(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rdi, %r14
	leaq	.LC5(%rip), %rdi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%esi, %r12d
	leaq	.LC4(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	call	fopen@PLT
	movq	%rax, 8(%rsp)
.L44:
	xorl	%edx, %edx
	movl	$1, %eax
.L51:
	cmpl	%edx, %r12d
	jle	.L56
	imull	$20, %eax, %eax
	incl	%edx
	jmp	.L51
.L56:
	cmpl	%ebx, %eax
	jle	.L57
	cmpl	$0, (%r14,%rbx,4)
	je	.L45
	movl	%ebx, 4(%rsp)
	movl	$1, %ebp
.L46:
	leal	-1(%rbp), %eax
	cmpl	%eax, %r12d
	jle	.L58
	movl	%r12d, %r13d
	xorl	%eax, %eax
	movl	$1, %ecx
	subl	%ebp, %r13d
.L50:
	cmpl	%r13d, %eax
	jge	.L59
	imull	$20, %ecx, %ecx
	incl	%eax
	jmp	.L50
.L59:
	movl	4(%rsp), %eax
	movq	8(%rsp), %rdi
	movl	$2, %esi
	cltd
	idivl	%ecx
	movq	%r15, %rdx
	leal	1(%rax), %ecx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	xorl	%eax, %eax
	movl	$1, %esi
.L48:
	cmpl	%r13d, %eax
	jge	.L60
	imull	$20, %esi, %esi
	incl	%eax
	jmp	.L48
.L60:
	movl	4(%rsp), %eax
	incl	%ebp
	cltd
	idivl	%esi
	movl	%edx, 4(%rsp)
	jmp	.L46
.L58:
	movl	(%r14,%rbx,4), %ecx
	movq	8(%rsp), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %rdx
	call	__fprintf_chk@PLT
.L45:
	incq	%rbx
	jmp	.L44
.L57:
	movq	8(%rsp), %rdi
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
	.cfi_endproc
.LFE2033:
	.size	_Z18print_hist_to_filePii, .-_Z18print_hist_to_filePii
	.globl	_Z30__device_stub__Z7n_countPiS_iiPiS_ii
	.type	_Z30__device_stub__Z7n_countPiS_iiPiS_ii, @function
_Z30__device_stub__Z7n_countPiS_iiPiS_ii:
.LFB2059:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L61
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z7n_countPiS_ii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L61:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L63
	call	__stack_chk_fail@PLT
.L63:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2059:
	.size	_Z30__device_stub__Z7n_countPiS_iiPiS_ii, .-_Z30__device_stub__Z7n_countPiS_iiPiS_ii
	.globl	_Z7n_countPiS_ii
	.type	_Z7n_countPiS_ii, @function
_Z7n_countPiS_ii:
.LFB2060:
	.cfi_startproc
	endbr64
	jmp	_Z30__device_stub__Z7n_countPiS_iiPiS_ii
	.cfi_endproc
.LFE2060:
	.size	_Z7n_countPiS_ii, .-_Z7n_countPiS_ii
	.section	.rodata.str1.1
.LC8:
	.string	"error : Invalid number of arguments\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2034:
	.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
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	cmpl	$3, %edi
	je	.L67
	movl	$2, %edi
	leaq	.LC8(%rip), %rsi
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L67:
	movq	8(%rsi), %rdi
	movq	%rsi, %rbx
	call	atoi@PLT
	movq	16(%rbx), %r15
	movl	$1, %ebx
	movl	%eax, %ebp
	xorl	%eax, %eax
.L68:
	cmpl	%eax, %ebp
	jle	.L75
	imull	$20, %ebx, %ebx
	incl	%eax
	jmp	.L68
.L75:
	movslq	%ebx, %rbx
	salq	$2, %rbx
	movq	%rbx, %rdi
	call	malloc@PLT
	movl	$80000, %edi
	movq	%rax, %r13
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r12
	call	malloc@PLT
	cvtsi2sdl	%ebp, %xmm1
	movsd	.LC9(%rip), %xmm0
	movq	%rax, %r14
	call	pow@PLT
	movq	%r13, %rdi
	cvttsd2siq	%xmm0, %rsi
	call	_Z15initialize_binsPij
	movq	%r15, %rdi
	movq	%r12, %rsi
	call	_Z11store_wordsPcPi
	movq	%rsp, %rdi
	movl	$80000, %esi
	movl	%eax, %r15d
	call	cudaMalloc@PLT
	movl	$252, %esi
	movl	%eax, %edi
	call	_Z13error_handler9cudaErrori
	leaq	8(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movl	$253, %esi
	movl	%eax, %edi
	call	_Z13error_handler9cudaErrori
	movq	8(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$257, %esi
	movl	%eax, %edi
	call	_Z13error_handler9cudaErrori
	movq	(%rsp), %rdi
	movl	$1, %ecx
	movq	%r12, %rsi
	movl	$80000, %edx
	call	cudaMemcpy@PLT
	movl	$258, %esi
	movl	%eax, %edi
	call	_Z13error_handler9cudaErrori
	leal	2047(%r15), %eax
	movl	$2048, %ecx
	xorl	%esi, %esi
	cltd
	idivl	%ecx
	leal	2047(%rbp), %ecx
	movl	$1, %edx
	movslq	%ecx, %rcx
.L70:
	cmpl	%esi, %ebp
	jle	.L76
	imull	$20, %edx, %edx
	incl	%esi
	jmp	.L70
.L76:
	movl	%eax, 16(%rsp)
	movl	$512, %esi
	xorl	%r9d, %r9d
	movabsq	$4294967297, %rax
	movq	%rax, 20(%rsp)
	movl	%edx, %eax
	movq	16(%rsp), %rdi
	cltd
	idivl	%esi
	movl	24(%rsp), %esi
	movl	$16777217, %edx
	salq	$8, %rdx
	cltq
	addq	%rcx, %rax
	movl	$1, %ecx
	leaq	0(,%rax,4), %r8
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L72
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdi
	movl	%r15d, %ecx
	movl	%ebp, %edx
	call	_Z30__device_stub__Z7n_countPiS_iiPiS_ii
.L72:
	movq	8(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdx
	movq	%r14, %rdi
	call	cudaMemcpy@PLT
	movl	$267, %esi
	movl	%eax, %edi
	call	_Z13error_handler9cudaErrori
	movl	%ebp, %esi
	movq	%r14, %rdi
	call	_Z18print_hist_to_filePii
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	%r14, %rdi
	call	free@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L73
	call	__stack_chk_fail@PLT
.L73:
	addq	$56, %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
.LFE2034:
	.size	main, .-main
	.section	.rodata.str1.1
.LC10:
	.string	"_Z7n_countPiS_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2062:
	.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	.LC10(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z7n_countPiS_ii(%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
.LFE2062:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC9:
	.long	0
	.long	1077149696
	.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:

/* Program : To find the histogram of N count words in a text file
 * Author : Anant Shah
 * Date : 17-9-2018
 * Roll Number : EE16B105
 **/


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

#define MAXWORDS 20000
#define MAX_WORD_LEN 20
#define ERROR_HANDLER(error_msg,line) error_handler(error_msg,line)
#define SHARED_BIN_SIZE 512
#define NUM_TASKS 8
#define NUM_THREADS 256

void error_handler(hipError_t error_msg,int line){
	/* Function to detect an error in a cuda command */
	if( error_msg != hipSuccess){
		printf("%s in %s at %d",hipGetErrorString(error_msg),__FILE__,line);
		exit(EXIT_FAILURE);
	}
}

__host__ int str_len(char *str){
	/* Function to find the length of a string */
	int len=0;
	while( *(str+len) != '\0'){
		len++;
	}
	return len;
}

__host__ __device__ void initialize_bins(int *bin,unsigned size){
	/* Function to initialize the array of bins */
	for(int i=0;i<size;i++){
		bin[i] = 0;
	}
}

void checkWord(char *curWord,int *count,int *words_len){
        /* Function to modify the words so as to remove the punctuations */
        int     length = str_len(curWord); /* Length of the string */
        int     i=0;
        int     let_found=0; /* Letter found (used to test if apostrophe occurs in the middle of the word) */
        char    letter;
        int     size=0;
        char    *new_curWord;
        new_curWord = (char *)malloc(MAX_WORD_LEN*sizeof(char));

        while(i<length){
                letter = curWord[i];
                if(letter==45){
                        words_len[(*count)]=size;
                        (*count)++;
                        int k=i+1;
                        while(k<length){
                                new_curWord[k-i-1] = curWord[k];
                                k++;
                        }
                        checkWord(new_curWord,count,words_len);
                        return;
                }
                else if(isalnum(letter)==0){
                        if(let_found!=0){
                                /* When a punctuation exists but is not the last element */
                                char    let_inc;
                                let_inc = curWord[i+1];
                                if(isalnum(let_inc)!=0) size++;
                        }
                        i++;
                }
                else{
                        i++;
                        size++;
                        let_found=1;
                }
        }
        words_len[(*count)] = size;
}

__host__ int store_words(char *filename,int *words_len){
	/* Function to read all the words from a file and store them */
	FILE	*ipf = fopen(filename,"r");
	int	*totalWordCount;
	char	curWord[40]; /* Reading in the current word from the file */

	totalWordCount = (int *)malloc(sizeof(int));
	(*totalWordCount)=0;

	while(fscanf(ipf,"%s",curWord)!=EOF && *totalWordCount<MAXWORDS){
		checkWord(curWord,totalWordCount,words_len);	
		(*totalWordCount)++;
	}
	return *totalWordCount;
}

__host__ __device__ int power(int base,int exp){
	int power = 1;
	for(int i=0;i<exp;i++){
		power=power*base;
	}
	return power;
}

__global__ void n_count(int *words_len,int *bins,int N,int num_words){
	/* Kernel function to count the n-count grams
	 * Parameters : words_len - Array consisting of the lengths of the words
	 *		bins - Array consisting of the bins of the histogram
	 *		N - value of N in n-count 
	 *		start_bin - Start bin value to be subtracted to obtain the bin value
	 */
	
	extern __shared__ int	s_words_len[]; /* Storing the world lengths in the shared memory */
	__shared__ int	s_hist[SHARED_BIN_SIZE]; /* Pointer in the shared memory to the privatized histogram */
	__shared__ int	s_hist_ind; /* Pointer in the shared memory which maps to the bin indices in the global memory(starting index) */
	__shared__ int	ps_hist_ind; /* keep a track of the previous pointer which maps the global memory bins to the shared memory  */
	__shared__ int	s_hist_hit; /* Number of times the shared memory histogram is hit */
	int  	*global_ind = (int *)&s_words_len[NUM_TASKS*NUM_THREADS+N-1];
	int	bdim = blockDim.x; /* Number of threads in the block in the x-direction */
	int 	bx = blockIdx.x; /* Block ID  */
	int 	tid = threadIdx.x; /* Thread ID in a block */
	int	bin; /* Bin of the set of words read by a thread */

	/* Initialize the private histogram with 0's */
	for(int i=0;i<(SHARED_BIN_SIZE+bdim-1)/bdim;i++){
		if( (i*bdim+tid) < (SHARED_BIN_SIZE)){
			s_hist[i*bdim+tid] = 0;
			s_hist_ind = 0;
			s_hist_hit = 0;
		}
	}
	if(tid < power(20,N)/SHARED_BIN_SIZE)		global_ind[tid] = 0;
	__syncthreads(); 

	/* Load the words from the global memory into the shared memory */
	for(int i=0;i<=NUM_TASKS;i++){
		/* Only 1 warp will have control divergence */
		if( (bx*NUM_TASKS*bdim+i*bdim+tid) < num_words){
			if( (i*bdim+tid) < (NUM_TASKS*bdim+N-1))		s_words_len[i*bdim+tid] = words_len[bx*NUM_TASKS*bdim+i*bdim+tid];
		}
	}
	__syncthreads();

	/* Read the lengths of the corresponding N words */
	/* Keep a condition so that it dosent read invalid words */
	for(int i=0;i<NUM_TASKS;i++){
		if( (bx*NUM_TASKS*bdim+i*bdim+tid) <= (num_words-N)){
			bin = 0;
			for(int j=0;j<N;j++){
				bin+=(s_words_len[i*bdim+j+tid]-1)*power(20,N-1-j); 
			}
			/* Check which bin they lie in and add accordingly */
			if( (bin>=s_hist_ind) && (bin<(s_hist_ind+SHARED_BIN_SIZE)) ){
				atomicAdd(&(s_hist[bin-s_hist_ind]),1);
				atomicAdd(&s_hist_hit,1);
			}
			else {								
				atomicAdd(&(bins[bin]),1);
				atomicAdd(&global_ind[bin/SHARED_BIN_SIZE],1);
			}
		}
		__syncthreads();
		if(i==NUM_TASKS/4){
			if(tid==0){
				/* Only 1 thread is required to perform this computation */
				ps_hist_ind = s_hist_ind;
				for(int j=0;j<power(20,N)/SHARED_BIN_SIZE;j++){
					if(global_ind[j]>s_hist_hit)	s_hist_ind = SHARED_BIN_SIZE*j;
				}
				s_hist_hit = 0;
			}
			__syncthreads();

		/* Now I synchronously need to load the existing histogram into the global memory and move the other histogram into the shared memory */
			if(ps_hist_ind != s_hist_ind){
				for(int j=0;j<(SHARED_BIN_SIZE+bdim-1)/bdim;j++){
					if( (j*bdim+tid) < (SHARED_BIN_SIZE)){
						if(ps_hist_ind+j*bdim+tid<power(20,N))		atomicAdd(&(bins[ps_hist_ind+j*bdim+tid]),s_hist[j*bdim+tid]);
						s_hist[j*bdim+tid] = 0;
					}
				}
				__syncthreads();
			}
		}
	}
	__syncthreads();

	/* Copy the privatized histogram back to the global memory */
	for(int i=0;i<(SHARED_BIN_SIZE+bdim-1)/bdim;i++){
		if( (i*bdim+tid) < (SHARED_BIN_SIZE)){
			if(s_hist_ind+i*bdim+tid<power(20,N))		atomicAdd(&(bins[s_hist_ind+i*bdim+tid]),s_hist[i*bdim+tid]);
		}
	}
}

void print_hist_to_file(int *hist,int N){
	FILE *fp;
	const char *file = "ee16b105_out.txt";
	fp = fopen(file,"w");

	for(int i=0;i<power(MAX_WORD_LEN,N);i++){
                int val = i;
		if(hist[i]!=0){
                	for(int j=0;j<N;j++){
                        	fprintf(fp,"%d ",(val/power(20,N-1-j))+1);
                        	val = val%power(20,N-1-j);
                	}
                	fprintf(fp,"%d\n",hist[i]);
		}
        }
	fclose(fp);
}

int main(int argc,char **argv){
	if(argc!=3){
		printf("error : Invalid number of arguments\n");
		exit(EXIT_FAILURE);
	}

	int 		N = atoi(argv[1]); /* N-grams to be found */
	char 		*filename = argv[2]; /* File which contains the text */

	int		*h_words_len; /* Store all the lengths of the words */
	int		*d_words_len; /* Store all the lengths of the words on the device */
	int		*h_bins; /* An array to store the bins. Each index will map to one bin. This will reside on the host */	
	int		*h_hist; /* Output histogram on the host */
	int		*d_bins; /* An array to store the bins on the device */
	size_t		size_bins; /* Size of the bin array */
	size_t		size_words_len; /* Size required to store all the words length */
	size_t		size_sharedmem; /* Size of the shared memory required by a block */
	int		total_count; /* Total number of words in the text file */

	/**************************** Allocating memory to the bins on the host ****************************/
	size_bins = sizeof(int)*power(MAX_WORD_LEN,N); /* There will be MAX_WORD_LEN^N combinations of N-count grams */
	size_words_len = sizeof(int)*MAXWORDS;

	h_bins = (int *)malloc(size_bins); /* Allocate memory to the bins on the device */
	h_words_len = (int *)malloc(size_words_len);
	h_hist = (int *)malloc(size_bins);	

	/********************************* Initialize the bins ********************************************/

	initialize_bins(h_bins,pow(MAX_WORD_LEN,N));
	total_count = store_words(filename,h_words_len);

	/********************************  Allocate memory on the device ***********************************/

	ERROR_HANDLER(hipMalloc((void **)&d_words_len,size_words_len),__LINE__);
	ERROR_HANDLER(hipMalloc((void **)&d_bins,size_bins),__LINE__);
	
	/******************************** Copy Data from the host to the device ****************************/

	ERROR_HANDLER(hipMemcpy(d_bins,h_bins,size_bins,hipMemcpyHostToDevice),__LINE__);
	ERROR_HANDLER(hipMemcpy(d_words_len,h_words_len,size_words_len,hipMemcpyHostToDevice),__LINE__);

	/******************************* Invoke the kernel to find the histogram ***************************/

	int	blocks = (total_count+NUM_THREADS*NUM_TASKS-1)/(NUM_THREADS*NUM_TASKS);
	size_sharedmem = sizeof(int)*(NUM_TASKS*NUM_THREADS+N-1)+sizeof(int)*(power(20,N)/SHARED_BIN_SIZE);	

	n_count<<<blocks,NUM_THREADS,size_sharedmem>>>(d_words_len,d_bins,N,total_count);

	ERROR_HANDLER(hipMemcpy(h_hist,d_bins,size_bins,hipMemcpyDeviceToHost),__LINE__);

	print_hist_to_file(h_hist,N);
	/******************************** Free the allocated memory ***************************************/

	hipFree(d_bins);
	hipFree(d_words_len);
	free(h_hist);
	free(h_bins);
	free(h_words_len);
}

	.text
	.file	"ee16b105.hip"
	.globl	_Z13error_handler10hipError_ti  # -- Begin function _Z13error_handler10hipError_ti
	.type	_Z13error_handler10hipError_ti,@function
_Z13error_handler10hipError_ti:         # @_Z13error_handler10hipError_ti
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jne	.LBB0_2
# %bb.1:
	retq
.LBB0_2:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%esi, %ebx
	callq	hipGetErrorString
	movl	$.L.str, %edi
	movl	$.L.str.1, %edx
	movq	%rax, %rsi
	movl	%ebx, %ecx
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z13error_handler10hipError_ti, .Lfunc_end0-_Z13error_handler10hipError_ti
	.cfi_endproc
                                        # -- End function
	.globl	_Z7str_lenPc                    # -- Begin function _Z7str_lenPc
	.type	_Z7str_lenPc,@function
_Z7str_lenPc:                           # @_Z7str_lenPc
	.cfi_startproc
# %bb.0:
	movl	$-1, %eax
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	incl	%eax
	cmpb	$0, (%rdi)
	leaq	1(%rdi), %rdi
	jne	.LBB1_1
# %bb.2:
	retq
.Lfunc_end1:
	.size	_Z7str_lenPc, .Lfunc_end1-_Z7str_lenPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z15initialize_binsPij          # -- Begin function _Z15initialize_binsPij
	.type	_Z15initialize_binsPij,@function
_Z15initialize_binsPij:                 # @_Z15initialize_binsPij
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	je	.LBB2_1
# %bb.2:                                # %.lr.ph.preheader
	movl	%esi, %edx
	shlq	$2, %rdx
	xorl	%esi, %esi
	jmp	memset@PLT                      # TAILCALL
.LBB2_1:                                # %._crit_edge
	retq
.Lfunc_end2:
	.size	_Z15initialize_binsPij, .Lfunc_end2-_Z15initialize_binsPij
	.cfi_endproc
                                        # -- End function
	.globl	_Z9checkWordPcPiS0_             # -- Begin function _Z9checkWordPcPiS0_
	.type	_Z9checkWordPcPiS0_,@function
_Z9checkWordPcPiS0_:                    # @_Z9checkWordPcPiS0_
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.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	%rdx, 16(%rsp)                  # 8-byte Spill
	movq	%rsi, 8(%rsp)                   # 8-byte Spill
.LBB3_1:                                # %tailrecurse
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_2 Depth 2
                                        #     Child Loop BB3_6 Depth 2
                                        #     Child Loop BB3_9 Depth 2
	movq	%rdi, %r12
	movq	$-1, %r13
	xorl	%eax, %eax
.LBB3_2:                                #   Parent Loop BB3_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%eax, %ebx
	cmpb	$0, 1(%r12,%r13)
	leaq	1(%r13), %r13
	leal	1(%rbx), %eax
	jne	.LBB3_2
# %bb.3:                                # %_Z7str_lenPc.exit
                                        #   in Loop: Header=BB3_1 Depth=1
	movl	$20, %edi
	callq	malloc
	testl	%r13d, %r13d
	jle	.LBB3_4
# %bb.5:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB3_1 Depth=1
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	movl	%ebx, %eax
	negq	%rax
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movl	$1, %ebp
	xorl	%ebx, %ebx
	xorl	%r14d, %r14d
	xorl	%r15d, %r15d
.LBB3_6:                                # %.lr.ph
                                        #   Parent Loop BB3_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movsbl	-1(%r12,%rbp), %edi
	cmpl	$45, %edi
	je	.LBB3_7
# %bb.10:                               #   in Loop: Header=BB3_6 Depth=2
	callq	isalnum
	testl	%eax, %eax
	je	.LBB3_11
# %bb.14:                               #   in Loop: Header=BB3_6 Depth=2
	incl	%r15d
	movl	$1, %r14d
	jmp	.LBB3_15
.LBB3_11:                               #   in Loop: Header=BB3_6 Depth=2
	testl	%r14d, %r14d
	je	.LBB3_12
# %bb.13:                               #   in Loop: Header=BB3_6 Depth=2
	movsbl	(%r12,%rbp), %edi
	callq	isalnum
	cmpl	$1, %eax
	sbbl	$-1, %r15d
	jmp	.LBB3_15
.LBB3_12:                               #   in Loop: Header=BB3_6 Depth=2
	xorl	%r14d, %r14d
.LBB3_15:                               #   in Loop: Header=BB3_6 Depth=2
	movq	32(%rsp), %rax                  # 8-byte Reload
	addq	%rbp, %rax
	incq	%rax
	incq	%rbp
	incl	%ebx
	cmpq	$1, %rax
	jne	.LBB3_6
	jmp	.LBB3_16
.LBB3_7:                                #   in Loop: Header=BB3_1 Depth=1
	movq	8(%rsp), %rdx                   # 8-byte Reload
	movslq	(%rdx), %rax
	movq	16(%rsp), %rcx                  # 8-byte Reload
	movl	%r15d, (%rcx,%rax,4)
	incl	(%rdx)
	cmpl	%r13d, %ebp
	movq	24(%rsp), %rdi                  # 8-byte Reload
	jge	.LBB3_1
# %bb.8:                                # %.lr.ph53.preheader
                                        #   in Loop: Header=BB3_1 Depth=1
	movl	%r13d, %eax
	movslq	%ebx, %rdx
	movq	%rdi, %rcx
	subq	%rdx, %rcx
.LBB3_9:                                # %.lr.ph53
                                        #   Parent Loop BB3_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movb	(%r12,%rbp), %dl
	movb	%dl, -1(%rcx,%rbp)
	incq	%rbp
	cmpq	%rax, %rbp
	jb	.LBB3_9
	jmp	.LBB3_1
.LBB3_4:
	xorl	%r15d, %r15d
.LBB3_16:                               # %_Z7str_lenPc.exit._crit_edge
	movq	8(%rsp), %rax                   # 8-byte Reload
	movslq	(%rax), %rax
	movq	16(%rsp), %rcx                  # 8-byte Reload
	movl	%r15d, (%rcx,%rax,4)
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z9checkWordPcPiS0_, .Lfunc_end3-_Z9checkWordPcPiS0_
	.cfi_endproc
                                        # -- End function
	.globl	_Z11store_wordsPcPi             # -- Begin function _Z11store_wordsPcPi
	.type	_Z11store_wordsPcPi,@function
_Z11store_wordsPcPi:                    # @_Z11store_wordsPcPi
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.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, %rbx
	movl	$.L.str.2, %esi
	callq	fopen
	movq	%rax, %r14
	movl	$4, %edi
	callq	malloc
	movq	%rax, %r15
	movl	$0, (%rax)
	xorl	%ebp, %ebp
	movq	%rsp, %rdx
	movl	$.L.str.3, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$-1, %eax
	je	.LBB4_4
# %bb.1:                                # %.lr.ph.preheader
	movq	%rsp, %r12
.LBB4_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%r12, %rdi
	movq	%r15, %rsi
	movq	%rbx, %rdx
	callq	_Z9checkWordPcPiS0_
	movl	(%r15), %r13d
	leal	1(%r13), %ebp
	movl	%ebp, (%r15)
	movl	$.L.str.3, %esi
	movq	%r14, %rdi
	movq	%r12, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$-1, %eax
	je	.LBB4_4
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB4_2 Depth=1
	cmpl	$19999, %r13d                   # imm = 0x4E1F
	jl	.LBB4_2
.LBB4_4:                                # %.critedge
	movl	%ebp, %eax
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z11store_wordsPcPi, .Lfunc_end4-_Z11store_wordsPcPi
	.cfi_endproc
                                        # -- End function
	.globl	_Z5powerii                      # -- Begin function _Z5powerii
	.type	_Z5powerii,@function
_Z5powerii:                             # @_Z5powerii
	.cfi_startproc
# %bb.0:
	movl	$1, %eax
	testl	%esi, %esi
	jle	.LBB5_2
.LBB5_1:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	imull	%edi, %eax
	decl	%esi
	jne	.LBB5_1
.LBB5_2:                                # %._crit_edge
	retq
.Lfunc_end5:
	.size	_Z5powerii, .Lfunc_end5-_Z5powerii
	.cfi_endproc
                                        # -- End function
	.globl	_Z22__device_stub__n_countPiS_ii # -- Begin function _Z22__device_stub__n_countPiS_ii
	.type	_Z22__device_stub__n_countPiS_ii,@function
_Z22__device_stub__n_countPiS_ii:       # @_Z22__device_stub__n_countPiS_ii
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z7n_countPiS_ii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end6:
	.size	_Z22__device_stub__n_countPiS_ii, .Lfunc_end6-_Z22__device_stub__n_countPiS_ii
	.cfi_endproc
                                        # -- End function
	.globl	_Z18print_hist_to_filePii       # -- Begin function _Z18print_hist_to_filePii
	.type	_Z18print_hist_to_filePii,@function
_Z18print_hist_to_filePii:              # @_Z18print_hist_to_filePii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebx
	movq	%rdi, %r14
	movl	$.L.str.4, %edi
	movl	$.L.str.5, %esi
	callq	fopen
	movq	%rax, %r15
	leal	-1(%rbx), %eax
	movl	%eax, 4(%rsp)                   # 4-byte Spill
	xorl	%r12d, %r12d
	movq	%r14, 8(%rsp)                   # 8-byte Spill
.LBB7_1:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB7_3 Depth 2
                                        #     Child Loop BB7_8 Depth 2
                                        #       Child Loop BB7_10 Depth 3
                                        #       Child Loop BB7_12 Depth 3
	movl	$1, %eax
	testl	%ebx, %ebx
	jle	.LBB7_4
# %bb.2:                                # %.lr.ph.i.preheader
                                        #   in Loop: Header=BB7_1 Depth=1
	movl	%ebx, %ecx
.LBB7_3:                                # %.lr.ph.i
                                        #   Parent Loop BB7_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	shll	$2, %eax
	leal	(%rax,%rax,4), %eax
	decl	%ecx
	jne	.LBB7_3
.LBB7_4:                                # %_Z5powerii.exit
                                        #   in Loop: Header=BB7_1 Depth=1
	cltq
	cmpq	%rax, %r12
	jge	.LBB7_18
# %bb.5:                                #   in Loop: Header=BB7_1 Depth=1
	movl	(%r14,%r12,4), %edx
	testl	%edx, %edx
	je	.LBB7_15
# %bb.6:                                # %.preheader
                                        #   in Loop: Header=BB7_1 Depth=1
	testl	%ebx, %ebx
	jle	.LBB7_14
# %bb.7:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB7_1 Depth=1
	xorl	%ebp, %ebp
	movl	4(%rsp), %r14d                  # 4-byte Reload
	movq	%r12, 16(%rsp)                  # 8-byte Spill
                                        # kill: def $r12d killed $r12d killed $r12 def $r12
.LBB7_8:                                # %.lr.ph
                                        #   Parent Loop BB7_1 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB7_10 Depth 3
                                        #       Child Loop BB7_12 Depth 3
	movl	%ebp, %eax
	notl	%eax
	addl	%ebx, %eax
	jle	.LBB7_16
# %bb.9:                                # %.lr.ph.i24.preheader
                                        #   in Loop: Header=BB7_8 Depth=2
	movl	$1, %ecx
	movl	%r14d, %eax
.LBB7_10:                               # %.lr.ph.i24
                                        #   Parent Loop BB7_1 Depth=1
                                        #     Parent Loop BB7_8 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	shll	$2, %ecx
	leal	(%rcx,%rcx,4), %ecx
	decl	%eax
	jne	.LBB7_10
# %bb.11:                               # %_Z5powerii.exit28
                                        #   in Loop: Header=BB7_8 Depth=2
	movl	%r12d, %eax
	cltd
	idivl	%ecx
                                        # kill: def $eax killed $eax def $rax
	leal	1(%rax), %edx
	xorl	%r13d, %r13d
	movl	$.L.str.6, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %ecx
.LBB7_12:                               # %.lr.ph.i30
                                        #   Parent Loop BB7_1 Depth=1
                                        #     Parent Loop BB7_8 Depth=2
                                        # =>    This Inner Loop Header: Depth=3
	shll	$2, %ecx
	leal	(%rcx,%rcx,4), %ecx
	incl	%r13d
	cmpl	%r13d, %r14d
	jne	.LBB7_12
	jmp	.LBB7_17
.LBB7_16:                               # %_Z5powerii.exit28.thread
                                        #   in Loop: Header=BB7_8 Depth=2
	leal	1(%r12), %edx
	movl	$.L.str.6, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %ecx
.LBB7_17:                               # %_Z5powerii.exit34
                                        #   in Loop: Header=BB7_8 Depth=2
	movl	%r12d, %eax
	cltd
	idivl	%ecx
	incl	%ebp
	decl	%r14d
	movl	%edx, %r12d
	cmpl	%ebx, %ebp
	jne	.LBB7_8
# %bb.13:                               # %._crit_edge.loopexit
                                        #   in Loop: Header=BB7_1 Depth=1
	movq	8(%rsp), %r14                   # 8-byte Reload
	movq	16(%rsp), %r12                  # 8-byte Reload
	movl	(%r14,%r12,4), %edx
.LBB7_14:                               # %._crit_edge
                                        #   in Loop: Header=BB7_1 Depth=1
	movl	$.L.str.7, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	fprintf
.LBB7_15:                               #   in Loop: Header=BB7_1 Depth=1
	incq	%r12
	jmp	.LBB7_1
.LBB7_18:
	movq	%r15, %rdi
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.Lfunc_end7:
	.size	_Z18print_hist_to_filePii, .Lfunc_end7-_Z18print_hist_to_filePii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI8_0:
	.quad	0x4034000000000000              # double 20
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$3, %edi
	jne	.LBB8_16
# %bb.1:
	movq	%rsi, %r14
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %ebx
	testl	%eax, %eax
	jle	.LBB8_2
# %bb.3:                                # %.lr.ph.i.preheader
	movl	$1, %eax
	movl	%ebx, %ecx
.LBB8_4:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	shll	$2, %eax
	leal	(%rax,%rax,4), %eax
	decl	%ecx
	jne	.LBB8_4
# %bb.5:                                # %_Z5powerii.exit.loopexit
	movl	%eax, %r12d
	shlq	$2, %r12
	jmp	.LBB8_6
.LBB8_2:
	movl	$4, %r12d
.LBB8_6:                                # %_Z5powerii.exit
	movq	16(%r14), %r14
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r15
	movl	$80000, %edi                    # imm = 0x13880
	callq	malloc
	movq	%rax, %r13
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	cvtsi2sd	%ebx, %xmm1
	movsd	.LCPI8_0(%rip), %xmm0           # xmm0 = mem[0],zero
	callq	pow
	cvttsd2si	%xmm0, %rax
	testl	%eax, %eax
	je	.LBB8_8
# %bb.7:                                # %.lr.ph.preheader.i
	movl	%eax, %edx
	shlq	$2, %rdx
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
.LBB8_8:                                # %_Z15initialize_binsPij.exit
	movq	%r14, %rdi
	movq	%r13, %rsi
	callq	_Z11store_wordsPcPi
	movl	%eax, %ebp
	leaq	16(%rsp), %rdi
	movl	$80000, %esi                    # imm = 0x13880
	callq	hipMalloc
	movl	%eax, %edi
	movl	$254, %esi
	callq	_Z13error_handler10hipError_ti
	leaq	8(%rsp), %r14
	movq	%r14, %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movl	%eax, %edi
	movl	$255, %esi
	callq	_Z13error_handler10hipError_ti
	movq	(%r14), %rdi
	movl	$1, %r14d
	movq	%r15, 24(%rsp)                  # 8-byte Spill
	movq	%r15, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$259, %esi                      # imm = 0x103
	callq	_Z13error_handler10hipError_ti
	leaq	16(%rsp), %rax
	movq	(%rax), %rdi
	movl	$80000, %edx                    # imm = 0x13880
	movq	%r13, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$260, %esi                      # imm = 0x104
	callq	_Z13error_handler10hipError_ti
	movl	$2047, %ecx                     # imm = 0x7FF
	leal	(%rcx,%rbp), %edx
	leal	4094(%rbp), %eax
	testl	%edx, %edx
	cmovnsl	%edx, %eax
	sarl	$11, %eax
	addl	%ebx, %ecx
	movslq	%ecx, %rcx
	testl	%ebx, %ebx
	jle	.LBB8_9
# %bb.10:                               # %.lr.ph.i35.preheader
	movl	%ebx, %edx
.LBB8_11:                               # %.lr.ph.i35
                                        # =>This Inner Loop Header: Depth=1
	shll	$2, %r14d
	leal	(%r14,%r14,4), %r14d
	decl	%edx
	jne	.LBB8_11
# %bb.12:                               # %_Z5powerii.exit39.loopexit
	shrl	$9, %r14d
	jmp	.LBB8_13
.LBB8_9:
	xorl	%r14d, %r14d
.LBB8_13:                               # %_Z5powerii.exit39
	movq	24(%rsp), %r15                  # 8-byte Reload
	addq	%rcx, %r14
	shlq	$2, %r14
	movl	%eax, %edi
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$256, %rdx                      # imm = 0x100
	movl	$1, %esi
	movl	$1, %ecx
	movq	%r14, %r8
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB8_15
# %bb.14:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	%ebx, %edx
	movl	%ebp, %ecx
	callq	_Z22__device_stub__n_countPiS_ii
.LBB8_15:
	movq	8(%rsp), %rsi
	movq	32(%rsp), %r14                  # 8-byte Reload
	movq	%r14, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	movl	$269, %esi                      # imm = 0x10D
	callq	_Z13error_handler10hipError_ti
	movq	%r14, %rdi
	movl	%ebx, %esi
	callq	_Z18print_hist_to_filePii
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$40, %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
.LBB8_16:
	.cfi_def_cfa_offset 96
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end8:
	.size	main, .Lfunc_end8-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	.LBB9_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB9_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z7n_countPiS_ii, %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_end9:
	.size	__hip_module_ctor, .Lfunc_end9-__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	.LBB10_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
.LBB10_2:
	retq
.Lfunc_end10:
	.size	__hip_module_dtor, .Lfunc_end10-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%s in %s at %d"
	.size	.L.str, 15

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/anantshah200/CS6023/master/Assignment3/ee16b105.hip"
	.size	.L.str.1, 114

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

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

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

	.type	.L.str.4,@object                # @.str.4
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.4:
	.asciz	"ee16b105_out.txt"
	.size	.L.str.4, 17

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"w"
	.size	.L.str.5, 2

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

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"%d\n"
	.size	.L.str.7, 4

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

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

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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"error : Invalid number of arguments"
	.size	.Lstr, 36

	.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 _Z22__device_stub__n_countPiS_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z7n_countPiS_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z7n_countPiS_ii        ; -- Begin function _Z7n_countPiS_ii
	.globl	_Z7n_countPiS_ii
	.p2align	8
	.type	_Z7n_countPiS_ii,@function
_Z7n_countPiS_ii:                       ; @_Z7n_countPiS_ii
; %bb.0:                                ; %.lr.ph.preheader
	s_load_b32 s2, s[0:1], 0x24
	v_mov_b32_e32 v5, v0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s8, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_cvt_f32_u32_e32 v1, s8
	s_add_i32 s2, s8, 0x1ff
	v_cvt_f32_u32_e32 v3, s2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_iflag_f32_e32 v2, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v2, v3, v2
	v_trunc_f32_e32 v2, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_fma_f32 v4, -v2, v1, v3
	v_mov_b32_e32 v3, v0
	v_cvt_u32_f32_e32 v2, v2
	v_cmp_ge_f32_e64 s2, |v4|, v1
	v_dual_mov_b32 v1, 0 :: v_dual_lshlrev_b32 v4, 2, v0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_readfirstlane_b32 s3, v2
	s_cmp_lg_u32 s2, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mov_b32_e32 v6, v4
	s_mov_b32 s2, 0
	s_addc_u32 s3, s3, 0
	s_lshl_b32 s12, s8, 2
	s_and_b32 s9, s3, 0x1ffff
.LBB0_1:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_mov_b32 s3, exec_lo
	v_cmpx_gt_u32_e32 0x200, v5
	s_cbranch_execz .LBB0_3
; %bb.2:                                ;   in Loop: Header=BB0_1 Depth=1
	v_mov_b32_e32 v2, v1
	ds_store_b32 v6, v1
	ds_store_b64 v1, v[1:2] offset:2048
.LBB0_3:                                ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s3
	v_add_nc_u32_e32 v6, s12, v6
	v_add_nc_u32_e32 v5, s8, v5
	s_add_i32 s2, s2, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_ge_u32 s2, s9
	s_cbranch_scc0 .LBB0_1
; %bb.4:                                ; %._crit_edge
	s_load_b64 s[2:3], s[0:1], 0x10
	s_mov_b32 s5, 0
	s_mov_b32 s4, 1
	s_waitcnt lgkmcnt(0)
	s_cmp_gt_i32 s2, 0
	s_cselect_b32 s10, -1, 0
	s_cmp_lt_i32 s2, 1
	s_cbranch_scc1 .LBB0_8
; %bb.5:                                ; %.lr.ph.i.preheader
	s_mov_b32 s5, s2
.LBB0_6:                                ; %.lr.ph.i
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_i32 s5, s5, -1
	s_mul_i32 s4, s4, 20
	s_cmp_lg_u32 s5, 0
	s_cbranch_scc1 .LBB0_6
; %bb.7:                                ; %_Z5powerii.exit.loopexit
	s_lshr_b32 s5, s4, 9
.LBB0_8:                                ; %_Z5powerii.exit
	s_lshl_b32 s4, s2, 2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_i32 s13, s4, 0x80c
	s_mov_b32 s4, exec_lo
	v_cmpx_gt_u32_e64 s5, v0
	s_cbranch_execz .LBB0_10
; %bb.9:
	v_lshl_add_u32 v1, v0, 2, s13
	v_mov_b32_e32 v2, 0
	ds_store_b32 v1, v2 offset:8188
.LBB0_10:
	s_or_b32 exec_lo, exec_lo, s4
	s_load_b128 s[4:7], s[0:1], 0x0
	v_add_nc_u32_e32 v4, 0x80c, v4
	s_lshl_b32 s0, s8, 3
	s_add_i32 s11, s2, -1
	s_mul_i32 s14, s15, s8
	s_add_i32 s1, s11, s0
	s_lshl_b32 s14, s14, 3
	s_mov_b32 s16, 9
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_11:                               ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v1, s14, v3
	v_cmp_gt_i32_e64 s0, s1, v3
	v_cmp_gt_i32_e32 vcc_lo, s3, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s17, vcc_lo, s0
	s_and_saveexec_b32 s0, s17
	s_cbranch_execz .LBB0_13
; %bb.12:                               ;   in Loop: Header=BB0_11 Depth=1
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[1:2], 2, v[1:2]
	v_add_co_u32 v1, vcc_lo, s4, v1
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v2, vcc_lo, s5, v2, vcc_lo
	global_load_b32 v1, v[1:2], off
	s_waitcnt vmcnt(0)
	ds_store_b32 v4, v1
.LBB0_13:                               ;   in Loop: Header=BB0_11 Depth=1
	s_or_b32 exec_lo, exec_lo, s0
	v_add_nc_u32_e32 v4, s12, v4
	v_add_nc_u32_e32 v3, s8, v3
	s_add_i32 s16, s16, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s16, 0
	s_cbranch_scc0 .LBB0_11
; %bb.14:
	v_cmp_eq_u32_e64 s0, 0, v0
	v_cndmask_b32_e64 v6, 0, 1, s10
	v_dual_mov_b32 v7, 0 :: v_dual_mov_b32 v8, 1
	v_mov_b32_e32 v9, 0x804
	s_lshl_b32 s4, s15, 3
	s_sub_i32 s3, s3, s2
	s_mov_b32 s5, 0
	s_add_i32 s12, s13, 0x1ffc
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_15:                               ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_18 Depth 2
                                        ;       Child Loop BB0_20 Depth 3
                                        ;     Child Loop BB0_31 Depth 2
                                        ;       Child Loop BB0_33 Depth 3
                                        ;     Child Loop BB0_42 Depth 2
                                        ;       Child Loop BB0_45 Depth 3
	s_add_i32 s1, s5, s4
	s_mov_b32 s13, exec_lo
	v_mad_u64_u32 v[1:2], null, s1, s8, v[0:1]
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_ge_i32_e64 s3, v1
	s_cbranch_execz .LBB0_28
; %bb.16:                               ; %.preheader154
                                        ;   in Loop: Header=BB0_15 Depth=1
	s_and_not1_b32 vcc_lo, exec_lo, s10
	s_cbranch_vccnz .LBB0_22
; %bb.17:                               ; %.lr.ph164
                                        ;   in Loop: Header=BB0_15 Depth=1
	v_mad_u64_u32 v[2:3], null, s5, s8, v[0:1]
	v_mov_b32_e32 v1, 0
	s_mov_b32 s1, 0
	s_mov_b32 s14, s11
.LBB0_18:                               ;   Parent Loop BB0_15 Depth=1
                                        ; =>  This Loop Header: Depth=2
                                        ;       Child Loop BB0_20 Depth 3
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_add_nc_u32_e32 v3, s1, v2
	s_not_b32 s15, s1
	s_add_i32 s15, s15, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_cmp_lt_i32 s15, 1
	v_lshl_add_u32 v3, v3, 2, 0x80c
	s_mov_b32 s15, 1
	ds_load_b32 v3, v3
	s_cbranch_scc1 .LBB0_21
; %bb.19:                               ; %.lr.ph.i131.preheader
                                        ;   in Loop: Header=BB0_18 Depth=2
	s_mov_b32 s16, s14
.LBB0_20:                               ; %.lr.ph.i131
                                        ;   Parent Loop BB0_15 Depth=1
                                        ;     Parent Loop BB0_18 Depth=2
                                        ; =>    This Inner Loop Header: Depth=3
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_i32 s16, s16, -1
	s_mul_i32 s15, s15, 20
	s_cmp_lg_u32 s16, 0
	s_cbranch_scc1 .LBB0_20
.LBB0_21:                               ; %_Z5powerii.exit135
                                        ;   in Loop: Header=BB0_18 Depth=2
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v5, -1, v3
	s_add_i32 s1, s1, 1
	s_add_i32 s14, s14, -1
	s_cmp_eq_u32 s1, s2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[3:4], null, s15, v5, v[1:2]
	v_mov_b32_e32 v1, v3
	s_cbranch_scc0 .LBB0_18
	s_branch .LBB0_23
.LBB0_22:                               ;   in Loop: Header=BB0_15 Depth=1
	v_mov_b32_e32 v1, 0
.LBB0_23:                               ; %Flow260
                                        ;   in Loop: Header=BB0_15 Depth=1
	ds_load_b32 v3, v7 offset:2052
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v2, 0x200, v3
	v_cmp_lt_i32_e32 vcc_lo, v1, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_i32_e64 s1, v1, v2
                                        ; implicit-def: $vgpr2
	s_or_b32 s1, vcc_lo, s1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_saveexec_b32 s14, s1
	s_xor_b32 s1, exec_lo, s14
	s_cbranch_execz .LBB0_25
; %bb.24:                               ;   in Loop: Header=BB0_15 Depth=1
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[3:4], 2, v[1:2]
	v_lshrrev_b32_e32 v2, 23, v2
	v_add_nc_u32_e32 v1, v1, v2
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v3, vcc_lo, s6, v3
	v_add_co_ci_u32_e32 v4, vcc_lo, s7, v4, vcc_lo
	s_delay_alu instid0(VALU_DEP_3)
	v_ashrrev_i32_e32 v1, 9, v1
	global_atomic_add_u32 v[3:4], v8, off
                                        ; implicit-def: $vgpr3
	v_lshl_add_u32 v2, v1, 2, s12
                                        ; implicit-def: $vgpr1
.LBB0_25:                               ; %Flow256
                                        ;   in Loop: Header=BB0_15 Depth=1
	s_and_not1_saveexec_b32 s1, s1
	s_cbranch_execz .LBB0_27
; %bb.26:                               ;   in Loop: Header=BB0_15 Depth=1
	v_sub_nc_u32_e32 v1, v1, v3
	s_delay_alu instid0(VALU_DEP_1)
	v_dual_mov_b32 v2, 0x800 :: v_dual_lshlrev_b32 v1, 2, v1
	ds_add_u32 v1, v8
.LBB0_27:                               ; %.sink.split
                                        ;   in Loop: Header=BB0_15 Depth=1
	s_or_b32 exec_lo, exec_lo, s1
	ds_add_u32 v2, v8
.LBB0_28:                               ; %Flow261
                                        ;   in Loop: Header=BB0_15 Depth=1
	s_or_b32 exec_lo, exec_lo, s13
	s_cmp_lg_u32 s5, 2
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc1 .LBB0_51
; %bb.29:                               ;   in Loop: Header=BB0_15 Depth=1
	s_and_saveexec_b32 s1, s0
	s_cbranch_execz .LBB0_40
; %bb.30:                               ;   in Loop: Header=BB0_15 Depth=1
	ds_load_b64 v[1:2], v7 offset:2048
	s_mov_b32 s13, 0
	s_waitcnt lgkmcnt(0)
	ds_store_b32 v7, v2 offset:2056
.LBB0_31:                               ;   Parent Loop BB0_15 Depth=1
                                        ; =>  This Loop Header: Depth=2
                                        ;       Child Loop BB0_33 Depth 3
	s_and_not1_b32 vcc_lo, exec_lo, s10
	s_mov_b32 s14, 1
	s_cbranch_vccnz .LBB0_34
; %bb.32:                               ; %.lr.ph.i137.preheader
                                        ;   in Loop: Header=BB0_31 Depth=2
	s_mov_b32 s15, s2
.LBB0_33:                               ; %.lr.ph.i137
                                        ;   Parent Loop BB0_15 Depth=1
                                        ;     Parent Loop BB0_31 Depth=2
                                        ; =>    This Inner Loop Header: Depth=3
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_i32 s15, s15, -1
	s_mul_i32 s14, s14, 20
	s_cmp_lg_u32 s15, 0
	s_cbranch_scc1 .LBB0_33
.LBB0_34:                               ; %_Z5powerii.exit141
                                        ;   in Loop: Header=BB0_31 Depth=2
	s_ashr_i32 s15, s14, 31
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshr_b32 s15, s15, 23
	s_add_i32 s14, s14, s15
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_ashr_i32 s14, s14, 9
	s_cmp_lt_i32 s13, s14
	s_mov_b32 s14, -1
	s_cbranch_scc0 .LBB0_38
; %bb.35:                               ;   in Loop: Header=BB0_31 Depth=2
	s_lshl_b32 s14, s13, 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s14, s12, s14
	v_mov_b32_e32 v2, s14
	ds_load_b32 v2, v2
	s_waitcnt lgkmcnt(0)
	v_cmp_le_i32_e32 vcc_lo, v2, v1
	s_cbranch_vccnz .LBB0_37
; %bb.36:                               ;   in Loop: Header=BB0_31 Depth=2
	s_lshl_b32 s14, s13, 9
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mov_b32_e32 v2, s14
	ds_store_b32 v7, v2 offset:2052
.LBB0_37:                               ;   in Loop: Header=BB0_31 Depth=2
	s_add_i32 s13, s13, 1
	s_mov_b32 s14, 0
.LBB0_38:                               ; %Flow253
                                        ;   in Loop: Header=BB0_31 Depth=2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_b32 vcc_lo, exec_lo, s14
	s_cbranch_vccz .LBB0_31
; %bb.39:                               ;   in Loop: Header=BB0_15 Depth=1
	ds_store_b32 v7, v7 offset:2048
.LBB0_40:                               ; %Flow254
                                        ;   in Loop: Header=BB0_15 Depth=1
	s_or_b32 exec_lo, exec_lo, s1
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_2addr_b32 v[1:2], v9 offset1:1
	s_waitcnt lgkmcnt(0)
	v_cmp_eq_u32_e32 vcc_lo, v2, v1
	s_cbranch_vccnz .LBB0_51
; %bb.41:                               ; %.lr.ph168
                                        ;   in Loop: Header=BB0_15 Depth=1
	s_mov_b32 s1, 0
.LBB0_42:                               ;   Parent Loop BB0_15 Depth=1
                                        ; =>  This Loop Header: Depth=2
                                        ;       Child Loop BB0_45 Depth 3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[3:4], null, s1, s8, v[0:1]
	s_mov_b32 s13, exec_lo
	v_cmpx_gt_u32_e32 0x200, v3
	s_cbranch_execz .LBB0_49
; %bb.43:                               ;   in Loop: Header=BB0_42 Depth=2
	s_and_not1_b32 vcc_lo, exec_lo, s10
	s_mov_b32 s14, 1
	s_cbranch_vccnz .LBB0_46
; %bb.44:                               ; %.lr.ph.i143.preheader
                                        ;   in Loop: Header=BB0_42 Depth=2
	s_mov_b32 s15, s2
.LBB0_45:                               ; %.lr.ph.i143
                                        ;   Parent Loop BB0_15 Depth=1
                                        ;     Parent Loop BB0_42 Depth=2
                                        ; =>    This Inner Loop Header: Depth=3
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_i32 s15, s15, -1
	s_mul_i32 s14, s14, 20
	s_cmp_lg_u32 s15, 0
	s_cbranch_scc1 .LBB0_45
.LBB0_46:                               ; %_Z5powerii.exit147
                                        ;   in Loop: Header=BB0_42 Depth=2
	v_add_nc_u32_e32 v4, v3, v2
	v_lshlrev_b32_e32 v1, 2, v3
	s_delay_alu instid0(VALU_DEP_2)
	v_cmp_gt_i32_e32 vcc_lo, s14, v4
	s_and_saveexec_b32 s14, vcc_lo
	s_cbranch_execz .LBB0_48
; %bb.47:                               ;   in Loop: Header=BB0_42 Depth=2
	ds_load_b32 v10, v1
	v_ashrrev_i32_e32 v5, 31, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[3:4], 2, v[4:5]
	v_add_co_u32 v3, vcc_lo, s6, v3
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v4, vcc_lo, s7, v4, vcc_lo
	s_waitcnt lgkmcnt(0)
	global_atomic_add_u32 v[3:4], v10, off
.LBB0_48:                               ;   in Loop: Header=BB0_42 Depth=2
	s_or_b32 exec_lo, exec_lo, s14
	ds_store_b32 v1, v7
.LBB0_49:                               ; %Flow249
                                        ;   in Loop: Header=BB0_42 Depth=2
	s_or_b32 exec_lo, exec_lo, s13
	s_add_i32 s1, s1, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_ge_u32 s1, s9
	s_cbranch_scc0 .LBB0_42
; %bb.50:                               ; %._crit_edge169
                                        ;   in Loop: Header=BB0_15 Depth=1
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
.LBB0_51:                               ;   in Loop: Header=BB0_15 Depth=1
	s_add_i32 s5, s5, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s5, 8
	s_cbranch_scc0 .LBB0_15
; %bb.52:                               ; %.lr.ph173
	v_mov_b32_e32 v1, 0
	s_barrier
	buffer_gl0_inv
	s_mov_b32 s0, 0
	ds_load_b32 v4, v1 offset:2052
.LBB0_53:                               ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_56 Depth 2
	v_mad_u64_u32 v[1:2], null, s0, s8, v[0:1]
	s_mov_b32 s1, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_u32_e32 0x200, v1
	s_cbranch_execz .LBB0_59
; %bb.54:                               ;   in Loop: Header=BB0_53 Depth=1
	v_cmp_ne_u32_e32 vcc_lo, 1, v6
	s_mov_b32 s3, 1
	s_cbranch_vccnz .LBB0_57
; %bb.55:                               ; %.lr.ph.i149.preheader
                                        ;   in Loop: Header=BB0_53 Depth=1
	s_mov_b32 s4, s2
.LBB0_56:                               ; %.lr.ph.i149
                                        ;   Parent Loop BB0_53 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_add_i32 s4, s4, -1
	s_mul_i32 s3, s3, 20
	s_cmp_lg_u32 s4, 0
	s_cbranch_scc1 .LBB0_56
.LBB0_57:                               ; %_Z5powerii.exit153
                                        ;   in Loop: Header=BB0_53 Depth=1
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v2, v1, v4
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_i32_e32 vcc_lo, s3, v2
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_59
; %bb.58:                               ;   in Loop: Header=BB0_53 Depth=1
	v_lshlrev_b32_e32 v1, 2, v1
	v_ashrrev_i32_e32 v3, 31, v2
	ds_load_b32 v5, v1
	v_lshlrev_b64 v[1:2], 2, v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v1, vcc_lo, s6, v1
	v_add_co_ci_u32_e32 v2, vcc_lo, s7, v2, vcc_lo
	s_waitcnt lgkmcnt(0)
	global_atomic_add_u32 v[1:2], v5, off
.LBB0_59:                               ; %Flow246
                                        ;   in Loop: Header=BB0_53 Depth=1
	s_or_b32 exec_lo, exec_lo, s1
	s_add_i32 s0, s0, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_ge_u32 s0, s9
	s_cbranch_scc0 .LBB0_53
; %bb.60:                               ; %._crit_edge174
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z7n_countPiS_ii
		.amdhsa_group_segment_fixed_size 2060
		.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 11
		.amdhsa_next_free_sgpr 18
		.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	_Z7n_countPiS_ii, .Lfunc_end0-_Z7n_countPiS_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1588
; NumSgprs: 20
; NumVgprs: 11
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 2060 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 20
; 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
	.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 s_words_len
	.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
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
      - .offset:         144
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .group_segment_fixed_size: 2060
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z7n_countPiS_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     20
    .sgpr_spill_count: 0
    .symbol:         _Z7n_countPiS_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     11
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
