#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>

#define BLOCK_SIZE 256
#define STR_SIZE 256
#define HALO 1 // halo width along one direction when advancing to the next iteration

#define BENCH_PRINT

void run(int argc, char** argv);

long rows, cols;
int* data;
#define M_SEED 9

void init(int argc, char** argv)
{
    char *folder;
    char *filepath;

	if (argc == 4)
    {
		cols = atol(argv[1]);
		rows = atol(argv[2]);
        folder = argv[3];
	}
    else
    {
        printf("Usage: %s row_len col_len folder\n", argv[0]);
        exit(0);
    }

    filepath = (char *)malloc(sizeof(char) * (strlen(folder) + 128));
    if (!filepath)
    {
        fprintf(stderr, "Cannot allocate filepath");
        exit(EXIT_FAILURE);
    }

    sprintf(filepath, "%s/data.mem", folder);

	FILE *fp;

	if ((fp = fopen(filepath, "w+")) == 0)
    {
        fprintf(stderr, "%s was not opened\n", filepath);
        exit(EXIT_FAILURE);
    }
    if (ftruncate(fileno(fp), sizeof(int) * rows * cols) != 0)
    {
		fprintf(stderr, "error: can not truncate %s\n", filepath);
        perror("ftruncate");
        exit(EXIT_FAILURE);
    }
    data = (int *)mmap(NULL, sizeof(int) * rows * cols, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, fileno(fp), 0);

    if (!data)
    {
        fprintf(stderr, "Cannot mmap %s.\n", filepath);
        exit(EXIT_FAILURE);
    }

	int seed = M_SEED;
	srand(seed);

	for (long i = 0; i < rows; i++)
    {
        for (long j = 0; j < cols; j++)
        {
            data[i * cols + j] = rand() % 10;
        }
    }

    fflush(fp);
	fclose(fp);	

    free(filepath);
}

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

    return EXIT_SUCCESS;
}


	.file	"tmpxft_002498c2_00000000-6_generator.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2044:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2044:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Cannot allocate filepath"
.LC1:
	.string	"%s/data.mem"
.LC2:
	.string	"w+"
.LC3:
	.string	"%s was not opened\n"
.LC4:
	.string	"error: can not truncate %s\n"
.LC5:
	.string	"ftruncate"
.LC6:
	.string	"Cannot mmap %s.\n"
.LC7:
	.string	"Usage: %s row_len col_len folder\n"
	.text
	.globl	_Z4initiPPc
	.type	_Z4initiPPc, @function
_Z4initiPPc:
.LFB2040:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movq	%rsi, %rbx
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	cmpl	$4, %edi
	jne	.L3
	movq	8(%rsi), %rdi
	call	atol@PLT
	movq	16(%rbx), %rdi
	movq	%rax, cols(%rip)
	call	atol@PLT
	movq	24(%rbx), %r8
	movq	%rax, rows(%rip)
	movq	%r8, %rdi
	movq	%r8, 8(%rsp)
	call	strlen@PLT
	leaq	128(%rax), %rbp
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	8(%rsp), %r8
	testq	%rax, %rax
	movq	%rax, %rbx
	jne	.L4
	movq	stderr(%rip), %rdi
	leaq	.LC0(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L13
.L4:
	movq	%rbp, %rdx
	movq	%rax, %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC1(%rip), %rcx
	call	__sprintf_chk@PLT
	leaq	.LC2(%rip), %rsi
	movq	%rbx, %rdi
	call	fopen@PLT
	movq	%rax, %rbp
	testq	%rax, %rax
	jne	.L5
	movq	stderr(%rip), %rdi
	movq	%rbx, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC3(%rip), %rdx
.L15:
	call	__fprintf_chk@PLT
.L13:
	movl	$1, %edi
.L14:
	call	exit@PLT
.L5:
	movq	rows(%rip), %r12
	imulq	cols(%rip), %r12
	movq	%rax, %rdi
	call	fileno@PLT
	movl	%eax, %edi
	salq	$2, %r12
	movq	%r12, %rsi
	call	ftruncate@PLT
	testl	%eax, %eax
	je	.L6
	movq	stderr(%rip), %rdi
	movq	%rbx, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC4(%rip), %rdx
	call	__fprintf_chk@PLT
	leaq	.LC5(%rip), %rdi
	call	perror@PLT
	jmp	.L13
.L6:
	movq	%rbp, %rdi
	call	fileno@PLT
	xorl	%r9d, %r9d
	movl	$16385, %ecx
	xorl	%edi, %edi
	movq	rows(%rip), %rsi
	movl	%eax, %r8d
	imulq	cols(%rip), %rsi
	movl	$3, %edx
	salq	$2, %rsi
	call	mmap@PLT
	movq	%rax, data(%rip)
	testq	%rax, %rax
	jne	.L7
	movq	stderr(%rip), %rdi
	movq	%rbx, %rcx
	movl	$2, %esi
	leaq	.LC6(%rip), %rdx
	jmp	.L15
.L7:
	movl	$9, %edi
	xorl	%r12d, %r12d
	movl	$10, %r14d
	call	srand@PLT
.L8:
	cmpq	rows(%rip), %r12
	jge	.L17
	xorl	%r13d, %r13d
.L10:
	cmpq	cols(%rip), %r13
	jge	.L18
	call	rand@PLT
	movq	cols(%rip), %rcx
	cltd
	idivl	%r14d
	movq	data(%rip), %rax
	imulq	%r12, %rcx
	addq	%r13, %rcx
	incq	%r13
	movl	%edx, (%rax,%rcx,4)
	jmp	.L10
.L18:
	incq	%r12
	jmp	.L8
.L17:
	movq	%rbp, %rdi
	call	fflush@PLT
	movq	%rbp, %rdi
	call	fclose@PLT
	addq	$16, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 48
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	free@PLT
.L3:
	.cfi_restore_state
	movq	(%rsi), %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %rsi
	call	__printf_chk@PLT
	xorl	%edi, %edi
	jmp	.L14
	.cfi_endproc
.LFE2040:
	.size	_Z4initiPPc, .-_Z4initiPPc
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2041:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	_Z4initiPPc
	xorl	%eax, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2041:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2067:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2067:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	data
	.bss
	.align 8
	.type	data, @object
	.size	data, 8
data:
	.zero	8
	.globl	cols
	.align 8
	.type	cols, @object
	.size	cols, 8
cols:
	.zero	8
	.globl	rows
	.align 8
	.type	rows, @object
	.size	rows, 8
rows:
	.zero	8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>

#define BLOCK_SIZE 256
#define STR_SIZE 256
#define HALO 1 // halo width along one direction when advancing to the next iteration

#define BENCH_PRINT

void run(int argc, char** argv);

long rows, cols;
int* data;
#define M_SEED 9

void init(int argc, char** argv)
{
    char *folder;
    char *filepath;

	if (argc == 4)
    {
		cols = atol(argv[1]);
		rows = atol(argv[2]);
        folder = argv[3];
	}
    else
    {
        printf("Usage: %s row_len col_len folder\n", argv[0]);
        exit(0);
    }

    filepath = (char *)malloc(sizeof(char) * (strlen(folder) + 128));
    if (!filepath)
    {
        fprintf(stderr, "Cannot allocate filepath");
        exit(EXIT_FAILURE);
    }

    sprintf(filepath, "%s/data.mem", folder);

	FILE *fp;

	if ((fp = fopen(filepath, "w+")) == 0)
    {
        fprintf(stderr, "%s was not opened\n", filepath);
        exit(EXIT_FAILURE);
    }
    if (ftruncate(fileno(fp), sizeof(int) * rows * cols) != 0)
    {
		fprintf(stderr, "error: can not truncate %s\n", filepath);
        perror("ftruncate");
        exit(EXIT_FAILURE);
    }
    data = (int *)mmap(NULL, sizeof(int) * rows * cols, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, fileno(fp), 0);

    if (!data)
    {
        fprintf(stderr, "Cannot mmap %s.\n", filepath);
        exit(EXIT_FAILURE);
    }

	int seed = M_SEED;
	srand(seed);

	for (long i = 0; i < rows; i++)
    {
        for (long j = 0; j < cols; j++)
        {
            data[i * cols + j] = rand() % 10;
        }
    }

    fflush(fp);
	fclose(fp);	

    free(filepath);
}

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

    return EXIT_SUCCESS;
}


	.text
	.file	"generator.hip"
	.globl	_Z4initiPPc                     # -- Begin function _Z4initiPPc
	.type	_Z4initiPPc,@function
_Z4initiPPc:                            # @_Z4initiPPc
	.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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	cmpl	$4, %edi
	jne	.LBB0_13
# %bb.1:
	movq	8(%rbx), %rdi
	callq	atol
	movq	%rax, cols(%rip)
	movq	16(%rbx), %rdi
	callq	atol
	movq	%rax, rows(%rip)
	movq	24(%rbx), %r14
	movq	%r14, %rdi
	callq	strlen
	leaq	128(%rax), %rdi
	callq	malloc
	testq	%rax, %rax
	je	.LBB0_14
# %bb.2:
	movq	%rax, %rbx
	movl	$.L.str.2, %esi
	movq	%rax, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	sprintf
	movl	$.L.str.3, %esi
	movq	%rbx, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_15
# %bb.3:
	movq	%rax, %r14
	movq	%rax, %rdi
	callq	fileno
	movq	rows(%rip), %rsi
	imulq	cols(%rip), %rsi
	shlq	$2, %rsi
	movl	%eax, %edi
	callq	ftruncate
	testl	%eax, %eax
	jne	.LBB0_16
# %bb.4:
	movq	rows(%rip), %r15
	imulq	cols(%rip), %r15
	shlq	$2, %r15
	movq	%r14, %rdi
	callq	fileno
	xorl	%edi, %edi
	movq	%r15, %rsi
	movl	$3, %edx
	movl	$16385, %ecx                    # imm = 0x4001
	movl	%eax, %r8d
	xorl	%r9d, %r9d
	callq	mmap
	movq	%rax, data(%rip)
	testq	%rax, %rax
	je	.LBB0_17
# %bb.5:
	movl	$9, %edi
	callq	srand
	movq	rows(%rip), %rcx
	testq	%rcx, %rcx
	jle	.LBB0_12
# %bb.6:                                # %.preheader.preheader
	movq	cols(%rip), %rax
	xorl	%r15d, %r15d
	xorl	%r12d, %r12d
.LBB0_7:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_9 Depth 2
	testq	%rax, %rax
	jle	.LBB0_11
# %bb.8:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB0_7 Depth=1
	xorl	%r13d, %r13d
.LBB0_9:                                # %.lr.ph
                                        #   Parent Loop BB0_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	callq	rand
	movslq	%eax, %rcx
	imulq	$1717986919, %rcx, %rax         # imm = 0x66666667
	movq	%rax, %rdx
	shrq	$63, %rdx
	sarq	$34, %rax
	addl	%edx, %eax
	addl	%eax, %eax
	leal	(%rax,%rax,4), %edx
	movq	cols(%rip), %rax
	movq	%r15, %rsi
	imulq	%rax, %rsi
	addq	data(%rip), %rsi
	subl	%edx, %ecx
	movl	%ecx, (%rsi,%r13,4)
	incq	%r13
	cmpq	%rax, %r13
	jl	.LBB0_9
# %bb.10:                               # %._crit_edge.loopexit
                                        #   in Loop: Header=BB0_7 Depth=1
	movq	rows(%rip), %rcx
.LBB0_11:                               # %._crit_edge
                                        #   in Loop: Header=BB0_7 Depth=1
	incq	%r12
	addq	$4, %r15
	cmpq	%rcx, %r12
	jl	.LBB0_7
.LBB0_12:                               # %._crit_edge31
	movq	%r14, %rdi
	callq	fflush
	movq	%r14, %rdi
	callq	fclose
	movq	%rbx, %rdi
	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
	jmp	free                            # TAILCALL
.LBB0_13:
	.cfi_def_cfa_offset 48
	movq	(%rbx), %rsi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	xorl	%edi, %edi
	callq	exit
.LBB0_14:
	movq	stderr(%rip), %rcx
	movl	$.L.str.1, %edi
	movl	$24, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	jmp	.LBB0_19
.LBB0_15:
	movq	stderr(%rip), %rdi
	movl	$.L.str.4, %esi
	jmp	.LBB0_18
.LBB0_16:
	movq	stderr(%rip), %rdi
	movl	$.L.str.5, %esi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$.L.str.6, %edi
	callq	perror
	jmp	.LBB0_19
.LBB0_17:
	movq	stderr(%rip), %rdi
	movl	$.L.str.7, %esi
.LBB0_18:
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
.LBB0_19:
	movl	$1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z4initiPPc, .Lfunc_end0-_Z4initiPPc
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	_Z4initiPPc
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	rows,@object                    # @rows
	.bss
	.globl	rows
	.p2align	3, 0x0
rows:
	.quad	0                               # 0x0
	.size	rows, 8

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

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

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Usage: %s row_len col_len folder\n"
	.size	.L.str, 34

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Cannot allocate filepath"
	.size	.L.str.1, 25

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"%s/data.mem"
	.size	.L.str.2, 12

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"w+"
	.size	.L.str.3, 3

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"%s was not opened\n"
	.size	.L.str.4, 19

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"error: can not truncate %s\n"
	.size	.L.str.5, 28

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"ftruncate"
	.size	.L.str.6, 10

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Cannot mmap %s.\n"
	.size	.L.str.7, 17

	.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 __hip_cuid_

	.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_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
