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

#define LSIZE 31
#define MIN_LIM 12.0
#define MAX_LIM 30.0

void check_input(int argc,char* argv[]);
__global__ void examine(float *d_coordinates,int *d_coords_within,int d_lines);
long calc_lines(char *filename);

int main(int argc,char * argv[])
{
  check_input(argc,argv);             // Check cmd inputs
  char *filename=argv[3];             // Variable initialization
  int coll = atoi(argv[1]);
  int exec_time=atoi(argv[2]);
  int threads=atoi(argv[4]);
  int BLOCKSIZE = atoi(argv[5]);
  long loop_count;
  loop_count =calc_lines(filename);						// Count the lines of input file
  FILE *input=fopen(filename,"r");                        // Open file with file descriptor
  struct cudaDeviceProp prop;
  cudaGetDeviceProperties(&prop,0);                       // Get gpu's properties information
  if(coll != -1)													// Handle max_collisions argument
  {
    if(coll>loop_count)
    {
      printf("[!] Warning: Specified collisions to be tested exceed the ones in input file\n");
      printf("[!] Setting the number of collisions to the maximum (taken from input file)\n");
    }
    else
    {
      if (coll<0) return 1;
      loop_count = coll;
    }
  }
  if (BLOCKSIZE==-1)                      // Handle blocksize argument
  {
      BLOCKSIZE=512;                      // A default value
  }
  else
  {
    if (BLOCKSIZE%prop.warpSize!=0 || BLOCKSIZE<=0)
    {
      printf("[-]Block_size must be a positive multiple of gpu's warp_size %d \n",prop.warpSize );
      return 5;
    }
  }
  if (threads!=-1)                        // Handle threads argument
  {
    if (threads<=0) return 4;
    if (threads%BLOCKSIZE!=0)
    {
      threads=(threads/BLOCKSIZE)*BLOCKSIZE;
    }
  }
  else
  {
    threads=prop.maxThreadsPerMultiProcessor*prop.multiProcessorCount;
  }
  // Print some information [ Usefull for debugging ]
  printf("[+] GPU-model: %s\tTotal GPU memory %ld MB \n",prop.name,prop.totalGlobalMem/(1024*1024) );
  printf("[!] You are trying to allocate %ld MBs of memmory on CPU-RAM and GPU-GlobalMem\n",threads*3*sizeof(float)/(1024*1024) );
  printf("[+] Launching %d GPU-Threads with BlockSize %d\n",threads,BLOCKSIZE );
  // Initialize CUDA WallClock-time counters as events
  cudaEvent_t start,stop;
  cudaEventCreate(&start);
  cudaEventCreate(&stop);

  dim3 blockSize(BLOCKSIZE);              // Declare CUDA Block size explicitly
  dim3 gridSize(threads/BLOCKSIZE);       // Declare CUDA Grid size explicitly
  float *h_coordinates=(float * )malloc(3*threads*sizeof(float));    // allocate Host memmory for elements to be read from file
  float *d_coordinates;
  int *d_coords_within,*h_coords_within=(int*)malloc(sizeof(int));    // allocate Host memmory for the counter of coordinates in area of interest
  *h_coords_within=0;
                                                          // Allocate memmory on CUDA capable Device for:
  cudaMalloc(&d_coordinates,3*threads*sizeof(float));     // input file's coordinates
  cudaMalloc(&d_coords_within,sizeof(int));               // coordinates counter

  cudaMemcpy(d_coords_within,h_coords_within,sizeof(int),cudaMemcpyHostToDevice);   // Initialize the value of cuounter on Device
  int i,j=0;
  float time_elapsed = 0;
  printf("[+] Working...\n" );
  cudaEventRecord(start);           // Starting time reference
  while(j<loop_count && (exec_time==-1?1:time_elapsed<exec_time))               // Main loop of the programm
  {
    if (j+threads>loop_count)
    {
      threads=loop_count-j;
      cudaFree(d_coordinates);
      cudaMalloc(&d_coordinates,3*threads*sizeof(float));
    }
    for(i=0;i<threads;i++)
    {
      fscanf(input,"%f %f %f",&h_coordinates[i*3],&h_coordinates[i*3+1],&h_coordinates[i*3+2]);   // Read cooordinates from file
    }
    cudaMemcpy(d_coordinates,h_coordinates,3*threads*sizeof(float),cudaMemcpyHostToDevice);       // Copy read cooordinates on Device
    examine<<<gridSize,blockSize>>>(d_coordinates,d_coords_within,3*threads);                     // Launch gpu kernel for calculations
    cudaEventRecord(stop);                               // Stop time reference
    cudaEventSynchronize(stop);                         // Block CPU until "stop" event is recorded
    cudaEventElapsedTime(&time_elapsed, start, stop);  // Calculate the time elapsed in milliseconds
    time_elapsed=time_elapsed/1000;                    // Convert milliseconds to seconds
    j+=threads;
  }
  // Destroy CUDA timers
  cudaEventDestroy(start);
  cudaEventDestroy(stop);

  cudaMemcpy(h_coords_within,d_coords_within,sizeof(int),cudaMemcpyDeviceToHost);   // Copy results from Device to Host

  //Printing results
  printf("[+] Main part of the program was being executed for :: %.3f :: sec)\n", time_elapsed);
  printf("[+] %ld coordinates have been analyzed\n[+] %d cooordinates were inside the area of interest\n[+] %ld coordinates read per second\n", loop_count, *h_coords_within, (time_elapsed<1?loop_count:loop_count/(int)time_elapsed));

  // Free Host and Device memory
  cudaFree(d_coordinates);
  cudaFree(d_coords_within);
  fclose(input);
  free(h_coordinates);
  free(h_coords_within);

  return 0;
}
__global__ void examine(float *d_coordinates,int *d_coords_within,int d_lines)
{
    int index=blockIdx.x*3*blockDim.x+3*threadIdx.x;                                                 // find the index of starting element for each thread on each block
    float coord1=d_coordinates[index],coord2=d_coordinates[index+1],coord3=d_coordinates[index+2];  // Copy cooordinates from GPU's global memory to thread's local memory
    if(index>=d_lines) return;
    if(coord1 >= MIN_LIM && coord1 <= MAX_LIM && coord2 >= MIN_LIM && coord2 <= MAX_LIM && coord3 >= MIN_LIM && coord3 <= MAX_LIM)
    {
      	                                                       // If the current coordinate is within the accepted limits,
              atomicAdd((unsigned int*)d_coords_within,1);    // So as threads do not mess up the values
    }
}
void check_input(int argc,char *argv[]) 																		// Handle number of arguments errors and show usage
{
	if (argc<6 || argc>6)
	{
		printf("[-] Usage: ./examine [max_collisions] [max_exec_time] [input_file] [Threads] [1D_blockSize]\nUse \"-1\": for no boundies \n");
		if (argc==2) if (!strcmp(argv[1],"--help"))
    {
      printf("max_collisions: Maximum number of collisions\nmax_exec_time: Maximum execution time\ninput_file: Filename to examine\nThreads: Number of gpu-threads to use / # Rows in memmory\n1D_blocksize: gpu-blocksize to use" );
      printf("\t ======Usefull info!======\n");
      printf("1) 1D_blockSize must be a multiple of 32. (or whatever warp_size is supported by your GPU)\n2) Threads should be a multiple of blockSize\n 3)These 2 parameters are important for performance\n" );
    }
		exit(2);
	}
}
long calc_lines(char *filename) 																						// Calculates the lines of input file
{
	FILE *file=fopen(filename,"r");
	fseek(file,0L,SEEK_END);															//set file position indicator right to the end-of-file
	long lines=ftell(file);																//store the number of bytes since the beginning of the file
	fseek(file,0L,SEEK_SET);
	fclose(file);
	return lines/LSIZE;																			//return lines count of the file
}

	.file	"tmpxft_00290bd8_00000000-6_examine.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"[-] Usage: ./examine [max_collisions] [max_exec_time] [input_file] [Threads] [1D_blockSize]\nUse \"-1\": for no boundies \n"
.LC1:
	.string	"--help"
.LC2:
	.string	"max_collisions: Maximum number of collisions\nmax_exec_time: Maximum execution time\ninput_file: Filename to examine\nThreads: Number of gpu-threads to use / # Rows in memmory\n1D_blocksize: gpu-blocksize to use"
.LC3:
	.string	"\t ======Usefull info!======\n"
.LC4:
	.string	"1) 1D_blockSize must be a multiple of 32. (or whatever warp_size is supported by your GPU)\n2) Threads should be a multiple of blockSize\n 3)These 2 parameters are important for performance\n"
	.text
	.globl	_Z11check_inputiPPc
	.type	_Z11check_inputiPPc, @function
_Z11check_inputiPPc:
.LFB2028:
	.cfi_startproc
	endbr64
	cmpl	$6, %edi
	je	.L2
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsi, %rbp
	leaq	.LC0(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	%edi, %ebx
	movl	$2, %edi
	pushq	%rax
	.cfi_def_cfa_offset 32
	xorl	%eax, %eax
	call	__printf_chk@PLT
	cmpl	$2, %ebx
	jne	.L4
	movq	8(%rbp), %rdi
	leaq	.LC1(%rip), %rsi
	call	strcmp@PLT
	testl	%eax, %eax
	jne	.L4
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L4:
	movl	$2, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	.cfi_restore 3
	.cfi_restore 6
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z11check_inputiPPc, .-_Z11check_inputiPPc
	.section	.rodata.str1.1
.LC5:
	.string	"r"
	.text
	.globl	_Z10calc_linesPc
	.type	_Z10calc_linesPc, @function
_Z10calc_linesPc:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	.LC5(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	pushq	%rax
	.cfi_def_cfa_offset 32
	call	fopen@PLT
	movl	$2, %edx
	xorl	%esi, %esi
	movq	%rax, %rbp
	movq	%rax, %rdi
	call	fseek@PLT
	movq	%rbp, %rdi
	call	ftell@PLT
	xorl	%edx, %edx
	movq	%rbp, %rdi
	xorl	%esi, %esi
	movq	%rax, %rbx
	call	fseek@PLT
	movq	%rbp, %rdi
	call	fclose@PLT
	movq	%rbx, %rax
	movl	$31, %ecx
	cqto
	idivq	%rcx
	popq	%rdx
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z10calc_linesPc, .-_Z10calc_linesPc
	.globl	_Z29__device_stub__Z7examinePfPiiPfPii
	.type	_Z29__device_stub__Z7examinePfPiiPfPii, @function
_Z29__device_stub__Z7examinePfPiiPfPii:
.LFB2054:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L11
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z7examinePfPii(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L11:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2054:
	.size	_Z29__device_stub__Z7examinePfPiiPfPii, .-_Z29__device_stub__Z7examinePfPiiPfPii
	.globl	_Z7examinePfPii
	.type	_Z7examinePfPii, @function
_Z7examinePfPii:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_Z29__device_stub__Z7examinePfPiiPfPii
	.cfi_endproc
.LFE2055:
	.size	_Z7examinePfPii, .-_Z7examinePfPii
	.section	.rodata.str1.1
.LC6:
	.string	"[!] Warning: Specified collisions to be tested exceed the ones in input file\n"
.LC7:
	.string	"[!] Setting the number of collisions to the maximum (taken from input file)\n"
.LC8:
	.string	"[-]Block_size must be a positive multiple of gpu's warp_size %d \n"
.LC9:
	.string	"[+] GPU-model: %s\tTotal GPU memory %ld MB \n"
.LC10:
	.string	"[!] You are trying to allocate %ld MBs of memmory on CPU-RAM and GPU-GlobalMem\n"
.LC11:
	.string	"[+] Launching %d GPU-Threads with BlockSize %d\n"
.LC13:
	.string	"[+] Working...\n"
.LC14:
	.string	"%f %f %f"
.LC16:
	.string	"[+] Main part of the program was being executed for :: %.3f :: sec)\n"
.LC18:
	.string	"[+] %ld coordinates have been analyzed\n[+] %d cooordinates were inside the area of interest\n[+] %ld coordinates read per second\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	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	$1144, %rsp
	.cfi_def_cfa_offset 1200
	movq	%fs:40, %rax
	movq	%rax, 1128(%rsp)
	xorl	%eax, %eax
	call	_Z11check_inputiPPc
	movq	8(%rbp), %rdi
	movq	24(%rbp), %r15
	call	atoi@PLT
	movq	16(%rbp), %rdi
	movl	%eax, 8(%rsp)
	call	atoi@PLT
	movq	32(%rbp), %rdi
	movl	%eax, %r14d
	call	atoi@PLT
	movq	40(%rbp), %rdi
	movl	%eax, %ebx
	call	atoi@PLT
	movq	%r15, %rdi
	movl	%eax, %r12d
	call	_Z10calc_linesPc
	movq	%r15, %rdi
	leaq	.LC5(%rip), %rsi
	leaq	96(%rsp), %r15
	movq	%rax, %r13
	call	fopen@PLT
	xorl	%esi, %esi
	movq	%r15, %rdi
	movq	%rax, 16(%rsp)
	call	cudaGetDeviceProperties_v2@PLT
	movl	8(%rsp), %edx
	cmpl	$-1, %edx
	je	.L17
	movslq	%edx, %rbp
	cmpq	%r13, %rbp
	jle	.L18
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L17:
	movq	%r13, %rbp
	jmp	.L19
.L18:
	movl	$1, %eax
	testl	%edx, %edx
	js	.L16
.L19:
	cmpl	$-1, %r12d
	je	.L37
	movl	%r12d, %eax
	movl	404(%rsp), %ecx
	cltd
	idivl	%ecx
	testl	%edx, %edx
	jne	.L40
	testl	%r12d, %r12d
	jg	.L21
.L40:
	movl	%ecx, %edx
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$5, %eax
	jmp	.L16
.L37:
	movl	$512, %r12d
.L21:
	cmpl	$-1, %ebx
	je	.L23
	movl	$4, %eax
	testl	%ebx, %ebx
	jle	.L16
	movl	%ebx, %eax
	cltd
	idivl	%r12d
	testl	%edx, %edx
	je	.L24
	imull	%r12d, %eax
	movl	%eax, %ebx
	jmp	.L24
.L23:
	movl	720(%rsp), %ebx
	imull	484(%rsp), %ebx
.L24:
	leal	(%rbx,%rbx,2), %r13d
	movq	%r15, %rdx
	leaq	.LC9(%rip), %rsi
	xorl	%eax, %eax
	movq	384(%rsp), %rcx
	movslq	%r13d, %r13
	movl	$2, %edi
	salq	$2, %r13
	shrq	$20, %rcx
	call	__printf_chk@PLT
	movq	%r13, %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	shrq	$20, %rdx
	leaq	.LC10(%rip), %rsi
	call	__printf_chk@PLT
	leaq	.LC11(%rip), %rsi
	movl	%ebx, %edx
	xorl	%eax, %eax
	movl	%r12d, %ecx
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	40(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	48(%rsp), %rdi
	call	cudaEventCreate@PLT
	movl	%ebx, %eax
	movq	%r13, %rdi
	movl	%r12d, 72(%rsp)
	movabsq	$4294967297, %rcx
	cltd
	idivl	%r12d
	movq	%rcx, 76(%rsp)
	movq	%rcx, 88(%rsp)
	movl	%eax, 84(%rsp)
	call	malloc@PLT
	movl	$4, %edi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	movq	%r13, %rsi
	leaq	56(%rsp), %rdi
	xorl	%r13d, %r13d
	movq	%rax, %r12
	xorl	%eax, %eax
	movl	%eax, (%r12)
	call	cudaMalloc@PLT
	leaq	64(%rsp), %rdi
	movl	$4, %esi
	call	cudaMalloc@PLT
	movq	64(%rsp), %rdi
	movl	$1, %ecx
	movq	%r12, %rsi
	movl	$4, %edx
	call	cudaMemcpy@PLT
	leaq	.LC13(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	movl	$0x00000000, 36(%rsp)
	call	__printf_chk@PLT
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
.L25:
	movslq	%r13d, %rax
	cmpq	%rbp, %rax
	jl	.L30
.L34:
	movq	40(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	48(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	64(%rsp), %rsi
	movl	$2, %ecx
	movq	%r12, %rdi
	movl	$4, %edx
	call	cudaMemcpy@PLT
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	36(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	%rbp, %r8
	movss	36(%rsp), %xmm0
	movss	.LC17(%rip), %xmm1
	comiss	%xmm0, %xmm1
	ja	.L31
	cvttss2sil	%xmm0, %ecx
	movq	%rbp, %rax
	cqto
	movslq	%ecx, %rcx
	idivq	%rcx
	movq	%rax, %r8
	jmp	.L31
.L30:
	cmpl	$-1, %r14d
	jne	.L55
.L33:
	leal	(%rbx,%r13), %eax
	cltq
	cmpq	%rbp, %rax
	jle	.L26
	movq	56(%rsp), %rdi
	movl	%ebp, %ebx
	subl	%r13d, %ebx
	call	cudaFree@PLT
	leal	(%rbx,%rbx,2), %esi
	leaq	56(%rsp), %rdi
	movslq	%esi, %rsi
	salq	$2, %rsi
	call	cudaMalloc@PLT
.L26:
	movq	8(%rsp), %r15
	xorl	%r9d, %r9d
.L27:
	cmpl	%r9d, %ebx
	jle	.L56
	movq	16(%rsp), %rdi
	leaq	4(%r15), %rcx
	leaq	8(%r15), %r8
	movq	%r15, %rdx
	leaq	.LC14(%rip), %rsi
	xorl	%eax, %eax
	movl	%r9d, 28(%rsp)
	addq	$12, %r15
	call	__isoc23_fscanf@PLT
	movl	28(%rsp), %r9d
	incl	%r9d
	jmp	.L27
.L56:
	leal	(%rbx,%rbx,2), %r15d
	movq	8(%rsp), %rsi
	movq	56(%rsp), %rdi
	movl	$1, %ecx
	movslq	%r15d, %rdx
	salq	$2, %rdx
	call	cudaMemcpy@PLT
	movl	80(%rsp), %ecx
	movl	92(%rsp), %esi
	xorl	%r9d, %r9d
	movq	72(%rsp), %rdx
	movq	84(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L29
	movq	64(%rsp), %rsi
	movq	56(%rsp), %rdi
	movl	%r15d, %edx
	call	_Z29__device_stub__Z7examinePfPiiPfPii
.L29:
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	addl	%ebx, %r13d
	call	cudaEventRecord@PLT
	movq	48(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	48(%rsp), %rdx
	movq	40(%rsp), %rsi
	leaq	36(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movss	36(%rsp), %xmm0
	divss	.LC15(%rip), %xmm0
	movss	%xmm0, 36(%rsp)
	jmp	.L25
.L55:
	cvtsi2ssl	%r14d, %xmm0
	comiss	36(%rsp), %xmm0
	ja	.L33
	jmp	.L34
.L31:
	movl	(%r12), %ecx
	movq	%rbp, %rdx
	leaq	.LC18(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	56(%rsp), %rdi
	call	cudaFree@PLT
	movq	64(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	fclose@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	xorl	%eax, %eax
.L16:
	movq	1128(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L35
	call	__stack_chk_fail@PLT
.L35:
	addq	$1144, %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
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC19:
	.string	"_Z7examinePfPii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC19(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z7examinePfPii(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2057:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC15:
	.long	1148846080
	.align 4
.LC17:
	.long	1065353216
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


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

#define LSIZE 31
#define MIN_LIM 12.0
#define MAX_LIM 30.0

void check_input(int argc,char* argv[]);
__global__ void examine(float *d_coordinates,int *d_coords_within,int d_lines);
long calc_lines(char *filename);

int main(int argc,char * argv[])
{
  check_input(argc,argv);             // Check cmd inputs
  char *filename=argv[3];             // Variable initialization
  int coll = atoi(argv[1]);
  int exec_time=atoi(argv[2]);
  int threads=atoi(argv[4]);
  int BLOCKSIZE = atoi(argv[5]);
  long loop_count;
  loop_count =calc_lines(filename);						// Count the lines of input file
  FILE *input=fopen(filename,"r");                        // Open file with file descriptor
  struct hipDeviceProp_t prop;
  hipGetDeviceProperties(&prop,0);                       // Get gpu's properties information
  if(coll != -1)													// Handle max_collisions argument
  {
    if(coll>loop_count)
    {
      printf("[!] Warning: Specified collisions to be tested exceed the ones in input file\n");
      printf("[!] Setting the number of collisions to the maximum (taken from input file)\n");
    }
    else
    {
      if (coll<0) return 1;
      loop_count = coll;
    }
  }
  if (BLOCKSIZE==-1)                      // Handle blocksize argument
  {
      BLOCKSIZE=512;                      // A default value
  }
  else
  {
    if (BLOCKSIZE%prop.warpSize!=0 || BLOCKSIZE<=0)
    {
      printf("[-]Block_size must be a positive multiple of gpu's warp_size %d \n",prop.warpSize );
      return 5;
    }
  }
  if (threads!=-1)                        // Handle threads argument
  {
    if (threads<=0) return 4;
    if (threads%BLOCKSIZE!=0)
    {
      threads=(threads/BLOCKSIZE)*BLOCKSIZE;
    }
  }
  else
  {
    threads=prop.maxThreadsPerMultiProcessor*prop.multiProcessorCount;
  }
  // Print some information [ Usefull for debugging ]
  printf("[+] GPU-model: %s\tTotal GPU memory %ld MB \n",prop.name,prop.totalGlobalMem/(1024*1024) );
  printf("[!] You are trying to allocate %ld MBs of memmory on CPU-RAM and GPU-GlobalMem\n",threads*3*sizeof(float)/(1024*1024) );
  printf("[+] Launching %d GPU-Threads with BlockSize %d\n",threads,BLOCKSIZE );
  // Initialize CUDA WallClock-time counters as events
  hipEvent_t start,stop;
  hipEventCreate(&start);
  hipEventCreate(&stop);

  dim3 blockSize(BLOCKSIZE);              // Declare CUDA Block size explicitly
  dim3 gridSize(threads/BLOCKSIZE);       // Declare CUDA Grid size explicitly
  float *h_coordinates=(float * )malloc(3*threads*sizeof(float));    // allocate Host memmory for elements to be read from file
  float *d_coordinates;
  int *d_coords_within,*h_coords_within=(int*)malloc(sizeof(int));    // allocate Host memmory for the counter of coordinates in area of interest
  *h_coords_within=0;
                                                          // Allocate memmory on CUDA capable Device for:
  hipMalloc(&d_coordinates,3*threads*sizeof(float));     // input file's coordinates
  hipMalloc(&d_coords_within,sizeof(int));               // coordinates counter

  hipMemcpy(d_coords_within,h_coords_within,sizeof(int),hipMemcpyHostToDevice);   // Initialize the value of cuounter on Device
  int i,j=0;
  float time_elapsed = 0;
  printf("[+] Working...\n" );
  hipEventRecord(start);           // Starting time reference
  while(j<loop_count && (exec_time==-1?1:time_elapsed<exec_time))               // Main loop of the programm
  {
    if (j+threads>loop_count)
    {
      threads=loop_count-j;
      hipFree(d_coordinates);
      hipMalloc(&d_coordinates,3*threads*sizeof(float));
    }
    for(i=0;i<threads;i++)
    {
      fscanf(input,"%f %f %f",&h_coordinates[i*3],&h_coordinates[i*3+1],&h_coordinates[i*3+2]);   // Read cooordinates from file
    }
    hipMemcpy(d_coordinates,h_coordinates,3*threads*sizeof(float),hipMemcpyHostToDevice);       // Copy read cooordinates on Device
    examine<<<gridSize,blockSize>>>(d_coordinates,d_coords_within,3*threads);                     // Launch gpu kernel for calculations
    hipEventRecord(stop);                               // Stop time reference
    hipEventSynchronize(stop);                         // Block CPU until "stop" event is recorded
    hipEventElapsedTime(&time_elapsed, start, stop);  // Calculate the time elapsed in milliseconds
    time_elapsed=time_elapsed/1000;                    // Convert milliseconds to seconds
    j+=threads;
  }
  // Destroy CUDA timers
  hipEventDestroy(start);
  hipEventDestroy(stop);

  hipMemcpy(h_coords_within,d_coords_within,sizeof(int),hipMemcpyDeviceToHost);   // Copy results from Device to Host

  //Printing results
  printf("[+] Main part of the program was being executed for :: %.3f :: sec)\n", time_elapsed);
  printf("[+] %ld coordinates have been analyzed\n[+] %d cooordinates were inside the area of interest\n[+] %ld coordinates read per second\n", loop_count, *h_coords_within, (time_elapsed<1?loop_count:loop_count/(int)time_elapsed));

  // Free Host and Device memory
  hipFree(d_coordinates);
  hipFree(d_coords_within);
  fclose(input);
  free(h_coordinates);
  free(h_coords_within);

  return 0;
}
__global__ void examine(float *d_coordinates,int *d_coords_within,int d_lines)
{
    int index=blockIdx.x*3*blockDim.x+3*threadIdx.x;                                                 // find the index of starting element for each thread on each block
    float coord1=d_coordinates[index],coord2=d_coordinates[index+1],coord3=d_coordinates[index+2];  // Copy cooordinates from GPU's global memory to thread's local memory
    if(index>=d_lines) return;
    if(coord1 >= MIN_LIM && coord1 <= MAX_LIM && coord2 >= MIN_LIM && coord2 <= MAX_LIM && coord3 >= MIN_LIM && coord3 <= MAX_LIM)
    {
      	                                                       // If the current coordinate is within the accepted limits,
              atomicAdd((unsigned int*)d_coords_within,1);    // So as threads do not mess up the values
    }
}
void check_input(int argc,char *argv[]) 																		// Handle number of arguments errors and show usage
{
	if (argc<6 || argc>6)
	{
		printf("[-] Usage: ./examine [max_collisions] [max_exec_time] [input_file] [Threads] [1D_blockSize]\nUse \"-1\": for no boundies \n");
		if (argc==2) if (!strcmp(argv[1],"--help"))
    {
      printf("max_collisions: Maximum number of collisions\nmax_exec_time: Maximum execution time\ninput_file: Filename to examine\nThreads: Number of gpu-threads to use / # Rows in memmory\n1D_blocksize: gpu-blocksize to use" );
      printf("\t ======Usefull info!======\n");
      printf("1) 1D_blockSize must be a multiple of 32. (or whatever warp_size is supported by your GPU)\n2) Threads should be a multiple of blockSize\n 3)These 2 parameters are important for performance\n" );
    }
		exit(2);
	}
}
long calc_lines(char *filename) 																						// Calculates the lines of input file
{
	FILE *file=fopen(filename,"r");
	fseek(file,0L,SEEK_END);															//set file position indicator right to the end-of-file
	long lines=ftell(file);																//store the number of bytes since the beginning of the file
	fseek(file,0L,SEEK_SET);
	fclose(file);
	return lines/LSIZE;																			//return lines count of the file
}

	.text
	.file	"examine.hip"
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI0_0:
	.long	0x447a0000                      # float 1000
.LCPI0_1:
	.long	0x3f800000                      # float 1
	.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	$1560, %rsp                     # imm = 0x618
	.cfi_def_cfa_offset 1616
	.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, %r14
	callq	_Z11check_inputiPPc
	movq	8(%r14), %rdi
	movq	24(%r14), %rbx
	callq	atoi
	movl	%eax, %ebp
	movq	16(%r14), %rdi
	callq	atoi
	movl	%eax, 20(%rsp)                  # 4-byte Spill
	movq	32(%r14), %rdi
	callq	atoi
	movl	%eax, %r15d
	movq	40(%r14), %rdi
	callq	atoi
	movl	%eax, %r13d
	movq	%rbx, %rdi
	callq	_Z10calc_linesPc
	movq	%rax, %r12
	movl	$.L.str, %esi
	movq	%rbx, %rdi
	callq	fopen
	movq	%rax, %rbx
	leaq	88(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	cmpl	$-1, %ebp
	je	.LBB0_1
# %bb.7:
	movslq	%ebp, %r14
	cmpq	%r14, %r12
	jge	.LBB0_9
# %bb.8:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.Lstr.1, %edi
	callq	puts@PLT
.LBB0_1:
	movq	%r12, %r14
	jmp	.LBB0_2
.LBB0_9:
	testl	%ebp, %ebp
	js	.LBB0_10
.LBB0_2:
	cmpl	$-1, %r13d
	je	.LBB0_3
# %bb.11:
	movl	396(%rsp), %esi
	movl	%r13d, %eax
	cltd
	idivl	%esi
	testl	%r13d, %r13d
	jle	.LBB0_13
# %bb.12:
	testl	%edx, %edx
	je	.LBB0_4
.LBB0_13:
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$5, %ebp
	jmp	.LBB0_31
.LBB0_3:
	movl	$512, %r13d                     # imm = 0x200
.LBB0_4:
	cmpl	$-1, %r15d
	je	.LBB0_15
# %bb.5:
	testl	%r15d, %r15d
	jle	.LBB0_6
# %bb.14:
	movl	%r15d, %eax
	xorl	%edx, %edx
	divl	%r13d
	subl	%edx, %r15d
	jmp	.LBB0_16
.LBB0_15:
	movl	476(%rsp), %r15d
	imull	712(%rsp), %r15d
.LBB0_16:
	leaq	88(%rsp), %rsi
	movq	288(%rsi), %rdx
	shrq	$20, %rdx
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	leal	(%r15,%r15,2), %eax
	movslq	%eax, %rbp
	shlq	$2, %rbp
	movq	%rbp, %rsi
	shrq	$20, %rsi
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.6, %edi
	movl	%r15d, %esi
	movl	%r13d, %edx
	xorl	%eax, %eax
	callq	printf
	leaq	48(%rsp), %rdi
	callq	hipEventCreate
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	movl	%r15d, %eax
	cltd
	idivl	%r13d
                                        # kill: def $eax killed $eax def $rax
	movq	%rax, 64(%rsp)                  # 8-byte Spill
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movl	$4, %edi
	callq	malloc
	movq	%rax, %r12
	xorl	%eax, %eax
	movl	%eax, (%r12)
	leaq	8(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	movl	%r13d, %ebp
	leaq	40(%rsp), %r13
	movl	$4, %esi
	movq	%r13, %rdi
	callq	hipMalloc
	movq	(%r13), %rdi
	movl	$4, %edx
	movq	%r12, 72(%rsp)                  # 8-byte Spill
	movq	%r12, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	xorl	%eax, %eax
	movl	%eax, 4(%rsp)
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	leaq	48(%rsp), %rax
	movq	(%rax), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	testq	%r14, %r14
	jle	.LBB0_28
# %bb.17:                               # %.lr.ph101
	movl	%ebp, %eax
	btsq	$32, %rax
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	btsq	$32, 64(%rsp)                   # 8-byte Folded Spill
	cvtsi2ssl	20(%rsp), %xmm0         # 4-byte Folded Reload
	movss	%xmm0, 60(%rsp)                 # 4-byte Spill
	xorps	%xmm0, %xmm0
	xorl	%r12d, %r12d
.LBB0_18:                               # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_24 Depth 2
	cmpl	$-1, 20(%rsp)                   # 4-byte Folded Reload
	je	.LBB0_20
# %bb.19:                               #   in Loop: Header=BB0_18 Depth=1
	movss	60(%rsp), %xmm1                 # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm0, %xmm1
	jbe	.LBB0_28
.LBB0_20:                               #   in Loop: Header=BB0_18 Depth=1
	leal	(%r15,%r12), %eax
	cltq
	cmpq	%rax, %r14
	jge	.LBB0_22
# %bb.21:                               #   in Loop: Header=BB0_18 Depth=1
	movl	%r14d, %r15d
	subl	%r12d, %r15d
	movq	8(%rsp), %rdi
	callq	hipFree
	leal	(%r15,%r15,2), %eax
	movslq	%eax, %rsi
	shlq	$2, %rsi
	leaq	8(%rsp), %rdi
	callq	hipMalloc
.LBB0_22:                               #   in Loop: Header=BB0_18 Depth=1
	testl	%r15d, %r15d
	jle	.LBB0_25
# %bb.23:                               # %.lr.ph.preheader
                                        #   in Loop: Header=BB0_18 Depth=1
	movl	%r15d, %ebp
	movq	32(%rsp), %r13                  # 8-byte Reload
.LBB0_24:                               # %.lr.ph
                                        #   Parent Loop BB0_18 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leaq	4(%r13), %rcx
	leaq	8(%r13), %r8
	movl	$.L.str.8, %esi
	movq	%rbx, %rdi
	movq	%r13, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	addq	$12, %r13
	decq	%rbp
	jne	.LBB0_24
.LBB0_25:                               # %._crit_edge
                                        #   in Loop: Header=BB0_18 Depth=1
	movq	8(%rsp), %rdi
	leal	(%r15,%r15,2), %r13d
	movslq	%r13d, %rdx
	shlq	$2, %rdx
	movq	32(%rsp), %rsi                  # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	movq	64(%rsp), %rdi                  # 8-byte Reload
	movl	$1, %esi
	movq	80(%rsp), %rdx                  # 8-byte Reload
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB0_27
# %bb.26:                               #   in Loop: Header=BB0_18 Depth=1
	movq	8(%rsp), %rdi
	movq	40(%rsp), %rsi
	movl	%r13d, %edx
	callq	_Z22__device_stub__examinePfPii
.LBB0_27:                               #   in Loop: Header=BB0_18 Depth=1
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	24(%rsp), %rdi
	callq	hipEventSynchronize
	movq	48(%rsp), %rsi
	movq	24(%rsp), %rdx
	leaq	4(%rsp), %rdi
	callq	hipEventElapsedTime
	movss	4(%rsp), %xmm0                  # xmm0 = mem[0],zero,zero,zero
	divss	.LCPI0_0(%rip), %xmm0
	movss	%xmm0, 4(%rsp)
	addl	%r15d, %r12d
	movslq	%r12d, %rax
	cmpq	%rax, %r14
	jg	.LBB0_18
.LBB0_28:                               # %.critedge
	movq	48(%rsp), %rdi
	callq	hipEventDestroy
	movq	24(%rsp), %rdi
	callq	hipEventDestroy
	movq	40(%rsp), %rsi
	movl	$4, %edx
	movq	72(%rsp), %r15                  # 8-byte Reload
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	xorps	%xmm0, %xmm0
	cvtss2sd	4(%rsp), %xmm0
	movl	$.L.str.9, %edi
	movb	$1, %al
	callq	printf
	movl	(%r15), %ecx
	movss	4(%rsp), %xmm0                  # xmm0 = mem[0],zero,zero,zero
	movss	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm0, %xmm1
	movq	%r14, %rax
	ja	.LBB0_30
# %bb.29:
	cvttss2si	%xmm0, %eax
	movslq	%eax, %rsi
	movq	%r14, %rax
	cqto
	idivq	%rsi
.LBB0_30:
	xorl	%ebp, %ebp
	movl	$.L.str.10, %edi
	movq	%r14, %rsi
	movl	%ecx, %edx
	movq	%rax, %rcx
	xorl	%eax, %eax
	callq	printf
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	fclose
	movq	32(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%r15, %rdi
	callq	free
	jmp	.LBB0_31
.LBB0_6:
	movl	$4, %ebp
	jmp	.LBB0_31
.LBB0_10:
	movl	$1, %ebp
.LBB0_31:
	movl	%ebp, %eax
	addq	$1560, %rsp                     # imm = 0x618
	.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_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z11check_inputiPPc             # -- Begin function _Z11check_inputiPPc
	.type	_Z11check_inputiPPc,@function
_Z11check_inputiPPc:                    # @_Z11check_inputiPPc
	.cfi_startproc
# %bb.0:
	cmpl	$6, %edi
	jne	.LBB1_1
# %bb.5:
	retq
.LBB1_1:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movl	%edi, %ebp
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	cmpl	$2, %ebp
	jne	.LBB1_4
# %bb.2:
	movq	8(%rbx), %rdi
	movl	$.L.str.12, %esi
	callq	strcmp
	testl	%eax, %eax
	jne	.LBB1_4
# %bb.3:
	movl	$.L.str.13, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.Lstr.4, %edi
	callq	puts@PLT
	movl	$.Lstr.5, %edi
	callq	puts@PLT
.LBB1_4:
	movl	$2, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z11check_inputiPPc, .Lfunc_end1-_Z11check_inputiPPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z10calc_linesPc                # -- Begin function _Z10calc_linesPc
	.type	_Z10calc_linesPc,@function
_Z10calc_linesPc:                       # @_Z10calc_linesPc
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, %rbx
	movq	%rax, %rdi
	xorl	%esi, %esi
	movl	$2, %edx
	callq	fseek
	movq	%rbx, %rdi
	callq	ftell
	movq	%rax, %r14
	movq	%rbx, %rdi
	xorl	%esi, %esi
	xorl	%edx, %edx
	callq	fseek
	movq	%rbx, %rdi
	callq	fclose
	movabsq	$-8925843906633654007, %rcx     # imm = 0x8421084210842109
	movq	%r14, %rax
	imulq	%rcx
	addq	%rdx, %r14
	movq	%r14, %rax
	shrq	$63, %rax
	sarq	$4, %r14
	addq	%rax, %r14
	movq	%r14, %rax
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z10calc_linesPc, .Lfunc_end2-_Z10calc_linesPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z22__device_stub__examinePfPii # -- Begin function _Z22__device_stub__examinePfPii
	.type	_Z22__device_stub__examinePfPii,@function
_Z22__device_stub__examinePfPii:        # @_Z22__device_stub__examinePfPii
	.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), %rcx
	movq	%rsi, (%rcx)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z7examinePfPii, %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_end3:
	.size	_Z22__device_stub__examinePfPii, .Lfunc_end3-_Z22__device_stub__examinePfPii
	.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	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z7examinePfPii, %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_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB5_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"r"
	.size	.L.str, 2

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"[-]Block_size must be a positive multiple of gpu's warp_size %d \n"
	.size	.L.str.3, 66

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"[+] GPU-model: %s\tTotal GPU memory %ld MB \n"
	.size	.L.str.4, 44

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"[!] You are trying to allocate %ld MBs of memmory on CPU-RAM and GPU-GlobalMem\n"
	.size	.L.str.5, 80

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"[+] Launching %d GPU-Threads with BlockSize %d\n"
	.size	.L.str.6, 48

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"%f %f %f"
	.size	.L.str.8, 9

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

	.type	.L.str.9,@object                # @.str.9
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.9:
	.asciz	"[+] Main part of the program was being executed for :: %.3f :: sec)\n"
	.size	.L.str.9, 69

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"[+] %ld coordinates have been analyzed\n[+] %d cooordinates were inside the area of interest\n[+] %ld coordinates read per second\n"
	.size	.L.str.10, 129

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"--help"
	.size	.L.str.12, 7

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"max_collisions: Maximum number of collisions\nmax_exec_time: Maximum execution time\ninput_file: Filename to examine\nThreads: Number of gpu-threads to use / # Rows in memmory\n1D_blocksize: gpu-blocksize to use"
	.size	.L.str.13, 208

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z7examinePfPii"
	.size	.L__unnamed_1, 16

	.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	"[!] Warning: Specified collisions to be tested exceed the ones in input file"
	.size	.Lstr, 77

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"[!] Setting the number of collisions to the maximum (taken from input file)"
	.size	.Lstr.1, 76

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"[+] Working..."
	.size	.Lstr.2, 15

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"[-] Usage: ./examine [max_collisions] [max_exec_time] [input_file] [Threads] [1D_blockSize]\nUse \"-1\": for no boundies "
	.size	.Lstr.3, 119

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"\t ======Usefull info!======"
	.size	.Lstr.4, 28

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"1) 1D_blockSize must be a multiple of 32. (or whatever warp_size is supported by your GPU)\n2) Threads should be a multiple of blockSize\n 3)These 2 parameters are important for performance"
	.size	.Lstr.5, 188

	.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__examinePfPii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z7examinePfPii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z7examinePfPii         ; -- Begin function _Z7examinePfPii
	.globl	_Z7examinePfPii
	.p2align	8
	.type	_Z7examinePfPii,@function
_Z7examinePfPii:                        ; @_Z7examinePfPii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b32 s3, s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_lshl_add_u32 v0, v1, 1, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i32_e64 s3, v0
	s_cbranch_execz .LBB0_4
; %bb.1:
	s_load_b128 s[4:7], s[0:1], 0x0
	v_ashrrev_i32_e32 v1, 31, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s4, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v1, vcc_lo
	global_load_b96 v[0:2], v[0:1], off
	s_waitcnt vmcnt(0)
	v_cmp_le_f32_e32 vcc_lo, 0x41400000, v0
	v_cmp_le_f32_e64 s0, 0x41400000, v1
	v_cmp_ge_f32_e64 s1, 0x41f00000, v0
	v_cmp_ge_f32_e64 s2, 0x41f00000, v1
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	s_and_b32 s0, vcc_lo, s0
	v_cmp_le_f32_e32 vcc_lo, 0x41400000, v2
	s_and_b32 s1, s0, s1
	v_cmp_ge_f32_e64 s0, 0x41f00000, v2
	s_and_b32 s1, s1, s2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_b32 s1, s1, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	s_and_b32 s0, s1, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_b32 exec_lo, exec_lo, s0
	s_cbranch_execz .LBB0_4
; %bb.2:
	s_mov_b32 s0, exec_lo
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mbcnt_lo_u32_b32 v0, s0, 0
	v_cmp_eq_u32_e32 vcc_lo, 0, v0
	s_and_b32 s1, exec_lo, vcc_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mov_b32 exec_lo, s1
	s_cbranch_execz .LBB0_4
; %bb.3:
	s_bcnt1_i32_b32 s0, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	v_dual_mov_b32 v0, 0 :: v_dual_mov_b32 v1, s0
	global_atomic_add_u32 v0, v1, s[6:7]
.LBB0_4:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z7examinePfPii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 3
		.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	_Z7examinePfPii, .Lfunc_end0-_Z7examinePfPii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 308
; NumSgprs: 18
; NumVgprs: 3
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 3
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .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
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z7examinePfPii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z7examinePfPii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     3
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
