// File: /workspace/github_c_makefile_star2_O0/DavidSM64[P]n64graphics_ci/n64graphics_ci.c
// Function: png2rgba
rgba *png2rgba(const char *png_filename, int *width, int *height)
{
    rgba *img = NULL;
    int w = 0;
    int h = 0;
    int channels = 0;
    int img_size;

    stbi_uc *data = stbi_load(png_filename, &w, &h, &channels, STBI_default);
    if (!data || w <= 0 || h <= 0) {
        ERROR("Error loading png file \"%s\"\n", png_filename);
        return NULL;
    }
    INFO("Read \"%s\" %dx%d channels: %d\n", png_filename, w, h, channels);

    img_size = w * h * sizeof(*img);
    img = malloc(img_size);
    if (!img) {
        ERROR("Error allocating %u bytes\n", img_size);
        return NULL;
    }

    switch (channels) {
    case 3: // red, green, blue
    case 4: // red, green, blue, alpha
        for (int j = 0; j < h; j++) {
            for (int i = 0; i < w; i++) {
                int idx = j*w + i;
                img[idx].red = data[channels*idx];
                img[idx].green = data[channels*idx + 1];
                img[idx].blue = data[channels*idx + 2];
                if (channels == 4) {
                    img[idx].alpha = data[channels*idx + 3];
                }
                else {
                    img[idx].alpha = 0xFF;
                }
            }
        }
        break;
    case 2: // grey, alpha
        for (int j = 0; j < h; j++) {
            for (int i = 0; i < w; i++) {
                int idx = j*w + i;
                img[idx].red = data[2 * idx];
                img[idx].green = data[2 * idx];
                img[idx].blue = data[2 * idx];
                img[idx].alpha = data[2 * idx + 1];
            }
        }
        break;
    default:
        ERROR("Don't know how to read channels: %d\n", channels);
        free(img);
        img = NULL;
    }

    // cleanup
    stbi_image_free(data);

    *width = w;
    *height = h;
    return img;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x34(%rbp), %rcx
xorl %r8d, %r8d
callq 0x4900
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x9c50
cmpl $0x0, -0x2c(%rbp)
jle 0x9c50
cmpl $0x0, -0x30(%rbp)
jg 0x9c79
movq 0x1e399(%rip), %rax     # 0x27ff0
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x1927f(%rip), %rsi     # 0x22ee4
movb $0x0, %al
callq 0x1170
movq $0x0, -0x8(%rbp)
jmp 0x9f08
leaq 0x1eac8(%rip), %rax     # 0x28748
cmpl $0x0, (%rax)
je 0x9ca1
movq -0x10(%rbp), %rsi
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %r8d
leaq 0x19267(%rip), %rdi     # 0x22f01
movb $0x0, %al
callq 0x10d0
movl -0x2c(%rbp), %eax
imull -0x30(%rbp), %eax
cltq
shlq $0x2, %rax
movl %eax, -0x38(%rbp)
movslq -0x38(%rbp), %rdi
callq 0x11d0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x9ced
movq 0x1e324(%rip), %rax     # 0x27ff0
movq (%rax), %rdi
movl -0x38(%rbp), %edx
leaq 0x191cd(%rip), %rsi     # 0x22ea6
movb $0x0, %al
callq 0x1170
movq $0x0, -0x8(%rbp)
jmp 0x9f08
movl -0x34(%rbp), %eax
movl %eax, -0x5c(%rbp)
subl $0x2, %eax
je 0x9df8
jmp 0x9cfe
movl -0x5c(%rbp), %eax
addl $-0x3, %eax
subl $0x1, %eax
ja 0x9eb9
jmp 0x9d0f
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x9df3
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x9de3
movl -0x44(%rbp), %eax
imull -0x2c(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
imull -0x4c(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx,4)
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
imull -0x4c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, 0x1(%rax,%rcx,4)
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
imull -0x4c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, 0x2(%rax,%rcx,4)
cmpl $0x4, -0x34(%rbp)
jne 0x9dc6
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
imull -0x4c(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, 0x3(%rax,%rcx,4)
jmp 0x9dd3
movq -0x28(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb $-0x1, 0x3(%rax,%rcx,4)
jmp 0x9dd5
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x9d29
jmp 0x9de5
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x9d16
jmp 0x9ee5
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x9eb7
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x9ea7
movl -0x50(%rbp), %eax
imull -0x2c(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movl -0x58(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movb %dl, (%rax,%rcx,4)
movq -0x40(%rbp), %rax
movl -0x58(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movb %dl, 0x1(%rax,%rcx,4)
movq -0x40(%rbp), %rax
movl -0x58(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movb %dl, 0x2(%rax,%rcx,4)
movq -0x40(%rbp), %rax
movl -0x58(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movb %dl, 0x3(%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x9e12
jmp 0x9ea9
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x9dff
jmp 0x9ee5
movq 0x1e130(%rip), %rax     # 0x27ff0
movq (%rax), %rdi
movl -0x34(%rbp), %edx
leaq 0x19052(%rip), %rsi     # 0x22f1f
movb $0x0, %al
callq 0x1170
movq -0x28(%rbp), %rdi
callq 0x1040
movq $0x0, -0x28(%rbp)
movq -0x40(%rbp), %rdi
callq 0x48c0
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)