#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <sys/time.h>
#include <algorithm>
#include <unistd.h>

#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)

// Max threadsize is 1024 32*32
typedef unsigned char ubyte;

void printWorld(ubyte *world, uint size);
void zeroWorld(ubyte *world, uint size);
void copy(ubyte *pattern, int patternsize, ubyte *world, uint size);
int coords(int x, int y, int size);

__global__ void game_of_life_turn(ubyte *world, ubyte *buffer_world, short size)
{

    // We need to find the x,y of the cell we are looking at
    // Because this is a 1d array we have to do some maths.
    uint x = threadIdx.x + (blockDim.x * blockIdx.x);
    uint y = threadIdx.y + (blockDim.y * blockIdx.y);

    // Find the y rows
    uint y_up = (y + 1) % size;
    uint y_down = (y + size - 1) % size;

    // Find the y offsets
    uint y_offset = y * size;
    uint y_up_offset = y_up * size;
    uint y_down_offset = y_down * size;

    //printf("(%d,%d) (%d %d) (%d %d) (%d, %d) (%d, %d)\n", x, y, threadIdx.x, threadIdx.y, blockDim.x, blockDim.y, blockIdx.x, blockIdx.y, gridDim.x, gridDim.y);
    uint x_left = (x - 1 + size) % size;
    uint x_right = (x + 1) % size;

    uint offset = x + y_offset;

    uint aliveCells = world[x_left + y_up_offset] +
                      world[x + y_up_offset] +
                      world[x_right + y_up_offset] +
                      world[x_left + y_offset] +
                      world[x_right + y_offset] +
                      world[x_left + y_down_offset] +
                      world[x + y_down_offset] +
                      world[x_right + y_down_offset];

    //Any live cell with two or three live neighbours survives.
    //Any dead cell with three live neighbours becomes a live cell.
    //All other live cells die in the next generation. Similarly, all other dead cells stay dead.
    buffer_world[offset] = aliveCells == 3 || (aliveCells == 2 && world[offset]) ? 1 : 0;
}

int main()
{
    // To keep the math easy the size of the world must be a square of a square, i.e. X^2^2
    // This is so we can easily divide up the world into square blocks for processing
    // To make it even easier size should be a poer of 2, i.e. 2^X
    uint size = 256;
    int turns = 10000;

    uint ncells = size * size;

    // With the max number of threads being 1024
    // The number of threads here will describe the number of blocks
    uint threadsCount = min(ncells, 1024);
    uint threadDimSize = sqrt(threadsCount);

    // Threads create a block of sqrt(threadCount)^2
    dim3 threadsPerBlock(threadDimSize, threadDimSize);

    // Now we need to find the number of blocks this is the size/ThreadDimSize
    uint blockDimSize = size / threadDimSize;
    dim3 numBlocks(blockDimSize, blockDimSize);

    // Lets make sure our math is correct
    // The number of cells is a multiple of threadcount
    assert(ncells % threadsCount == 0);
    // the number of blocks * num of threads = size
    assert(blockDimSize * threadDimSize == size);

    //printf("Size %d, ncells %d, Threads: %d, ThreadDimSize %d, BlockDimSize %d\n", size, ncells, threadsCount, threadDimSize, blockDimSize);

    // We make a 1d array of bytes, ehere each byte is a cell, to describe the world
    ubyte *host_world;
    int worldSize = sizeof(ubyte) * ncells;
    host_world = (ubyte *)malloc(worldSize);

    // We setup the world by first zeroing it out, then copying a pattern (this is the glider)
    zeroWorld(host_world, size);
    ubyte pattern[5][5] = {
        {0, 0, 0, 1, 0},
        {0, 1, 0, 1, 0},
        {0, 0, 1, 1, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
    };
    copy((ubyte *)pattern, 5, host_world, size);
    // printWorld(host_world, size);

    // Set up the Device Memory by create the world and a buffer
    // Then by Mallocing on the device, then copying the world over to the device
    ubyte *device_world, *device_buffer_world;
    cudaMalloc((void **)&device_world, worldSize);
    cudaMalloc((void **)&device_buffer_world, worldSize);
    cudaMemcpy(device_world, host_world, worldSize, cudaMemcpyHostToDevice);

    // Time some stuff
    struct timeval t0, t1;
    gettimeofday(&t0, NULL);

    // Run the world
    int turn;
    for (turn = 0; turn < turns; turn++)
    {
        game_of_life_turn<<<numBlocks, threadsPerBlock>>>(device_world, device_buffer_world, size);
        std::swap(device_world, device_buffer_world);
    }

    // Finish timing
    gettimeofday(&t1, NULL);

    // Copy the value of the world back to host memory
    cudaMemcpy(host_world, device_world, worldSize, cudaMemcpyDeviceToHost);

    // system("clear");
    // printWorld(host_world, size);

    // How many seconds it took to execute
    float seconds = t1.tv_sec - t0.tv_sec + 1E-6 * (t1.tv_usec - t0.tv_usec);
    // How many total calculations
    float MMcellCalculations = (1.0 * turns * ncells) / 1000000;
    // Millions of Calculations per second
    float MMcellsCalculatedperSecond = MMcellCalculations / seconds;

    printf("CUDA: %d, %f MMCps in %f\n", size, MMcellsCalculatedperSecond, seconds);

    // Free all the Device and host memory
    cudaFree(device_world);
    cudaFree(device_buffer_world);
    free(host_world);

    return 0;
}

void copy(ubyte *pattern, int patternsize, ubyte *world, uint size)
{

    ubyte x, y;
    for (y = 0; y < patternsize; y++)
    {
        for (x = 0; x < patternsize; x++)
        {
            world[x + (size * y)] = pattern[x + (y * patternsize)];
        }
    }
}

void zeroWorld(ubyte *world, uint size)
{
    int x, y;

    for (y = 0; y < size; ++y)
    {
        for (x = 0; x < size; ++x)
        {
            world[x + (y * size)] = 0;
        }
    }
}

void printWorld(ubyte *world, uint size)
{
    int x, y;

    printf("   ------   \n");
    for (y = 0; y < size; y++)
    {
        for (x = 0; x < size; x++)
        {
            printf("%d", world[x + (y * size)]);
        }
        printf("\n");
    }
    printf("   ------   \n\n");
}

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

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2328:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2328:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z4copyPhiS_j
	.type	_Z4copyPhiS_j, @function
_Z4copyPhiS_j:
.LFB2323:
	.cfi_startproc
	endbr64
	xorl	%r8d, %r8d
	movq	%rdi, %r9
	movq	%rdx, %r10
	movzbl	%r8b, %eax
	cmpl	%esi, %eax
	jl	.L9
	ret
.L9:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
.L5:
	movl	%eax, %ebx
	imull	%ecx, %eax
	xorl	%edi, %edi
	imull	%esi, %ebx
	movl	%eax, %edx
	xorl	%eax, %eax
.L4:
	addl	%ebx, %eax
	cltq
	movb	(%r9,%rax), %r11b
	movzbl	%dil, %eax
	incl	%edi
	addl	%edx, %eax
	movl	%eax, %eax
	movb	%r11b, (%r10,%rax)
	movzbl	%dil, %eax
	cmpl	%esi, %eax
	jl	.L4
	incl	%r8d
	movzbl	%r8b, %eax
	cmpl	%esi, %eax
	jl	.L5
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2323:
	.size	_Z4copyPhiS_j, .-_Z4copyPhiS_j
	.globl	_Z9zeroWorldPhj
	.type	_Z9zeroWorldPhj, @function
_Z9zeroWorldPhj:
.LFB2324:
	.cfi_startproc
	endbr64
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.L12:
	cmpl	%esi, %edx
	je	.L16
	xorl	%eax, %eax
.L13:
	leal	(%rax,%rcx), %r8d
	incl	%eax
	movb	$0, (%rdi,%r8)
	cmpl	%esi, %eax
	jne	.L13
	incl	%edx
	addl	%esi, %ecx
	jmp	.L12
.L16:
	ret
	.cfi_endproc
.LFE2324:
	.size	_Z9zeroWorldPhj, .-_Z9zeroWorldPhj
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"   ------   \n"
.LC1:
	.string	"%d"
.LC2:
	.string	"\n"
.LC3:
	.string	"   ------   \n\n"
	.text
	.globl	_Z10printWorldPhj
	.type	_Z10printWorldPhj, @function
_Z10printWorldPhj:
.LFB2325:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%eax, %eax
	xorl	%r15d, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	%esi, %r14d
	leaq	.LC0(%rip), %rsi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdi, %r13
	movl	$2, %edi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%r14, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	pushq	%rcx
	.cfi_def_cfa_offset 64
	call	__printf_chk@PLT
.L18:
	cmpq	%r14, %r12
	je	.L23
	movl	%r15d, %ebx
.L19:
	movl	%ebx, %eax
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	incl	%ebx
	movzbl	0(%r13,%rax), %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leal	(%r15,%rbp), %eax
	cmpl	%eax, %ebx
	jne	.L19
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	incq	%r12
	movl	%ebx, %r15d
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L18
.L23:
	popq	%rdx
	.cfi_def_cfa_offset 56
	leaq	.LC3(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 48
	movl	$2, %edi
	popq	%rbp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2325:
	.size	_Z10printWorldPhj, .-_Z10printWorldPhj
	.globl	_Z40__device_stub__Z17game_of_life_turnPhS_sPhS_s
	.type	_Z40__device_stub__Z17game_of_life_turnPhS_sPhS_s, @function
_Z40__device_stub__Z17game_of_life_turnPhS_sPhS_s:
.LFB2350:
	.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
	movw	%dx, 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	.L24
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z17game_of_life_turnPhS_s(%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
.L24:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L26
	call	__stack_chk_fail@PLT
.L26:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2350:
	.size	_Z40__device_stub__Z17game_of_life_turnPhS_sPhS_s, .-_Z40__device_stub__Z17game_of_life_turnPhS_sPhS_s
	.globl	_Z17game_of_life_turnPhS_s
	.type	_Z17game_of_life_turnPhS_s, @function
_Z17game_of_life_turnPhS_s:
.LFB2351:
	.cfi_startproc
	endbr64
	movswl	%dx, %edx
	jmp	_Z40__device_stub__Z17game_of_life_turnPhS_sPhS_s
	.cfi_endproc
.LFE2351:
	.size	_Z17game_of_life_turnPhS_s, .-_Z17game_of_life_turnPhS_s
	.section	.rodata.str1.1
.LC6:
	.string	"CUDA: %d, %f MMCps in %f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2320:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	$65536, %edi
	movl	$10000, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	subq	$120, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	movabsq	$137438953504, %rax
	movl	$1, 24(%rsp)
	movl	$1, 36(%rsp)
	movq	%rax, 16(%rsp)
	movabsq	$34359738376, %rax
	movq	%rax, 28(%rsp)
	call	malloc@PLT
	movl	$256, %esi
	movq	%rax, %rdi
	movq	%rax, %rbx
	call	_Z9zeroWorldPhj
	movl	$16777217, %eax
	movl	$256, %ecx
	movabsq	$1103806595073, %rdx
	salq	$24, %rax
	movq	%rdx, 87(%rsp)
	xorl	%edx, %edx
	leaq	79(%rsp), %rdi
	movq	%rax, 79(%rsp)
	movl	$257, %eax
	movl	$5, %esi
	salq	$24, %rax
	movq	%rdx, 96(%rsp)
	movq	%rbx, %rdx
	movq	%rax, 88(%rsp)
	call	_Z4copyPhiS_j
	movq	%rsp, %rdi
	movl	$65536, %esi
	call	cudaMalloc@PLT
	leaq	8(%rsp), %rdi
	movl	$65536, %esi
	call	cudaMalloc@PLT
	movq	(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$65536, %edx
	call	cudaMemcpy@PLT
	leaq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	gettimeofday@PLT
.L31:
	movl	24(%rsp), %ecx
	movq	16(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	28(%rsp), %rdi
	movl	36(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L30
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdi
	movl	$256, %edx
	call	_Z40__device_stub__Z17game_of_life_turnPhS_sPhS_s
.L30:
	movq	(%rsp), %rax
	movq	8(%rsp), %rdx
	movq	%rax, 8(%rsp)
	movq	%rdx, (%rsp)
	decl	%ebp
	jne	.L31
	xorl	%esi, %esi
	leaq	56(%rsp), %rdi
	call	gettimeofday@PLT
	movq	(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdi
	movl	$65536, %edx
	call	cudaMemcpy@PLT
	movq	64(%rsp), %rax
	subq	48(%rsp), %rax
	movl	$256, %edx
	cvtsi2sdq	%rax, %xmm1
	movq	56(%rsp), %rax
	subq	40(%rsp), %rax
	leaq	.LC6(%rip), %rsi
	mulsd	.LC4(%rip), %xmm1
	cvtsi2sdq	%rax, %xmm0
	movl	$2, %edi
	movb	$2, %al
	addsd	%xmm0, %xmm1
	movss	.LC5(%rip), %xmm0
	cvtsd2ss	%xmm1, %xmm1
	divss	%xmm1, %xmm0
	cvtss2sd	%xmm1, %xmm1
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
	call	__stack_chk_fail@PLT
.L32:
	addq	$120, %rsp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2320:
	.size	main, .-main
	.section	.rodata.str1.1
.LC7:
	.string	"_Z17game_of_life_turnPhS_s"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2353:
	.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	.LC7(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z17game_of_life_turnPhS_s(%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
.LFE2353:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC4:
	.long	-1598689907
	.long	1051772663
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC5:
	.long	1143199498
	.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 <assert.h>
#include <hip/hip_runtime.h>

#include <sys/time.h>
#include <algorithm>
#include <unistd.h>

#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)

// Max threadsize is 1024 32*32
typedef unsigned char ubyte;

void printWorld(ubyte *world, uint size);
void zeroWorld(ubyte *world, uint size);
void copy(ubyte *pattern, int patternsize, ubyte *world, uint size);
int coords(int x, int y, int size);

__global__ void game_of_life_turn(ubyte *world, ubyte *buffer_world, short size)
{

    // We need to find the x,y of the cell we are looking at
    // Because this is a 1d array we have to do some maths.
    uint x = threadIdx.x + (blockDim.x * blockIdx.x);
    uint y = threadIdx.y + (blockDim.y * blockIdx.y);

    // Find the y rows
    uint y_up = (y + 1) % size;
    uint y_down = (y + size - 1) % size;

    // Find the y offsets
    uint y_offset = y * size;
    uint y_up_offset = y_up * size;
    uint y_down_offset = y_down * size;

    //printf("(%d,%d) (%d %d) (%d %d) (%d, %d) (%d, %d)\n", x, y, threadIdx.x, threadIdx.y, blockDim.x, blockDim.y, blockIdx.x, blockIdx.y, gridDim.x, gridDim.y);
    uint x_left = (x - 1 + size) % size;
    uint x_right = (x + 1) % size;

    uint offset = x + y_offset;

    uint aliveCells = world[x_left + y_up_offset] +
                      world[x + y_up_offset] +
                      world[x_right + y_up_offset] +
                      world[x_left + y_offset] +
                      world[x_right + y_offset] +
                      world[x_left + y_down_offset] +
                      world[x + y_down_offset] +
                      world[x_right + y_down_offset];

    //Any live cell with two or three live neighbours survives.
    //Any dead cell with three live neighbours becomes a live cell.
    //All other live cells die in the next generation. Similarly, all other dead cells stay dead.
    buffer_world[offset] = aliveCells == 3 || (aliveCells == 2 && world[offset]) ? 1 : 0;
}

int main()
{
    // To keep the math easy the size of the world must be a square of a square, i.e. X^2^2
    // This is so we can easily divide up the world into square blocks for processing
    // To make it even easier size should be a poer of 2, i.e. 2^X
    uint size = 256;
    int turns = 10000;

    uint ncells = size * size;

    // With the max number of threads being 1024
    // The number of threads here will describe the number of blocks
    uint threadsCount = min(ncells, 1024);
    uint threadDimSize = sqrt(threadsCount);

    // Threads create a block of sqrt(threadCount)^2
    dim3 threadsPerBlock(threadDimSize, threadDimSize);

    // Now we need to find the number of blocks this is the size/ThreadDimSize
    uint blockDimSize = size / threadDimSize;
    dim3 numBlocks(blockDimSize, blockDimSize);

    // Lets make sure our math is correct
    // The number of cells is a multiple of threadcount
    assert(ncells % threadsCount == 0);
    // the number of blocks * num of threads = size
    assert(blockDimSize * threadDimSize == size);

    //printf("Size %d, ncells %d, Threads: %d, ThreadDimSize %d, BlockDimSize %d\n", size, ncells, threadsCount, threadDimSize, blockDimSize);

    // We make a 1d array of bytes, ehere each byte is a cell, to describe the world
    ubyte *host_world;
    int worldSize = sizeof(ubyte) * ncells;
    host_world = (ubyte *)malloc(worldSize);

    // We setup the world by first zeroing it out, then copying a pattern (this is the glider)
    zeroWorld(host_world, size);
    ubyte pattern[5][5] = {
        {0, 0, 0, 1, 0},
        {0, 1, 0, 1, 0},
        {0, 0, 1, 1, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
    };
    copy((ubyte *)pattern, 5, host_world, size);
    // printWorld(host_world, size);

    // Set up the Device Memory by create the world and a buffer
    // Then by Mallocing on the device, then copying the world over to the device
    ubyte *device_world, *device_buffer_world;
    hipMalloc((void **)&device_world, worldSize);
    hipMalloc((void **)&device_buffer_world, worldSize);
    hipMemcpy(device_world, host_world, worldSize, hipMemcpyHostToDevice);

    // Time some stuff
    struct timeval t0, t1;
    gettimeofday(&t0, NULL);

    // Run the world
    int turn;
    for (turn = 0; turn < turns; turn++)
    {
        game_of_life_turn<<<numBlocks, threadsPerBlock>>>(device_world, device_buffer_world, size);
        std::swap(device_world, device_buffer_world);
    }

    // Finish timing
    gettimeofday(&t1, NULL);

    // Copy the value of the world back to host memory
    hipMemcpy(host_world, device_world, worldSize, hipMemcpyDeviceToHost);

    // system("clear");
    // printWorld(host_world, size);

    // How many seconds it took to execute
    float seconds = t1.tv_sec - t0.tv_sec + 1E-6 * (t1.tv_usec - t0.tv_usec);
    // How many total calculations
    float MMcellCalculations = (1.0 * turns * ncells) / 1000000;
    // Millions of Calculations per second
    float MMcellsCalculatedperSecond = MMcellCalculations / seconds;

    printf("CUDA: %d, %f MMCps in %f\n", size, MMcellsCalculatedperSecond, seconds);

    // Free all the Device and host memory
    hipFree(device_world);
    hipFree(device_buffer_world);
    free(host_world);

    return 0;
}

void copy(ubyte *pattern, int patternsize, ubyte *world, uint size)
{

    ubyte x, y;
    for (y = 0; y < patternsize; y++)
    {
        for (x = 0; x < patternsize; x++)
        {
            world[x + (size * y)] = pattern[x + (y * patternsize)];
        }
    }
}

void zeroWorld(ubyte *world, uint size)
{
    int x, y;

    for (y = 0; y < size; ++y)
    {
        for (x = 0; x < size; ++x)
        {
            world[x + (y * size)] = 0;
        }
    }
}

void printWorld(ubyte *world, uint size)
{
    int x, y;

    printf("   ------   \n");
    for (y = 0; y < size; y++)
    {
        for (x = 0; x < size; x++)
        {
            printf("%d", world[x + (y * size)]);
        }
        printf("\n");
    }
    printf("   ------   \n\n");
}

	.text
	.file	"main.hip"
	.globl	_Z32__device_stub__game_of_life_turnPhS_s # -- Begin function _Z32__device_stub__game_of_life_turnPhS_s
	.type	_Z32__device_stub__game_of_life_turnPhS_s,@function
_Z32__device_stub__game_of_life_turnPhS_s: # @_Z32__device_stub__game_of_life_turnPhS_s
	.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	14(%rsp), %rsi
	movw	%dx, (%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	$_Z17game_of_life_turnPhS_s, %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_end0:
	.size	_Z32__device_stub__game_of_life_turnPhS_s, .Lfunc_end0-_Z32__device_stub__game_of_life_turnPhS_s
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI1_1:
	.long	0x4423d70a                      # float 655.359985
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:                                # %_Z9zeroWorldPhj.exit
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$88, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$1, %edi
	movl	$65536, %esi                    # imm = 0x10000
	callq	calloc@PLT
	movq	%rax, %rbx
	movabsq	$281474993487872, %rax          # imm = 0x1000001000000
	movq	%rax, 16(%rsp)
	movl	$1, 24(%rsp)
	movw	$257, 28(%rsp)                  # imm = 0x101
	movq	$0, 30(%rsp)
	movl	$0, 37(%rsp)
	xorl	%eax, %eax
	movq	%rbx, %rcx
.LBB1_1:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movb	20(%rsp,%rax), %dl
	movb	%dl, 4(%rcx)
	movl	16(%rsp,%rax), %edx
	movl	%edx, (%rcx)
	addq	$5, %rax
	addq	$256, %rcx                      # imm = 0x100
	cmpq	$25, %rax
	jne	.LBB1_1
# %bb.2:                                # %_Z4copyPhiS_j.exit
	movq	%rsp, %r14
	movl	$65536, %esi                    # imm = 0x10000
	movq	%r14, %rdi
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movl	$65536, %esi                    # imm = 0x10000
	callq	hipMalloc
	movq	(%r14), %rdi
	movl	$65536, %edx                    # imm = 0x10000
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	56(%rsp), %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movl	$10000, %ebp                    # imm = 0x2710
	movabsq	$34359738376, %r14              # imm = 0x800000008
	movabsq	$137438953504, %r15             # imm = 0x2000000020
.LBB1_3:                                # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	movl	$1, %esi
	movq	%r15, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_5
# %bb.4:                                #   in Loop: Header=BB1_3 Depth=1
	movq	(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	$256, %edx                      # imm = 0x100
	callq	_Z32__device_stub__game_of_life_turnPhS_s
.LBB1_5:                                #   in Loop: Header=BB1_3 Depth=1
	movq	(%rsp), %rax
	movq	8(%rsp), %rcx
	movq	%rcx, (%rsp)
	movq	%rax, 8(%rsp)
	decl	%ebp
	jne	.LBB1_3
# %bb.6:
	leaq	72(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	gettimeofday
	movq	(%rsp), %rsi
	movl	$65536, %edx                    # imm = 0x10000
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	(%r14), %rax
	movq	8(%r14), %rcx
	subq	56(%rsp), %rax
	cvtsi2sd	%rax, %xmm0
	subq	64(%rsp), %rcx
	cvtsi2sd	%rcx, %xmm1
	mulsd	.LCPI1_0(%rip), %xmm1
	addsd	%xmm0, %xmm1
	cvtsd2ss	%xmm1, %xmm1
	movss	.LCPI1_1(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	divss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm1
	movl	$.L.str, %edi
	movl	$256, %esi                      # imm = 0x100
	movb	$2, %al
	callq	printf
	movq	(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$88, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z9zeroWorldPhj                 # -- Begin function _Z9zeroWorldPhj
	.type	_Z9zeroWorldPhj,@function
_Z9zeroWorldPhj:                        # @_Z9zeroWorldPhj
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	je	.LBB2_5
# %bb.1:                                # %.preheader.preheader
	movl	%esi, %eax
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.LBB2_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_3 Depth 2
	xorl	%esi, %esi
.LBB2_3:                                #   Parent Loop BB2_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%rcx,%rsi), %r8d
	movb	$0, (%rdi,%r8)
	incq	%rsi
	cmpq	%rsi, %rax
	jne	.LBB2_3
# %bb.4:                                #   in Loop: Header=BB2_2 Depth=1
	incl	%edx
	addq	%rax, %rcx
	cmpl	%eax, %edx
	jne	.LBB2_2
.LBB2_5:                                # %._crit_edge
	retq
.Lfunc_end2:
	.size	_Z9zeroWorldPhj, .Lfunc_end2-_Z9zeroWorldPhj
	.cfi_endproc
                                        # -- End function
	.globl	_Z4copyPhiS_j                   # -- Begin function _Z4copyPhiS_j
	.type	_Z4copyPhiS_j,@function
_Z4copyPhiS_j:                          # @_Z4copyPhiS_j
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB3_6
# %bb.1:                                # %.lr.ph.preheader
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movl	%ecx, %eax
	xorl	%ecx, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
.LBB3_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_3 Depth 2
	movl	%ecx, %r10d
	addq	%rdi, %r10
	xorl	%r11d, %r11d
.LBB3_3:                                #   Parent Loop BB3_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movb	(%r10,%r11), %bl
	leal	(%r8,%r11), %r14d
	movb	%bl, (%rdx,%r14)
	incq	%r11
	movzbl	%r11b, %ebx
	cmpl	%esi, %ebx
	jb	.LBB3_3
# %bb.4:                                # %._crit_edge
                                        #   in Loop: Header=BB3_2 Depth=1
	incl	%r9d
	movzbl	%r9b, %r10d
	addq	%rax, %r8
	addl	%esi, %ecx
	cmpl	%esi, %r10d
	jb	.LBB3_2
# %bb.5:
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
.LBB3_6:                                # %._crit_edge19
	retq
.Lfunc_end3:
	.size	_Z4copyPhiS_j, .Lfunc_end3-_Z4copyPhiS_j
	.cfi_endproc
                                        # -- End function
	.globl	_Z10printWorldPhj               # -- Begin function _Z10printWorldPhj
	.type	_Z10printWorldPhj,@function
_Z10printWorldPhj:                      # @_Z10printWorldPhj
	.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	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movq	%rdi, %rbx
	movl	$.Lstr, %edi
	callq	puts@PLT
	testl	%ebp, %ebp
	je	.LBB4_5
# %bb.1:                                # %.preheader.preheader
	movl	%ebp, %r14d
	xorl	%r15d, %r15d
	xorl	%ebp, %ebp
.LBB4_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_3 Depth 2
	xorl	%r12d, %r12d
.LBB4_3:                                #   Parent Loop BB4_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	leal	(%r15,%r12), %eax
	movzbl	(%rbx,%rax), %esi
	movl	$.L.str.2, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r12
	cmpq	%r12, %r14
	jne	.LBB4_3
# %bb.4:                                #   in Loop: Header=BB4_2 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incl	%ebp
	addq	%r14, %r15
	cmpl	%r14d, %ebp
	jne	.LBB4_2
.LBB4_5:                                # %._crit_edge
	movl	$.Lstr.1, %edi
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	puts@PLT                        # TAILCALL
.Lfunc_end4:
	.size	_Z10printWorldPhj, .Lfunc_end4-_Z10printWorldPhj
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z17game_of_life_turnPhS_s, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB6_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z17game_of_life_turnPhS_s,@object # @_Z17game_of_life_turnPhS_s
	.section	.rodata,"a",@progbits
	.globl	_Z17game_of_life_turnPhS_s
	.p2align	3, 0x0
_Z17game_of_life_turnPhS_s:
	.quad	_Z32__device_stub__game_of_life_turnPhS_s
	.size	_Z17game_of_life_turnPhS_s, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"CUDA: %d, %f MMCps in %f\n"
	.size	.L.str, 26

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

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z17game_of_life_turnPhS_s"
	.size	.L__unnamed_1, 27

	.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	"   ------   "
	.size	.Lstr, 13

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"   ------   \n"
	.size	.Lstr.1, 14

	.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__game_of_life_turnPhS_s
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z17game_of_life_turnPhS_s
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z17game_of_life_turnPhS_s ; -- Begin function _Z17game_of_life_turnPhS_s
	.globl	_Z17game_of_life_turnPhS_s
	.p2align	8
	.type	_Z17game_of_life_turnPhS_s,@function
_Z17game_of_life_turnPhS_s:             ; @_Z17game_of_life_turnPhS_s
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x10
	s_load_b32 s3, s[0:1], 0x24
	s_mov_b32 s5, 0
	s_mov_b32 s6, 0
	s_mov_b32 s7, exec_lo
	s_waitcnt lgkmcnt(0)
	s_sext_i32_i16 s4, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_cvt_f32_u32_e32 v1, s4
	s_sub_i32 s2, 0, s4
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cvt_u32_f32_e32 v4, v1
	v_bfe_u32 v1, v0, 10, 10
	v_mul_lo_u32 v5, s2, v4
	s_lshr_b32 s2, s3, 16
	s_delay_alu instid0(VALU_DEP_2) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[2:3], null, s15, s2, v[1:2]
	v_and_b32_e32 v3, 0x3ff, v0
	s_and_b32 s2, s3, 0xffff
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_hi_u32 v5, v4, v5
	v_mad_u64_u32 v[0:1], null, s14, s2, v[3:4]
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v1, 1, v2
	s_add_i32 s2, s4, -1
	v_add_nc_u32_e32 v3, v4, v5
	v_add_nc_u32_e32 v4, s2, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_hi_u32 v5, v1, v3
	v_mul_hi_u32 v8, v4, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v5, v5, s4
	v_mul_lo_u32 v8, v8, s4
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_sub_nc_u32_e32 v1, v1, v5
	v_sub_nc_u32_e32 v4, v4, v8
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_3)
	v_subrev_nc_u32_e32 v5, s4, v1
	v_cmp_le_u32_e32 vcc_lo, s4, v1
	v_add_nc_u32_e32 v6, s2, v0
	s_load_b128 s[0:3], s[0:1], 0x0
	v_cndmask_b32_e32 v1, v1, v5, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_mul_hi_u32 v9, v6, v3
	v_subrev_nc_u32_e32 v5, s4, v4
	v_cmp_le_u32_e32 vcc_lo, s4, v4
	v_cndmask_b32_e32 v4, v4, v5, vcc_lo
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v9, v9, s4
	v_sub_nc_u32_e32 v6, v6, v9
	v_subrev_nc_u32_e32 v9, s4, v1
	v_add_nc_u32_e32 v7, 1, v0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_le_u32_e32 vcc_lo, s4, v6
	v_mul_hi_u32 v3, v7, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_lo_u32 v3, v3, s4
	v_sub_nc_u32_e32 v3, v7, v3
	v_subrev_nc_u32_e32 v7, s4, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_subrev_nc_u32_e32 v8, s4, v3
	v_cndmask_b32_e32 v5, v6, v7, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s4, v1
	v_subrev_nc_u32_e32 v7, s4, v4
	v_cndmask_b32_e32 v6, v1, v9, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s4, v3
	v_mul_lo_u32 v1, v2, s4
	v_subrev_nc_u32_e32 v2, s4, v5
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v6, v6, s4
	v_cndmask_b32_e32 v3, v3, v8, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s4, v5
	v_subrev_nc_u32_e32 v8, s4, v3
	v_cndmask_b32_e32 v2, v5, v2, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s4, v4
	s_delay_alu instid0(VALU_DEP_2)
	v_dual_cndmask_b32 v4, v4, v7 :: v_dual_add_nc_u32 v5, v2, v6
	v_cmp_le_u32_e32 vcc_lo, s4, v3
	v_add_nc_u32_e32 v7, v0, v6
	s_waitcnt lgkmcnt(0)
	s_clause 0x1
	global_load_u8 v5, v5, s[0:1]
	global_load_u8 v7, v7, s[0:1]
	v_dual_cndmask_b32 v3, v3, v8 :: v_dual_add_nc_u32 v8, v1, v2
	v_mul_lo_u32 v4, v4, s4
	s_mov_b32 s4, -1
	s_delay_alu instid0(VALU_DEP_2)
	v_add_nc_u32_e32 v6, v3, v6
	s_clause 0x1
	global_load_u8 v6, v6, s[0:1]
	global_load_u8 v8, v8, s[0:1]
	v_add_nc_u32_e32 v9, v3, v1
	v_add_nc_u32_e32 v2, v2, v4
	v_add_nc_u32_e32 v10, v4, v0
	v_add_nc_u32_e32 v3, v3, v4
	s_clause 0x3
	global_load_u8 v4, v9, s[0:1]
	global_load_u8 v2, v2, s[0:1]
	global_load_u8 v9, v10, s[0:1]
	global_load_u8 v3, v3, s[0:1]
	s_waitcnt vmcnt(6)
	v_add_nc_u32_e32 v5, v7, v5
	s_waitcnt vmcnt(4)
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_add3_u32 v5, v5, v6, v8
	s_waitcnt vmcnt(2)
	v_add3_u32 v2, v5, v4, v2
	s_waitcnt vmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add3_u32 v2, v2, v9, v3
	v_cmpx_lt_i32_e32 2, v2
	s_xor_b32 s7, exec_lo, s7
; %bb.1:                                ; %LeafBlock57
	v_cmp_ne_u32_e32 vcc_lo, 3, v2
                                        ; implicit-def: $vgpr2
	s_and_b32 s6, vcc_lo, exec_lo
; %bb.2:                                ; %Flow
	s_and_not1_saveexec_b32 s7, s7
; %bb.3:                                ; %LeafBlock
	v_cmp_ne_u32_e32 vcc_lo, 2, v2
	s_and_not1_b32 s6, s6, exec_lo
	s_mov_b32 s5, exec_lo
	s_and_b32 s8, vcc_lo, exec_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 s6, s6, s8
; %bb.4:                                ; %Flow59
	s_or_b32 exec_lo, exec_lo, s7
	s_and_saveexec_b32 s7, s6
	s_delay_alu instid0(SALU_CYCLE_1)
	s_xor_b32 s6, exec_lo, s7
; %bb.5:                                ; %.fold.split
	s_and_not1_b32 s4, s4, exec_lo
	s_and_not1_b32 s5, s5, exec_lo
; %bb.6:                                ; %Flow60
	s_or_b32 exec_lo, exec_lo, s6
	v_add_nc_u32_e32 v0, v1, v0
	s_and_saveexec_b32 s6, s5
	s_cbranch_execz .LBB0_8
; %bb.7:
	global_load_u8 v1, v0, s[0:1]
	s_and_not1_b32 s0, s4, exec_lo
	s_waitcnt vmcnt(0)
	v_cmp_ne_u16_e32 vcc_lo, 0, v1
	s_and_b32 s1, vcc_lo, exec_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 s4, s0, s1
.LBB0_8:
	s_or_b32 exec_lo, exec_lo, s6
	v_cndmask_b32_e64 v1, 0, 1, s4
	global_store_b8 v0, v1, s[2:3]
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z17game_of_life_turnPhS_s
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.amdhsa_user_sgpr_count 14
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 11
		.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	_Z17game_of_life_turnPhS_s, .Lfunc_end0-_Z17game_of_life_turnPhS_s
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 744
; NumSgprs: 18
; NumVgprs: 11
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 11
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.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:           2
        .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:           _Z17game_of_life_turnPhS_s
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z17game_of_life_turnPhS_s.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     11
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
