// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl $0x40, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
leaq 0xb017(%rip), %rsi      # 0xe24b
callq 0x1190
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x3269
movq -0x10(%rbp), %rsi
leaq 0xb000(%rip), %rdi      # 0xe24e
movb $0x0, %al
callq 0x1040
movq $0x0, -0x8(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x3470
movq -0x70(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x50(%rbp), %rdx
callq 0x1210
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x329e
movq -0x70(%rbp), %rdi
leaq 0xafdf(%rip), %rsi      # 0xe26e
movl $0x3, %edx
callq 0x10a0
cmpl $0x0, %eax
je 0x32b2
movq $0x0, -0x8(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x3470
jmp 0x32b4
movq -0x70(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x50(%rbp), %rdx
callq 0x1210
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x32e3
movq $0x0, -0x8(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x3470
jmp 0x32e5
movq -0x70(%rbp), %rdi
leaq 0xaf82(%rip), %rsi      # 0xe272
movl $0x1, %edx
callq 0x10a0
cmpl $0x0, %eax
je 0x32b4
movq -0x70(%rbp), %rdi
leaq 0xaf6a(%rip), %rsi      # 0xe274
leaq -0x40(%rbp), %rdx
leaq -0x44(%rbp), %rcx
movb $0x0, %al
callq 0x1150
movl %eax, -0x18(%rbp)
cmpl $0x2, -0x18(%rbp)
jge 0x3336
movq $0x0, -0x8(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x3470
movq -0x50(%rbp), %rdi
leaq 0xaf36(%rip), %rsi      # 0xe277
leaq -0x48(%rbp), %rdx
movb $0x0, %al
callq 0x1230
movl %eax, -0x18(%rbp)
cmpl $0x1, -0x18(%rbp)
jl 0x335e
cmpl $0xff, -0x48(%rbp)
je 0x3372
movq $0x0, -0x8(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0x3470
movq -0x50(%rbp), %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x1170
movl $0x0, -0x1c(%rbp)
movl -0x40(%rbp), %eax
imull -0x44(%rbp), %eax
movl %eax, -0x20(%rbp)
movl $0x10, %edi
callq 0x11b0
movq %rax, -0x60(%rbp)
movl -0x40(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
movl -0x44(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x40(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
movl -0x44(%rbp), %eax
imulq %rax, %rdi
callq 0x11b0
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0x11f0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x33e7
jmp 0x3455
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jne 0x33f1
jmp 0x3455
leaq -0x3b(%rbp), %rdi
movq -0x50(%rbp), %rcx
movl $0x1, %esi
movl $0x3, %edx
callq 0x11e0
cmpq $0x0, %rax
je 0x3447
movb -0x3b(%rbp), %dl
movq -0x58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movb %dl, (%rax,%rcx,4)
movb -0x3a(%rbp), %dl
movq -0x58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movb %dl, 0x1(%rax,%rcx,4)
movb -0x39(%rbp), %dl
movq -0x58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movb %dl, 0x2(%rax,%rcx,4)
movq -0x58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movb $-0x1, 0x3(%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x33d0
movq -0x58(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x64(%rbp)
movq -0x28(%rbp), %rax
movq %rax, %rsp
movq -0x8(%rbp), %rax
movq %rbp, %rsp
popq %rbp
retq