#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/time.h>

// #define NUM_PARTICLES 10000
// #define NUM_ITERATIONS 10000
// int TPB = 16;

#define SEED 10
#define EPSILON 1e-5

typedef struct {
    float3 position;
    float3 velocity;
} Particle;


// Deterministically generates a "random" float, provided a seed and 3 integers.
__host__ __device__ float gen_random(int seed, int a, int b, int c) {
    return (float)((seed * a + b) % c) / c;
}


// Given an array of particles and an index, print that particle.
void printParticle(Particle* particles, int index){
    printf("%f %f %f %f %f %f\n",
    particles[index].position.x, particles[index].position.y, particles[index].position.z,
    particles[index].velocity.x, particles[index].velocity.y, particles[index].velocity.z);
}


// Compare two arrays of Particles. If their position coordinates are all within EPSILON of each other,
// return true, else false.
__host__ bool arraysMatch(Particle* arr1, Particle* arr2, int num_particles)
{
    for (int i = 0; i < num_particles; i++) {
        if (fabs(arr1[i].position.x - arr2[i].position.x) > EPSILON ||
        fabs(arr1[i].position.y - arr2[i].position.y) > EPSILON ||
        fabs(arr1[i].position.z - arr2[i].position.z) > EPSILON)
        return false;
    }
    return true;
}


// Get the current time
double cpuSecond() {
    struct timeval tp;
    gettimeofday(&tp,NULL);
    return ((double)tp.tv_sec + (double)tp.tv_usec*1.e-6);
}


// Replaces the x, y and z values in a float3 to random values between 0 and 1.
void randomizeFloat3(float3* f3) {
    f3->x = (float) rand() / RAND_MAX;
    f3->y = (float) rand() / RAND_MAX;
    f3->z = (float) rand() / RAND_MAX;
}


// Randomizes the position and velocity of all Particles in an array.
void randomizeParticles(Particle* particles, int num_particles) {
    srand(0);
    for (int i = 0; i < num_particles; i++) {
        randomizeFloat3(&particles[i].position);
        randomizeFloat3(&particles[i].velocity);
    }
}


// Updates a particle's position by its velocity, then updates its velocity
__host__ __device__ void updateParticle(Particle* particle, int id, int iter, int num_particles) {
    int dt = 1;

    // update position
    particle->position.x += dt * particle->velocity.x;
    particle->position.y += dt * particle->velocity.y;
    particle->position.z += dt * particle->velocity.z;

    // update the velocity randomly
    particle->velocity.x += gen_random(SEED, id, iter, num_particles);
    particle->velocity.y += gen_random(SEED, id, iter, num_particles);
    particle->velocity.z += gen_random(SEED, id, iter, num_particles);
}


// CPU function that updates a given particle.
void cpu_updatePositionAndVelocity(Particle* particle, int id, int iter, int num_particles) {
    updateParticle(particle, id, iter, num_particles);
}


// Kernel that finds a given Particle's ID then updates it if within range.
__global__ void gpu_updatePositionAndVelocity(Particle* particles, int iter, int num_particles) {
    const int id = blockIdx.x * blockDim.x + threadIdx.x;

    if (id >= num_particles) // If out of bounds, ignore the Particle.
    return;
    else
    updateParticle(&particles[id], id, iter, num_particles);
}


// Perform the update step for all Particles in the array on CPU with a for loop.
void cpu_updateParticles(Particle* particles, int iter, int num_particles) {
    // srand(time(NULL))
    for (int i = 0; i < num_particles; i++) {
        cpu_updatePositionAndVelocity(&particles[i], i, iter, num_particles);
    }
}


// Perform the update step for all Particles in the array by launching GPU kernels.
void gpu_updateParticles(Particle* particles, int iter, int num_particles, int tpb) {
    gpu_updatePositionAndVelocity<<<(num_particles + tpb - 1)/tpb, tpb>>>(particles, iter, num_particles);
}


int main(int argc, char** argv) {
    printf("Running the simulations with the following params:\n");

    if (argc < 5) {
        printf("Usage: ./a NUM_PARTICLES NUM_ITERATIONS TPB INCLUDE_CPU\nExample usage: ./a 10000 10000 32 include_cpu\n");
        return -1;
    }


    // reading the command line arguments, without any kind of error checking
    const int num_particles = (int) strtol(argv[1], NULL, 10); // e.g. 10000 - NULL is the endpointer and 10 is the base
    const int num_iterations = (int) strtol(argv[2], NULL, 10); // e.g. 10000
    const int tpb = (int) strtol(argv[3], NULL, 10); // e.g. 32
    const char* include_cpu = argv[4];
    printf("======== %s: %d, %s: %d, %s: %d\n\n", "num_particles", num_particles, "num_iterations", num_iterations, "tpb", tpb);


    // Declare variables
    Particle *c_particles, *g_particles, *g_result;
    double iStart, iElaps;

    // Initialize array for CPU
    c_particles = (Particle*) malloc(num_particles*sizeof(Particle));
    randomizeParticles(c_particles, num_particles);

    // Initialize array for GPU - particle positions/velocities in device memory are a copy of those in host memory
    // g_result = (Particle*) malloc(num_particles*sizeof(Particle)); // Used to store the result of GPU simulation
    // cudaMallocHost(&g_result, num_particles*sizeof(Particle));
    // cudaMalloc(&g_particles, num_particles*sizeof(Particle));
    cudaMallocManaged(&g_particles, num_particles*sizeof(Particle));

    iStart = cpuSecond();

    memcpy(g_particles, c_particles, num_particles*sizeof(Particle));
    double copy_time = cpuSecond() - iStart;


    // CPU Version
    if (strcmp(include_cpu, "include_cpu") == 0) {  // perfrom CPU version if wanted by the user
        printf("CPU simulation started...\n"); fflush(stdout);
        iStart = cpuSecond();
        for (int i = 0; i < num_iterations; i++) {
            cpu_updateParticles(c_particles, i, num_particles);
        }
        iElaps = cpuSecond() - iStart;
        printf("Done in %f!\n\n", iElaps); fflush(stdout);
    }
    else
        printf("Excluded the CPU experiment...\n\n");

    // GPU Version
    printf("GPU simulation started...\n"); fflush(stdout);
    iStart = cpuSecond();
    for (int i = 0; i < num_iterations; i++) {
        // cudaMemcpy(g_particles, g_result, num_particles*sizeof(Particle), cudaMemcpyHostToDevice);
        gpu_updateParticles(g_particles, i, num_particles, tpb);
        cudaDeviceSynchronize();
        // cudaMemcpy(g_result, g_particles, num_particles*sizeof(Particle), cudaMemcpyDeviceToHost);
    }
    iElaps = cpuSecond() - iStart;

    printf("Done in %f!\n\n", iElaps + copy_time); fflush(stdout);

    // copying the result back from the GPU memory to the CUP memory
    // cudaMemcpy(g_result, g_particles, num_particles*sizeof(Particle), cudaMemcpyDeviceToHost);

    // if CPU version is perfromed, then compare it with GPU version
    if (strcmp(include_cpu, "include_cpu") == 0)
        printf(arraysMatch(g_particles, c_particles, num_particles) ? "Results match!\n" : "Results are wrong!\n");
        // printf(arraysMatch(g_result, c_particles, num_particles) ? "Results match!\n" : "Results are wrong!\n");
    printf("========================================================== \n\n\n");

    // Free arrays
    free(c_particles);
    cudaFree(g_particles);
}

	.file	"tmpxft_00271ea9_00000000-6_exercise_2b.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2040:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2040:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z10gen_randomiiii
	.type	_Z10gen_randomiiii, @function
_Z10gen_randomiiii:
.LFB2027:
	.cfi_startproc
	endbr64
	imull	%esi, %edi
	cvtsi2ssl	%ecx, %xmm1
	leal	(%rdi,%rdx), %eax
	cltd
	idivl	%ecx
	cvtsi2ssl	%edx, %xmm0
	divss	%xmm1, %xmm0
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z10gen_randomiiii, .-_Z10gen_randomiiii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%f %f %f %f %f %f\n"
	.text
	.globl	_Z13printParticleP8Particlei
	.type	_Z13printParticleP8Particlei, @function
_Z13printParticleP8Particlei:
.LFB2028:
	.cfi_startproc
	endbr64
	movslq	%esi, %rsi
	movb	$6, %al
	imulq	$24, %rsi, %rsi
	addq	%rsi, %rdi
	leaq	.LC0(%rip), %rsi
	cvtss2sd	(%rdi), %xmm0
	cvtss2sd	20(%rdi), %xmm5
	cvtss2sd	16(%rdi), %xmm4
	cvtss2sd	12(%rdi), %xmm3
	cvtss2sd	8(%rdi), %xmm2
	cvtss2sd	4(%rdi), %xmm1
	movl	$2, %edi
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z13printParticleP8Particlei, .-_Z13printParticleP8Particlei
	.globl	_Z11arraysMatchP8ParticleS0_i
	.type	_Z11arraysMatchP8ParticleS0_i, @function
_Z11arraysMatchP8ParticleS0_i:
.LFB2029:
	.cfi_startproc
	endbr64
	movsd	.LC2(%rip), %xmm2
	movq	%rdi, %rcx
	xorl	%eax, %eax
	xorl	%edi, %edi
	movss	.LC1(%rip), %xmm1
.L5:
	cmpl	%edx, %edi
	jge	.L12
	movss	(%rcx,%rax), %xmm0
	subss	(%rsi,%rax), %xmm0
	andps	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	comisd	%xmm2, %xmm0
	ja	.L10
	movss	4(%rcx,%rax), %xmm0
	subss	4(%rsi,%rax), %xmm0
	andps	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	comisd	%xmm2, %xmm0
	ja	.L10
	movss	8(%rcx,%rax), %xmm0
	subss	8(%rsi,%rax), %xmm0
	addq	$24, %rax
	andps	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	comisd	%xmm2, %xmm0
	ja	.L10
	incl	%edi
	jmp	.L5
.L12:
	movb	$1, %al
	ret
.L10:
	xorl	%eax, %eax
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z11arraysMatchP8ParticleS0_i, .-_Z11arraysMatchP8ParticleS0_i
	.globl	_Z9cpuSecondv
	.type	_Z9cpuSecondv, @function
_Z9cpuSecondv:
.LFB2030:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	xorl	%esi, %esi
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	cvtsi2sdq	16(%rsp), %xmm0
	mulsd	.LC3(%rip), %xmm0
	cvtsi2sdq	8(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L14
	call	__stack_chk_fail@PLT
.L14:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z9cpuSecondv, .-_Z9cpuSecondv
	.globl	_Z15randomizeFloat3P6float3
	.type	_Z15randomizeFloat3P6float3, @function
_Z15randomizeFloat3P6float3:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC4(%rip), %xmm0
	movss	%xmm0, (%rbx)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC4(%rip), %xmm0
	movss	%xmm0, 4(%rbx)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC4(%rip), %xmm0
	movss	%xmm0, 8(%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z15randomizeFloat3P6float3, .-_Z15randomizeFloat3P6float3
	.globl	_Z18randomizeParticlesP8Particlei
	.type	_Z18randomizeParticlesP8Particlei, @function
_Z18randomizeParticlesP8Particlei:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	xorl	%r12d, %r12d
	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
	xorl	%edi, %edi
	call	srand@PLT
.L19:
	cmpl	%ebp, %r12d
	jge	.L22
	movq	%rbx, %rdi
	incl	%r12d
	call	_Z15randomizeFloat3P6float3
	leaq	12(%rbx), %rdi
	addq	$24, %rbx
	call	_Z15randomizeFloat3P6float3
	jmp	.L19
.L22:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z18randomizeParticlesP8Particlei, .-_Z18randomizeParticlesP8Particlei
	.globl	_Z14updateParticleP8Particleiii
	.type	_Z14updateParticleP8Particleiii, @function
_Z14updateParticleP8Particleiii:
.LFB2033:
	.cfi_startproc
	endbr64
	imull	$10, %esi, %eax
	movss	12(%rdi), %xmm3
	movss	(%rdi), %xmm0
	movss	16(%rdi), %xmm2
	movss	20(%rdi), %xmm1
	cvtsi2ssl	%ecx, %xmm4
	addss	%xmm3, %xmm0
	addl	%edx, %eax
	cltd
	idivl	%ecx
	movss	%xmm0, (%rdi)
	movss	4(%rdi), %xmm0
	addss	%xmm2, %xmm0
	movss	%xmm0, 4(%rdi)
	movss	8(%rdi), %xmm0
	addss	%xmm1, %xmm0
	movss	%xmm0, 8(%rdi)
	cvtsi2ssl	%edx, %xmm0
	divss	%xmm4, %xmm0
	addss	%xmm0, %xmm3
	addss	%xmm0, %xmm2
	addss	%xmm0, %xmm1
	movss	%xmm3, 12(%rdi)
	movss	%xmm2, 16(%rdi)
	movss	%xmm1, 20(%rdi)
	ret
	.cfi_endproc
.LFE2033:
	.size	_Z14updateParticleP8Particleiii, .-_Z14updateParticleP8Particleiii
	.globl	_Z29cpu_updatePositionAndVelocityP8Particleiii
	.type	_Z29cpu_updatePositionAndVelocityP8Particleiii, @function
_Z29cpu_updatePositionAndVelocityP8Particleiii:
.LFB2034:
	.cfi_startproc
	endbr64
	jmp	_Z14updateParticleP8Particleiii
	.cfi_endproc
.LFE2034:
	.size	_Z29cpu_updatePositionAndVelocityP8Particleiii, .-_Z29cpu_updatePositionAndVelocityP8Particleiii
	.globl	_Z19cpu_updateParticlesP8Particleii
	.type	_Z19cpu_updateParticlesP8Particleii, @function
_Z19cpu_updateParticlesP8Particleii:
.LFB2035:
	.cfi_startproc
	endbr64
	movl	%esi, %r9d
	movl	%edx, %ecx
	xorl	%r8d, %r8d
.L26:
	cmpl	%ecx, %r8d
	jge	.L28
	movl	%r8d, %esi
	movl	%r9d, %edx
	incl	%r8d
	call	_Z14updateParticleP8Particleiii
	addq	$24, %rdi
	jmp	.L26
.L28:
	ret
	.cfi_endproc
.LFE2035:
	.size	_Z19cpu_updateParticlesP8Particleii, .-_Z19cpu_updateParticlesP8Particleii
	.globl	_Z59__device_stub__Z29gpu_updatePositionAndVelocityP8ParticleiiP8Particleii
	.type	_Z59__device_stub__Z29gpu_updatePositionAndVelocityP8ParticleiiP8Particleii, @function
_Z59__device_stub__Z29gpu_updatePositionAndVelocityP8ParticleiiP8Particleii:
.LFB2062:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	32(%rsp), %rdi
	movl	%esi, 4(%rsp)
	leaq	44(%rsp), %rsi
	movl	%edx, (%rsp)
	leaq	16(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L29
	pushq	24(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z29gpu_updatePositionAndVelocityP8Particleii(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 144
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L29:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L31
	call	__stack_chk_fail@PLT
.L31:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2062:
	.size	_Z59__device_stub__Z29gpu_updatePositionAndVelocityP8ParticleiiP8Particleii, .-_Z59__device_stub__Z29gpu_updatePositionAndVelocityP8ParticleiiP8Particleii
	.globl	_Z29gpu_updatePositionAndVelocityP8Particleii
	.type	_Z29gpu_updatePositionAndVelocityP8Particleii, @function
_Z29gpu_updatePositionAndVelocityP8Particleii:
.LFB2063:
	.cfi_startproc
	endbr64
	jmp	_Z59__device_stub__Z29gpu_updatePositionAndVelocityP8ParticleiiP8Particleii
	.cfi_endproc
.LFE2063:
	.size	_Z29gpu_updatePositionAndVelocityP8Particleii, .-_Z29gpu_updatePositionAndVelocityP8Particleii
	.globl	_Z19gpu_updateParticlesP8Particleiii
	.type	_Z19gpu_updateParticlesP8Particleiii, @function
_Z19gpu_updateParticlesP8Particleiii:
.LFB2036:
	.cfi_startproc
	endbr64
	leal	-1(%rdx,%rcx), %eax
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	%esi, %r12d
	xorl	%r9d, %r9d
	movabsq	$4294967297, %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rdi, %rbp
	xorl	%r8d, %r8d
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movl	%edx, %ebx
	cltd
	idivl	%ecx
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	movq	%rsi, 24(%rsp)
	movl	%ecx, 20(%rsp)
	movl	28(%rsp), %ecx
	movq	%rsi, 12(%rsp)
	movq	20(%rsp), %rdx
	movl	16(%rsp), %esi
	movl	%eax, 8(%rsp)
	movq	8(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L34
	addq	$32, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 32
	movl	%ebx, %edx
	movl	%r12d, %esi
	movq	%rbp, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	_Z59__device_stub__Z29gpu_updatePositionAndVelocityP8ParticleiiP8Particleii
.L34:
	.cfi_restore_state
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z19gpu_updateParticlesP8Particleiii, .-_Z19gpu_updateParticlesP8Particleiii
	.section	.rodata.str1.1
.LC5:
	.string	"Results match!\n"
.LC6:
	.string	"Results are wrong!\n"
.LC7:
	.string	"Running the simulations with the following params:\n"
.LC8:
	.string	"Usage: ./a NUM_PARTICLES NUM_ITERATIONS TPB INCLUDE_CPU\nExample usage: ./a 10000 10000 32 include_cpu\n"
.LC9:
	.string	"num_iterations"
.LC10:
	.string	"num_particles"
.LC11:
	.string	"======== %s: %d, %s: %d, %s: %d\n\n"
.LC12:
	.string	"tpb"
.LC13:
	.string	"include_cpu"
.LC14:
	.string	"CPU simulation started...\n"
.LC15:
	.string	"Done in %f!\n\n"
.LC16:
	.string	"Excluded the CPU experiment...\n\n"
.LC17:
	.string	"GPU simulation started...\n"
.LC18:
	.string	"========================================================== \n\n\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2037:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rsi, %r12
	leaq	.LC7(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edi, %ebx
	movl	$2, %edi
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	cmpl	$4, %ebx
	jg	.L38
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	orl	$-1, %eax
	jmp	.L37
.L38:
	movq	8(%r12), %rdi
	movl	$10, %edx
	xorl	%esi, %esi
	call	__isoc23_strtol@PLT
	movq	16(%r12), %rdi
	movl	$10, %edx
	xorl	%esi, %esi
	movq	%rax, %rbx
	movl	%eax, %ebp
	call	__isoc23_strtol@PLT
	movq	24(%r12), %rdi
	movl	$10, %edx
	xorl	%esi, %esi
	movq	%rax, %r13
	movl	%eax, %r14d
	call	__isoc23_strtol@PLT
	movl	%ebx, %ecx
	movq	32(%r12), %r12
	movslq	%ebx, %rbx
	pushq	%rax
	.cfi_def_cfa_offset 120
	movl	%eax, %r15d
	leaq	.LC12(%rip), %rax
	movl	%r13d, %r9d
	pushq	%rax
	.cfi_def_cfa_offset 128
	imulq	$24, %rbx, %rbx
	xorl	%eax, %eax
	leaq	.LC9(%rip), %r8
	leaq	.LC10(%rip), %rdx
	leaq	.LC11(%rip), %rsi
	movl	$2, %edi
	leaq	.LC13(%rip), %r13
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	call	malloc@PLT
	movl	%ebp, %esi
	movq	%rax, %rdi
	movq	%rax, 24(%rsp)
	call	_Z18randomizeParticlesP8Particlei
	popq	%rax
	.cfi_def_cfa_offset 120
	popq	%rdx
	.cfi_def_cfa_offset 112
	movq	%rbx, %rsi
	movl	$1, %edx
	leaq	32(%rsp), %rdi
	call	cudaMallocManaged@PLT
	call	_Z9cpuSecondv
	movq	32(%rsp), %rdi
	movq	8(%rsp), %rsi
	movq	%rbx, %rcx
	rep movsb
	movsd	%xmm0, 16(%rsp)
	call	_Z9cpuSecondv
	subsd	16(%rsp), %xmm0
	movq	%r13, %rsi
	movq	%r12, %rdi
	movsd	%xmm0, 16(%rsp)
	call	strcmp@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.L40
	leaq	.LC14(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	stdout(%rip), %rdi
	call	fflush@PLT
	call	_Z9cpuSecondv
	movsd	%xmm0, 24(%rsp)
.L41:
	cmpl	%r14d, %ebx
	jge	.L51
	movq	8(%rsp), %rdi
	movl	%ebx, %esi
	movl	%ebp, %edx
	incl	%ebx
	call	_Z19cpu_updateParticlesP8Particleii
	jmp	.L41
.L51:
	call	_Z9cpuSecondv
	subsd	24(%rsp), %xmm0
	leaq	.LC15(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	stdout(%rip), %rdi
	call	fflush@PLT
	jmp	.L43
.L40:
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L43:
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%ebx, %ebx
	call	__printf_chk@PLT
	movq	stdout(%rip), %rdi
	call	fflush@PLT
	call	_Z9cpuSecondv
	movsd	%xmm0, 24(%rsp)
.L44:
	cmpl	%r14d, %ebx
	jge	.L52
	movq	32(%rsp), %rdi
	movl	%ebx, %esi
	movl	%r15d, %ecx
	movl	%ebp, %edx
	incl	%ebx
	call	_Z19gpu_updateParticlesP8Particleiii
	call	cudaDeviceSynchronize@PLT
	jmp	.L44
.L52:
	call	_Z9cpuSecondv
	subsd	24(%rsp), %xmm0
	addsd	16(%rsp), %xmm0
	movb	$1, %al
	leaq	.LC15(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	stdout(%rip), %rdi
	call	fflush@PLT
	movq	%r13, %rsi
	movq	%r12, %rdi
	call	strcmp@PLT
	testl	%eax, %eax
	jne	.L46
	movq	8(%rsp), %rsi
	movq	32(%rsp), %rdi
	movl	%ebp, %edx
	call	_Z11arraysMatchP8ParticleS0_i
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	testb	%al, %al
	leaq	.LC5(%rip), %rax
	cmovne	%rax, %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L46:
	leaq	.LC18(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
.L37:
	movq	40(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L48
	call	__stack_chk_fail@PLT
.L48:
	addq	$56, %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
.LFE2037:
	.size	main, .-main
	.section	.rodata.str1.1
.LC19:
	.string	"_Z29gpu_updatePositionAndVelocityP8Particleii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2065:
	.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	_Z29gpu_updatePositionAndVelocityP8Particleii(%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
.LFE2065:
	.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.cst16,"aM",@progbits,16
	.align 16
.LC1:
	.long	2147483647
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC2:
	.long	-1998362383
	.long	1055193269
	.align 8
.LC3:
	.long	-1598689907
	.long	1051772663
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC4:
	.long	805306368
	.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 <time.h>
#include <stdlib.h>
#include <sys/time.h>

// #define NUM_PARTICLES 10000
// #define NUM_ITERATIONS 10000
// int TPB = 16;

#define SEED 10
#define EPSILON 1e-5

typedef struct {
    float3 position;
    float3 velocity;
} Particle;


// Deterministically generates a "random" float, provided a seed and 3 integers.
__host__ __device__ float gen_random(int seed, int a, int b, int c) {
    return (float)((seed * a + b) % c) / c;
}


// Given an array of particles and an index, print that particle.
void printParticle(Particle* particles, int index){
    printf("%f %f %f %f %f %f\n",
    particles[index].position.x, particles[index].position.y, particles[index].position.z,
    particles[index].velocity.x, particles[index].velocity.y, particles[index].velocity.z);
}


// Compare two arrays of Particles. If their position coordinates are all within EPSILON of each other,
// return true, else false.
__host__ bool arraysMatch(Particle* arr1, Particle* arr2, int num_particles)
{
    for (int i = 0; i < num_particles; i++) {
        if (fabs(arr1[i].position.x - arr2[i].position.x) > EPSILON ||
        fabs(arr1[i].position.y - arr2[i].position.y) > EPSILON ||
        fabs(arr1[i].position.z - arr2[i].position.z) > EPSILON)
        return false;
    }
    return true;
}


// Get the current time
double cpuSecond() {
    struct timeval tp;
    gettimeofday(&tp,NULL);
    return ((double)tp.tv_sec + (double)tp.tv_usec*1.e-6);
}


// Replaces the x, y and z values in a float3 to random values between 0 and 1.
void randomizeFloat3(float3* f3) {
    f3->x = (float) rand() / RAND_MAX;
    f3->y = (float) rand() / RAND_MAX;
    f3->z = (float) rand() / RAND_MAX;
}


// Randomizes the position and velocity of all Particles in an array.
void randomizeParticles(Particle* particles, int num_particles) {
    srand(0);
    for (int i = 0; i < num_particles; i++) {
        randomizeFloat3(&particles[i].position);
        randomizeFloat3(&particles[i].velocity);
    }
}


// Updates a particle's position by its velocity, then updates its velocity
__host__ __device__ void updateParticle(Particle* particle, int id, int iter, int num_particles) {
    int dt = 1;

    // update position
    particle->position.x += dt * particle->velocity.x;
    particle->position.y += dt * particle->velocity.y;
    particle->position.z += dt * particle->velocity.z;

    // update the velocity randomly
    particle->velocity.x += gen_random(SEED, id, iter, num_particles);
    particle->velocity.y += gen_random(SEED, id, iter, num_particles);
    particle->velocity.z += gen_random(SEED, id, iter, num_particles);
}


// CPU function that updates a given particle.
void cpu_updatePositionAndVelocity(Particle* particle, int id, int iter, int num_particles) {
    updateParticle(particle, id, iter, num_particles);
}


// Kernel that finds a given Particle's ID then updates it if within range.
__global__ void gpu_updatePositionAndVelocity(Particle* particles, int iter, int num_particles) {
    const int id = blockIdx.x * blockDim.x + threadIdx.x;

    if (id >= num_particles) // If out of bounds, ignore the Particle.
    return;
    else
    updateParticle(&particles[id], id, iter, num_particles);
}


// Perform the update step for all Particles in the array on CPU with a for loop.
void cpu_updateParticles(Particle* particles, int iter, int num_particles) {
    // srand(time(NULL))
    for (int i = 0; i < num_particles; i++) {
        cpu_updatePositionAndVelocity(&particles[i], i, iter, num_particles);
    }
}


// Perform the update step for all Particles in the array by launching GPU kernels.
void gpu_updateParticles(Particle* particles, int iter, int num_particles, int tpb) {
    gpu_updatePositionAndVelocity<<<(num_particles + tpb - 1)/tpb, tpb>>>(particles, iter, num_particles);
}


int main(int argc, char** argv) {
    printf("Running the simulations with the following params:\n");

    if (argc < 5) {
        printf("Usage: ./a NUM_PARTICLES NUM_ITERATIONS TPB INCLUDE_CPU\nExample usage: ./a 10000 10000 32 include_cpu\n");
        return -1;
    }


    // reading the command line arguments, without any kind of error checking
    const int num_particles = (int) strtol(argv[1], NULL, 10); // e.g. 10000 - NULL is the endpointer and 10 is the base
    const int num_iterations = (int) strtol(argv[2], NULL, 10); // e.g. 10000
    const int tpb = (int) strtol(argv[3], NULL, 10); // e.g. 32
    const char* include_cpu = argv[4];
    printf("======== %s: %d, %s: %d, %s: %d\n\n", "num_particles", num_particles, "num_iterations", num_iterations, "tpb", tpb);


    // Declare variables
    Particle *c_particles, *g_particles, *g_result;
    double iStart, iElaps;

    // Initialize array for CPU
    c_particles = (Particle*) malloc(num_particles*sizeof(Particle));
    randomizeParticles(c_particles, num_particles);

    // Initialize array for GPU - particle positions/velocities in device memory are a copy of those in host memory
    // g_result = (Particle*) malloc(num_particles*sizeof(Particle)); // Used to store the result of GPU simulation
    // cudaMallocHost(&g_result, num_particles*sizeof(Particle));
    // cudaMalloc(&g_particles, num_particles*sizeof(Particle));
    hipMallocManaged(&g_particles, num_particles*sizeof(Particle));

    iStart = cpuSecond();

    memcpy(g_particles, c_particles, num_particles*sizeof(Particle));
    double copy_time = cpuSecond() - iStart;


    // CPU Version
    if (strcmp(include_cpu, "include_cpu") == 0) {  // perfrom CPU version if wanted by the user
        printf("CPU simulation started...\n"); fflush(stdout);
        iStart = cpuSecond();
        for (int i = 0; i < num_iterations; i++) {
            cpu_updateParticles(c_particles, i, num_particles);
        }
        iElaps = cpuSecond() - iStart;
        printf("Done in %f!\n\n", iElaps); fflush(stdout);
    }
    else
        printf("Excluded the CPU experiment...\n\n");

    // GPU Version
    printf("GPU simulation started...\n"); fflush(stdout);
    iStart = cpuSecond();
    for (int i = 0; i < num_iterations; i++) {
        // cudaMemcpy(g_particles, g_result, num_particles*sizeof(Particle), cudaMemcpyHostToDevice);
        gpu_updateParticles(g_particles, i, num_particles, tpb);
        hipDeviceSynchronize();
        // cudaMemcpy(g_result, g_particles, num_particles*sizeof(Particle), cudaMemcpyDeviceToHost);
    }
    iElaps = cpuSecond() - iStart;

    printf("Done in %f!\n\n", iElaps + copy_time); fflush(stdout);

    // copying the result back from the GPU memory to the CUP memory
    // cudaMemcpy(g_result, g_particles, num_particles*sizeof(Particle), cudaMemcpyDeviceToHost);

    // if CPU version is perfromed, then compare it with GPU version
    if (strcmp(include_cpu, "include_cpu") == 0)
        printf(arraysMatch(g_particles, c_particles, num_particles) ? "Results match!\n" : "Results are wrong!\n");
        // printf(arraysMatch(g_result, c_particles, num_particles) ? "Results match!\n" : "Results are wrong!\n");
    printf("========================================================== \n\n\n");

    // Free arrays
    free(c_particles);
    hipFree(g_particles);
}

	.text
	.file	"exercise_2b.hip"
	.globl	_Z10gen_randomiiii              # -- Begin function _Z10gen_randomiiii
	.type	_Z10gen_randomiiii,@function
_Z10gen_randomiiii:                     # @_Z10gen_randomiiii
	.cfi_startproc
# %bb.0:
                                        # kill: def $edx killed $edx def $rdx
                                        # kill: def $edi killed $edi def $rdi
	imull	%esi, %edi
	leal	(%rdi,%rdx), %eax
	cltd
	idivl	%ecx
	cvtsi2ss	%edx, %xmm0
	cvtsi2ss	%ecx, %xmm1
	divss	%xmm1, %xmm0
	retq
.Lfunc_end0:
	.size	_Z10gen_randomiiii, .Lfunc_end0-_Z10gen_randomiiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z13printParticleP8Particlei    # -- Begin function _Z13printParticleP8Particlei
	.type	_Z13printParticleP8Particlei,@function
_Z13printParticleP8Particlei:           # @_Z13printParticleP8Particlei
	.cfi_startproc
# %bb.0:
	movslq	%esi, %rax
	leaq	(%rax,%rax,2), %rax
	cvtss2sd	(%rdi,%rax,8), %xmm0
	cvtss2sd	4(%rdi,%rax,8), %xmm1
	cvtss2sd	8(%rdi,%rax,8), %xmm2
	cvtss2sd	12(%rdi,%rax,8), %xmm3
	cvtss2sd	16(%rdi,%rax,8), %xmm4
	cvtss2sd	20(%rdi,%rax,8), %xmm5
	movl	$.L.str, %edi
	movb	$6, %al
	jmp	printf                          # TAILCALL
.Lfunc_end1:
	.size	_Z13printParticleP8Particlei, .Lfunc_end1-_Z13printParticleP8Particlei
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z11arraysMatchP8ParticleS0_i
.LCPI2_0:
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.long	0x7fffffff                      # float NaN
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI2_1:
	.quad	0x3ee4f8b588e368f1              # double 1.0000000000000001E-5
	.text
	.globl	_Z11arraysMatchP8ParticleS0_i
	.type	_Z11arraysMatchP8ParticleS0_i,@function
_Z11arraysMatchP8ParticleS0_i:          # @_Z11arraysMatchP8ParticleS0_i
	.cfi_startproc
# %bb.0:
	testl	%edx, %edx
	jle	.LBB2_1
# %bb.3:                                # %.lr.ph.preheader
	movss	(%rdi), %xmm0                   # xmm0 = mem[0],zero,zero,zero
	subss	(%rsi), %xmm0
	andps	.LCPI2_0(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	ucomisd	.LCPI2_1(%rip), %xmm0
	jbe	.LBB2_5
# %bb.4:
	xorl	%eax, %eax
	jmp	.LBB2_2
.LBB2_1:
	movb	$1, %al
.LBB2_2:                                # %._crit_edge
	andb	$1, %al
                                        # kill: def $al killed $al killed $eax
	retq
.LBB2_5:                                # %.lr.ph26.preheader
	movl	%edx, %ecx
	movl	$4, %edx
	movl	$1, %r8d
	xorl	%eax, %eax
	movaps	.LCPI2_0(%rip), %xmm0           # xmm0 = [NaN,NaN,NaN,NaN]
	movsd	.LCPI2_1(%rip), %xmm1           # xmm1 = mem[0],zero
.LBB2_8:                                # %.lr.ph26
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rdi,%rdx), %xmm2              # xmm2 = mem[0],zero,zero,zero
	subss	(%rsi,%rdx), %xmm2
	andps	%xmm0, %xmm2
	cvtss2sd	%xmm2, %xmm2
	ucomisd	%xmm1, %xmm2
	ja	.LBB2_2
# %bb.9:                                #   in Loop: Header=BB2_8 Depth=1
	movss	4(%rdi,%rdx), %xmm2             # xmm2 = mem[0],zero,zero,zero
	subss	4(%rsi,%rdx), %xmm2
	andps	%xmm0, %xmm2
	cvtss2sd	%xmm2, %xmm2
	ucomisd	%xmm1, %xmm2
	ja	.LBB2_2
# %bb.6:                                #   in Loop: Header=BB2_8 Depth=1
	cmpq	%rcx, %r8
	setae	%al
	je	.LBB2_2
# %bb.7:                                # %.lr.ph
                                        #   in Loop: Header=BB2_8 Depth=1
	movss	20(%rdi,%rdx), %xmm2            # xmm2 = mem[0],zero,zero,zero
	subss	20(%rsi,%rdx), %xmm2
	andps	%xmm0, %xmm2
	cvtss2sd	%xmm2, %xmm2
	addq	$24, %rdx
	incq	%r8
	ucomisd	%xmm1, %xmm2
	jbe	.LBB2_8
	jmp	.LBB2_2
.Lfunc_end2:
	.size	_Z11arraysMatchP8ParticleS0_i, .Lfunc_end2-_Z11arraysMatchP8ParticleS0_i
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z9cpuSecondv
.LCPI3_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	_Z9cpuSecondv
	.type	_Z9cpuSecondv,@function
_Z9cpuSecondv:                          # @_Z9cpuSecondv
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	movq	%rsp, %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	cvtsi2sdq	(%rbx), %xmm1
	cvtsi2sdq	8(%rbx), %xmm0
	mulsd	.LCPI3_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z9cpuSecondv, .Lfunc_end3-_Z9cpuSecondv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z15randomizeFloat3P15HIP_vector_typeIfLj3EE
.LCPI4_0:
	.long	0x30000000                      # float 4.65661287E-10
	.text
	.globl	_Z15randomizeFloat3P15HIP_vector_typeIfLj3EE
	.type	_Z15randomizeFloat3P15HIP_vector_typeIfLj3EE,@function
_Z15randomizeFloat3P15HIP_vector_typeIfLj3EE: # @_Z15randomizeFloat3P15HIP_vector_typeIfLj3EE
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	callq	rand
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI4_0(%rip), %xmm0
	movss	%xmm0, (%rbx)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI4_0(%rip), %xmm0
	movss	%xmm0, 4(%rbx)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI4_0(%rip), %xmm0
	movss	%xmm0, 8(%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z15randomizeFloat3P15HIP_vector_typeIfLj3EE, .Lfunc_end4-_Z15randomizeFloat3P15HIP_vector_typeIfLj3EE
	.cfi_endproc
                                        # -- End function
	.globl	_Z18randomizeParticlesP8Particlei # -- Begin function _Z18randomizeParticlesP8Particlei
	.type	_Z18randomizeParticlesP8Particlei,@function
_Z18randomizeParticlesP8Particlei:      # @_Z18randomizeParticlesP8Particlei
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movq	%rdi, %rbx
	xorl	%edi, %edi
	callq	srand
	testl	%ebp, %ebp
	jle	.LBB5_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%ebp, %r14d
.LBB5_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%rbx, %rdi
	callq	_Z15randomizeFloat3P15HIP_vector_typeIfLj3EE
	leaq	12(%rbx), %rdi
	callq	_Z15randomizeFloat3P15HIP_vector_typeIfLj3EE
	addq	$24, %rbx
	decq	%r14
	jne	.LBB5_2
.LBB5_3:                                # %._crit_edge
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	_Z18randomizeParticlesP8Particlei, .Lfunc_end5-_Z18randomizeParticlesP8Particlei
	.cfi_endproc
                                        # -- End function
	.globl	_Z14updateParticleP8Particleiii # -- Begin function _Z14updateParticleP8Particleiii
	.type	_Z14updateParticleP8Particleiii,@function
_Z14updateParticleP8Particleiii:        # @_Z14updateParticleP8Particleiii
	.cfi_startproc
# %bb.0:
                                        # kill: def $edx killed $edx def $rdx
                                        # kill: def $esi killed $esi def $rsi
	movss	12(%rdi), %xmm1                 # xmm1 = mem[0],zero,zero,zero
	movss	16(%rdi), %xmm0                 # xmm0 = mem[0],zero,zero,zero
	movss	(%rdi), %xmm2                   # xmm2 = mem[0],zero,zero,zero
	addss	%xmm1, %xmm2
	movss	%xmm2, (%rdi)
	movss	4(%rdi), %xmm2                  # xmm2 = mem[0],zero,zero,zero
	addss	%xmm0, %xmm2
	movss	%xmm2, 4(%rdi)
	movss	20(%rdi), %xmm2                 # xmm2 = mem[0],zero,zero,zero
	movss	8(%rdi), %xmm3                  # xmm3 = mem[0],zero,zero,zero
	addss	%xmm2, %xmm3
	movss	%xmm3, 8(%rdi)
	leal	(%rsi,%rsi,4), %eax
	leal	(%rdx,%rax,2), %eax
	cltd
	idivl	%ecx
	xorps	%xmm3, %xmm3
	cvtsi2ss	%edx, %xmm3
	cvtsi2ss	%ecx, %xmm4
	divss	%xmm4, %xmm3
	addss	%xmm3, %xmm1
	movss	%xmm1, 12(%rdi)
	addss	%xmm3, %xmm0
	movss	%xmm0, 16(%rdi)
	addss	%xmm2, %xmm3
	movss	%xmm3, 20(%rdi)
	retq
.Lfunc_end6:
	.size	_Z14updateParticleP8Particleiii, .Lfunc_end6-_Z14updateParticleP8Particleiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z29cpu_updatePositionAndVelocityP8Particleiii # -- Begin function _Z29cpu_updatePositionAndVelocityP8Particleiii
	.type	_Z29cpu_updatePositionAndVelocityP8Particleiii,@function
_Z29cpu_updatePositionAndVelocityP8Particleiii: # @_Z29cpu_updatePositionAndVelocityP8Particleiii
	.cfi_startproc
# %bb.0:
                                        # kill: def $edx killed $edx def $rdx
                                        # kill: def $esi killed $esi def $rsi
	movss	12(%rdi), %xmm1                 # xmm1 = mem[0],zero,zero,zero
	movss	16(%rdi), %xmm0                 # xmm0 = mem[0],zero,zero,zero
	movss	(%rdi), %xmm2                   # xmm2 = mem[0],zero,zero,zero
	addss	%xmm1, %xmm2
	movss	%xmm2, (%rdi)
	movss	4(%rdi), %xmm2                  # xmm2 = mem[0],zero,zero,zero
	addss	%xmm0, %xmm2
	movss	%xmm2, 4(%rdi)
	movss	20(%rdi), %xmm2                 # xmm2 = mem[0],zero,zero,zero
	movss	8(%rdi), %xmm3                  # xmm3 = mem[0],zero,zero,zero
	addss	%xmm2, %xmm3
	movss	%xmm3, 8(%rdi)
	leal	(%rsi,%rsi,4), %eax
	leal	(%rdx,%rax,2), %eax
	cltd
	idivl	%ecx
	xorps	%xmm3, %xmm3
	cvtsi2ss	%edx, %xmm3
	cvtsi2ss	%ecx, %xmm4
	divss	%xmm4, %xmm3
	addss	%xmm3, %xmm1
	movss	%xmm1, 12(%rdi)
	addss	%xmm3, %xmm0
	movss	%xmm0, 16(%rdi)
	addss	%xmm2, %xmm3
	movss	%xmm3, 20(%rdi)
	retq
.Lfunc_end7:
	.size	_Z29cpu_updatePositionAndVelocityP8Particleiii, .Lfunc_end7-_Z29cpu_updatePositionAndVelocityP8Particleiii
	.cfi_endproc
                                        # -- End function
	.globl	_Z44__device_stub__gpu_updatePositionAndVelocityP8Particleii # -- Begin function _Z44__device_stub__gpu_updatePositionAndVelocityP8Particleii
	.type	_Z44__device_stub__gpu_updatePositionAndVelocityP8Particleii,@function
_Z44__device_stub__gpu_updatePositionAndVelocityP8Particleii: # @_Z44__device_stub__gpu_updatePositionAndVelocityP8Particleii
	.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	$96, %rsp
	.cfi_def_cfa_offset 144
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rcx
	movl	%esi, (%rcx)
	movq	%rsp, %rsi
	movl	%edx, (%rsi)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%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	$_Z29gpu_updatePositionAndVelocityP8Particleii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$112, %rsp
	.cfi_adjust_cfa_offset -112
	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_end8:
	.size	_Z44__device_stub__gpu_updatePositionAndVelocityP8Particleii, .Lfunc_end8-_Z44__device_stub__gpu_updatePositionAndVelocityP8Particleii
	.cfi_endproc
                                        # -- End function
	.globl	_Z19cpu_updateParticlesP8Particleii # -- Begin function _Z19cpu_updateParticlesP8Particleii
	.type	_Z19cpu_updateParticlesP8Particleii,@function
_Z19cpu_updateParticlesP8Particleii:    # @_Z19cpu_updateParticlesP8Particleii
	.cfi_startproc
# %bb.0:
	testl	%edx, %edx
	jle	.LBB9_3
# %bb.1:                                # %.lr.ph
	movl	%edx, %ecx
	cvtsi2ss	%edx, %xmm0
	movl	%edx, %eax
	shlq	$3, %rax
	leaq	(%rax,%rax,2), %r8
	xorl	%r9d, %r9d
.LBB9_2:                                # =>This Inner Loop Header: Depth=1
	movss	12(%rdi,%r9), %xmm1             # xmm1 = mem[0],zero,zero,zero
	movss	16(%rdi,%r9), %xmm2             # xmm2 = mem[0],zero,zero,zero
	movss	(%rdi,%r9), %xmm3               # xmm3 = mem[0],zero,zero,zero
	addss	%xmm1, %xmm3
	movss	%xmm3, (%rdi,%r9)
	movss	4(%rdi,%r9), %xmm3              # xmm3 = mem[0],zero,zero,zero
	addss	%xmm2, %xmm3
	movss	%xmm3, 4(%rdi,%r9)
	movss	20(%rdi,%r9), %xmm3             # xmm3 = mem[0],zero,zero,zero
	movss	8(%rdi,%r9), %xmm4              # xmm4 = mem[0],zero,zero,zero
	addss	%xmm3, %xmm4
	movss	%xmm4, 8(%rdi,%r9)
	movl	%esi, %eax
	cltd
	idivl	%ecx
	xorps	%xmm4, %xmm4
	cvtsi2ss	%edx, %xmm4
	divss	%xmm0, %xmm4
	addss	%xmm4, %xmm1
	movss	%xmm1, 12(%rdi,%r9)
	addss	%xmm4, %xmm2
	movss	%xmm2, 16(%rdi,%r9)
	addss	%xmm3, %xmm4
	movss	%xmm4, 20(%rdi,%r9)
	addl	$10, %esi
	addq	$24, %r9
	cmpq	%r9, %r8
	jne	.LBB9_2
.LBB9_3:                                # %._crit_edge
	retq
.Lfunc_end9:
	.size	_Z19cpu_updateParticlesP8Particleii, .Lfunc_end9-_Z19cpu_updateParticlesP8Particleii
	.cfi_endproc
                                        # -- End function
	.globl	_Z19gpu_updateParticlesP8Particleiii # -- Begin function _Z19gpu_updateParticlesP8Particleiii
	.type	_Z19gpu_updateParticlesP8Particleiii,@function
_Z19gpu_updateParticlesP8Particleiii:   # @_Z19gpu_updateParticlesP8Particleiii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %rbp, -16
                                        # kill: def $ecx killed $ecx def $rcx
	movl	%edx, %ebx
	movl	%esi, %ebp
	movq	%rdi, %r14
	leal	(%rbx,%rcx), %eax
	decl	%eax
	cltd
	idivl	%ecx
                                        # kill: def $eax killed $eax def $rax
	btsq	$32, %rax
	movl	%ecx, %edx
	btsq	$32, %rdx
	movq	%rax, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	je	.LBB10_2
# %bb.1:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB10_2:
	.cfi_def_cfa_offset 32
	movq	%r14, %rdi
	movl	%ebp, %esi
	movl	%ebx, %edx
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	_Z44__device_stub__gpu_updatePositionAndVelocityP8Particleii # TAILCALL
.Lfunc_end10:
	.size	_Z19gpu_updateParticlesP8Particleiii, .Lfunc_end10-_Z19gpu_updateParticlesP8Particleiii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI11_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.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	$72, %rsp
	.cfi_def_cfa_offset 128
	.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
	movl	%edi, %ebx
	movl	$.Lstr, %edi
	callq	puts@PLT
	cmpl	$4, %ebx
	jg	.LBB11_2
# %bb.1:
	movl	$.Lstr.5, %edi
	callq	puts@PLT
	movl	$-1, %r12d
	jmp	.LBB11_11
.LBB11_2:
	movq	8(%r14), %rdi
	xorl	%esi, %esi
	movl	$10, %edx
	callq	__isoc23_strtol
	movq	%rax, %rbx
	movq	16(%r14), %rdi
	xorl	%esi, %esi
	movl	$10, %edx
	callq	__isoc23_strtol
	movq	%rax, %r12
	movq	24(%r14), %rdi
	xorl	%esi, %esi
	movl	$10, %edx
	callq	__isoc23_strtol
	movq	%rax, %r13
	movq	32(%r14), %rax
	movq	%rax, 40(%rsp)                  # 8-byte Spill
	movl	%r13d, (%rsp)
	movl	$.L.str.3, %edi
	movl	$.L.str.4, %esi
	movl	$.L.str.5, %ecx
	movl	$.L.str.6, %r9d
	movl	%ebx, %edx
	movl	%r12d, %r8d
	xorl	%eax, %eax
	callq	printf
	movslq	%ebx, %r15
	leaq	(,%r15,8), %rax
	leaq	(%rax,%rax,2), %rbp
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, %r14
	movq	%rax, %rdi
	movl	%r15d, %esi
	callq	_Z18randomizeParticlesP8Particlei
	leaq	16(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, %edx
	callq	hipMallocManaged
	leaq	48(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	cvtsi2sdq	(%r15), %xmm0
	cvtsi2sdq	8(%r15), %xmm1
	mulsd	.LCPI11_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 24(%rsp)                 # 8-byte Spill
	leaq	16(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r14, %rsi
	movq	%rbp, %rdx
	callq	memcpy@PLT
	leaq	48(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r15), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r15), %xmm1
	mulsd	.LCPI11_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 32(%rsp)                 # 8-byte Spill
	movl	$.L.str.7, %esi
	movq	40(%rsp), %rdi                  # 8-byte Reload
	callq	strcmp
	testl	%eax, %eax
	je	.LBB11_3
# %bb.6:
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	jmp	.LBB11_7
.LBB11_3:
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movq	stdout(%rip), %rdi
	callq	fflush
	xorl	%ebp, %ebp
	leaq	48(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r15), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r15), %xmm1
	mulsd	.LCPI11_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 64(%rsp)                 # 8-byte Spill
	testl	%r12d, %r12d
	jle	.LBB11_4
.LBB11_5:                               # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	movl	%ebp, %esi
	movl	%ebx, %edx
	callq	_Z19cpu_updateParticlesP8Particleii
	incl	%ebp
	cmpl	%ebp, %r12d
	jne	.LBB11_5
.LBB11_4:                               # %._crit_edge
	leaq	48(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm1, %xmm1
	cvtsi2sdq	(%r15), %xmm1
	xorps	%xmm0, %xmm0
	cvtsi2sdq	8(%r15), %xmm0
	mulsd	.LCPI11_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	subsd	64(%rsp), %xmm0                 # 8-byte Folded Reload
	movl	$.L.str.9, %edi
	movb	$1, %al
	callq	printf
	movq	stdout(%rip), %rdi
	callq	fflush
.LBB11_7:
	movsd	32(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	subsd	24(%rsp), %xmm0                 # 8-byte Folded Reload
	movsd	%xmm0, 32(%rsp)                 # 8-byte Spill
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movq	stdout(%rip), %rdi
	callq	fflush
	xorl	%ebp, %ebp
	leaq	48(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r15), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r15), %xmm1
	mulsd	.LCPI11_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	movsd	%xmm1, 24(%rsp)                 # 8-byte Spill
	testl	%r12d, %r12d
	jle	.LBB11_8
.LBB11_12:                              # %.lr.ph47
                                        # =>This Inner Loop Header: Depth=1
	movq	16(%rsp), %rdi
	movl	%ebp, %esi
	movl	%ebx, %edx
	movl	%r13d, %ecx
	callq	_Z19gpu_updateParticlesP8Particleiii
	callq	hipDeviceSynchronize
	incl	%ebp
	cmpl	%ebp, %r12d
	jne	.LBB11_12
.LBB11_8:                               # %._crit_edge48
	xorl	%r12d, %r12d
	leaq	48(%rsp), %r15
	movq	%r15, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	xorps	%xmm0, %xmm0
	cvtsi2sdq	(%r15), %xmm0
	xorps	%xmm1, %xmm1
	cvtsi2sdq	8(%r15), %xmm1
	mulsd	.LCPI11_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	subsd	24(%rsp), %xmm1                 # 8-byte Folded Reload
	movsd	32(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	addsd	%xmm1, %xmm0
	movl	$.L.str.9, %edi
	movb	$1, %al
	callq	printf
	movq	stdout(%rip), %rdi
	callq	fflush
	movl	$.L.str.7, %esi
	movq	40(%rsp), %rdi                  # 8-byte Reload
	callq	strcmp
	testl	%eax, %eax
	jne	.LBB11_10
# %bb.9:
	movq	16(%rsp), %rdi
	movq	%r14, %rsi
	movl	%ebx, %edx
	callq	_Z11arraysMatchP8ParticleS0_i
	movl	$.L.str.12, %ecx
	movl	$.L.str.13, %edi
	testb	%al, %al
	cmovneq	%rcx, %rdi
	xorl	%eax, %eax
	callq	printf
.LBB11_10:
	movl	$.Lstr.4, %edi
	callq	puts@PLT
	movq	%r14, %rdi
	callq	free
	movq	16(%rsp), %rdi
	callq	hipFree
.LBB11_11:
	movl	%r12d, %eax
	addq	$72, %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_end11:
	.size	main, .Lfunc_end11-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB12_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB12_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z29gpu_updatePositionAndVelocityP8Particleii, %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_end12:
	.size	__hip_module_ctor, .Lfunc_end12-__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	.LBB13_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
.LBB13_2:
	retq
.Lfunc_end13:
	.size	__hip_module_dtor, .Lfunc_end13-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%f %f %f %f %f %f\n"
	.size	.L.str, 19

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

	.type	.L.str.3,@object                # @.str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.3:
	.asciz	"======== %s: %d, %s: %d, %s: %d\n\n"
	.size	.L.str.3, 34

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"num_particles"
	.size	.L.str.4, 14

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"num_iterations"
	.size	.L.str.5, 15

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

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

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Done in %f!\n\n"
	.size	.L.str.9, 14

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Results match!\n"
	.size	.L.str.12, 16

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Results are wrong!\n"
	.size	.L.str.13, 20

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z29gpu_updatePositionAndVelocityP8Particleii"
	.size	.L__unnamed_1, 46

	.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	"Running the simulations with the following params:"
	.size	.Lstr, 51

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Excluded the CPU experiment...\n"
	.size	.Lstr.1, 32

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"CPU simulation started..."
	.size	.Lstr.2, 26

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"GPU simulation started..."
	.size	.Lstr.3, 26

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"========================================================== \n\n"
	.size	.Lstr.4, 62

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"Usage: ./a NUM_PARTICLES NUM_ITERATIONS TPB INCLUDE_CPU\nExample usage: ./a 10000 10000 32 include_cpu"
	.size	.Lstr.5, 102

	.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 _Z44__device_stub__gpu_updatePositionAndVelocityP8Particleii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z29gpu_updatePositionAndVelocityP8Particleii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z29gpu_updatePositionAndVelocityP8Particleii ; -- Begin function _Z29gpu_updatePositionAndVelocityP8Particleii
	.globl	_Z29gpu_updatePositionAndVelocityP8Particleii
	.p2align	8
	.type	_Z29gpu_updatePositionAndVelocityP8Particleii,@function
_Z29gpu_updatePositionAndVelocityP8Particleii: ; @_Z29gpu_updatePositionAndVelocityP8Particleii
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x1c
	s_load_b64 s[2:3], s[0:1], 0x8
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s4, v[0:1]
	s_mov_b32 s4, exec_lo
	v_cmpx_gt_i32_e64 s3, v1
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b64 s[0:1], s[0:1], 0x0
	v_mad_u64_u32 v[10:11], null, v1, 10, s[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v11, 31, v10
	v_add_nc_u32_e32 v10, v10, v11
	s_delay_alu instid0(VALU_DEP_1)
	v_xor_b32_e32 v10, v10, v11
	s_waitcnt lgkmcnt(0)
	v_mad_i64_i32 v[6:7], null, v1, 24, s[0:1]
	s_ashr_i32 s0, s3, 31
	s_clause 0x1
	global_load_b128 v[2:5], v[6:7], off
	global_load_b64 v[8:9], v[6:7], off offset:16
	s_add_i32 s1, s3, s0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_xor_b32 s0, s1, s0
	v_cvt_f32_u32_e32 v0, s0
	s_sub_i32 s1, 0, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_iflag_f32_e32 v0, v0
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v0, 0x4f7ffffe, v0
	v_cvt_u32_f32_e32 v0, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v1, s1, v0
	v_mul_hi_u32 v1, v0, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v0, v0, v1
	v_mul_hi_u32 v0, v10, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v0, v0, s0
	v_sub_nc_u32_e32 v0, v10, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_subrev_nc_u32_e32 v1, s0, v0
	v_cmp_le_u32_e32 vcc_lo, s0, v0
	v_cndmask_b32_e32 v0, v0, v1, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_subrev_nc_u32_e32 v1, s0, v0
	v_cmp_le_u32_e32 vcc_lo, s0, v0
	v_cndmask_b32_e32 v0, v0, v1, vcc_lo
	v_cvt_f32_i32_e32 v1, s3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_xor_b32_e32 v0, v0, v11
	v_sub_nc_u32_e32 v0, v0, v11
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_f32_i32_e32 v0, v0
	v_div_scale_f32 v10, null, v1, v1, v0
	v_div_scale_f32 v13, vcc_lo, v0, v1, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v11, v10
	s_waitcnt_depctr 0xfff
	v_fma_f32 v12, -v10, v11, 1.0
	v_fmac_f32_e32 v11, v12, v11
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v12, v13, v11
	v_fma_f32 v14, -v10, v12, v13
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v12, v14, v11
	v_fma_f32 v10, -v10, v12, v13
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f32 v10, v10, v11, v12
	v_div_fixup_f32 v10, v10, v1, v0
	s_waitcnt vmcnt(0)
	v_dual_add_f32 v0, v5, v2 :: v_dual_add_f32 v1, v8, v3
	v_add_f32_e32 v2, v9, v4
	s_delay_alu instid0(VALU_DEP_3)
	v_add_f32_e32 v3, v5, v10
	v_add_f32_e32 v4, v8, v10
	v_add_f32_e32 v5, v9, v10
	s_clause 0x1
	global_store_b128 v[6:7], v[0:3], off
	global_store_b64 v[6:7], v[4:5], off offset:16
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z29gpu_updatePositionAndVelocityP8Particleii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.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 15
		.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	_Z29gpu_updatePositionAndVelocityP8Particleii, .Lfunc_end0-_Z29gpu_updatePositionAndVelocityP8Particleii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 440
; NumSgprs: 18
; NumVgprs: 15
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 15
; 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
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z29gpu_updatePositionAndVelocityP8Particleii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z29gpu_updatePositionAndVelocityP8Particleii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     15
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
