// File: /workspace/github_c_makefile_star2_O2/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 0x5062(%rip), %rsi      # 0x7237
callq 0x1190
testq %rax, %rax
je 0x2214
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
pushq $0x40
popq %rsi
movq %rax, %rdx
callq 0x1230
testq %rax, %rax
je 0x220f
leaq 0x505c(%rip), %rsi      # 0x725a
leaq 0x10(%rsp), %rdi
pushq $0x3
popq %rdx
callq 0x1160
testl %eax, %eax
je 0x223a
xorl %r14d, %r14d
jmp 0x2228
leaq 0x501f(%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
pushq $0x40
popq %rbp
xorl %r14d, %r14d
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x1230
testq %rax, %rax
je 0x2228
cmpb $0x23, 0x10(%rsp)
je 0x2245
leaq 0x4ff9(%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 0x2228
leaq 0x4fd7(%rip), %rsi      # 0x7261
xorl %r14d, %r14d
leaq 0x4(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1250
testl %eax, %eax
jle 0x2228
cmpl $0xff, 0x4(%rsp)
jne 0x2228
pushq $0x1
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x1170
movl 0xc(%rsp), %r12d
movl 0x8(%rsp), %r13d
movl %r13d, %r15d
imull %r12d, %r15d
pushq $0x10
popq %rdi
callq 0x11b0
movq %rax, %r14
movl %r12d, (%rax)
movl %r13d, 0x4(%rax)
imulq %r13, %r12
shlq $0x2, %r12
movq %r12, %rdi
callq 0x11b0
movq %rax, %r12
xorl %r13d, %r13d
pushq $0x3
popq %rbp
movq %rbx, %rdi
callq 0x1210
testl %eax, %eax
jne 0x2346
cmpq %r13, %r15
je 0x2346
leaq 0x1(%rsp), %rdi
pushq $0x1
popq %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x11f0
testq %rax, %rax
je 0x2341
movb 0x1(%rsp), %al
movb %al, (%r12,%r13,4)
movb 0x2(%rsp), %al
movb %al, 0x1(%r12,%r13,4)
movb 0x3(%rsp), %al
movb %al, 0x2(%r12,%r13,4)
movb $-0x1, 0x3(%r12,%r13,4)
incq %r13
jmp 0x22f8
movq %r12, 0x8(%r14)
jmp 0x2228