#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

struct PPMImage {
    size_t x;
    size_t y;
    uint8_t *data;
};


PPMImage * readPPM(const char *path)
{
    // WARNING - heavily untested and not error checked code
    //  also doesn't check for comment lines
    PPMImage *img = NULL;
    FILE *myfile = fopen(path, "r");

    char magic_number[16];
    int x, y, high;
    int c;
    size_t count;

    fscanf(myfile, "%15s", magic_number);
    if (strncmp(magic_number, "P6", 16) != 0) {
        fprintf(stderr, "readPPM only expects P6\n");
        goto cleanup;
    }

    fscanf(myfile, "%d %d", &x, &y);
    fscanf(myfile, "%d", &high);

    if (high != 255) {
        fprintf(stderr, "readPPM only expects max_val of 255\n");
        goto cleanup;
    }

    // skip whitespace
    c = fgetc(myfile);
    while (!feof(myfile) && isspace(c)) {
        c = fgetc(myfile);
    }
    ungetc(c, myfile);

    img = (PPMImage *)malloc(sizeof(PPMImage));
    if (img == NULL)
        goto cleanup;
    img->x = x;
    img->y = y;
    img->data = (uint8_t *)malloc(sizeof(uint8_t) * x * y);
    count = fread(img->data, sizeof(uint8_t), x * y, myfile);

    if (count != x * y * sizeof(uint8_t)) {
        fprintf(stderr, "readPPM read incorrect number of bytes for data\n");
        free(img->data);
        free(img);
        img = NULL;
    }

cleanup:
    fclose(myfile);
    return img;
}


void freePPM(PPMImage *img)
{
    free(img->data);
    free(img);
}






int main(int argc, char const *argv[])
{
    if (argc < 2) {
        fprintf(stderr, "Usage: %s [raw-image]\n");
        exit(1);
    }

    PPMImage * img = readPPM(argv[1]);


    freePPM(img);

    return 0;
}

	.file	"tmpxft_00260b40_00000000-6_03_image_blurring.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
.LC1:
	.string	"%15s"
.LC2:
	.string	"P6"
.LC3:
	.string	"readPPM only expects P6\n"
.LC4:
	.string	"%d %d"
.LC5:
	.string	"%d"
.LC6:
	.string	"readPPM only expects max_val of 255\n"
.LC7:
	.string	"readPPM read incorrect number of bytes for data\n"
	.text
	.globl	_Z7readPPMPKc
	.type	_Z7readPPMPKc, @function
_Z7readPPMPKc:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC0(%rip), %rsi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$56, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rbp
	call	fopen@PLT
	movq	%rbp, %rdx
	leaq	.LC1(%rip), %rsi
	movq	%rax, %rdi
	movq	%rax, %rbx
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	leaq	.LC2(%rip), %rsi
	movq	%rbp, %rdi
	call	strcmp@PLT
	leaq	.LC3(%rip), %rdx
	testl	%eax, %eax
	jne	.L17
	leaq	16(%rsp), %rcx
	leaq	12(%rsp), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	leaq	.LC4(%rip), %rsi
	call	__isoc23_fscanf@PLT
	xorl	%eax, %eax
	leaq	20(%rsp), %rdx
	movq	%rbx, %rdi
	leaq	.LC5(%rip), %rsi
	call	__isoc23_fscanf@PLT
	cmpl	$255, 20(%rsp)
	je	.L5
	leaq	.LC6(%rip), %rdx
.L17:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L4:
	xorl	%r13d, %r13d
	jmp	.L6
.L19:
	movl	%ebp, %edi
	call	isspace@PLT
	testl	%eax, %eax
	je	.L7
.L5:
	movq	%rbx, %rdi
	call	fgetc@PLT
	movq	%rbx, %rdi
	movl	%eax, %ebp
	call	feof@PLT
	testl	%eax, %eax
	je	.L19
.L7:
	movl	%ebp, %edi
	movq	%rbx, %rsi
	call	ungetc@PLT
	movl	$24, %edi
	call	malloc@PLT
	movq	%rax, %rbp
	movq	%rax, %r13
	testq	%rax, %rax
	je	.L4
	movslq	12(%rsp), %r12
	movq	%r12, (%rax)
	movslq	16(%rsp), %rax
	imulq	%rax, %r12
	movq	%rax, 8(%rbp)
	movq	%r12, %rdi
	call	malloc@PLT
	movl	$1, %edx
	movq	%rbx, %r8
	movq	%r12, %rcx
	movq	%rax, 16(%rbp)
	movq	%rax, %rdi
	movq	%r12, %rsi
	call	__fread_chk@PLT
	movq	%rax, %rdx
	movl	12(%rsp), %eax
	imull	16(%rsp), %eax
	cltq
	cmpq	%rdx, %rax
	je	.L6
	movq	stderr(%rip), %rdi
	leaq	.LC7(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	16(%rbp), %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	jmp	.L4
.L6:
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L9
	call	__stack_chk_fail@PLT
.L9:
	addq	$56, %rsp
	.cfi_def_cfa_offset 40
	movq	%r13, %rax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z7readPPMPKc, .-_Z7readPPMPKc
	.globl	_Z7freePPMP8PPMImage
	.type	_Z7freePPMP8PPMImage, @function
_Z7freePPMP8PPMImage:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	16(%rdi), %rdi
	call	free@PLT
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z7freePPMP8PPMImage, .-_Z7freePPMP8PPMImage
	.section	.rodata.str1.1
.LC8:
	.string	"Usage: %s [raw-image]\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	decl	%edi
	pushq	%rcx
	.cfi_def_cfa_offset 16
	jg	.L23
	movq	stderr(%rip), %rdi
	leaq	.LC8(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L23:
	movq	8(%rsi), %rdi
	call	_Z7readPPMPKc
	movq	%rax, %rdi
	call	_Z7freePPMP8PPMImage
	xorl	%eax, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.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
.LFE2055:
	.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
	.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 <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

struct PPMImage {
    size_t x;
    size_t y;
    uint8_t *data;
};


PPMImage * readPPM(const char *path)
{
    // WARNING - heavily untested and not error checked code
    //  also doesn't check for comment lines
    PPMImage *img = NULL;
    FILE *myfile = fopen(path, "r");

    char magic_number[16];
    int x, y, high;
    int c;
    size_t count;

    fscanf(myfile, "%15s", magic_number);
    if (strncmp(magic_number, "P6", 16) != 0) {
        fprintf(stderr, "readPPM only expects P6\n");
        goto cleanup;
    }

    fscanf(myfile, "%d %d", &x, &y);
    fscanf(myfile, "%d", &high);

    if (high != 255) {
        fprintf(stderr, "readPPM only expects max_val of 255\n");
        goto cleanup;
    }

    // skip whitespace
    c = fgetc(myfile);
    while (!feof(myfile) && isspace(c)) {
        c = fgetc(myfile);
    }
    ungetc(c, myfile);

    img = (PPMImage *)malloc(sizeof(PPMImage));
    if (img == NULL)
        goto cleanup;
    img->x = x;
    img->y = y;
    img->data = (uint8_t *)malloc(sizeof(uint8_t) * x * y);
    count = fread(img->data, sizeof(uint8_t), x * y, myfile);

    if (count != x * y * sizeof(uint8_t)) {
        fprintf(stderr, "readPPM read incorrect number of bytes for data\n");
        free(img->data);
        free(img);
        img = NULL;
    }

cleanup:
    fclose(myfile);
    return img;
}


void freePPM(PPMImage *img)
{
    free(img->data);
    free(img);
}






int main(int argc, char const *argv[])
{
    if (argc < 2) {
        fprintf(stderr, "Usage: %s [raw-image]\n");
        exit(1);
    }

    PPMImage * img = readPPM(argv[1]);


    freePPM(img);

    return 0;
}

	.text
	.file	"03_image_blurring.hip"
	.globl	_Z7readPPMPKc                   # -- Begin function _Z7readPPMPKc
	.type	_Z7readPPMPKc,@function
_Z7readPPMPKc:                          # @_Z7readPPMPKc
	.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
	subq	$32, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, %rbx
	leaq	16(%rsp), %r14
	movl	$.L.str.1, %esi
	movq	%rax, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	$13904, %eax                    # imm = 0x3650
	xorl	(%r14), %eax
	movzbl	2(%r14), %ecx
	orw	%ax, %cx
	jne	.LBB0_1
# %bb.3:
	leaq	8(%rsp), %rdx
	leaq	4(%rsp), %rcx
	movl	$.L.str.4, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	leaq	12(%rsp), %r14
	movl	$.L.str.5, %esi
	movq	%rbx, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$255, (%r14)
	jne	.LBB0_4
.LBB0_5:                                # =>This Inner Loop Header: Depth=1
	movq	%rbx, %rdi
	callq	fgetc
	movl	%eax, %ebp
	movq	%rbx, %rdi
	callq	feof
	testl	%eax, %eax
	jne	.LBB0_7
# %bb.6:                                # %.lr.ph
                                        #   in Loop: Header=BB0_5 Depth=1
	movl	%ebp, %edi
	callq	isspace
	testl	%eax, %eax
	jne	.LBB0_5
.LBB0_7:                                # %.critedge
	movl	%ebp, %edi
	movq	%rbx, %rsi
	callq	ungetc
	movl	$24, %edi
	callq	malloc
	testq	%rax, %rax
	je	.LBB0_10
# %bb.8:
	movq	%rax, %r14
	movslq	8(%rsp), %rax
	movq	%rax, (%r14)
	movslq	4(%rsp), %r12
	movq	%r12, 8(%r14)
	imulq	%rax, %r12
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r15
	movq	%rax, 16(%r14)
	movl	$1, %esi
	movq	%rax, %rdi
	movq	%r12, %rdx
	movq	%rbx, %rcx
	callq	fread
	movslq	8(%rsp), %rcx
	movslq	4(%rsp), %rdx
	imulq	%rcx, %rdx
	cmpq	%rdx, %rax
	je	.LBB0_11
# %bb.9:
	movq	stderr(%rip), %rcx
	movl	$.L.str.7, %edi
	movl	$48, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	movq	%r15, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	jmp	.LBB0_10
.LBB0_1:
	movq	stderr(%rip), %rcx
	movl	$.L.str.3, %edi
	movl	$24, %esi
	jmp	.LBB0_2
.LBB0_4:
	movq	stderr(%rip), %rcx
	movl	$.L.str.6, %edi
	movl	$36, %esi
.LBB0_2:
	movl	$1, %edx
	callq	fwrite@PLT
.LBB0_10:
	xorl	%r14d, %r14d
.LBB0_11:
	movq	%rbx, %rdi
	callq	fclose
	movq	%r14, %rax
	addq	$32, %rsp
	.cfi_def_cfa_offset 48
	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
	retq
.Lfunc_end0:
	.size	_Z7readPPMPKc, .Lfunc_end0-_Z7readPPMPKc
	.cfi_endproc
                                        # -- End function
	.globl	_Z7freePPMP8PPMImage            # -- Begin function _Z7freePPMP8PPMImage
	.type	_Z7freePPMP8PPMImage,@function
_Z7freePPMP8PPMImage:                   # @_Z7freePPMP8PPMImage
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	16(%rdi), %rdi
	callq	free
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	free                            # TAILCALL
.Lfunc_end1:
	.size	_Z7freePPMP8PPMImage, .Lfunc_end1-_Z7freePPMP8PPMImage
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	cmpl	$1, %edi
	jle	.LBB2_2
# %bb.1:
	movq	8(%rsi), %rdi
	callq	_Z7readPPMPKc
	movq	%rax, %rbx
	movq	16(%rax), %rdi
	callq	free
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB2_2:
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rdi
	movl	$.L.str.8, %esi
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"r"
	.size	.L.str, 2

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

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

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"readPPM only expects P6\n"
	.size	.L.str.3, 25

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

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

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"readPPM only expects max_val of 255\n"
	.size	.L.str.6, 37

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"readPPM read incorrect number of bytes for data\n"
	.size	.L.str.7, 49

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Usage: %s [raw-image]\n"
	.size	.L.str.8, 23

	.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
