/*
 * The Game of Life
 *
 * a cell is born, if it has exactly three neighbours
 * a cell dies of loneliness, if it has less than two neighbours
 * a cell dies of overcrowding, if it has more than three neighbours
 * a cell survives to the next generation, if it does not die of loneliness
 * or overcrowding
 *
 * In this version, a 2D array of ints is used.  A 1 cell is on, a 0 cell is off.
 * The game plays a number of steps (given by the input), printing to the screen each time.  'x' printed
 * means on, space means off.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef unsigned char bool_t;
typedef unsigned char cell_t;

#define TILE_SIZE 8
#define KERNEL_SIZE 3
#define SHARED_MEMORY_SIZE (TILE_SIZE + KERNEL_SIZE - 1)

#define gpuErrchk(ans)                        \
    {                                         \
        gpuAssert((ans), __FILE__, __LINE__); \
    }

inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort = true) {
    if (code != cudaSuccess) {
        fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
        if (abort)
            exit(code);
    }
}

/**
 * Allocates the board as a 1D array instead of the previous 2D array
 * @param flat_size
 * @param outer_grid_size
 * @return An allocated board of the given flat_size
 */
cell_t *allocate_board_flat(unsigned int flat_size, int outer_grid_size) {
    cell_t *board = (cell_t *) malloc(sizeof(cell_t) * flat_size);

    // Since there is no dependency between the indices we can collapse the loop
    // Only if the grid is large enough
#pragma omp parallel for collapse(2) if(outer_grid_size >= 5000)
    for (int i = 0; i < outer_grid_size; ++i) {
        for (int k = 0; k < KERNEL_SIZE / 2; ++k) {
            // Fill first rows
            board[k * outer_grid_size + i] = 0;

            // Fill last rows
            board[(outer_grid_size - (k + 1)) * outer_grid_size + i] = 0;

            // Fill left columns
            board[i * outer_grid_size + k] = 0;

            // Fill right columns
            board[i * outer_grid_size + (outer_grid_size - (k + 1))] = 0;
        }
    }

    return board;
}

/**
 * Processes one step of the game of life
 * @param d_board The original board
 * @param d_newboard The new board
 * @param inner_size The inner grid size
 * @param outer_size The padded outer grid size
 */
__global__ void playKernelSMDynamic(const cell_t *d_board, cell_t *d_newboard, int inner_size, int outer_size) {
    unsigned short bx = blockIdx.x;
    unsigned short by = blockIdx.y;
    unsigned short tx = threadIdx.x;
    unsigned short ty = threadIdx.y;

    // Calculate the row and col for the output array
    unsigned short row = by * TILE_SIZE + ty + (KERNEL_SIZE / 2);
    unsigned short col = bx * TILE_SIZE + tx + (KERNEL_SIZE / 2);

    __shared__ cell_t ds_neighbors[SHARED_MEMORY_SIZE][SHARED_MEMORY_SIZE];

    // These indices are the inner grid's indices
    // For example, with a 5000x5000 padded on each side, we get a 5002x5002 grid
    // To access the index (0, 0) of the inner grid, we must access the index (1, 1) of the padded grid
    unsigned short idx_inner_x = tx + (KERNEL_SIZE / 2);
    unsigned short idx_inner_y = ty + (KERNEL_SIZE / 2);

    unsigned short blockIndex = ty + tx * TILE_SIZE;

    // Using unsigned short reduces the duration of each kernel by ~100 us (~930 us to ~830 us)
    // Here, each thread is responsible for loading between one and two values into the shared memory array
    for (unsigned short incr = blockIndex; incr < SHARED_MEMORY_SIZE * SHARED_MEMORY_SIZE; incr += TILE_SIZE * TILE_SIZE) {
        unsigned short ry = incr % SHARED_MEMORY_SIZE;
        unsigned short rx = incr / SHARED_MEMORY_SIZE;

        unsigned short gy = ry + by * TILE_SIZE;
        unsigned short gx = rx + bx * TILE_SIZE;

        // Required to avoid accessing out of bounds
        if (gy < outer_size && gx < outer_size) {
            ds_neighbors[ry][rx] = d_board[gy * outer_size + gx];
        }
    }

    // Required so we don't fill the outer padded grid
    if (row > inner_size || col > inner_size) {
        return;
    }

    // Sync threads now, no need to wait for the threads that exit
    __syncthreads();

    unsigned short a = 0;

    // Instead of using the serial function that had multiple conditions checking for boundaries, simply loop over all
    // neighbors as the shared memory array is padded on each side too
    for (unsigned short j = 0; j < KERNEL_SIZE; ++j) {
        for (unsigned short i = 0; i < KERNEL_SIZE; ++i) {
            a += ds_neighbors[j + idx_inner_y - (KERNEL_SIZE / 2)][i + idx_inner_x - (KERNEL_SIZE / 2)];
        }
    }
    a -= ds_neighbors[idx_inner_y][idx_inner_x];

    if (a == 2)
        d_newboard[row * outer_size + col] = ds_neighbors[idx_inner_y][idx_inner_x];
    if (a == 3)
        d_newboard[row * outer_size + col] = 1;
    if (a < 2)
        d_newboard[row * outer_size + col] = 0;
    if (a > 3)
        d_newboard[row * outer_size + col] = 0;
}

/**
 * Print the game of the life board from a flat 1D array
 * @param board The board
 * @param inner_size The inner grid size
 * @param outer_size The padded outer grid size
 */
void print_flat(cell_t *board, int inner_size, int outer_size) {
    int i, j;
    /* for each row */
    for (j = 0; j < inner_size; j++) {
        /* print each column position... */
        for (i = 0; i < inner_size; i++)
            printf("%c", board[(j + (KERNEL_SIZE / 2)) * outer_size + (i + (KERNEL_SIZE / 2))] ? 'x' : ' ');
        /* followed by a carriage return */
        printf("\n");
    }
}

/**
 * Read the game of the life board into a flat 1D array from a file
 * @param f The file to read
 * @param board The allocated 1D array
 * @param inner_size The inner grid size
 * @param outer_size The padded outer grid size
 */
void read_file_flat(FILE *f, cell_t *board, int inner_size, int outer_size) {
    int i, j;
    size_t len;
    char *s = (char *) malloc(inner_size + 10);

    for (j = 0; j < inner_size; j++) {
        /* get a string */
        fgets(s, inner_size + 10, f);

        // Get the length of the line that was just read since fgets adds a '\0' at the end of a line
        len = strlen(s) - 1;

        /* copy the string to the life board */
        for (i = 0; i < inner_size; i++) {
            // If i is less than the line's length, read the value at that index, otherwise fill with a blank (0)
            // The serial version was accessing uninitialized memory due to the first line of judge.in not being
            // as long as the grid size expects it to be
            // Those accesses sometimes added 'x's where they shouldn't have been if the accessed memory wasn't empty,
            // causing the result to be wrong with a small number of steps

            // Since the grid is padded on each side to avoid boundary conditions, only fill the 'inner' part
            board[(j + (KERNEL_SIZE / 2)) * outer_size + (i + (KERNEL_SIZE / 2))] = i < len ? s[i] == 'x' : 0;
        }
    }
}

int main() {
    // Host variables
    unsigned short size, steps, i, grid_size, outer_grid_size;
    unsigned int flat_size;
    FILE *f_in;
    cell_t *h_prev;
    bool_t writeOutput = 1, evenSteps;

    // Device variables
    cell_t *d_prev, *d_next;

    f_in = stdin;

    // Read the input file and write its content in the host array
    fscanf(f_in, "%d %d", &size, &steps);

    // Create a border around the grid to avoid dealing with boundary conditions
    // Assuming a KERNEL_SIZE of 3, a 5000x5000 grid will be padded with 1 extra row/column on each side, resulting
    // in a 5002x5002 grid
    outer_grid_size = size + (2 * (KERNEL_SIZE / 2));
    flat_size = outer_grid_size * outer_grid_size;
    evenSteps = steps % 2 == 0;

    // Allocate a 'flat' array instead of the previous 2D array so the memory is contiguous for faster accesses
    h_prev = allocate_board_flat(flat_size, outer_grid_size);
    read_file_flat(f_in, h_prev, size, outer_grid_size);
    fclose(f_in);

    // Assuming we only have square grids, the grid size is the same for both dimensions
    // Here, we round up the result to make sure even a grid size that's not a multiple of 32 will be fully processed
    // at the cost of having more blocks than necessary
    grid_size = int(ceilf((float) size / TILE_SIZE));

    dim3 dimGrid(grid_size, grid_size, 1);

    // In our case, a TILE_SIZE of 8 gives the best results, with 16 and 32 being slightly slower (+ ~20 ms)
    dim3 dimBlock(TILE_SIZE, TILE_SIZE, 1);

    // Allocate device arrays
    gpuErrchk(cudaMalloc((void **) &d_prev, flat_size * sizeof(cell_t)));
    gpuErrchk(cudaMalloc((void **) &d_next, flat_size * sizeof(cell_t)));

    // Copy the data from the host array to the device array
    gpuErrchk(cudaMemcpy(d_prev, h_prev, flat_size * sizeof(cell_t), cudaMemcpyHostToDevice));

    for (i = 0; i < int(ceilf((float) steps / 2)); i++) {
//        printf("Step: %d\n", 2 * i);

        // Instead of using cudaMemcpy and a buffer or swapping pointers, run the same kernel with the variables inverted
        // The data remains on the GPU at all times
        playKernelSMDynamic<<<dimGrid, dimBlock>>>(d_prev, d_next, size, outer_grid_size);

        // Condition to make sure the second kernel is only executed if necessary
        // For example, 500 steps can be divided by 2 (judge.in)
        // But 11 steps can't (life.in), thus ceilf(steps / 2) returns 12 which would result in an extra step
        if (evenSteps || (2 * i + 1) < steps) {
//            printf("Step: %d\n", 2 * i + 1);
            playKernelSMDynamic<<<dimGrid, dimBlock>>>(d_next, d_prev, size, outer_grid_size);
        }
    }

    // Copy data back from the device array to the host array
    gpuErrchk(cudaMemcpy(h_prev, evenSteps ? d_prev : d_next, flat_size * sizeof(cell_t), cudaMemcpyDeviceToHost));

    // Deallocate device arrays
    gpuErrchk(cudaFree(d_next));
    gpuErrchk(cudaFree(d_prev));

    if (writeOutput) {
        print_flat(h_prev, size, outer_grid_size);
    }

    free(h_prev);

    return EXIT_SUCCESS;
}

	.file	"tmpxft_00355891_00000000-6_life_cuda_SM.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata._Z9gpuAssert9cudaErrorPKcib.str1.1,"aMS",@progbits,1
.LC0:
	.string	"GPUassert: %s %s %d\n"
	.section	.text._Z9gpuAssert9cudaErrorPKcib,"axG",@progbits,_Z9gpuAssert9cudaErrorPKcib,comdat
	.weak	_Z9gpuAssert9cudaErrorPKcib
	.type	_Z9gpuAssert9cudaErrorPKcib, @function
_Z9gpuAssert9cudaErrorPKcib:
.LFB2027:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L8
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	%edi, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movl	%edx, 12(%rsp)
	movq	%rsi, (%rsp)
	call	cudaGetErrorString@PLT
	movl	12(%rsp), %r9d
	movq	(%rsp), %r8
	leaq	.LC0(%rip), %rdx
	movq	stderr(%rip), %rdi
	movq	%rax, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	testb	%bpl, %bpl
	je	.L2
	movl	%ebx, %edi
	call	exit@PLT
.L2:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
.L8:
	.cfi_restore 3
	.cfi_restore 6
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9gpuAssert9cudaErrorPKcib, .-_Z9gpuAssert9cudaErrorPKcib
	.text
	.globl	_Z19allocate_board_flatji
	.type	_Z19allocate_board_flatji, @function
_Z19allocate_board_flatji:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	%edi, %edi
	movl	%esi, %ebx
	call	malloc@PLT
	leal	-1(%rbx), %edi
	movl	%ebx, %ecx
	movslq	%ebx, %r8
	imull	%edi, %ecx
	movq	%rax, %rsi
	xorl	%edx, %edx
	movslq	%ecx, %rcx
	addq	%rax, %rcx
.L14:
	cmpl	%edx, %ebx
	jle	.L12
	movb	$0, (%rax,%rdx)
	movb	$0, (%rcx,%rdx)
	incq	%rdx
	movb	$0, (%rsi)
	movb	$0, (%rsi,%rdi)
	addq	%r8, %rsi
	jmp	.L14
.L12:
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z19allocate_board_flatji, .-_Z19allocate_board_flatji
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"%c"
.LC2:
	.string	"\n"
	.text
	.globl	_Z10print_flatPhii
	.type	_Z10print_flatPhii, @function
_Z10print_flatPhii:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movslq	%esi, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	xorl	%r13d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movslq	%edx, %r12
	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
	leaq	(%rdi,%r12), %rbx
	pushq	%rcx
	.cfi_def_cfa_offset 64
.L17:
	cmpl	%ebp, %r13d
	jge	.L24
	incl	%r13d
	movq	%rbx, %r15
.L19:
	cmpb	$1, 1(%r15)
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	sbbl	%edx, %edx
	xorl	%eax, %eax
	incq	%r15
	andl	$-88, %edx
	addl	$120, %edx
	call	__printf_chk@PLT
	leaq	(%r14,%rbx), %rax
	cmpq	%rax, %r15
	jne	.L19
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	addq	%r12, %rbx
	call	__printf_chk@PLT
	jmp	.L17
.L24:
	popq	%rax
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z10print_flatPhii, .-_Z10print_flatPhii
	.globl	_Z14read_file_flatP8_IO_FILEPhii
	.type	_Z14read_file_flatP8_IO_FILEPhii, @function
_Z14read_file_flatP8_IO_FILEPhii:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leal	10(%rdx), %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movslq	%r15d, %rax
	movslq	%ecx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%edx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rsi, %rbx
	leaq	(%rbx,%r14), %rbp
	xorl	%ebx, %ebx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	%rdi, 8(%rsp)
	movq	%rax, %rdi
	movq	%rax, (%rsp)
	call	malloc@PLT
	movq	%rax, %r13
.L26:
	cmpl	%r12d, %ebx
	jge	.L33
	movq	8(%rsp), %rcx
	movq	(%rsp), %rsi
	movl	%r15d, %edx
	movq	%r13, %rdi
	incl	%ebx
	call	__fgets_chk@PLT
	movq	%r13, %rdi
	call	strlen@PLT
	xorl	%edx, %edx
	decq	%rax
.L28:
	xorl	%ecx, %ecx
	cmpq	%rax, %rdx
	jnb	.L27
	xorl	%ecx, %ecx
	cmpb	$120, 0(%r13,%rdx)
	sete	%cl
.L27:
	movb	%cl, 1(%rbp,%rdx)
	incq	%rdx
	cmpl	%edx, %r12d
	jg	.L28
	addq	%r14, %rbp
	jmp	.L26
.L33:
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z14read_file_flatP8_IO_FILEPhii, .-_Z14read_file_flatP8_IO_FILEPhii
	.globl	_Z44__device_stub__Z19playKernelSMDynamicPKhPhiiPKhPhii
	.type	_Z44__device_stub__Z19playKernelSMDynamicPKhPhiiPKhPhii, @function
_Z44__device_stub__Z19playKernelSMDynamicPKhPhiiPKhPhii:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L34
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z19playKernelSMDynamicPKhPhii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L34:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L36
	call	__stack_chk_fail@PLT
.L36:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z44__device_stub__Z19playKernelSMDynamicPKhPhiiPKhPhii, .-_Z44__device_stub__Z19playKernelSMDynamicPKhPhiiPKhPhii
	.globl	_Z19playKernelSMDynamicPKhPhii
	.type	_Z19playKernelSMDynamicPKhPhii, @function
_Z19playKernelSMDynamicPKhPhii:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z44__device_stub__Z19playKernelSMDynamicPKhPhiiPKhPhii
	.cfi_endproc
.LFE2057:
	.size	_Z19playKernelSMDynamicPKhPhii, .-_Z19playKernelSMDynamicPKhPhii
	.section	.rodata.str1.1
.LC3:
	.string	"%d %d"
.LC5:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/btglr/game_of_life_cuda/master/life_cuda_SM.cu"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC3(%rip), %rsi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	stdin(%rip), %r14
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	30(%rsp), %rcx
	leaq	28(%rsp), %rdx
	movq	%r14, %rdi
	call	__isoc23_fscanf@PLT
	movl	28(%rsp), %r12d
	movw	30(%rsp), %r13w
	leal	2(%r12), %ebx
	andl	$1, %r13d
	movzwl	%bx, %ebx
	movl	%ebx, %ebp
	movl	%ebx, %esi
	imull	%ebx, %ebp
	movl	%ebp, %edi
	movslq	%ebp, %rbp
	call	_Z19allocate_board_flatji
	movzwl	%r12w, %edx
	movl	%ebx, %ecx
	movq	%r14, %rdi
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	leaq	.LC5(%rip), %r12
	call	_Z14read_file_flatP8_IO_FILEPhii
	movq	%r14, %rdi
	xorl	%r14d, %r14d
	call	fclose@PLT
	movzwl	28(%rsp), %eax
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC4(%rip), %xmm0
	call	ceilf@PLT
	leaq	32(%rsp), %rdi
	movq	%rbp, %rsi
	movl	$1, 56(%rsp)
	cvttss2sil	%xmm0, %eax
	movl	$1, 68(%rsp)
	movzwl	%ax, %eax
	movl	%eax, 48(%rsp)
	movl	%eax, 52(%rsp)
	movabsq	$34359738376, %rax
	movq	%rax, 60(%rsp)
	call	cudaMalloc@PLT
	movl	$1, %ecx
	movl	$231, %edx
	movq	%r12, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	leaq	40(%rsp), %rdi
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	movl	$1, %ecx
	movl	$232, %edx
	movq	%r12, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	movq	8(%rsp), %rsi
	movq	32(%rsp), %rdi
	movq	%rbp, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$1, %ecx
	movl	$235, %edx
	movq	%r12, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
.L40:
	movzwl	30(%rsp), %eax
	movzwl	%r14w, %r15d
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC6(%rip), %xmm0
	call	ceilf@PLT
	cvttss2sil	%xmm0, %eax
	cmpl	%eax, %r15d
	jge	.L53
	movl	68(%rsp), %ecx
	movq	60(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	48(%rsp), %rdi
	movl	56(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L41
	movzwl	28(%rsp), %edx
	movq	40(%rsp), %rsi
	movl	%ebx, %ecx
	movq	32(%rsp), %rdi
	call	_Z44__device_stub__Z19playKernelSMDynamicPKhPhiiPKhPhii
.L41:
	testw	%r13w, %r13w
	je	.L42
	movzwl	30(%rsp), %eax
	leal	1(%r15,%r15), %edx
	cmpl	%eax, %edx
	jge	.L43
.L42:
	movl	68(%rsp), %ecx
	movq	60(%rsp), %rdx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	48(%rsp), %rdi
	movl	56(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L43
	movzwl	28(%rsp), %edx
	movq	32(%rsp), %rsi
	movl	%ebx, %ecx
	movq	40(%rsp), %rdi
	call	_Z44__device_stub__Z19playKernelSMDynamicPKhPhiiPKhPhii
.L43:
	incl	%r14d
	jmp	.L40
.L53:
	testw	%r13w, %r13w
	jne	.L46
	movq	32(%rsp), %rsi
	jmp	.L47
.L46:
	movq	40(%rsp), %rsi
.L47:
	movq	8(%rsp), %rdi
	movl	$2, %ecx
	movq	%rbp, %rdx
	call	cudaMemcpy@PLT
	movl	$1, %ecx
	movl	$254, %edx
	movq	%r12, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movl	$1, %ecx
	movl	$257, %edx
	movq	%r12, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movl	$1, %ecx
	movl	$258, %edx
	movq	%r12, %rsi
	movl	%eax, %edi
	call	_Z9gpuAssert9cudaErrorPKcib
	movq	8(%rsp), %rdi
	movzwl	28(%rsp), %esi
	movl	%ebx, %edx
	call	_Z10print_flatPhii
	movq	8(%rsp), %rdi
	call	free@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L48
	call	__stack_chk_fail@PLT
.L48:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC7:
	.string	"_Z19playKernelSMDynamicPKhPhii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.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	_Z19playKernelSMDynamicPKhPhii(%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
.LFE2059:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC4:
	.long	1040187392
	.align 4
.LC6:
	.long	1056964608
	.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:

/*
 * The Game of Life
 *
 * a cell is born, if it has exactly three neighbours
 * a cell dies of loneliness, if it has less than two neighbours
 * a cell dies of overcrowding, if it has more than three neighbours
 * a cell survives to the next generation, if it does not die of loneliness
 * or overcrowding
 *
 * In this version, a 2D array of ints is used.  A 1 cell is on, a 0 cell is off.
 * The game plays a number of steps (given by the input), printing to the screen each time.  'x' printed
 * means on, space means off.
 *
 */

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

typedef unsigned char bool_t;
typedef unsigned char cell_t;

#define TILE_SIZE 8
#define KERNEL_SIZE 3
#define SHARED_MEMORY_SIZE (TILE_SIZE + KERNEL_SIZE - 1)

#define gpuErrchk(ans)                        \
    {                                         \
        gpuAssert((ans), __FILE__, __LINE__); \
    }

inline void gpuAssert(hipError_t code, const char *file, int line, bool abort = true) {
    if (code != hipSuccess) {
        fprintf(stderr, "GPUassert: %s %s %d\n", hipGetErrorString(code), file, line);
        if (abort)
            exit(code);
    }
}

/**
 * Allocates the board as a 1D array instead of the previous 2D array
 * @param flat_size
 * @param outer_grid_size
 * @return An allocated board of the given flat_size
 */
cell_t *allocate_board_flat(unsigned int flat_size, int outer_grid_size) {
    cell_t *board = (cell_t *) malloc(sizeof(cell_t) * flat_size);

    // Since there is no dependency between the indices we can collapse the loop
    // Only if the grid is large enough
#pragma omp parallel for collapse(2) if(outer_grid_size >= 5000)
    for (int i = 0; i < outer_grid_size; ++i) {
        for (int k = 0; k < KERNEL_SIZE / 2; ++k) {
            // Fill first rows
            board[k * outer_grid_size + i] = 0;

            // Fill last rows
            board[(outer_grid_size - (k + 1)) * outer_grid_size + i] = 0;

            // Fill left columns
            board[i * outer_grid_size + k] = 0;

            // Fill right columns
            board[i * outer_grid_size + (outer_grid_size - (k + 1))] = 0;
        }
    }

    return board;
}

/**
 * Processes one step of the game of life
 * @param d_board The original board
 * @param d_newboard The new board
 * @param inner_size The inner grid size
 * @param outer_size The padded outer grid size
 */
__global__ void playKernelSMDynamic(const cell_t *d_board, cell_t *d_newboard, int inner_size, int outer_size) {
    unsigned short bx = blockIdx.x;
    unsigned short by = blockIdx.y;
    unsigned short tx = threadIdx.x;
    unsigned short ty = threadIdx.y;

    // Calculate the row and col for the output array
    unsigned short row = by * TILE_SIZE + ty + (KERNEL_SIZE / 2);
    unsigned short col = bx * TILE_SIZE + tx + (KERNEL_SIZE / 2);

    __shared__ cell_t ds_neighbors[SHARED_MEMORY_SIZE][SHARED_MEMORY_SIZE];

    // These indices are the inner grid's indices
    // For example, with a 5000x5000 padded on each side, we get a 5002x5002 grid
    // To access the index (0, 0) of the inner grid, we must access the index (1, 1) of the padded grid
    unsigned short idx_inner_x = tx + (KERNEL_SIZE / 2);
    unsigned short idx_inner_y = ty + (KERNEL_SIZE / 2);

    unsigned short blockIndex = ty + tx * TILE_SIZE;

    // Using unsigned short reduces the duration of each kernel by ~100 us (~930 us to ~830 us)
    // Here, each thread is responsible for loading between one and two values into the shared memory array
    for (unsigned short incr = blockIndex; incr < SHARED_MEMORY_SIZE * SHARED_MEMORY_SIZE; incr += TILE_SIZE * TILE_SIZE) {
        unsigned short ry = incr % SHARED_MEMORY_SIZE;
        unsigned short rx = incr / SHARED_MEMORY_SIZE;

        unsigned short gy = ry + by * TILE_SIZE;
        unsigned short gx = rx + bx * TILE_SIZE;

        // Required to avoid accessing out of bounds
        if (gy < outer_size && gx < outer_size) {
            ds_neighbors[ry][rx] = d_board[gy * outer_size + gx];
        }
    }

    // Required so we don't fill the outer padded grid
    if (row > inner_size || col > inner_size) {
        return;
    }

    // Sync threads now, no need to wait for the threads that exit
    __syncthreads();

    unsigned short a = 0;

    // Instead of using the serial function that had multiple conditions checking for boundaries, simply loop over all
    // neighbors as the shared memory array is padded on each side too
    for (unsigned short j = 0; j < KERNEL_SIZE; ++j) {
        for (unsigned short i = 0; i < KERNEL_SIZE; ++i) {
            a += ds_neighbors[j + idx_inner_y - (KERNEL_SIZE / 2)][i + idx_inner_x - (KERNEL_SIZE / 2)];
        }
    }
    a -= ds_neighbors[idx_inner_y][idx_inner_x];

    if (a == 2)
        d_newboard[row * outer_size + col] = ds_neighbors[idx_inner_y][idx_inner_x];
    if (a == 3)
        d_newboard[row * outer_size + col] = 1;
    if (a < 2)
        d_newboard[row * outer_size + col] = 0;
    if (a > 3)
        d_newboard[row * outer_size + col] = 0;
}

/**
 * Print the game of the life board from a flat 1D array
 * @param board The board
 * @param inner_size The inner grid size
 * @param outer_size The padded outer grid size
 */
void print_flat(cell_t *board, int inner_size, int outer_size) {
    int i, j;
    /* for each row */
    for (j = 0; j < inner_size; j++) {
        /* print each column position... */
        for (i = 0; i < inner_size; i++)
            printf("%c", board[(j + (KERNEL_SIZE / 2)) * outer_size + (i + (KERNEL_SIZE / 2))] ? 'x' : ' ');
        /* followed by a carriage return */
        printf("\n");
    }
}

/**
 * Read the game of the life board into a flat 1D array from a file
 * @param f The file to read
 * @param board The allocated 1D array
 * @param inner_size The inner grid size
 * @param outer_size The padded outer grid size
 */
void read_file_flat(FILE *f, cell_t *board, int inner_size, int outer_size) {
    int i, j;
    size_t len;
    char *s = (char *) malloc(inner_size + 10);

    for (j = 0; j < inner_size; j++) {
        /* get a string */
        fgets(s, inner_size + 10, f);

        // Get the length of the line that was just read since fgets adds a '\0' at the end of a line
        len = strlen(s) - 1;

        /* copy the string to the life board */
        for (i = 0; i < inner_size; i++) {
            // If i is less than the line's length, read the value at that index, otherwise fill with a blank (0)
            // The serial version was accessing uninitialized memory due to the first line of judge.in not being
            // as long as the grid size expects it to be
            // Those accesses sometimes added 'x's where they shouldn't have been if the accessed memory wasn't empty,
            // causing the result to be wrong with a small number of steps

            // Since the grid is padded on each side to avoid boundary conditions, only fill the 'inner' part
            board[(j + (KERNEL_SIZE / 2)) * outer_size + (i + (KERNEL_SIZE / 2))] = i < len ? s[i] == 'x' : 0;
        }
    }
}

int main() {
    // Host variables
    unsigned short size, steps, i, grid_size, outer_grid_size;
    unsigned int flat_size;
    FILE *f_in;
    cell_t *h_prev;
    bool_t writeOutput = 1, evenSteps;

    // Device variables
    cell_t *d_prev, *d_next;

    f_in = stdin;

    // Read the input file and write its content in the host array
    fscanf(f_in, "%d %d", &size, &steps);

    // Create a border around the grid to avoid dealing with boundary conditions
    // Assuming a KERNEL_SIZE of 3, a 5000x5000 grid will be padded with 1 extra row/column on each side, resulting
    // in a 5002x5002 grid
    outer_grid_size = size + (2 * (KERNEL_SIZE / 2));
    flat_size = outer_grid_size * outer_grid_size;
    evenSteps = steps % 2 == 0;

    // Allocate a 'flat' array instead of the previous 2D array so the memory is contiguous for faster accesses
    h_prev = allocate_board_flat(flat_size, outer_grid_size);
    read_file_flat(f_in, h_prev, size, outer_grid_size);
    fclose(f_in);

    // Assuming we only have square grids, the grid size is the same for both dimensions
    // Here, we round up the result to make sure even a grid size that's not a multiple of 32 will be fully processed
    // at the cost of having more blocks than necessary
    grid_size = int(ceilf((float) size / TILE_SIZE));

    dim3 dimGrid(grid_size, grid_size, 1);

    // In our case, a TILE_SIZE of 8 gives the best results, with 16 and 32 being slightly slower (+ ~20 ms)
    dim3 dimBlock(TILE_SIZE, TILE_SIZE, 1);

    // Allocate device arrays
    gpuErrchk(hipMalloc((void **) &d_prev, flat_size * sizeof(cell_t)));
    gpuErrchk(hipMalloc((void **) &d_next, flat_size * sizeof(cell_t)));

    // Copy the data from the host array to the device array
    gpuErrchk(hipMemcpy(d_prev, h_prev, flat_size * sizeof(cell_t), hipMemcpyHostToDevice));

    for (i = 0; i < int(ceilf((float) steps / 2)); i++) {
//        printf("Step: %d\n", 2 * i);

        // Instead of using cudaMemcpy and a buffer or swapping pointers, run the same kernel with the variables inverted
        // The data remains on the GPU at all times
        playKernelSMDynamic<<<dimGrid, dimBlock>>>(d_prev, d_next, size, outer_grid_size);

        // Condition to make sure the second kernel is only executed if necessary
        // For example, 500 steps can be divided by 2 (judge.in)
        // But 11 steps can't (life.in), thus ceilf(steps / 2) returns 12 which would result in an extra step
        if (evenSteps || (2 * i + 1) < steps) {
//            printf("Step: %d\n", 2 * i + 1);
            playKernelSMDynamic<<<dimGrid, dimBlock>>>(d_next, d_prev, size, outer_grid_size);
        }
    }

    // Copy data back from the device array to the host array
    gpuErrchk(hipMemcpy(h_prev, evenSteps ? d_prev : d_next, flat_size * sizeof(cell_t), hipMemcpyDeviceToHost));

    // Deallocate device arrays
    gpuErrchk(hipFree(d_next));
    gpuErrchk(hipFree(d_prev));

    if (writeOutput) {
        print_flat(h_prev, size, outer_grid_size);
    }

    free(h_prev);

    return EXIT_SUCCESS;
}

	.text
	.file	"life_cuda_SM.hip"
	.globl	_Z19allocate_board_flatji       # -- Begin function _Z19allocate_board_flatji
	.type	_Z19allocate_board_flatji,@function
_Z19allocate_board_flatji:              # @_Z19allocate_board_flatji
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	%esi, %ebx
	movl	%edi, %edi
	callq	malloc
	testl	%ebx, %ebx
	jle	.LBB0_3
# %bb.1:                                # %.critedge.lr.ph
	leal	-1(%rbx), %ecx
	movl	%ecx, %edx
	imull	%ebx, %edx
	movslq	%edx, %rdi
	movl	%ebx, %edx
	movq	%rax, %rsi
	addq	%rdi, %rsi
	xorl	%edi, %edi
	movq	%rax, %r8
	xorl	%r9d, %r9d
.LBB0_2:                                # %.critedge
                                        # =>This Inner Loop Header: Depth=1
	movb	%dil, (%rax,%r9)
	movb	%dil, (%rsi,%r9)
	movb	%dil, (%r8)
	movb	%dil, (%r8,%rcx)
	incq	%r9
	addq	%rdx, %r8
	cmpq	%r9, %rdx
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z19allocate_board_flatji, .Lfunc_end0-_Z19allocate_board_flatji
	.cfi_endproc
                                        # -- End function
	.globl	_Z34__device_stub__playKernelSMDynamicPKhPhii # -- Begin function _Z34__device_stub__playKernelSMDynamicPKhPhii
	.type	_Z34__device_stub__playKernelSMDynamicPKhPhii,@function
_Z34__device_stub__playKernelSMDynamicPKhPhii: # @_Z34__device_stub__playKernelSMDynamicPKhPhii
	.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), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%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	$_Z19playKernelSMDynamicPKhPhii, %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_end1:
	.size	_Z34__device_stub__playKernelSMDynamicPKhPhii, .Lfunc_end1-_Z34__device_stub__playKernelSMDynamicPKhPhii
	.cfi_endproc
                                        # -- End function
	.globl	_Z10print_flatPhii              # -- Begin function _Z10print_flatPhii
	.type	_Z10print_flatPhii,@function
_Z10print_flatPhii:                     # @_Z10print_flatPhii
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB2_8
# %bb.1:                                # %.preheader.lr.ph
	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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movslq	%edx, %rbx
	movl	%esi, %r14d
	leaq	(%rbx,%rdi), %r15
	incq	%r15
	xorl	%r12d, %r12d
.LBB2_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB2_3 Depth 2
	incq	%r12
	xorl	%r13d, %r13d
.LBB2_3:                                #   Parent Loop BB2_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpb	$0, (%r15,%r13)
	movl	$32, %edi
	je	.LBB2_5
# %bb.4:                                #   in Loop: Header=BB2_3 Depth=2
	movl	$120, %edi
.LBB2_5:                                #   in Loop: Header=BB2_3 Depth=2
	incq	%r13
	callq	putchar@PLT
	cmpq	%r13, %r14
	jne	.LBB2_3
# %bb.6:                                # %._crit_edge
                                        #   in Loop: Header=BB2_2 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	addq	%rbx, %r15
	cmpq	%r14, %r12
	jne	.LBB2_2
# %bb.7:
	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
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r13
	.cfi_restore %r14
	.cfi_restore %r15
.LBB2_8:                                # %._crit_edge14
	retq
.Lfunc_end2:
	.size	_Z10print_flatPhii, .Lfunc_end2-_Z10print_flatPhii
	.cfi_endproc
                                        # -- End function
	.globl	_Z14read_file_flatP8_IO_FILEPhii # -- Begin function _Z14read_file_flatP8_IO_FILEPhii
	.type	_Z14read_file_flatP8_IO_FILEPhii,@function
_Z14read_file_flatP8_IO_FILEPhii:       # @_Z14read_file_flatP8_IO_FILEPhii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%ecx, %r12d
	movl	%edx, %r13d
	movq	%rsi, %r15
	movq	%rdi, (%rsp)                    # 8-byte Spill
	leal	10(%r13), %ebp
	movslq	%ebp, %rdi
	callq	malloc
	testl	%r13d, %r13d
	jle	.LBB3_8
# %bb.1:                                # %.lr.ph24
	movq	%rax, %r14
	movslq	%r12d, %r12
	movl	%r13d, %r13d
	addq	%r12, %r15
	incq	%r15
	xorl	%ebx, %ebx
.LBB3_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_3 Depth 2
	movq	%r14, %rdi
	movl	%ebp, %esi
	movq	(%rsp), %rdx                    # 8-byte Reload
	callq	fgets
	movq	%r14, %rdi
	callq	strlen
	decq	%rax
	incq	%rbx
	xorl	%ecx, %ecx
.LBB3_3:                                #   Parent Loop BB3_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpq	%rcx, %rax
	jbe	.LBB3_4
# %bb.5:                                #   in Loop: Header=BB3_3 Depth=2
	cmpb	$120, (%r14,%rcx)
	sete	%dl
	jmp	.LBB3_6
.LBB3_4:                                #   in Loop: Header=BB3_3 Depth=2
	xorl	%edx, %edx
.LBB3_6:                                #   in Loop: Header=BB3_3 Depth=2
	movb	%dl, (%r15,%rcx)
	incq	%rcx
	cmpq	%rcx, %r13
	jne	.LBB3_3
# %bb.7:                                # %._crit_edge
                                        #   in Loop: Header=BB3_2 Depth=1
	addq	%r12, %r15
	cmpq	%r13, %rbx
	jne	.LBB3_2
.LBB3_8:                                # %._crit_edge25
	addq	$8, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z14read_file_flatP8_IO_FILEPhii, .Lfunc_end3-_Z14read_file_flatP8_IO_FILEPhii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI4_0:
	.long	0x3e000000                      # float 0.125
.LCPI4_1:
	.long	0x3f000000                      # float 0.5
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	stdin(%rip), %r12
	leaq	14(%rsp), %r13
	leaq	12(%rsp), %r14
	movl	$.L.str.2, %esi
	movq	%r12, %rdi
	movq	%r13, %rdx
	movq	%r14, %rcx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movzwl	(%r13), %ebx
	addl	$2, %ebx
	movl	$65535, %eax                    # imm = 0xFFFF
	andl	%eax, %ebx
	movl	%ebx, %ebp
	imull	%ebp, %ebp
	movzwl	(%r14), %r14d
	movl	%ebp, %edi
	movl	%ebx, %esi
	callq	_Z19allocate_board_flatji
	movq	%rax, %r15
	movzwl	(%r13), %edx
	movq	%r12, %rdi
	movq	%rax, %rsi
	movl	%ebx, %ecx
	callq	_Z14read_file_flatP8_IO_FILEPhii
	movq	%r12, %rdi
	movq	%rbp, %r12
	callq	fclose
	movzwl	(%r13), %eax
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI4_0(%rip), %xmm0
	callq	ceilf@PLT
	movss	%xmm0, 36(%rsp)                 # 4-byte Spill
	leaq	16(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB4_1
# %bb.3:                                # %_Z9gpuAssert10hipError_tPKcib.exit
	leaq	24(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB4_4
# %bb.5:                                # %_Z9gpuAssert10hipError_tPKcib.exit44
	movq	16(%rsp), %rdi
	movq	%r15, 48(%rsp)                  # 8-byte Spill
	movq	%r15, %rsi
	movq	%r12, 40(%rsp)                  # 8-byte Spill
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB4_22
# %bb.6:                                # %_Z9gpuAssert10hipError_tPKcib.exit46.preheader
	movzwl	12(%rsp), %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI4_1(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %eax
	testl	%eax, %eax
	jle	.LBB4_15
# %bb.7:                                # %.lr.ph.preheader
	cvttss2si	36(%rsp), %eax          # 4-byte Folded Reload
	movl	$65535, %ecx                    # imm = 0xFFFF
	andl	%ecx, %eax
	movq	%rax, %rbp
	shlq	$32, %rbp
	orq	%rax, %rbp
	xorl	%r15d, %r15d
	movw	$1, %r13w
	movabsq	$34359738376, %r12              # imm = 0x800000008
.LBB4_8:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%r12, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_10
# %bb.9:                                #   in Loop: Header=BB4_8 Depth=1
	movq	16(%rsp), %rdi
	movq	24(%rsp), %rsi
	movzwl	14(%rsp), %edx
	movl	%ebx, %ecx
	callq	_Z34__device_stub__playKernelSMDynamicPKhPhii
.LBB4_10:                               #   in Loop: Header=BB4_8 Depth=1
	testb	$1, %r14b
	je	.LBB4_12
# %bb.11:                               #   in Loop: Header=BB4_8 Depth=1
	leal	1(,%r15,2), %eax
	movzwl	12(%rsp), %ecx
	cmpl	%ecx, %eax
	jae	.LBB4_14
.LBB4_12:                               #   in Loop: Header=BB4_8 Depth=1
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%r12, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_14
# %bb.13:                               #   in Loop: Header=BB4_8 Depth=1
	movq	24(%rsp), %rdi
	movq	16(%rsp), %rsi
	movzwl	14(%rsp), %edx
	movl	%ebx, %ecx
	callq	_Z34__device_stub__playKernelSMDynamicPKhPhii
.LBB4_14:                               # %_Z9gpuAssert10hipError_tPKcib.exit46
                                        #   in Loop: Header=BB4_8 Depth=1
	movzwl	%r13w, %r15d
	movzwl	12(%rsp), %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI4_1(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %eax
	incl	%r13d
	cmpl	%eax, %r15d
	jl	.LBB4_8
.LBB4_15:                               # %_Z9gpuAssert10hipError_tPKcib.exit46._crit_edge
	testb	$1, %r14b
	leaq	16(%rsp), %rax
	leaq	24(%rsp), %rcx
	cmoveq	%rax, %rcx
	movq	(%rcx), %rsi
	movq	48(%rsp), %r14                  # 8-byte Reload
	movq	%r14, %rdi
	movq	40(%rsp), %rdx                  # 8-byte Reload
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB4_16
# %bb.17:                               # %_Z9gpuAssert10hipError_tPKcib.exit48
	movq	24(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	jne	.LBB4_18
# %bb.19:                               # %_Z9gpuAssert10hipError_tPKcib.exit50
	movq	16(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	jne	.LBB4_20
# %bb.21:                               # %_Z9gpuAssert10hipError_tPKcib.exit52
	movzwl	14(%rsp), %esi
	movq	%r14, %rdi
	movl	%ebx, %edx
	callq	_Z10print_flatPhii
	movq	%r14, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB4_1:
	.cfi_def_cfa_offset 112
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.4, %esi
	movl	$.L.str.3, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$233, %r8d
	jmp	.LBB4_2
.LBB4_4:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.4, %esi
	movl	$.L.str.3, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$234, %r8d
	jmp	.LBB4_2
.LBB4_22:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.4, %esi
	movl	$.L.str.3, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$237, %r8d
	jmp	.LBB4_2
.LBB4_16:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.4, %esi
	movl	$.L.str.3, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$256, %r8d                      # imm = 0x100
	jmp	.LBB4_2
.LBB4_18:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.4, %esi
	movl	$.L.str.3, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$259, %r8d                      # imm = 0x103
	jmp	.LBB4_2
.LBB4_20:
	movl	%eax, %ebp
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.4, %esi
	movl	$.L.str.3, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$260, %r8d                      # imm = 0x104
.LBB4_2:
	xorl	%eax, %eax
	callq	fprintf
	movl	%ebp, %edi
	callq	exit
.Lfunc_end4:
	.size	main, .Lfunc_end4-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.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	$_Z19playKernelSMDynamicPKhPhii, %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	_Z19playKernelSMDynamicPKhPhii,@object # @_Z19playKernelSMDynamicPKhPhii
	.section	.rodata,"a",@progbits
	.globl	_Z19playKernelSMDynamicPKhPhii
	.p2align	3, 0x0
_Z19playKernelSMDynamicPKhPhii:
	.quad	_Z34__device_stub__playKernelSMDynamicPKhPhii
	.size	_Z19playKernelSMDynamicPKhPhii, 8

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

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/btglr/game_of_life_cuda/master/life_cuda_SM.hip"
	.size	.L.str.3, 110

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"GPUassert: %s %s %d\n"
	.size	.L.str.4, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z19playKernelSMDynamicPKhPhii"
	.size	.L__unnamed_1, 31

	.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

	.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 _Z34__device_stub__playKernelSMDynamicPKhPhii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z19playKernelSMDynamicPKhPhii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z19playKernelSMDynamicPKhPhii ; -- Begin function _Z19playKernelSMDynamicPKhPhii
	.globl	_Z19playKernelSMDynamicPKhPhii
	.p2align	8
	.type	_Z19playKernelSMDynamicPKhPhii,@function
_Z19playKernelSMDynamicPKhPhii:         ; @_Z19playKernelSMDynamicPKhPhii
; %bb.0:
	s_clause 0x1
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b64 s[2:3], s[0:1], 0x10
	v_and_b32_e32 v2, 0x3ff, v0
	v_bfe_u32 v3, v0, 10, 10
	s_lshl_b32 s8, s15, 3
	s_lshl_b32 s1, s14, 3
	s_mov_b32 s9, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshl_add_u32 v1, v2, 3, v3
	v_cmpx_gt_u32_e32 0x64, v1
	s_cbranch_execz .LBB0_5
; %bb.1:                                ; %.lr.ph.preheader
	s_mov_b32 s10, 0
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_and_b32_e32 v0, 0xff, v1
	v_mul_lo_u16 v0, 0xcd, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshrrev_b16 v0, 11, v0
	v_mul_lo_u16 v4, v0, 10
	v_and_b32_e32 v5, 0xffff, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_sub_nc_u16 v4, v1, v4
	v_add_nc_u32_e32 v7, s1, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_and_b32_e32 v4, 0xff, v4
	v_add_nc_u32_e32 v0, s8, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_and_b32_e32 v6, 0xffff, v0
	v_and_b32_e32 v0, 0xffff, v7
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_gt_i32_e32 vcc_lo, s3, v6
	v_cmp_gt_u32_e64 s0, s3, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s11, vcc_lo, s0
	s_and_saveexec_b32 s0, s11
	s_cbranch_execz .LBB0_4
; %bb.3:                                ;   in Loop: Header=BB0_2 Depth=1
	v_mad_u64_u32 v[7:8], null, v6, s3, v[0:1]
	v_mad_u32_u24 v4, v4, 10, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v0, 31, v7
	v_add_co_u32 v6, vcc_lo, s4, v7
	v_add_co_ci_u32_e32 v7, vcc_lo, s5, v0, vcc_lo
	global_load_u8 v0, v[6:7], off
	s_waitcnt vmcnt(0)
	ds_store_b8 v4, v0
.LBB0_4:                                ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s0
	v_add_nc_u32_e32 v0, 64, v1
	v_cmp_lt_u32_e32 vcc_lo, 35, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_mov_b32_e32 v1, v0
	s_or_b32 s10, vcc_lo, s10
	s_and_not1_b32 exec_lo, exec_lo, s10
	s_cbranch_execnz .LBB0_2
.LBB0_5:                                ; %Flow200
	s_or_b32 exec_lo, exec_lo, s9
	v_add3_u32 v0, v3, s8, 1
	s_mov_b32 s0, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_and_b32_e32 v1, 0xffff, v0
	s_waitcnt lgkmcnt(0)
	v_cmpx_ge_i32_e64 s2, v1
	s_cbranch_execz .LBB0_21
; %bb.6:
	v_add3_u32 v0, v2, s1, 1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_and_b32_e32 v0, 0xffff, v0
	v_cmp_ge_u32_e32 vcc_lo, s2, v0
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_21
; %bb.7:
	v_mad_u32_u24 v2, v3, 10, v2
	v_mov_b32_e32 v4, 0
	s_mov_b32 s0, 0
	s_barrier
	buffer_gl0_inv
.LBB0_8:                                ; %.preheader
                                        ; =>This Inner Loop Header: Depth=1
	v_add_nc_u32_e32 v3, s0, v2
	s_add_i32 s0, s0, 10
	ds_load_u8 v5, v3
	ds_load_u8 v6, v3 offset:1
	ds_load_u8 v3, v3 offset:2
	s_cmp_lg_u32 s0, 30
	s_waitcnt lgkmcnt(2)
	v_add_nc_u16 v4, v4, v5
	s_waitcnt lgkmcnt(1)
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_add_nc_u16 v4, v4, v6
	s_waitcnt lgkmcnt(0)
	v_add_nc_u16 v4, v4, v3
	s_cbranch_scc1 .LBB0_8
; %bb.9:                                ; %NodeBlock
	ds_load_u8 v3, v2 offset:11
	s_mov_b32 s0, 0
	s_mov_b32 s2, exec_lo
                                        ; implicit-def: $sgpr1
	s_waitcnt lgkmcnt(0)
	v_sub_nc_u16 v2, v4, v3
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_lt_i16_e32 2, v2
	s_xor_b32 s2, exec_lo, s2
	s_cbranch_execz .LBB0_13
; %bb.10:                               ; %LeafBlock193
	s_mov_b32 s4, exec_lo
                                        ; implicit-def: $sgpr1
	v_cmpx_eq_u16_e32 3, v2
; %bb.11:
	s_mov_b32 s0, exec_lo
	s_mov_b32 s1, 1
; %bb.12:                               ; %Flow196
	s_or_b32 exec_lo, exec_lo, s4
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_b32 s0, s0, exec_lo
                                        ; implicit-def: $vgpr3
.LBB0_13:                               ; %Flow195
	s_or_saveexec_b32 s2, s2
	v_mov_b32_e32 v4, s1
	s_xor_b32 exec_lo, exec_lo, s2
; %bb.14:                               ; %LeafBlock
	v_cmp_eq_u16_e32 vcc_lo, 2, v2
	v_mov_b32_e32 v4, v3
	s_and_not1_b32 s0, s0, exec_lo
	s_and_b32 s1, vcc_lo, exec_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 s0, s0, s1
; %bb.15:                               ; %Flow197
	s_or_b32 exec_lo, exec_lo, s2
	s_and_saveexec_b32 s1, s0
	s_cbranch_execz .LBB0_17
; %bb.16:                               ; %.sink.split
	v_mad_u64_u32 v[5:6], null, v1, s3, v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v3, 31, v5
	v_add_co_u32 v5, vcc_lo, s6, v5
	v_add_co_ci_u32_e32 v6, vcc_lo, s7, v3, vcc_lo
	global_store_b8 v[5:6], v4, off
.LBB0_17:
	s_or_b32 exec_lo, exec_lo, s1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mov_b32 s0, exec_lo
	v_cmpx_gt_u16_e32 2, v2
	s_cbranch_execz .LBB0_19
; %bb.18:
	v_mad_u64_u32 v[3:4], null, v1, s3, v[0:1]
	v_mov_b32_e32 v5, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_ashrrev_i32_e32 v4, 31, v3
	v_add_co_u32 v3, vcc_lo, s6, v3
	v_add_co_ci_u32_e32 v4, vcc_lo, s7, v4, vcc_lo
	global_store_b8 v[3:4], v5, off
.LBB0_19:
	s_or_b32 exec_lo, exec_lo, s0
	v_cmp_lt_u16_e32 vcc_lo, 3, v2
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_21
; %bb.20:
	v_mad_u64_u32 v[2:3], null, v1, s3, v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_ashrrev_i32_e32 v1, 31, v2
	v_add_co_u32 v0, vcc_lo, s6, v2
	v_mov_b32_e32 v2, 0
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v1, vcc_lo
	global_store_b8 v[0:1], v2, off
.LBB0_21:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z19playKernelSMDynamicPKhPhii
		.amdhsa_group_segment_fixed_size 100
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 24
		.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 9
		.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	_Z19playKernelSMDynamicPKhPhii, .Lfunc_end0-_Z19playKernelSMDynamicPKhPhii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 772
; NumSgprs: 18
; NumVgprs: 9
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 100 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 9
; 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:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 100
    .kernarg_segment_align: 8
    .kernarg_segment_size: 24
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z19playKernelSMDynamicPKhPhii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z19playKernelSMDynamicPKhPhii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     9
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
