// File: /workspace/github_c_makefile_star2_O3/Nealsbo[P]C-Software-Renderer/src/image.c
// Function: Bitmap_LoadPPM6
bitmap_t *Bitmap_LoadPPM6( const char *fileName ) {
    int BUFLEN = 64;
    int r, i, map_pointer;
    char buf[BUFLEN], *t;
    unsigned char color_buffer[3];
    unsigned int width, height, cdepth;

    FILE     *fp;
    color_t  *map;
    bitmap_t *image;

    if( ( fp = fopen(fileName, "rb") ) == NULL ) {
        printf( "Error: Cannot open file: '%s'!\n", fileName );
        return NULL;
    }

    t = fgets( buf, BUFLEN, fp );

    if ( (t == NULL) || ( strncmp(buf, "P6\n", 3) != 0 ) )
        return NULL;

    do {
        t = fgets( buf, BUFLEN, fp );
        if ( t == NULL )
            return NULL;
    } while ( strncmp(buf, "#", 1) == 0 );

    r = sscanf( buf, "%u %u", &width, &height );
    if ( r < 2 )
        return NULL;

    r = fscanf( fp, "%u", &cdepth );
    if ( (r < 1) || ( cdepth != 255 ) )
        return NULL;

    fseek( fp, 1, SEEK_CUR );

    i = 0;
    map_pointer = width * height;

    image = (bitmap_t *)malloc( sizeof(bitmap_t) );
    image->width  = width;
    image->height = height;
    map = (color_t *)malloc( sizeof(color_t) * width * height );

    while( !feof(fp) ) {
        if( i == map_pointer ) break;
        if( fread( color_buffer, 1, 3, fp ) ) {
            map[i].r = color_buffer[0];
            map[i].g = color_buffer[1];
            map[i].b = color_buffer[2];
            map[i].a = 255;
        } i++;
    }
    image->bitmap = map;
    return image;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r15
leaq 0x4fe7(%rip), %rsi      # 0x7237
callq 0x1180
testq %rax, %rax
je 0x228f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
movl $0x40, %esi
movq %rax, %rdx
callq 0x1220
testq %rax, %rax
je 0x228a
movl $0x3650, %eax           # imm = 0x3650
xorl 0x10(%rsp), %eax
movzbl 0x12(%rsp), %ecx
xorl $0xa, %ecx
orw %ax, %cx
je 0x22b5
xorl %r14d, %r14d
jmp 0x22a3
leaq 0x4fa4(%rip), %rdi      # 0x723a
xorl %r14d, %r14d
movq %r15, %rsi
xorl %eax, %eax
callq 0x1040
movq %r14, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %r15
xorl %r14d, %r14d
movq %r15, %rdi
movl $0x40, %esi
movq %rbx, %rdx
callq 0x1220
testq %rax, %rax
je 0x22a3
cmpb $0x23, 0x10(%rsp)
je 0x22bd
leaq 0x4f7e(%rip), %rsi      # 0x725e
xorl %r14d, %r14d
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x1140
cmpl $0x2, %eax
jl 0x22a3
leaq 0x4f5c(%rip), %rsi      # 0x7261
xorl %r14d, %r14d
leaq 0x4(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1240
testl %eax, %eax
jle 0x22a3
cmpl $0xff, 0x4(%rsp)
jne 0x22a3
movl $0x1, %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x1160
movl 0xc(%rsp), %r15d
movl 0x8(%rsp), %r12d
movl %r12d, %ebp
imull %r15d, %ebp
movl $0x10, %edi
callq 0x11a0
movq %rax, %r14
movl %r15d, (%rax)
movl %r12d, 0x4(%rax)
imulq %r12, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x11a0
movq %rax, %r15
movq %rbx, %rdi
callq 0x1200
testl %eax, %eax
setne %al
testl %ebp, %ebp
sete %cl
orb %al, %cl
jne 0x23e8
movl %ebp, %r13d
decq %r13
xorl %ebp, %ebp
leaq 0x1(%rsp), %r12
movl $0x1, %esi
movl $0x3, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0x11e0
testq %rax, %rax
je 0x23d0
movb 0x1(%rsp), %al
movb %al, (%r15,%rbp,4)
movb 0x2(%rsp), %al
movb %al, 0x1(%r15,%rbp,4)
movb 0x3(%rsp), %al
movb %al, 0x2(%r15,%rbp,4)
movb $-0x1, 0x3(%r15,%rbp,4)
movq %rbx, %rdi
callq 0x1200
testl %eax, %eax
jne 0x23e8
leaq 0x1(%rbp), %rax
cmpq %rbp, %r13
movq %rax, %rbp
jne 0x2396
movq %r15, 0x8(%r14)
jmp 0x22a3