#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <cuda.h>
#include <cuda_runtime_api.h>
//https://proofwiki.org/wiki/Product_of_Triangular_Matrices

int max_per_row = 0;
__global__
void devTrianglesCount(int* col_indx,  int* csr_rows, int nnz, int rows, int* out_sum);

/** 
 * Description: Reads the data from the mtx files.
 * The first row contains 3 integers: rows columns of the sparse graph 
 * and the number of non zero elements. The non zero elements are stored in 
 * COO format. Also the data have one-based indexing. While reading them we tra them
 * to zero based indexing.
 * 
 * @param data char[] the name of the file to read
 * @param row_indx int*  where the rows of the nnz are stored
 * @param col_indx int*  where  the columns of the nnz are stored
 * @param nnz int* the number of non zero elements
 * @param rows int* the number of rows
 * @param cols itn* the number of columns
 */
void readData(char data[], int **row_indx, int **col_indx, int* nnz, int * rows, int* cols){
    FILE *f = fopen(data,"r");
    fscanf(f, "%d %d %d\n",rows, cols, nnz);
    printf("-READ %d %d %d\n",*rows,*cols,*nnz);
    col_indx[0] = (int*)malloc((*nnz)*sizeof(int));
    row_indx[0] = (int*)malloc((*nnz)*sizeof(int));
    for(int i = 0; i < *nnz; i++){
        fscanf(f, "%d %d", &col_indx[0][i] , &row_indx[0][i]);
        // data have 1 base index
        // transform to 0-based index
        col_indx[0][i]--;
        row_indx[0][i]--;
    }

    fclose(f);
}

/**
 * Description: Returns an array with the non zero rows in compressed format: (length rows insteadn of nnz).
 * Combined with the column index we have the CSR represantion of the sparse graph. Also finds the max non zero
 * elements per row and updates the global variable max_per_row
 * 
 * @param rows int 
 * @param nnz int
 * @param row_indx int* the row vector from the COO format.
 * 
 * 
 * Returns:
 *         csr_rows int*
 */
int* COOtoCSR(int rows, int nnz, int* row_indx){
    // initialize
    int* csr_rows = (int*)malloc(rows*sizeof(int));
    for(int i = 0; i < rows; i++){
        csr_rows[i] = 0;
    }

    // Transformation to CSR 
    for(int i = 0; i < nnz; i++){
        int index = row_indx[i]+1;
        if(index < rows)
            csr_rows[index]++;
    }
    for(int i = 1; i < rows; i++){
        if(csr_rows[i] > max_per_row){
            max_per_row = csr_rows[i];
        }
        csr_rows[i] += csr_rows[i-1];
    }

    return csr_rows;
}

void printTime(struct timeval start, struct timeval end, char* str){
    unsigned long ss,es,su,eu,s,u;
    ss  =start.tv_sec;
    su = start.tv_usec;
    es = end.tv_sec;
    eu = end.tv_usec;
    s = es - ss;
    if(eu > su){
        u = eu - su;
    }else{
        s--;
        u = 1000000 + eu - su;
    }
   
    printf("%s,%lu,%lu\n",str,s,u);
}






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

    if(argc != 2){
        printf("Invalid arguments\n");
        return 1;
    }



    //cudaDeviceReset();
    struct timeval start,end,ALLSTART,ALLEND;

    // "auto.mtx"; // "data.csv"; //  "great-britain_osm.mtx"; // "delaunay_n22.mtx"; //
    printf("-Dataset: %s\n",argv[1]);
    int rows,cols,nnz;
    int *col_indx, *row_indx;
    int sum;

    /* Read Data in COO format and transform to 0 based index */
    gettimeofday(&start,NULL);
    readData(argv[1],&row_indx,&col_indx,&nnz,&rows,&cols);
    gettimeofday(&end,NULL);
    printTime(start,end, "Read Data");


        
    // Transform to CSR
    gettimeofday(&start,NULL);
    int* csr_rows = COOtoCSR(rows, nnz, row_indx);
    // We no longer need row_indx since we have csr_rows
    free(row_indx);
    gettimeofday(&end,NULL);
    printTime(start,end, "CSR");
    
    printf("-MAX PER ROW = %d\n",max_per_row);
    
    gettimeofday(&start,NULL);
    cudaError_t cuer;
    int *cu_col_indx,  *cu_csr_rows;
    int* cu_sum;
    cuer = cudaMalloc(&cu_col_indx,nnz*sizeof(int));
    printf("-%s\n",cudaGetErrorName(cuer));
    cuer = cudaMalloc(&cu_csr_rows,rows*sizeof(int));
    printf("-%s\n",cudaGetErrorName(cuer));
    cuer = cudaMalloc(&cu_sum,rows*sizeof(int));
    printf("-%s\n",cudaGetErrorName(cuer));
    

    cuer = cudaMemcpy(cu_col_indx,col_indx,nnz*sizeof(int),cudaMemcpyHostToDevice);
    printf("-%s\n",cudaGetErrorName(cuer));
    cuer = cudaMemcpy(cu_csr_rows,csr_rows,rows*sizeof(int),cudaMemcpyHostToDevice);
    printf("-%s\n",cudaGetErrorName(cuer));

    int* res = (int*)malloc(rows*sizeof(int));
    for(int i = 0; i < rows; i++){
        res[i] = 0;
    }

    cudaMemcpy(cu_sum,res,rows*sizeof(int),cudaMemcpyHostToDevice);

    gettimeofday(&end,NULL);
    printTime(start,end, "CUDA data transfer");


    gettimeofday(&start,NULL);
    int threads = max_per_row;
    if(max_per_row > 250){
        return 1;
    }

    int blcoksize = rows/(512*512) + 1;
    printf("-blocksize %d %d\n", blcoksize, 512*512);
    devTrianglesCount<<<dim3(512,512,blcoksize),threads>>>(cu_col_indx, cu_csr_rows, nnz, rows, cu_sum);
    printf("-%s\n",cudaGetErrorName(cuer));




    cuer = cudaMemcpy(res,cu_sum,rows*sizeof(int),cudaMemcpyDeviceToHost);
    printf("-%s\n",cudaGetErrorName(cuer));


    sum = 0;
    for(int i = 0; i < rows; i++){
        
        sum += res[i];
    }

    printf("-Cuda triangles = %d\n",sum);
    gettimeofday(&end,NULL);
    printTime(start,end,"CUDA");
    
}


__global__
void devTrianglesCount(int* col_indx, int* csr_rows, int nnz, int num_of_rows, int* out_sum){
    int row = blockIdx.x*gridDim.y*gridDim.z + blockIdx.y*gridDim.z + blockIdx.z;
    int id = threadIdx.x;

    if(row >= num_of_rows){
        return;
    }
    __shared__ int start_row;
    __shared__ int end_row;
    __shared__ int len;
    __shared__ int* row_ptr;
    __shared__ int current_row[64];

    __shared__ int sh_len[64];
    __shared__ int* sh_ptr[64];
    __shared__ int sh_cols[64][64];
    __shared__ int sh_sum[64];
    sh_sum[id] = 0;
    __syncthreads();
    // Get the current row
    if(id == 0){
        start_row = csr_rows[row];
        if(row == num_of_rows-1){
            end_row = nnz;
        }else{
            end_row = csr_rows[row+1];
        }
        len = end_row - start_row;
        row_ptr = &col_indx[start_row];
    }
    __syncthreads();
    if(id < len){
        current_row[id] = row_ptr[id];
    }
    __syncthreads();

    // Get info for each column
    if(id < len){
        int tmp_col = current_row[id];
        int tmp_start = csr_rows[tmp_col];
        int tmp_end;
        if(tmp_col == num_of_rows-1){
            tmp_end = nnz;
        }else{
            tmp_end = csr_rows[tmp_col+1];
        }
        sh_len[id] =  tmp_end - tmp_start;
        sh_ptr[id] = &col_indx[tmp_start];
    }

    __syncthreads();
    for(int i = 0; i < len; i++){
        if(id < sh_len[i]){
            sh_cols[i][id] = sh_ptr[i][id];
        }
    }
    __syncthreads();

    if(id < len){
        int a = 0;
        int b = 0;
        int sum = 0;
        while(1){
            if(a == len || b == sh_len[id]){
                break;
            }

            int b1 = current_row[a] == sh_cols[id][b];
            int b2 = current_row[a] > sh_cols[id][b];
            int b3 = current_row[a] < sh_cols[id][b];

            a = a + b1 + b3;
            b = b + b1 + b2;
            sum = sum + b1;
        }

        sh_sum[id] = sum;
    }
    __syncthreads();
    if(id == 0){
        int sum = 0;
        for(int i = 0; i < len; i++){
            sum += sh_sum[i];
        }
        out_sum[row] = sum;
    }
    __syncthreads();
    
}

	.file	"tmpxft_0029ec00_00000000-6_main.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
.LC1:
	.string	"%d %d %d\n"
.LC2:
	.string	"-READ %d %d %d\n"
.LC3:
	.string	"%d %d"
	.text
	.globl	_Z8readDataPcPPiS1_S0_S0_S0_
	.type	_Z8readDataPcPPiS1_S0_S0_S0_, @function
_Z8readDataPcPPiS1_S0_S0_S0_:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rsi, %r15
	leaq	.LC0(%rip), %rsi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%r9, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%r8, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rcx, %rbx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	call	fopen@PLT
	movq	%r14, %rcx
	movq	%rbx, %r8
	movq	%rbp, %rdx
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movl	(%r14), %ecx
	movl	(%rbx), %r8d
	movl	$2, %edi
	movl	0(%rbp), %edx
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	leaq	.LC3(%rip), %r14
	call	__printf_chk@PLT
	movslq	(%rbx), %rdi
	salq	$2, %rdi
	call	malloc@PLT
	movslq	(%rbx), %rdi
	movq	%rax, 0(%r13)
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rax, (%r15)
.L3:
	cmpl	%r12d, (%rbx)
	jle	.L7
	movq	(%r15), %rcx
	movq	0(%r13), %rdx
	movq	%r14, %rsi
	xorl	%eax, %eax
	leaq	0(,%r12,4), %rbp
	movq	8(%rsp), %rdi
	incq	%r12
	addq	%rbp, %rcx
	addq	%rbp, %rdx
	call	__isoc23_fscanf@PLT
	movq	0(%r13), %rax
	addq	%rbp, %rax
	addq	(%r15), %rbp
	decl	(%rax)
	decl	0(%rbp)
	jmp	.L3
.L7:
	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
.LFE2027:
	.size	_Z8readDataPcPPiS1_S0_S0_S0_, .-_Z8readDataPcPPiS1_S0_S0_S0_
	.globl	_Z8COOtoCSRiiPi
	.type	_Z8COOtoCSRiiPi, @function
_Z8COOtoCSRiiPi:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movslq	%edi, %rdi
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	%esi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rdi, %rbx
	salq	$2, %rdi
	call	malloc@PLT
	xorl	%ecx, %ecx
.L9:
	cmpl	%ecx, %ebx
	jle	.L23
	xorl	%edx, %edx
	movl	%edx, (%rax,%rcx,4)
	incq	%rcx
	jmp	.L9
.L23:
	xorl	%esi, %esi
.L11:
	cmpl	%esi, %ebp
	jle	.L24
	movl	(%r12,%rsi,4), %edx
	leal	1(%rdx), %ecx
	cmpl	%ecx, %ebx
	jle	.L12
	movslq	%ecx, %rcx
	incl	(%rax,%rcx,4)
.L12:
	incq	%rsi
	jmp	.L11
.L24:
	movl	max_per_row(%rip), %esi
	movl	$1, %edx
	xorl	%edi, %edi
.L14:
	cmpl	%edx, %ebx
	jle	.L25
	movl	(%rax,%rdx,4), %ecx
	cmpl	%esi, %ecx
	jle	.L15
	movl	%ecx, %esi
	movb	$1, %dil
.L15:
	addl	-4(%rax,%rdx,4), %ecx
	movl	%ecx, (%rax,%rdx,4)
	incq	%rdx
	jmp	.L14
.L25:
	testb	%dil, %dil
	je	.L8
	movl	%esi, max_per_row(%rip)
.L8:
	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	_Z8COOtoCSRiiPi, .-_Z8COOtoCSRiiPi
	.section	.rodata.str1.1
.LC4:
	.string	"%s,%lu,%lu\n"
	.text
	.globl	_Z9printTime7timevalS_Pc
	.type	_Z9printTime7timevalS_Pc, @function
_Z9printTime7timevalS_Pc:
.LFB2029:
	.cfi_startproc
	endbr64
	movq	%r8, %rax
	subq	%rdi, %rdx
	cmpq	%rcx, %rsi
	jb	.L29
	decq	%rdx
	addq	$1000000, %rcx
.L29:
	subq	%rsi, %rcx
	movl	$2, %edi
	leaq	.LC4(%rip), %rsi
	movq	%rcx, %r8
	movq	%rdx, %rcx
	movq	%rax, %rdx
	xorl	%eax, %eax
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z9printTime7timevalS_Pc, .-_Z9printTime7timevalS_Pc
	.globl	_Z43__device_stub__Z17devTrianglesCountPiS_iiS_PiS_iiS_
	.type	_Z43__device_stub__Z17devTrianglesCountPiS_iiS_PiS_iiS_, @function
_Z43__device_stub__Z17devTrianglesCountPiS_iiS_PiS_iiS_:
.LFB2055:
	.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
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	40(%rsp), %rcx
	movq	%r8, (%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	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%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	.L30
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z17devTrianglesCountPiS_iiS_(%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
.L30:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
	call	__stack_chk_fail@PLT
.L32:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z43__device_stub__Z17devTrianglesCountPiS_iiS_PiS_iiS_, .-_Z43__device_stub__Z17devTrianglesCountPiS_iiS_PiS_iiS_
	.globl	_Z17devTrianglesCountPiS_iiS_
	.type	_Z17devTrianglesCountPiS_iiS_, @function
_Z17devTrianglesCountPiS_iiS_:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z43__device_stub__Z17devTrianglesCountPiS_iiS_PiS_iiS_
	.cfi_endproc
.LFE2056:
	.size	_Z17devTrianglesCountPiS_iiS_, .-_Z17devTrianglesCountPiS_iiS_
	.section	.rodata.str1.1
.LC5:
	.string	"Invalid arguments\n"
.LC6:
	.string	"-Dataset: %s\n"
.LC7:
	.string	"Read Data"
.LC8:
	.string	"CSR"
.LC9:
	.string	"-MAX PER ROW = %d\n"
.LC10:
	.string	"-%s\n"
.LC11:
	.string	"CUDA data transfer"
.LC12:
	.string	"-blocksize %d %d\n"
.LC13:
	.string	"-Cuda triangles = %d\n"
.LC14:
	.string	"CUDA"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.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
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$136, %rsp
	.cfi_def_cfa_offset 192
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	cmpl	$2, %edi
	je	.L36
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L37
.L36:
	movq	8(%rsi), %rdx
	movq	%rsi, %rbx
	leaq	88(%rsp), %rbp
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	leaq	104(%rsp), %r13
	call	__printf_chk@PLT
	xorl	%esi, %esi
	movq	%rbp, %rdi
	call	gettimeofday@PLT
	movq	8(%rbx), %rdi
	leaq	16(%rsp), %r9
	leaq	20(%rsp), %rcx
	leaq	24(%rsp), %rdx
	leaq	12(%rsp), %r8
	leaq	32(%rsp), %rsi
	call	_Z8readDataPcPPiS1_S0_S0_S0_
	xorl	%esi, %esi
	movq	%r13, %rdi
	call	gettimeofday@PLT
	movq	112(%rsp), %rcx
	movq	104(%rsp), %rdx
	leaq	.LC7(%rip), %r8
	movq	88(%rsp), %rdi
	movq	96(%rsp), %rsi
	call	_Z9printTime7timevalS_Pc
	xorl	%esi, %esi
	movq	%rbp, %rdi
	call	gettimeofday@PLT
	movq	32(%rsp), %rbx
	movl	20(%rsp), %esi
	movl	12(%rsp), %edi
	movq	%rbx, %rdx
	call	_Z8COOtoCSRiiPi
	movq	%rbx, %rdi
	movq	%rax, %r12
	call	free@PLT
	xorl	%esi, %esi
	movq	%r13, %rdi
	call	gettimeofday@PLT
	movq	112(%rsp), %rcx
	movq	104(%rsp), %rdx
	leaq	.LC8(%rip), %r8
	movq	88(%rsp), %rdi
	movq	96(%rsp), %rsi
	call	_Z9printTime7timevalS_Pc
	movl	max_per_row(%rip), %edx
	leaq	.LC9(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	xorl	%esi, %esi
	movq	%rbp, %rdi
	call	gettimeofday@PLT
	movslq	20(%rsp), %rsi
	leaq	40(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	cudaGetErrorName@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movslq	12(%rsp), %rsi
	leaq	48(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	cudaGetErrorName@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movslq	12(%rsp), %rsi
	leaq	56(%rsp), %rdi
	salq	$2, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %edi
	call	cudaGetErrorName@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movslq	20(%rsp), %rdx
	movq	24(%rsp), %rsi
	movl	$1, %ecx
	movq	40(%rsp), %rdi
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	cudaGetErrorName@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movslq	12(%rsp), %rdx
	movq	48(%rsp), %rdi
	movq	%r12, %rsi
	movl	$1, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	movl	%eax, %r14d
	call	cudaGetErrorName@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movslq	12(%rsp), %r15
	movq	%r15, %rbx
	salq	$2, %r15
	movq	%r15, %rdi
	call	malloc@PLT
	movq	%rax, %r12
	xorl	%eax, %eax
.L38:
	cmpl	%eax, %ebx
	jle	.L47
	xorl	%edx, %edx
	movl	%edx, (%r12,%rax,4)
	incq	%rax
	jmp	.L38
.L47:
	movq	56(%rsp), %rdi
	movq	%r15, %rdx
	movl	$1, %ecx
	movq	%r12, %rsi
	call	cudaMemcpy@PLT
	xorl	%esi, %esi
	movq	%r13, %rdi
	call	gettimeofday@PLT
	movq	104(%rsp), %rdx
	movq	112(%rsp), %rcx
	leaq	.LC11(%rip), %r8
	movq	88(%rsp), %rdi
	movq	96(%rsp), %rsi
	call	_Z9printTime7timevalS_Pc
	xorl	%esi, %esi
	movq	%rbp, %rdi
	call	gettimeofday@PLT
	movl	max_per_row(%rip), %r15d
	cmpl	$250, %r15d
	jle	.L40
.L37:
	movl	$1, %eax
	jmp	.L35
.L40:
	movl	12(%rsp), %eax
	movl	$262144, %ecx
	movl	$2, %edi
	leaq	.LC12(%rip), %rsi
	cltd
	idivl	%ecx
	leal	1(%rax), %ebp
	xorl	%eax, %eax
	movl	%ebp, %edx
	call	__printf_chk@PLT
	movl	%r15d, 76(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	movl	%ebp, %esi
	movabsq	$2199023256064, %rdi
	movq	%rax, 80(%rsp)
	movl	84(%rsp), %ecx
	movq	76(%rsp), %rdx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L42
	movq	56(%rsp), %r8
	movl	12(%rsp), %ecx
	movl	20(%rsp), %edx
	movq	48(%rsp), %rsi
	movq	40(%rsp), %rdi
	call	_Z43__device_stub__Z17devTrianglesCountPiS_iiS_PiS_iiS_
.L42:
	movl	%r14d, %edi
	call	cudaGetErrorName@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movslq	12(%rsp), %rdx
	movq	56(%rsp), %rsi
	movq	%r12, %rdi
	movl	$2, %ecx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movl	%eax, %edi
	call	cudaGetErrorName@PLT
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	12(%rsp), %ecx
	xorl	%eax, %eax
	xorl	%edx, %edx
.L43:
	cmpl	%eax, %ecx
	jle	.L48
	addl	(%r12,%rax,4), %edx
	incq	%rax
	jmp	.L43
.L48:
	leaq	.LC13(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%esi, %esi
	movq	%r13, %rdi
	call	gettimeofday@PLT
	movq	104(%rsp), %rdx
	movq	112(%rsp), %rcx
	leaq	.LC14(%rip), %r8
	movq	88(%rsp), %rdi
	movq	96(%rsp), %rsi
	call	_Z9printTime7timevalS_Pc
	xorl	%eax, %eax
.L35:
	movq	120(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L45
	call	__stack_chk_fail@PLT
.L45:
	addq	$136, %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	main, .-main
	.section	.rodata.str1.1
.LC15:
	.string	"_Z17devTrianglesCountPiS_iiS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2058:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC15(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z17devTrianglesCountPiS_iiS_(%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
.LFE2058:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	max_per_row
	.bss
	.align 4
	.type	max_per_row, @object
	.size	max_per_row, 4
max_per_row:
	.zero	4
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <hip/hip_runtime.h>
#include <hip/hip_runtime_api.h>
//https://proofwiki.org/wiki/Product_of_Triangular_Matrices

int max_per_row = 0;
__global__
void devTrianglesCount(int* col_indx,  int* csr_rows, int nnz, int rows, int* out_sum);

/** 
 * Description: Reads the data from the mtx files.
 * The first row contains 3 integers: rows columns of the sparse graph 
 * and the number of non zero elements. The non zero elements are stored in 
 * COO format. Also the data have one-based indexing. While reading them we tra them
 * to zero based indexing.
 * 
 * @param data char[] the name of the file to read
 * @param row_indx int*  where the rows of the nnz are stored
 * @param col_indx int*  where  the columns of the nnz are stored
 * @param nnz int* the number of non zero elements
 * @param rows int* the number of rows
 * @param cols itn* the number of columns
 */
void readData(char data[], int **row_indx, int **col_indx, int* nnz, int * rows, int* cols){
    FILE *f = fopen(data,"r");
    fscanf(f, "%d %d %d\n",rows, cols, nnz);
    printf("-READ %d %d %d\n",*rows,*cols,*nnz);
    col_indx[0] = (int*)malloc((*nnz)*sizeof(int));
    row_indx[0] = (int*)malloc((*nnz)*sizeof(int));
    for(int i = 0; i < *nnz; i++){
        fscanf(f, "%d %d", &col_indx[0][i] , &row_indx[0][i]);
        // data have 1 base index
        // transform to 0-based index
        col_indx[0][i]--;
        row_indx[0][i]--;
    }

    fclose(f);
}

/**
 * Description: Returns an array with the non zero rows in compressed format: (length rows insteadn of nnz).
 * Combined with the column index we have the CSR represantion of the sparse graph. Also finds the max non zero
 * elements per row and updates the global variable max_per_row
 * 
 * @param rows int 
 * @param nnz int
 * @param row_indx int* the row vector from the COO format.
 * 
 * 
 * Returns:
 *         csr_rows int*
 */
int* COOtoCSR(int rows, int nnz, int* row_indx){
    // initialize
    int* csr_rows = (int*)malloc(rows*sizeof(int));
    for(int i = 0; i < rows; i++){
        csr_rows[i] = 0;
    }

    // Transformation to CSR 
    for(int i = 0; i < nnz; i++){
        int index = row_indx[i]+1;
        if(index < rows)
            csr_rows[index]++;
    }
    for(int i = 1; i < rows; i++){
        if(csr_rows[i] > max_per_row){
            max_per_row = csr_rows[i];
        }
        csr_rows[i] += csr_rows[i-1];
    }

    return csr_rows;
}

void printTime(struct timeval start, struct timeval end, char* str){
    unsigned long ss,es,su,eu,s,u;
    ss  =start.tv_sec;
    su = start.tv_usec;
    es = end.tv_sec;
    eu = end.tv_usec;
    s = es - ss;
    if(eu > su){
        u = eu - su;
    }else{
        s--;
        u = 1000000 + eu - su;
    }
   
    printf("%s,%lu,%lu\n",str,s,u);
}






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

    if(argc != 2){
        printf("Invalid arguments\n");
        return 1;
    }



    //cudaDeviceReset();
    struct timeval start,end,ALLSTART,ALLEND;

    // "auto.mtx"; // "data.csv"; //  "great-britain_osm.mtx"; // "delaunay_n22.mtx"; //
    printf("-Dataset: %s\n",argv[1]);
    int rows,cols,nnz;
    int *col_indx, *row_indx;
    int sum;

    /* Read Data in COO format and transform to 0 based index */
    gettimeofday(&start,NULL);
    readData(argv[1],&row_indx,&col_indx,&nnz,&rows,&cols);
    gettimeofday(&end,NULL);
    printTime(start,end, "Read Data");


        
    // Transform to CSR
    gettimeofday(&start,NULL);
    int* csr_rows = COOtoCSR(rows, nnz, row_indx);
    // We no longer need row_indx since we have csr_rows
    free(row_indx);
    gettimeofday(&end,NULL);
    printTime(start,end, "CSR");
    
    printf("-MAX PER ROW = %d\n",max_per_row);
    
    gettimeofday(&start,NULL);
    hipError_t cuer;
    int *cu_col_indx,  *cu_csr_rows;
    int* cu_sum;
    cuer = hipMalloc(&cu_col_indx,nnz*sizeof(int));
    printf("-%s\n",hipGetErrorName(cuer));
    cuer = hipMalloc(&cu_csr_rows,rows*sizeof(int));
    printf("-%s\n",hipGetErrorName(cuer));
    cuer = hipMalloc(&cu_sum,rows*sizeof(int));
    printf("-%s\n",hipGetErrorName(cuer));
    

    cuer = hipMemcpy(cu_col_indx,col_indx,nnz*sizeof(int),hipMemcpyHostToDevice);
    printf("-%s\n",hipGetErrorName(cuer));
    cuer = hipMemcpy(cu_csr_rows,csr_rows,rows*sizeof(int),hipMemcpyHostToDevice);
    printf("-%s\n",hipGetErrorName(cuer));

    int* res = (int*)malloc(rows*sizeof(int));
    for(int i = 0; i < rows; i++){
        res[i] = 0;
    }

    hipMemcpy(cu_sum,res,rows*sizeof(int),hipMemcpyHostToDevice);

    gettimeofday(&end,NULL);
    printTime(start,end, "CUDA data transfer");


    gettimeofday(&start,NULL);
    int threads = max_per_row;
    if(max_per_row > 250){
        return 1;
    }

    int blcoksize = rows/(512*512) + 1;
    printf("-blocksize %d %d\n", blcoksize, 512*512);
    devTrianglesCount<<<dim3(512,512,blcoksize),threads>>>(cu_col_indx, cu_csr_rows, nnz, rows, cu_sum);
    printf("-%s\n",hipGetErrorName(cuer));




    cuer = hipMemcpy(res,cu_sum,rows*sizeof(int),hipMemcpyDeviceToHost);
    printf("-%s\n",hipGetErrorName(cuer));


    sum = 0;
    for(int i = 0; i < rows; i++){
        
        sum += res[i];
    }

    printf("-Cuda triangles = %d\n",sum);
    gettimeofday(&end,NULL);
    printTime(start,end,"CUDA");
    
}


__global__
void devTrianglesCount(int* col_indx, int* csr_rows, int nnz, int num_of_rows, int* out_sum){
    int row = blockIdx.x*gridDim.y*gridDim.z + blockIdx.y*gridDim.z + blockIdx.z;
    int id = threadIdx.x;

    if(row >= num_of_rows){
        return;
    }
    __shared__ int start_row;
    __shared__ int end_row;
    __shared__ int len;
    __shared__ int* row_ptr;
    __shared__ int current_row[64];

    __shared__ int sh_len[64];
    __shared__ int* sh_ptr[64];
    __shared__ int sh_cols[64][64];
    __shared__ int sh_sum[64];
    sh_sum[id] = 0;
    __syncthreads();
    // Get the current row
    if(id == 0){
        start_row = csr_rows[row];
        if(row == num_of_rows-1){
            end_row = nnz;
        }else{
            end_row = csr_rows[row+1];
        }
        len = end_row - start_row;
        row_ptr = &col_indx[start_row];
    }
    __syncthreads();
    if(id < len){
        current_row[id] = row_ptr[id];
    }
    __syncthreads();

    // Get info for each column
    if(id < len){
        int tmp_col = current_row[id];
        int tmp_start = csr_rows[tmp_col];
        int tmp_end;
        if(tmp_col == num_of_rows-1){
            tmp_end = nnz;
        }else{
            tmp_end = csr_rows[tmp_col+1];
        }
        sh_len[id] =  tmp_end - tmp_start;
        sh_ptr[id] = &col_indx[tmp_start];
    }

    __syncthreads();
    for(int i = 0; i < len; i++){
        if(id < sh_len[i]){
            sh_cols[i][id] = sh_ptr[i][id];
        }
    }
    __syncthreads();

    if(id < len){
        int a = 0;
        int b = 0;
        int sum = 0;
        while(1){
            if(a == len || b == sh_len[id]){
                break;
            }

            int b1 = current_row[a] == sh_cols[id][b];
            int b2 = current_row[a] > sh_cols[id][b];
            int b3 = current_row[a] < sh_cols[id][b];

            a = a + b1 + b3;
            b = b + b1 + b2;
            sum = sum + b1;
        }

        sh_sum[id] = sum;
    }
    __syncthreads();
    if(id == 0){
        int sum = 0;
        for(int i = 0; i < len; i++){
            sum += sh_sum[i];
        }
        out_sum[row] = sum;
    }
    __syncthreads();
    
}

	.text
	.file	"main.hip"
	.globl	_Z8readDataPcPPiS1_S0_S0_S0_    # -- Begin function _Z8readDataPcPPiS1_S0_S0_S0_
	.type	_Z8readDataPcPPiS1_S0_S0_S0_,@function
_Z8readDataPcPPiS1_S0_S0_S0_:           # @_Z8readDataPcPPiS1_S0_S0_S0_
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%r9, %r13
	movq	%r8, %rbp
	movq	%rcx, %rbx
	movq	%rdx, %r14
	movq	%rsi, %r15
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, %r12
	movl	$.L.str.1, %esi
	movq	%rax, %rdi
	movq	%rbp, %rdx
	movq	%r13, %rcx
	movq	%rbx, %r8
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	(%rbp), %esi
	movl	(%r13), %edx
	movl	(%rbx), %ecx
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	movslq	(%rbx), %rbp
	leaq	(,%rbp,4), %r13
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, (%r14)
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, (%r15)
	testq	%rbp, %rbp
	jle	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	movq	%rax, %rcx
	movq	(%r14), %rdx
	xorl	%r13d, %r13d
	xorl	%ebp, %ebp
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	addq	%r13, %rdx
	addq	%r13, %rcx
	movl	$.L.str.3, %esi
	movq	%r12, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movq	(%r14), %rdx
	decl	(%rdx,%r13)
	movq	(%r15), %rcx
	decl	(%rcx,%r13)
	incq	%rbp
	movslq	(%rbx), %rax
	addq	$4, %r13
	cmpq	%rax, %rbp
	jl	.LBB0_2
.LBB0_3:                                # %._crit_edge
	movq	%r12, %rdi
	addq	$8, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.Lfunc_end0:
	.size	_Z8readDataPcPPiS1_S0_S0_S0_, .Lfunc_end0-_Z8readDataPcPPiS1_S0_S0_S0_
	.cfi_endproc
                                        # -- End function
	.globl	_Z8COOtoCSRiiPi                 # -- Begin function _Z8COOtoCSRiiPi
	.type	_Z8COOtoCSRiiPi,@function
_Z8COOtoCSRiiPi:                        # @_Z8COOtoCSRiiPi
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdx, %r14
	movl	%esi, %r15d
	movl	%edi, %ebp
	movslq	%edi, %r13
	leaq	(,%r13,4), %rdi
	callq	malloc
	movq	%rax, %rbx
	movl	%ebp, %r12d
	testl	%r13d, %r13d
	jle	.LBB1_2
# %bb.1:                                # %.lr.ph.preheader
	leaq	(,%r12,4), %rdx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
.LBB1_2:                                # %.preheader31
	testl	%r15d, %r15d
	jle	.LBB1_7
# %bb.3:                                # %.lr.ph34.preheader
	movl	%r15d, %eax
	xorl	%ecx, %ecx
.LBB1_4:                                # %.lr.ph34
                                        # =>This Inner Loop Header: Depth=1
	movslq	(%r14,%rcx,4), %rdx
	leal	1(%rdx), %esi
	cmpl	%ebp, %esi
	jge	.LBB1_6
# %bb.5:                                #   in Loop: Header=BB1_4 Depth=1
	incl	4(%rbx,%rdx,4)
.LBB1_6:                                #   in Loop: Header=BB1_4 Depth=1
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB1_4
.LBB1_7:                                # %.preheader
	cmpl	$2, %ebp
	jl	.LBB1_12
# %bb.8:                                # %.lr.ph36.preheader
	movl	max_per_row(%rip), %ecx
	movl	$1, %eax
.LBB1_9:                                # %.lr.ph36
                                        # =>This Inner Loop Header: Depth=1
	movl	(%rbx,%rax,4), %edx
	cmpl	%ecx, %edx
	jle	.LBB1_11
# %bb.10:                               #   in Loop: Header=BB1_9 Depth=1
	movl	%edx, max_per_row(%rip)
	movl	%edx, %ecx
.LBB1_11:                               #   in Loop: Header=BB1_9 Depth=1
	addl	-4(%rbx,%rax,4), %edx
	movl	%edx, (%rbx,%rax,4)
	incq	%rax
	cmpq	%rax, %r12
	jne	.LBB1_9
.LBB1_12:                               # %._crit_edge
	movq	%rbx, %rax
	addq	$8, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z8COOtoCSRiiPi, .Lfunc_end1-_Z8COOtoCSRiiPi
	.cfi_endproc
                                        # -- End function
	.globl	_Z9printTime7timevalS_Pc        # -- Begin function _Z9printTime7timevalS_Pc
	.type	_Z9printTime7timevalS_Pc,@function
_Z9printTime7timevalS_Pc:               # @_Z9printTime7timevalS_Pc
	.cfi_startproc
# %bb.0:
	subq	%rdi, %rdx
	xorl	%eax, %eax
	cmpq	%rsi, %rcx
	setbe	%al
	leaq	1000000(%rcx), %rdi
	cmovbeq	%rdi, %rcx
	subq	%rax, %rdx
	subq	%rsi, %rcx
	movl	$.L.str.4, %edi
	movq	%r8, %rsi
	xorl	%eax, %eax
	jmp	printf                          # TAILCALL
.Lfunc_end2:
	.size	_Z9printTime7timevalS_Pc, .Lfunc_end2-_Z9printTime7timevalS_Pc
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$104, %rsp
	.cfi_def_cfa_offset 160
	.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	$2, %edi
	jne	.LBB3_1
# %bb.2:
	movq	%rsi, %rbx
	movq	8(%rsi), %rsi
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	leaq	56(%rsp), %r13
	movq	%r13, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	8(%rbx), %rdi
	leaq	96(%rsp), %r12
	leaq	48(%rsp), %rdx
	leaq	20(%rsp), %r15
	leaq	4(%rsp), %rbx
	leaq	76(%rsp), %r9
	movq	%r12, %rsi
	movq	%r15, %rcx
	movq	%rbx, %r8
	callq	_Z8readDataPcPPiS1_S0_S0_S0_
	leaq	80(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	8(%r13), %rax
	movq	(%r14), %rdx
	subq	(%r13), %rdx
	movq	8(%r14), %rsi
	movl	$1000000, %ebp                  # imm = 0xF4240
	leaq	(%rsi,%rbp), %rcx
	xorl	%edi, %edi
	cmpq	%rax, %rsi
	cmovaq	%rsi, %rcx
	setbe	%dil
	subq	%rdi, %rdx
	subq	%rax, %rcx
	movl	$.L.str.4, %edi
	movl	$.L.str.7, %esi
	xorl	%eax, %eax
	callq	printf
	movq	%r13, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	(%rbx), %edi
	movl	(%r15), %esi
	movq	(%r12), %r12
	movq	%r12, %rdx
	callq	_Z8COOtoCSRiiPi
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	movq	%r12, %rdi
	callq	free
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	8(%r13), %rax
	movq	(%r14), %rdx
	movq	8(%r14), %rcx
	subq	(%r13), %rdx
	addq	%rcx, %rbp
	xorl	%esi, %esi
	cmpq	%rax, %rcx
	cmovaq	%rcx, %rbp
	setbe	%sil
	subq	%rsi, %rdx
	subq	%rax, %rbp
	movl	$.L.str.4, %edi
	movl	$.L.str.8, %esi
	movq	%rbp, %rcx
	xorl	%eax, %eax
	callq	printf
	movl	max_per_row(%rip), %esi
	movl	$.L.str.9, %edi
	xorl	%eax, %eax
	callq	printf
	movq	%r13, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movslq	(%r15), %rsi
	shlq	$2, %rsi
	leaq	40(%rsp), %r14
	movq	%r14, %rdi
	callq	hipMalloc
	movl	%eax, %edi
	callq	hipGetErrorName
	movl	$.L.str.10, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movslq	(%rbx), %rsi
	shlq	$2, %rsi
	leaq	32(%rsp), %r13
	movq	%r13, %rdi
	callq	hipMalloc
	movl	%eax, %edi
	callq	hipGetErrorName
	movl	$.L.str.10, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movslq	(%rbx), %rsi
	shlq	$2, %rsi
	leaq	8(%rsp), %rdi
	callq	hipMalloc
	movl	%eax, %edi
	callq	hipGetErrorName
	movl	$.L.str.10, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	(%r14), %rdi
	leaq	48(%rsp), %rax
	movq	(%rax), %rsi
	movslq	(%r15), %rdx
	shlq	$2, %rdx
	movl	$1, %r14d
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	hipGetErrorName
	movl	$.L.str.10, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	(%r13), %rdi
	movslq	(%rbx), %rdx
	shlq	$2, %rdx
	movq	24(%rsp), %rsi                  # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	movl	%eax, %ebp
	movl	%eax, %edi
	callq	hipGetErrorName
	movl	$.L.str.10, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movslq	(%rbx), %r12
	leaq	(,%r12,4), %r15
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %rbx
	testq	%r12, %r12
	jle	.LBB3_4
# %bb.3:                                # %.lr.ph.preheader
	movl	%r12d, %edx
	shlq	$2, %rdx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
.LBB3_4:                                # %._crit_edge
	movq	8(%rsp), %rdi
	movq	%rbx, %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	80(%rsp), %r12
	movq	%r12, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	leaq	56(%rsp), %r15
	movq	8(%r15), %rax
	movq	(%r12), %rdx
	movq	8(%r12), %rsi
	subq	(%r15), %rdx
	leaq	1000000(%rsi), %rcx
	xorl	%edi, %edi
	cmpq	%rax, %rsi
	cmovaq	%rsi, %rcx
	setbe	%dil
	subq	%rdi, %rdx
	subq	%rax, %rcx
	movl	$.L.str.4, %edi
	movl	$.L.str.11, %esi
	xorl	%eax, %eax
	callq	printf
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	max_per_row(%rip), %r12d
	cmpl	$250, %r12d
	jg	.LBB3_11
# %bb.5:
	movl	4(%rsp), %eax
	leal	262143(%rax), %r14d
	testl	%eax, %eax
	cmovnsl	%eax, %r14d
	sarl	$18, %r14d
	incl	%r14d
	xorl	%r15d, %r15d
	movl	$.L.str.12, %edi
	movl	%r14d, %esi
	movl	$262144, %edx                   # imm = 0x40000
	xorl	%eax, %eax
	callq	printf
	btsq	$32, %r12
	movabsq	$2199023256064, %rdi            # imm = 0x20000000200
	movl	%r14d, %esi
	movq	%r12, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_7
# %bb.6:
	movq	40(%rsp), %rdi
	movq	32(%rsp), %rsi
	movl	20(%rsp), %edx
	movl	4(%rsp), %ecx
	movq	8(%rsp), %r8
	callq	_Z32__device_stub__devTrianglesCountPiS_iiS_
.LBB3_7:
	movl	%ebp, %edi
	callq	hipGetErrorName
	movl	$.L.str.10, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	8(%rsp), %rsi
	movslq	4(%rsp), %rdx
	shlq	$2, %rdx
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movl	%eax, %edi
	callq	hipGetErrorName
	movl	$.L.str.10, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	4(%rsp), %eax
	testl	%eax, %eax
	jle	.LBB3_10
# %bb.8:                                # %.lr.ph66.preheader
	xorl	%ecx, %ecx
	xorl	%r15d, %r15d
.LBB3_9:                                # %.lr.ph66
                                        # =>This Inner Loop Header: Depth=1
	addl	(%rbx,%rcx,4), %r15d
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB3_9
.LBB3_10:                               # %._crit_edge67
	xorl	%r14d, %r14d
	movl	$.L.str.13, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	leaq	80(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	64(%rsp), %rax
	movq	(%rbx), %rdx
	movq	8(%rbx), %rsi
	subq	56(%rsp), %rdx
	leaq	1000000(%rsi), %rcx
	xorl	%edi, %edi
	cmpq	%rax, %rsi
	cmovaq	%rsi, %rcx
	setbe	%dil
	subq	%rdi, %rdx
	subq	%rax, %rcx
	movl	$.L.str.4, %edi
	movl	$.L.str.14, %esi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB3_11
.LBB3_1:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$1, %r14d
.LBB3_11:
	movl	%r14d, %eax
	addq	$104, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z32__device_stub__devTrianglesCountPiS_iiS_ # -- Begin function _Z32__device_stub__devTrianglesCountPiS_iiS_
	.type	_Z32__device_stub__devTrianglesCountPiS_iiS_,@function
_Z32__device_stub__devTrianglesCountPiS_iiS_: # @_Z32__device_stub__devTrianglesCountPiS_iiS_
	.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	4(%rsp), %rsi
	movl	%edx, (%rsi)
	movq	%rsp, %rdx
	movl	%ecx, (%rdx)
	leaq	24(%rsp), %rcx
	movq	%r8, (%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	$_Z17devTrianglesCountPiS_iiS_, %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_end4:
	.size	_Z32__device_stub__devTrianglesCountPiS_iiS_, .Lfunc_end4-_Z32__device_stub__devTrianglesCountPiS_iiS_
	.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	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z17devTrianglesCountPiS_iiS_, %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_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__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	.LBB6_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
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	max_per_row,@object             # @max_per_row
	.bss
	.globl	max_per_row
	.p2align	2, 0x0
max_per_row:
	.long	0                               # 0x0
	.size	max_per_row, 4

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

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

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"-READ %d %d %d\n"
	.size	.L.str.2, 16

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

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

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"-Dataset: %s\n"
	.size	.L.str.6, 14

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Read Data"
	.size	.L.str.7, 10

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"CSR"
	.size	.L.str.8, 4

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"-MAX PER ROW = %d\n"
	.size	.L.str.9, 19

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"-%s\n"
	.size	.L.str.10, 5

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"CUDA data transfer"
	.size	.L.str.11, 19

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"-blocksize %d %d\n"
	.size	.L.str.12, 18

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

	.type	.L.str.13,@object               # @.str.13
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.13:
	.asciz	"-Cuda triangles = %d\n"
	.size	.L.str.13, 22

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"CUDA"
	.size	.L.str.14, 5

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z17devTrianglesCountPiS_iiS_"
	.size	.L__unnamed_1, 30

	.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	"Invalid arguments"
	.size	.Lstr, 18

	.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 _Z32__device_stub__devTrianglesCountPiS_iiS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z17devTrianglesCountPiS_iiS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z17devTrianglesCountPiS_iiS_ ; -- Begin function _Z17devTrianglesCountPiS_iiS_
	.globl	_Z17devTrianglesCountPiS_iiS_
	.p2align	8
	.type	_Z17devTrianglesCountPiS_iiS_,@function
_Z17devTrianglesCountPiS_iiS_:          ; @_Z17devTrianglesCountPiS_iiS_
; %bb.0:
	s_clause 0x1
	s_load_b64 s[2:3], s[0:1], 0x24
	s_load_b64 s[10:11], s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s2, s2, s13
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s2, s2, s14
	s_mul_i32 s2, s2, s3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s8, s2, s15
	s_cmp_ge_i32 s8, s11
	s_cbranch_scc1 .LBB0_30
; %bb.1:
	s_load_b128 s[4:7], s[0:1], 0x0
	v_lshlrev_b32_e32 v5, 2, v0
	v_mov_b32_e32 v1, 0
	v_cmp_eq_u32_e64 s2, 0, v0
	ds_store_b32 v5, v1 offset:17408
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_and_saveexec_b32 s3, s2
	s_cbranch_execz .LBB0_5
; %bb.2:
	s_ashr_i32 s9, s8, 31
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[12:13], s[8:9], 2
	s_add_u32 s14, s6, s12
	s_addc_u32 s15, s7, s13
	s_add_i32 s9, s11, -1
	s_load_b32 s12, s[14:15], 0x0
	s_cmp_eq_u32 s8, s9
	s_mov_b32 s9, s10
	s_cbranch_scc1 .LBB0_4
; %bb.3:
	s_load_b32 s9, s[14:15], 0x4
.LBB0_4:
	s_waitcnt lgkmcnt(0)
	s_ashr_i32 s13, s12, 31
	s_sub_i32 s9, s9, s12
	s_lshl_b64 s[12:13], s[12:13], 2
	v_dual_mov_b32 v3, 0 :: v_dual_mov_b32 v4, s9
	s_add_u32 s12, s4, s12
	s_addc_u32 s13, s5, s13
	s_delay_alu instid0(SALU_CYCLE_1)
	v_dual_mov_b32 v1, s12 :: v_dual_mov_b32 v2, s13
	ds_store_b32 v3, v4 offset:17672
	ds_store_b64 v3, v[1:2] offset:17664
.LBB0_5:                                ; %Flow129
	s_or_b32 exec_lo, exec_lo, s3
	v_dual_mov_b32 v1, 0 :: v_dual_add_nc_u32 v4, 0x4400, v5
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_mov_b32 s3, exec_lo
	ds_load_b32 v2, v1 offset:17672
	s_waitcnt lgkmcnt(0)
	v_cmpx_lt_i32_e64 v0, v2
	s_cbranch_execz .LBB0_7
; %bb.6:
	ds_load_b64 v[2:3], v1 offset:17664
	v_lshlrev_b32_e32 v6, 2, v0
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1)
	v_add_co_u32 v2, vcc_lo, v2, v6
	v_add_co_ci_u32_e32 v3, vcc_lo, 0, v3, vcc_lo
	flat_load_b32 v2, v[2:3]
	s_waitcnt vmcnt(0) lgkmcnt(0)
	ds_store_b32 v6, v2 offset:16896
.LBB0_7:
	s_or_b32 exec_lo, exec_lo, s3
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_b32 v1, v1 offset:17672
	s_mov_b32 s3, exec_lo
	s_waitcnt lgkmcnt(0)
	v_cmpx_lt_i32_e64 v0, v1
	s_cbranch_execz .LBB0_11
; %bb.8:
	v_lshlrev_b32_e32 v6, 2, v0
	ds_load_b32 v7, v6 offset:16896
	s_waitcnt lgkmcnt(0)
	v_ashrrev_i32_e32 v8, 31, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[7:8]
	v_add_co_u32 v2, vcc_lo, s6, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_add_co_ci_u32_e32 v3, vcc_lo, s7, v3, vcc_lo
	s_add_i32 s6, s11, -1
	v_cmp_ne_u32_e32 vcc_lo, s6, v7
	v_mov_b32_e32 v7, s10
	global_load_b32 v1, v[2:3], off
	s_and_saveexec_b32 s6, vcc_lo
	s_cbranch_execz .LBB0_10
; %bb.9:
	global_load_b32 v7, v[2:3], off offset:4
.LBB0_10:
	s_or_b32 exec_lo, exec_lo, s6
	s_waitcnt vmcnt(0)
	v_ashrrev_i32_e32 v2, 31, v1
	v_sub_nc_u32_e32 v7, v7, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 2, v[1:2]
	v_add_co_u32 v1, vcc_lo, s4, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v2, vcc_lo, s5, v3, vcc_lo
	v_lshlrev_b32_e32 v3, 3, v0
	ds_store_b32 v6, v7 offset:17152
	ds_store_b64 v3, v[1:2] offset:16384
.LBB0_11:                               ; %Flow128
	s_or_b32 exec_lo, exec_lo, s3
	v_mov_b32_e32 v1, 0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_b32 v2, v1 offset:17672
	s_waitcnt lgkmcnt(0)
	v_cmp_gt_i32_e32 vcc_lo, 1, v2
	s_cbranch_vccnz .LBB0_16
; %bb.12:                               ; %.lr.ph.preheader
	v_mov_b32_e32 v3, v5
	s_movk_i32 s3, 0x4300
	s_movk_i32 s4, 0x4000
.LBB0_13:                               ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	v_mov_b32_e32 v6, s3
	s_mov_b32 s5, exec_lo
	ds_load_b32 v6, v6
	s_waitcnt lgkmcnt(0)
	v_cmpx_lt_i32_e64 v0, v6
	s_cbranch_execz .LBB0_15
; %bb.14:                               ;   in Loop: Header=BB0_13 Depth=1
	v_mov_b32_e32 v6, s4
	ds_load_b64 v[6:7], v6
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v6, vcc_lo, v6, v5
	v_add_co_ci_u32_e32 v7, vcc_lo, 0, v7, vcc_lo
	flat_load_b32 v6, v[6:7]
	s_waitcnt vmcnt(0) lgkmcnt(0)
	ds_store_b32 v3, v6
.LBB0_15:                               ;   in Loop: Header=BB0_13 Depth=1
	s_or_b32 exec_lo, exec_lo, s5
	v_add_nc_u32_e32 v2, -1, v2
	v_add_nc_u32_e32 v3, 0x100, v3
	s_add_i32 s3, s3, 4
	s_add_i32 s4, s4, 8
	s_delay_alu instid0(VALU_DEP_2)
	v_cmp_eq_u32_e32 vcc_lo, 0, v2
	s_cbranch_vccz .LBB0_13
.LBB0_16:                               ; %._crit_edge
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_b32 v1, v1 offset:17672
	s_mov_b32 s5, exec_lo
	s_waitcnt lgkmcnt(0)
	v_cmpx_lt_i32_e64 v0, v1
	s_cbranch_execz .LBB0_24
; %bb.17:                               ; %.preheader82
	v_cmp_eq_u32_e32 vcc_lo, 0, v1
	v_mov_b32_e32 v2, 0
	s_mov_b32 s6, 0
	s_cbranch_vccnz .LBB0_23
; %bb.18:                               ; %.lr.ph87
	ds_load_b32 v3, v5 offset:17152
	v_dual_mov_b32 v2, 0 :: v_dual_mov_b32 v5, 0
	v_mov_b32_e32 v6, 0
                                        ; implicit-def: $sgpr7
.LBB0_19:                               ; =>This Inner Loop Header: Depth=1
	s_or_b32 s7, s7, exec_lo
	s_mov_b32 s9, exec_lo
	s_waitcnt lgkmcnt(0)
	v_cmpx_ne_u32_e64 v5, v3
	s_cbranch_execz .LBB0_21
; %bb.20:                               ;   in Loop: Header=BB0_19 Depth=1
	v_lshlrev_b32_e32 v7, 2, v5
	v_lshlrev_b32_e32 v8, 2, v6
	s_delay_alu instid0(VALU_DEP_2)
	v_lshl_add_u32 v7, v0, 8, v7
	ds_load_b32 v8, v8 offset:16896
	ds_load_b32 v7, v7
	s_waitcnt lgkmcnt(0)
	v_cmp_eq_u32_e32 vcc_lo, v8, v7
	v_cmp_lt_i32_e64 s3, v8, v7
	v_cmp_gt_i32_e64 s4, v8, v7
	v_cndmask_b32_e64 v9, 0, 1, vcc_lo
	v_add_co_ci_u32_e32 v2, vcc_lo, 0, v2, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_ci_u32_e64 v6, s3, v6, v9, s3
	v_add_co_ci_u32_e64 v5, s4, v5, v9, s4
	s_and_not1_b32 s4, s7, exec_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_eq_u32_e64 s3, v6, v1
	s_and_b32 s3, s3, exec_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 s7, s4, s3
.LBB0_21:                               ; %Flow122
                                        ;   in Loop: Header=BB0_19 Depth=1
	s_or_b32 exec_lo, exec_lo, s9
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s3, exec_lo, s7
	s_or_b32 s6, s3, s6
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s6
	s_cbranch_execnz .LBB0_19
; %bb.22:                               ; %Flow123
	s_or_b32 exec_lo, exec_lo, s6
.LBB0_23:                               ; %._crit_edge88
	ds_store_b32 v4, v2
.LBB0_24:                               ; %Flow125
	s_or_b32 exec_lo, exec_lo, s5
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_and_saveexec_b32 s3, s2
	s_cbranch_execz .LBB0_29
; %bb.25:                               ; %.preheader
	v_mov_b32_e32 v0, 0
	ds_load_b32 v1, v0 offset:17672
	s_waitcnt lgkmcnt(0)
	v_cmp_gt_i32_e32 vcc_lo, 1, v1
	s_cbranch_vccnz .LBB0_28
; %bb.26:                               ; %.lr.ph93.preheader
	v_mov_b32_e32 v0, 0
	s_movk_i32 s2, 0x4400
.LBB0_27:                               ; %.lr.ph93
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(SALU_CYCLE_1)
	v_dual_mov_b32 v2, s2 :: v_dual_add_nc_u32 v1, -1, v1
	s_add_i32 s2, s2, 4
	ds_load_b32 v2, v2
	v_cmp_eq_u32_e32 vcc_lo, 0, v1
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v0, v2, v0
	s_cbranch_vccz .LBB0_27
.LBB0_28:                               ; %._crit_edge94
	s_load_b64 s[0:1], s[0:1], 0x18
	s_ashr_i32 s9, s8, 31
	v_mov_b32_e32 v1, 0
	s_lshl_b64 s[4:5], s[8:9], 2
	s_waitcnt lgkmcnt(0)
	s_add_u32 s0, s0, s4
	s_addc_u32 s1, s1, s5
	global_store_b32 v1, v0, s[0:1]
.LBB0_29:                               ; %Flow121
	s_or_b32 exec_lo, exec_lo, s3
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
.LBB0_30:
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z17devTrianglesCountPiS_iiS_
		.amdhsa_group_segment_fixed_size 17680
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.amdhsa_user_sgpr_count 13
		.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 1
		.amdhsa_system_sgpr_workgroup_id_z 1
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 10
		.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	_Z17devTrianglesCountPiS_iiS_, .Lfunc_end0-_Z17devTrianglesCountPiS_iiS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1132
; NumSgprs: 18
; NumVgprs: 10
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 17680 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 10
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 13
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 1
; 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
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .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: 17680
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z17devTrianglesCountPiS_iiS_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z17devTrianglesCountPiS_iiS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     10
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
