#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cuda.h>
#include <cuda_runtime.h>

#define dT 0.2f
#define G 0.6f
#define BLOCK_SIZE 64

// Global variables
int num_planets;
int num_timesteps;

// Host arrays
float2* velocities;
float4* planets;

// Device arrays 
float2* velocities_d;
float4* planets_d;


// Parse command line arguments
void parse_args(int argc, char** argv){
    if(argc != 2){
        printf("Useage: nbody num_timesteps\n");
        exit(-1);
    }
    
    num_timesteps = strtol(argv[1], 0, 10);
}

// Reads planets from planets.txt
void read_planets(){

    FILE* file = fopen("planets.txt", "r");
    if(file == NULL){
        printf("'planets.txt' not found. Exiting\n");
        exit(-1);
    }

    char line[200];
    fgets(line, 200, file);
    sscanf(line, "%d", &num_planets);

    planets = (float4*)malloc(sizeof(float4)*num_planets);
    velocities = (float2*)malloc(sizeof(float2)*num_planets);

    for(int p = 0; p < num_planets; p++){
        fgets(line, 200, file);
        sscanf(line, "%f %f %f %f %f",
                &planets[p].x,
                &planets[p].y,
                &velocities[p].x,
                &velocities[p].y,
                &planets[p].z);
    }

    fclose(file);
}

// Writes planets to file
void write_planets(int timestep){
    char name[20];
    int n = sprintf(name, "planets_out.txt");

    FILE* file = fopen(name, "wr+");

    for(int p = 0; p < num_planets; p++){
        fprintf(file, "%f %f %f %f %f\n",
                planets[p].x,
                planets[p].y,
                velocities[p].x,
                velocities[p].y,
                planets[p].z);
    }

    fclose(file);
}

// TODO 7. Calculate the change in velocity for p, caused by the interaction with q
__device__ float2 calculate_velocity_change_planet(float4 p, float4 q){
    float2 r;
    r.x = q.x - p.x;
    r.y = q.y - p.y;
    if(r.x == 0 && r.y == 0) {
        float2 v = {0.0f, 0.0f};
        return v;
    }
    float abs_dist = sqrt(r.x*r.x + r.y*r.y);
    float dist_cubed = abs_dist*abs_dist*abs_dist;
    float2 dv;
    dv.x = dT*G*q.z/dist_cubed * r.x;
    dv.y = dT*G*q.z/dist_cubed * r.y;
    return dv;
}

// TODO 5. Calculate the change in velocity for my_planet, caused by the interactions with a block of planets
__device__ float2 calculate_velocity_change_block(float4 my_planet, float4* shared_planets){
    float2 velocity = {0.0f, 0.0f};

    for(int i = 0; i < blockDim.x; i++) {
        float2 tempv = calculate_velocity_change_planet(my_planet, shared_planets[i]);
        velocity.x += tempv.x;
        velocity.y += tempv.y;
    }

    return velocity;
}

// TODO 4. Update the velocities by calculating the planet interactions
__global__ void update_velocities(float4* planets, float2* velocities, int num_planets){
    int thread_id = blockIdx.x * blockDim.x + threadIdx.x;
    float4 my_planet = planets[thread_id];

    __shared__ float4 shared_planets[BLOCK_SIZE];

    for(int i = 0; i < num_planets; i += blockDim.x) {
        shared_planets[threadIdx.x] = planets[i + threadIdx.x];
        __syncthreads();

        float2 tempv = calculate_velocity_change_block(my_planet, shared_planets);
        velocities[thread_id].x += tempv.x;
        velocities[thread_id].y += tempv.y;
        __syncthreads();
    }
}

// TODO 7. Update the positions of the planets using the new velocities
__global__ void update_positions(float4* planets, float2* velocities, int num_planets){
    int thread_id = blockIdx.x * blockDim.x + threadIdx.x;
    
    planets[thread_id].x += velocities[thread_id].x * dT;
    planets[thread_id].y += velocities[thread_id].y * dT;
}


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

    parse_args(argc, argv);
    read_planets();

    // TODO 1. Allocate device memory, and transfer data to device 

    cudaMalloc(&planets_d, sizeof(float4)*num_planets);
    cudaMalloc(&velocities_d, sizeof(float2)*num_planets);

    cudaMemcpy(planets_d, planets, sizeof(float4)*num_planets, cudaMemcpyHostToDevice);
    cudaMemcpy(velocities_d, velocities, sizeof(float2)*num_planets, cudaMemcpyHostToDevice);

    // Calculating the number of blocks
    int num_blocks = num_planets/BLOCK_SIZE + ((num_planets%BLOCK_SIZE == 0) ? 0 : 1);

    // Main loop
    for(int t = 0; t < num_timesteps; t++){
        // TODO 2. Call kernels

        update_velocities<<<num_blocks, BLOCK_SIZE>>>(planets_d, velocities_d, num_planets);

        update_positions<<<num_blocks, BLOCK_SIZE>>>(planets_d, velocities_d, num_planets);
    }

    // TODO 3. Transfer data back to host

    cudaMemcpy(velocities, velocities_d, sizeof(float2)*num_planets, cudaMemcpyDeviceToHost);
    cudaMemcpy(planets, planets_d, sizeof(float4)*num_planets, cudaMemcpyDeviceToHost);

    // Output
    write_planets(num_timesteps);
}

	.file	"tmpxft_00294a72_00000000-6_nbody_cuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2035:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Useage: nbody num_timesteps\n"
	.text
	.globl	_Z10parse_argsiPPc
	.type	_Z10parse_argsiPPc, @function
_Z10parse_argsiPPc:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rdx
	.cfi_def_cfa_offset 16
	cmpl	$2, %edi
	je	.L3
	movl	$2, %edi
	leaq	.LC0(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L3:
	movq	8(%rsi), %rdi
	movl	$10, %edx
	xorl	%esi, %esi
	call	__isoc23_strtol@PLT
	movl	%eax, num_timesteps(%rip)
	popq	%rax
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z10parse_argsiPPc, .-_Z10parse_argsiPPc
	.section	.rodata.str1.1
.LC1:
	.string	"r"
.LC2:
	.string	"planets.txt"
.LC3:
	.string	"'planets.txt' not found. Exiting\n"
.LC4:
	.string	"%d"
.LC5:
	.string	"%f %f %f %f %f"
	.text
	.globl	_Z12read_planetsv
	.type	_Z12read_planetsv, @function
_Z12read_planetsv:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC1(%rip), %rsi
	leaq	.LC2(%rip), %rdi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$216, %rsp
	.cfi_def_cfa_offset 256
	movq	%fs:40, %rax
	movq	%rax, 200(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L7
	movl	$2, %edi
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L7:
	movq	%rsp, %r12
	movq	%rax, %rcx
	movl	$200, %edx
	movl	$200, %esi
	movq	%r12, %rdi
	movq	%rax, %rbx
	leaq	.LC5(%rip), %r13
	call	__fgets_chk@PLT
	leaq	num_planets(%rip), %rdx
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	movq	%r12, %rdi
	call	__isoc23_sscanf@PLT
	movslq	num_planets(%rip), %rbp
	movq	%rbp, %rdi
	salq	$4, %rdi
	call	malloc@PLT
	leaq	0(,%rbp,8), %rdi
	xorl	%ebp, %ebp
	movq	%rax, planets(%rip)
	call	malloc@PLT
	movq	%rax, velocities(%rip)
.L8:
	cmpl	%ebp, num_planets(%rip)
	jle	.L12
	movq	%rbx, %rcx
	movl	$200, %edx
	movl	$200, %esi
	movq	%r12, %rdi
	call	__fgets_chk@PLT
	movq	%rbp, %rdx
	movq	%r13, %rsi
	movq	%r12, %rdi
	movq	velocities(%rip), %rax
	salq	$4, %rdx
	addq	planets(%rip), %rdx
	leaq	4(%rdx), %rcx
	leaq	(%rax,%rbp,8), %r8
	pushq	%rax
	.cfi_def_cfa_offset 264
	leaq	8(%rdx), %rax
	incq	%rbp
	pushq	%rax
	.cfi_def_cfa_offset 272
	leaq	4(%r8), %r9
	xorl	%eax, %eax
	call	__isoc23_sscanf@PLT
	popq	%rdx
	.cfi_def_cfa_offset 264
	popq	%rcx
	.cfi_def_cfa_offset 256
	jmp	.L8
.L12:
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	200(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$216, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z12read_planetsv, .-_Z12read_planetsv
	.section	.rodata.str1.1
.LC6:
	.string	"planets_out.txt"
.LC7:
	.string	"wr+"
.LC8:
	.string	"%f %f %f %f %f\n"
	.text
	.globl	_Z13write_planetsi
	.type	_Z13write_planetsi, @function
_Z13write_planetsi:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC6(%rip), %rsi
	leaq	.LC8(%rip), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%ebx, %ebx
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	leaq	4(%rsp), %rdi
	call	strcpy@PLT
	leaq	.LC7(%rip), %rsi
	movq	%rax, %rdi
	call	fopen@PLT
	movq	%rax, %rbp
.L14:
	cmpl	%ebx, num_planets(%rip)
	jle	.L18
	movq	velocities(%rip), %rdx
	movq	%rbx, %rax
	movl	$2, %esi
	movq	%rbp, %rdi
	salq	$4, %rax
	addq	planets(%rip), %rax
	leaq	(%rdx,%rbx,8), %rdx
	cvtss2sd	(%rax), %xmm0
	cvtss2sd	8(%rax), %xmm4
	cvtss2sd	4(%rax), %xmm1
	cvtss2sd	4(%rdx), %xmm3
	cvtss2sd	(%rdx), %xmm2
	movb	$5, %al
	movq	%r12, %rdx
	call	__fprintf_chk@PLT
	incq	%rbx
	jmp	.L14
.L18:
	movq	%rbp, %rdi
	call	fclose@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L16
	call	__stack_chk_fail@PLT
.L16:
	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
.LFE2029:
	.size	_Z13write_planetsi, .-_Z13write_planetsi
	.globl	_Z32calculate_velocity_change_planet6float4S_
	.type	_Z32calculate_velocity_change_planet6float4S_, @function
_Z32calculate_velocity_change_planet6float4S_:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z32calculate_velocity_change_planet6float4S_, .-_Z32calculate_velocity_change_planet6float4S_
	.globl	_Z31calculate_velocity_change_block6float4PS_
	.type	_Z31calculate_velocity_change_block6float4PS_, @function
_Z31calculate_velocity_change_block6float4PS_:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2031:
	.size	_Z31calculate_velocity_change_block6float4PS_, .-_Z31calculate_velocity_change_block6float4PS_
	.globl	_Z52__device_stub__Z17update_velocitiesP6float4P6float2iP6float4P6float2i
	.type	_Z52__device_stub__Z17update_velocitiesP6float4P6float2iP6float4P6float2i, @function
_Z52__device_stub__Z17update_velocitiesP6float4P6float2iP6float4P6float2i:
.LFB2057:
	.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	.L23
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z17update_velocitiesP6float4P6float2i(%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
.L23:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2057:
	.size	_Z52__device_stub__Z17update_velocitiesP6float4P6float2iP6float4P6float2i, .-_Z52__device_stub__Z17update_velocitiesP6float4P6float2iP6float4P6float2i
	.globl	_Z17update_velocitiesP6float4P6float2i
	.type	_Z17update_velocitiesP6float4P6float2i, @function
_Z17update_velocitiesP6float4P6float2i:
.LFB2058:
	.cfi_startproc
	endbr64
	jmp	_Z52__device_stub__Z17update_velocitiesP6float4P6float2iP6float4P6float2i
	.cfi_endproc
.LFE2058:
	.size	_Z17update_velocitiesP6float4P6float2i, .-_Z17update_velocitiesP6float4P6float2i
	.globl	_Z51__device_stub__Z16update_positionsP6float4P6float2iP6float4P6float2i
	.type	_Z51__device_stub__Z16update_positionsP6float4P6float2iP6float4P6float2i, @function
_Z51__device_stub__Z16update_positionsP6float4P6float2iP6float4P6float2i:
.LFB2059:
	.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	.L28
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z16update_positionsP6float4P6float2i(%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
.L28:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L30
	call	__stack_chk_fail@PLT
.L30:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2059:
	.size	_Z51__device_stub__Z16update_positionsP6float4P6float2iP6float4P6float2i, .-_Z51__device_stub__Z16update_positionsP6float4P6float2iP6float4P6float2i
	.globl	_Z16update_positionsP6float4P6float2i
	.type	_Z16update_positionsP6float4P6float2i, @function
_Z16update_positionsP6float4P6float2i:
.LFB2060:
	.cfi_startproc
	endbr64
	jmp	_Z51__device_stub__Z16update_positionsP6float4P6float2iP6float4P6float2i
	.cfi_endproc
.LFE2060:
	.size	_Z16update_positionsP6float4P6float2i, .-_Z16update_positionsP6float4P6float2i
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	$67108865, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	xorl	%ebx, %ebx
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	call	_Z10parse_argsiPPc
	call	_Z12read_planetsv
	movslq	num_planets(%rip), %rsi
	leaq	planets_d(%rip), %rdi
	salq	$4, %rsi
	call	cudaMalloc@PLT
	movslq	num_planets(%rip), %rsi
	leaq	velocities_d(%rip), %rdi
	salq	$3, %rsi
	call	cudaMalloc@PLT
	movslq	num_planets(%rip), %rdx
	movl	$1, %ecx
	movq	planets(%rip), %rsi
	movq	planets_d(%rip), %rdi
	salq	$4, %rdx
	call	cudaMemcpy@PLT
	movslq	num_planets(%rip), %rdx
	movl	$1, %ecx
	movq	velocities(%rip), %rsi
	movq	velocities_d(%rip), %rdi
	salq	$3, %rdx
	call	cudaMemcpy@PLT
	movl	num_planets(%rip), %eax
	movl	$64, %ecx
	testb	$63, %al
	setne	%bl
	cltd
	xorl	%r12d, %r12d
	salq	$6, %rbp
	idivl	%ecx
	addl	%eax, %ebx
.L34:
	cmpl	%r12d, num_timesteps(%rip)
	jle	.L39
	movl	%ebx, 8(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%rbp, %rdx
	movabsq	$4294967297, %rax
	movl	$1, %ecx
	movq	%rbp, 20(%rsp)
	movq	%rax, 12(%rsp)
	movq	8(%rsp), %rdi
	movl	16(%rsp), %esi
	movl	$1, 28(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L35
	movl	num_planets(%rip), %edx
	movq	velocities_d(%rip), %rsi
	movq	planets_d(%rip), %rdi
	call	_Z52__device_stub__Z17update_velocitiesP6float4P6float2iP6float4P6float2i
.L35:
	movl	%ebx, 8(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%rbp, %rdx
	movabsq	$4294967297, %rax
	movl	$1, %ecx
	movq	%rbp, 20(%rsp)
	movq	%rax, 12(%rsp)
	movq	8(%rsp), %rdi
	movl	16(%rsp), %esi
	movl	$1, 28(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L36
	movl	num_planets(%rip), %edx
	movq	velocities_d(%rip), %rsi
	movq	planets_d(%rip), %rdi
	call	_Z51__device_stub__Z16update_positionsP6float4P6float2iP6float4P6float2i
.L36:
	incl	%r12d
	jmp	.L34
.L39:
	movslq	num_planets(%rip), %rdx
	movq	velocities_d(%rip), %rsi
	movl	$2, %ecx
	movq	velocities(%rip), %rdi
	salq	$3, %rdx
	call	cudaMemcpy@PLT
	movslq	num_planets(%rip), %rdx
	movl	$2, %ecx
	movq	planets_d(%rip), %rsi
	movq	planets(%rip), %rdi
	salq	$4, %rdx
	call	cudaMemcpy@PLT
	movl	num_timesteps(%rip), %edi
	call	_Z13write_planetsi
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	xorl	%eax, %eax
	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	main, .-main
	.section	.rodata.str1.1
.LC9:
	.string	"_Z16update_positionsP6float4P6float2i"
.LC10:
	.string	"_Z17update_velocitiesP6float4P6float2i"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2062:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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	.LC9(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z16update_positionsP6float4P6float2i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC10(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z17update_velocitiesP6float4P6float2i(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	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	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2062:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	planets_d
	.bss
	.align 8
	.type	planets_d, @object
	.size	planets_d, 8
planets_d:
	.zero	8
	.globl	velocities_d
	.align 8
	.type	velocities_d, @object
	.size	velocities_d, 8
velocities_d:
	.zero	8
	.globl	planets
	.align 8
	.type	planets, @object
	.size	planets, 8
planets:
	.zero	8
	.globl	velocities
	.align 8
	.type	velocities, @object
	.size	velocities, 8
velocities:
	.zero	8
	.globl	num_timesteps
	.align 4
	.type	num_timesteps, @object
	.size	num_timesteps, 4
num_timesteps:
	.zero	4
	.globl	num_planets
	.align 4
	.type	num_planets, @object
	.size	num_planets, 4
num_planets:
	.zero	4
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

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


#define dT 0.2f
#define G 0.6f
#define BLOCK_SIZE 64

// Global variables
int num_planets;
int num_timesteps;

// Host arrays
float2* velocities;
float4* planets;

// Device arrays 
float2* velocities_d;
float4* planets_d;


// Parse command line arguments
void parse_args(int argc, char** argv){
    if(argc != 2){
        printf("Useage: nbody num_timesteps\n");
        exit(-1);
    }
    
    num_timesteps = strtol(argv[1], 0, 10);
}

// Reads planets from planets.txt
void read_planets(){

    FILE* file = fopen("planets.txt", "r");
    if(file == NULL){
        printf("'planets.txt' not found. Exiting\n");
        exit(-1);
    }

    char line[200];
    fgets(line, 200, file);
    sscanf(line, "%d", &num_planets);

    planets = (float4*)malloc(sizeof(float4)*num_planets);
    velocities = (float2*)malloc(sizeof(float2)*num_planets);

    for(int p = 0; p < num_planets; p++){
        fgets(line, 200, file);
        sscanf(line, "%f %f %f %f %f",
                &planets[p].x,
                &planets[p].y,
                &velocities[p].x,
                &velocities[p].y,
                &planets[p].z);
    }

    fclose(file);
}

// Writes planets to file
void write_planets(int timestep){
    char name[20];
    int n = sprintf(name, "planets_out.txt");

    FILE* file = fopen(name, "wr+");

    for(int p = 0; p < num_planets; p++){
        fprintf(file, "%f %f %f %f %f\n",
                planets[p].x,
                planets[p].y,
                velocities[p].x,
                velocities[p].y,
                planets[p].z);
    }

    fclose(file);
}

// TODO 7. Calculate the change in velocity for p, caused by the interaction with q
__device__ float2 calculate_velocity_change_planet(float4 p, float4 q){
    float2 r;
    r.x = q.x - p.x;
    r.y = q.y - p.y;
    if(r.x == 0 && r.y == 0) {
        float2 v = {0.0f, 0.0f};
        return v;
    }
    float abs_dist = sqrt(r.x*r.x + r.y*r.y);
    float dist_cubed = abs_dist*abs_dist*abs_dist;
    float2 dv;
    dv.x = dT*G*q.z/dist_cubed * r.x;
    dv.y = dT*G*q.z/dist_cubed * r.y;
    return dv;
}

// TODO 5. Calculate the change in velocity for my_planet, caused by the interactions with a block of planets
__device__ float2 calculate_velocity_change_block(float4 my_planet, float4* shared_planets){
    float2 velocity = {0.0f, 0.0f};

    for(int i = 0; i < blockDim.x; i++) {
        float2 tempv = calculate_velocity_change_planet(my_planet, shared_planets[i]);
        velocity.x += tempv.x;
        velocity.y += tempv.y;
    }

    return velocity;
}

// TODO 4. Update the velocities by calculating the planet interactions
__global__ void update_velocities(float4* planets, float2* velocities, int num_planets){
    int thread_id = blockIdx.x * blockDim.x + threadIdx.x;
    float4 my_planet = planets[thread_id];

    __shared__ float4 shared_planets[BLOCK_SIZE];

    for(int i = 0; i < num_planets; i += blockDim.x) {
        shared_planets[threadIdx.x] = planets[i + threadIdx.x];
        __syncthreads();

        float2 tempv = calculate_velocity_change_block(my_planet, shared_planets);
        velocities[thread_id].x += tempv.x;
        velocities[thread_id].y += tempv.y;
        __syncthreads();
    }
}

// TODO 7. Update the positions of the planets using the new velocities
__global__ void update_positions(float4* planets, float2* velocities, int num_planets){
    int thread_id = blockIdx.x * blockDim.x + threadIdx.x;
    
    planets[thread_id].x += velocities[thread_id].x * dT;
    planets[thread_id].y += velocities[thread_id].y * dT;
}


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

    parse_args(argc, argv);
    read_planets();

    // TODO 1. Allocate device memory, and transfer data to device 

    hipMalloc(&planets_d, sizeof(float4)*num_planets);
    hipMalloc(&velocities_d, sizeof(float2)*num_planets);

    hipMemcpy(planets_d, planets, sizeof(float4)*num_planets, hipMemcpyHostToDevice);
    hipMemcpy(velocities_d, velocities, sizeof(float2)*num_planets, hipMemcpyHostToDevice);

    // Calculating the number of blocks
    int num_blocks = num_planets/BLOCK_SIZE + ((num_planets%BLOCK_SIZE == 0) ? 0 : 1);

    // Main loop
    for(int t = 0; t < num_timesteps; t++){
        // TODO 2. Call kernels

        update_velocities<<<num_blocks, BLOCK_SIZE>>>(planets_d, velocities_d, num_planets);

        update_positions<<<num_blocks, BLOCK_SIZE>>>(planets_d, velocities_d, num_planets);
    }

    // TODO 3. Transfer data back to host

    hipMemcpy(velocities, velocities_d, sizeof(float2)*num_planets, hipMemcpyDeviceToHost);
    hipMemcpy(planets, planets_d, sizeof(float4)*num_planets, hipMemcpyDeviceToHost);

    // Output
    write_planets(num_timesteps);
}

	.text
	.file	"nbody_cuda.hip"
	.globl	_Z10parse_argsiPPc              # -- Begin function _Z10parse_argsiPPc
	.type	_Z10parse_argsiPPc,@function
_Z10parse_argsiPPc:                     # @_Z10parse_argsiPPc
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	cmpl	$2, %edi
	jne	.LBB0_2
# %bb.1:
	movq	8(%rsi), %rdi
	xorl	%esi, %esi
	movl	$10, %edx
	callq	__isoc23_strtol
	movl	%eax, num_timesteps(%rip)
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.LBB0_2:
	.cfi_def_cfa_offset 16
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$-1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z10parse_argsiPPc, .Lfunc_end0-_Z10parse_argsiPPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z12read_planetsv               # -- Begin function _Z12read_planetsv
	.type	_Z12read_planetsv,@function
_Z12read_planetsv:                      # @_Z12read_planetsv
	.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	$224, %rsp
	.cfi_def_cfa_offset 272
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$.L.str.1, %edi
	movl	$.L.str.2, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB1_5
# %bb.1:
	movq	%rax, %rbx
	leaq	16(%rsp), %r14
	movq	%r14, %rdi
	movl	$200, %esi
	movq	%rax, %rdx
	callq	fgets
	movl	$.L.str.4, %esi
	movl	$num_planets, %edx
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	__isoc23_sscanf
	movslq	num_planets(%rip), %r14
	movq	%r14, %rdi
	shlq	$4, %rdi
	callq	malloc
	movq	%rax, planets(%rip)
	leaq	(,%r14,8), %rdi
	callq	malloc
	movq	%rax, velocities(%rip)
	testq	%r14, %r14
	jle	.LBB1_4
# %bb.2:                                # %.lr.ph.preheader
	movl	$4, %r15d
	movl	$8, %r12d
	leaq	16(%rsp), %r14
	xorl	%r13d, %r13d
.LBB1_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	movl	$200, %esi
	movq	%rbx, %rdx
	callq	fgets
	movq	planets(%rip), %rax
	leaq	(%rax,%r12), %rsi
	leaq	(%rax,%r12), %rdx
	addq	$-8, %rdx
	leaq	(%rax,%r12), %rcx
	addq	$-4, %rcx
	movq	velocities(%rip), %r9
	leaq	(%r9,%r15), %r8
	addq	$-4, %r8
	addq	%r15, %r9
	movq	%rsi, (%rsp)
	movl	$.L.str.5, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	__isoc23_sscanf
	incq	%r13
	movslq	num_planets(%rip), %rax
	addq	$8, %r15
	addq	$16, %r12
	cmpq	%rax, %r13
	jl	.LBB1_3
.LBB1_4:                                # %._crit_edge
	movq	%rbx, %rdi
	callq	fclose
	addq	$224, %rsp
	.cfi_def_cfa_offset 48
	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
.LBB1_5:
	.cfi_def_cfa_offset 272
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	$-1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z12read_planetsv, .Lfunc_end1-_Z12read_planetsv
	.cfi_endproc
                                        # -- End function
	.globl	_Z13write_planetsi              # -- Begin function _Z13write_planetsi
	.type	_Z13write_planetsi,@function
_Z13write_planetsi:                     # @_Z13write_planetsi
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movaps	.L.str.6(%rip), %xmm0
	movq	%rsp, %rdi
	movaps	%xmm0, (%rdi)
	movl	$.L.str.7, %esi
	callq	fopen
	movq	%rax, %rbx
	cmpl	$0, num_planets(%rip)
	jle	.LBB2_3
# %bb.1:                                # %.lr.ph.preheader
	movl	$4, %r14d
	xorl	%r15d, %r15d
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	planets(%rip), %rax
	xorps	%xmm0, %xmm0
	cvtss2sd	-8(%rax,%r14,2), %xmm0
	xorps	%xmm1, %xmm1
	cvtss2sd	-4(%rax,%r14,2), %xmm1
	movq	velocities(%rip), %rcx
	xorps	%xmm2, %xmm2
	cvtss2sd	-4(%rcx,%r14), %xmm2
	xorps	%xmm3, %xmm3
	cvtss2sd	(%rcx,%r14), %xmm3
	xorps	%xmm4, %xmm4
	cvtss2sd	(%rax,%r14,2), %xmm4
	movl	$.L.str.8, %esi
	movq	%rbx, %rdi
	movb	$5, %al
	callq	fprintf
	incq	%r15
	movslq	num_planets(%rip), %rax
	addq	$8, %r14
	cmpq	%rax, %r15
	jl	.LBB2_2
.LBB2_3:                                # %._crit_edge
	movq	%rbx, %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.Lfunc_end2:
	.size	_Z13write_planetsi, .Lfunc_end2-_Z13write_planetsi
	.cfi_endproc
                                        # -- End function
	.globl	_Z32__device_stub__update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi # -- Begin function _Z32__device_stub__update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.type	_Z32__device_stub__update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi,@function
_Z32__device_stub__update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi: # @_Z32__device_stub__update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.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	$_Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi, %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	_Z32__device_stub__update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi, .Lfunc_end3-_Z32__device_stub__update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.cfi_endproc
                                        # -- End function
	.globl	_Z31__device_stub__update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi # -- Begin function _Z31__device_stub__update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.type	_Z31__device_stub__update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi,@function
_Z31__device_stub__update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi: # @_Z31__device_stub__update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.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	$_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi, %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_end4:
	.size	_Z31__device_stub__update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi, .Lfunc_end4-_Z31__device_stub__update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.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 %r15, -16
	callq	_Z10parse_argsiPPc
	callq	_Z12read_planetsv
	movslq	num_planets(%rip), %rsi
	shlq	$4, %rsi
	movl	$planets_d, %edi
	callq	hipMalloc
	movslq	num_planets(%rip), %rsi
	shlq	$3, %rsi
	movl	$velocities_d, %edi
	callq	hipMalloc
	movq	planets_d(%rip), %rdi
	movq	planets(%rip), %rsi
	movslq	num_planets(%rip), %rdx
	shlq	$4, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	velocities_d(%rip), %rdi
	movq	velocities(%rip), %rsi
	movslq	num_planets(%rip), %rdx
	shlq	$3, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	cmpl	$0, num_timesteps(%rip)
	jle	.LBB5_7
# %bb.1:                                # %.lr.ph
	movabsq	$4294967360, %rbx               # imm = 0x100000040
	movl	num_planets(%rip), %eax
	movl	%eax, %ecx
	andl	$63, %ecx
	leal	63(%rax), %edx
	testl	%eax, %eax
	cmovnsl	%eax, %edx
	sarl	$6, %edx
	cmpl	$1, %ecx
	sbbl	$-1, %edx
	leaq	(%rdx,%rbx), %r14
	addq	$-64, %r14
	xorl	%r15d, %r15d
.LBB5_2:                                # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB5_4
# %bb.3:                                #   in Loop: Header=BB5_2 Depth=1
	movq	planets_d(%rip), %rdi
	movq	velocities_d(%rip), %rsi
	movl	num_planets(%rip), %edx
	callq	_Z32__device_stub__update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
.LBB5_4:                                #   in Loop: Header=BB5_2 Depth=1
	movq	%r14, %rdi
	movl	$1, %esi
	movq	%rbx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB5_6
# %bb.5:                                #   in Loop: Header=BB5_2 Depth=1
	movq	planets_d(%rip), %rdi
	movq	velocities_d(%rip), %rsi
	movl	num_planets(%rip), %edx
	callq	_Z31__device_stub__update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
.LBB5_6:                                #   in Loop: Header=BB5_2 Depth=1
	incl	%r15d
	cmpl	num_timesteps(%rip), %r15d
	jl	.LBB5_2
.LBB5_7:                                # %._crit_edge
	movq	velocities(%rip), %rdi
	movq	velocities_d(%rip), %rsi
	movslq	num_planets(%rip), %rdx
	shlq	$3, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	planets(%rip), %rdi
	movq	planets_d(%rip), %rsi
	movslq	num_planets(%rip), %rdx
	shlq	$4, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	callq	_Z13write_planetsi
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	main, .Lfunc_end5-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:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__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	.LBB7_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
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	num_planets,@object             # @num_planets
	.bss
	.globl	num_planets
	.p2align	2, 0x0
num_planets:
	.long	0                               # 0x0
	.size	num_planets, 4

	.type	num_timesteps,@object           # @num_timesteps
	.globl	num_timesteps
	.p2align	2, 0x0
num_timesteps:
	.long	0                               # 0x0
	.size	num_timesteps, 4

	.type	velocities,@object              # @velocities
	.globl	velocities
	.p2align	3, 0x0
velocities:
	.quad	0
	.size	velocities, 8

	.type	planets,@object                 # @planets
	.globl	planets
	.p2align	3, 0x0
planets:
	.quad	0
	.size	planets, 8

	.type	velocities_d,@object            # @velocities_d
	.globl	velocities_d
	.p2align	3, 0x0
velocities_d:
	.quad	0
	.size	velocities_d, 8

	.type	planets_d,@object               # @planets_d
	.globl	planets_d
	.p2align	3, 0x0
planets_d:
	.quad	0
	.size	planets_d, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"planets.txt"
	.size	.L.str.1, 12

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

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

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

	.type	.L.str.6,@object                # @.str.6
	.section	.rodata.str1.16,"aMS",@progbits,1
	.p2align	4, 0x0
.L.str.6:
	.asciz	"planets_out.txt"
	.size	.L.str.6, 16

	.type	.L.str.7,@object                # @.str.7
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.7:
	.asciz	"wr+"
	.size	.L.str.7, 4

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"%f %f %f %f %f\n"
	.size	.L.str.8, 16

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

	.type	_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi,@object # @_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.globl	_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.p2align	3, 0x0
_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi:
	.quad	_Z31__device_stub__update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.size	_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi, 8

	.type	.L__unnamed_1,@object           # @0
	.section	.rodata.str1.1,"aMS",@progbits,1
.L__unnamed_1:
	.asciz	"_Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi"
	.size	.L__unnamed_1, 58

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi"
	.size	.L__unnamed_2, 57

	.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	"Useage: nbody num_timesteps"
	.size	.Lstr, 28

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"'planets.txt' not found. Exiting"
	.size	.Lstr.1, 33

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z32__device_stub__update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.addrsig_sym _Z31__device_stub__update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym num_planets
	.addrsig_sym velocities_d
	.addrsig_sym planets_d
	.addrsig_sym _Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.addrsig_sym _Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi ; -- Begin function _Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.globl	_Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.p2align	8
	.type	_Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi,@function
_Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi: ; @_Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
; %bb.0:
	s_load_b32 s2, s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	s_cmp_lt_i32 s2, 1
	s_cbranch_scc1 .LBB0_8
; %bb.1:                                ; %.lr.ph
	s_clause 0x1
	s_load_b32 s3, s[0:1], 0x24
	s_load_b128 s[4:7], s[0:1], 0x0
	v_dual_mov_b32 v10, 0 :: v_dual_lshlrev_b32 v11, 4, v0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s1, s3, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[5:6], null, s15, s1, v[0:1]
	s_cmp_lg_u32 s1, 0
	s_cselect_b32 s3, -1, 0
	v_ashrrev_i32_e32 v6, 31, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[1:2], 4, v[5:6]
	v_add_co_u32 v1, vcc_lo, s4, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e32 v2, vcc_lo, s5, v2, vcc_lo
	global_load_b128 v[1:4], v[1:2], off
	s_waitcnt vmcnt(0)
	v_lshlrev_b64 v[3:4], 3, v[5:6]
	v_add_co_u32 v7, vcc_lo, s6, v3
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v8, vcc_lo, s7, v4, vcc_lo
	s_mov_b32 s6, 0
.LBB0_2:                                ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_mov_b32 v12, 0 :: v_dual_add_nc_u32 v9, s6, v0
	v_lshlrev_b64 v[3:4], 4, v[9:10]
	v_mov_b32_e32 v9, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v3, vcc_lo, s4, v3
	v_add_co_ci_u32_e32 v4, vcc_lo, s5, v4, vcc_lo
	s_and_not1_b32 vcc_lo, exec_lo, s3
	global_load_b128 v[3:6], v[3:4], off
	s_waitcnt vmcnt(0)
	ds_store_b128 v11, v[3:6]
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_cbranch_vccnz .LBB0_7
; %bb.3:                                ; %.lr.ph.i.preheader
                                        ;   in Loop: Header=BB0_2 Depth=1
	v_dual_mov_b32 v9, 0 :: v_dual_mov_b32 v12, 0
	s_mov_b32 s7, 0
	s_mov_b32 s8, s1
.LBB0_4:                                ; %.lr.ph.i
                                        ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	v_mov_b32_e32 v3, s7
	v_mov_b32_e32 v13, 0
	ds_load_b128 v[3:6], v3
	s_waitcnt lgkmcnt(0)
	v_mov_b32_e32 v6, 0
	v_dual_sub_f32 v4, v4, v2 :: v_dual_sub_f32 v3, v3, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_neq_f32_e64 s0, 0, v4
	v_cmp_neq_f32_e32 vcc_lo, 0, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s0, vcc_lo, s0
	s_and_saveexec_b32 s9, s0
	s_cbranch_execz .LBB0_6
; %bb.5:                                ;   in Loop: Header=BB0_4 Depth=2
	v_dual_mul_f32 v6, v4, v4 :: v_dual_mul_f32 v5, 0x3df5c290, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v6, v3, v3
	v_mul_f32_e32 v13, 0x4f800000, v6
	v_cmp_gt_f32_e32 vcc_lo, 0xf800000, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v6, v6, v13, vcc_lo
	v_sqrt_f32_e32 v13, v6
	s_waitcnt_depctr 0xfff
	v_add_nc_u32_e32 v14, -1, v13
	v_add_nc_u32_e32 v15, 1, v13
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v16, -v14, v13, v6
	v_fma_f32 v17, -v15, v13, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_f32_e64 s0, 0, v16
	v_cndmask_b32_e64 v13, v13, v14, s0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_lt_f32_e64 s0, 0, v17
	v_cndmask_b32_e64 v13, v13, v15, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v14, 0x37800000, v13
	v_cndmask_b32_e32 v13, v13, v14, vcc_lo
	v_cmp_class_f32_e64 vcc_lo, v6, 0x260
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v6, v13, v6, vcc_lo
	v_mul_f32_e32 v13, v6, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v6, v6, v13
	v_div_scale_f32 v13, null, v6, v6, v5
	v_div_scale_f32 v16, vcc_lo, v5, v6, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v14, v13
	s_waitcnt_depctr 0xfff
	v_fma_f32 v15, -v13, v14, 1.0
	v_fmac_f32_e32 v14, v15, v14
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v15, v16, v14
	v_fma_f32 v17, -v13, v15, v16
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v15, v17, v14
	v_fma_f32 v13, -v13, v15, v16
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f32 v13, v13, v14, v15
	v_div_fixup_f32 v5, v13, v6, v5
	s_delay_alu instid0(VALU_DEP_1)
	v_mul_f32_e32 v13, v3, v5
	v_mul_f32_e32 v6, v4, v5
.LBB0_6:                                ; %_Z32calculate_velocity_change_planet15HIP_vector_typeIfLj4EES0_.exit.i
                                        ;   in Loop: Header=BB0_4 Depth=2
	s_or_b32 exec_lo, exec_lo, s9
	s_delay_alu instid0(VALU_DEP_1)
	v_dual_add_f32 v9, v9, v13 :: v_dual_add_f32 v12, v12, v6
	s_add_i32 s8, s8, -1
	s_add_i32 s7, s7, 16
	s_cmp_lg_u32 s8, 0
	s_cbranch_scc1 .LBB0_4
.LBB0_7:                                ; %_Z31calculate_velocity_change_block15HIP_vector_typeIfLj4EEPS0_.exit
                                        ;   in Loop: Header=BB0_2 Depth=1
	global_load_b64 v[3:4], v[7:8], off
	s_add_i32 s6, s6, s1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_ge_i32 s6, s2
	s_waitcnt vmcnt(0)
	v_dual_add_f32 v3, v9, v3 :: v_dual_add_f32 v4, v12, v4
	global_store_b64 v[7:8], v[3:4], off
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_cbranch_scc0 .LBB0_2
.LBB0_8:                                ; %._crit_edge
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
		.amdhsa_group_segment_fixed_size 1024
		.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 18
		.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	_Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi, .Lfunc_end0-_Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 684
; NumSgprs: 18
; NumVgprs: 18
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 1024 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 18
; 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
	.protected	_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi ; -- Begin function _Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.globl	_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
	.p2align	8
	.type	_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi,@function
_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi: ; @_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
; %bb.0:
	s_clause 0x1
	s_load_b32 s4, s[0:1], 0x24
	s_load_b128 s[0:3], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s4, s4, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s4, v[0:1]
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshlrev_b64 v[3:4], 3, v[1:2]
	v_lshlrev_b64 v[0:1], 4, v[1:2]
	v_add_co_u32 v2, vcc_lo, s2, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_ci_u32_e32 v3, vcc_lo, s3, v4, vcc_lo
	v_add_co_u32 v0, vcc_lo, s0, v0
	s_delay_alu instid0(VALU_DEP_4)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_load_b32 v6, v[2:3], off
	global_load_b64 v[4:5], v[0:1], off
	s_waitcnt vmcnt(0)
	v_fmamk_f32 v4, v6, 0x3e4ccccd, v4
	global_store_b32 v[0:1], v4, off
	global_load_b32 v2, v[2:3], off offset:4
	s_waitcnt vmcnt(0)
	v_fmac_f32_e32 v5, 0x3e4ccccd, v2
	global_store_b32 v[0:1], v5, off offset:4
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
		.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 7
		.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_end1:
	.size	_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi, .Lfunc_end1-_Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 176
; NumSgprs: 18
; NumVgprs: 7
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 7
; 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: 1024
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z17update_velocitiesP15HIP_vector_typeIfLj4EEPS_IfLj2EEi.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     18
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
  - .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:           _Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z16update_positionsP15HIP_vector_typeIfLj4EEPS_IfLj2EEi.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     7
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
