// File: /workspace/github_c_makefile_star2_O0/DavidSM64[P]n64graphics_ci/n64graphics_ci.c
// Function: rawci2rgba
rgba *rawci2rgba(const uint8_t *rawci, const uint8_t *palette, int width, int height, int depth)
{
    uint8_t *raw_rgba;
    rgba *img = NULL;
    int raw_size;
    int pal_index_1;
    int pal_index_2;

    // first convert to raw RGBA
    raw_size = 2 * width * height;
    raw_rgba = malloc(raw_size);
    if (!raw_rgba) {
        ERROR("Error allocating %u bytes\n", raw_size);
        return NULL;
    }

    if (depth == 4) { // CI4
        for (int i = 0; i < (width * height) / 2; i++) {
            pal_index_1 = rawci[i] >> 4;
            pal_index_2 = rawci[i] & 0xF;
            raw_rgba[i * 4 + 0] = palette[pal_index_1 * 2 + 0];
            raw_rgba[i * 4 + 1] = palette[pal_index_1 * 2 + 1];
            raw_rgba[i * 4 + 2] = palette[pal_index_2 * 2 + 0];
            raw_rgba[i * 4 + 3] = palette[pal_index_2 * 2 + 1];
        }
    } else { // CI8
        for (int i = 0; i < width * height; i++) {
            pal_index_1 = rawci[i];
            raw_rgba[i * 2 + 0] = palette[pal_index_1 * 2 + 0];
            raw_rgba[i * 2 + 1] = palette[pal_index_1 * 2 + 1];
        }
    }

    // then convert to RGBA image data
    img = raw2rgba(raw_rgba, width, height, 16);

    free(raw_rgba);

    return img;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq $0x0, -0x38(%rbp)
movl -0x1c(%rbp), %eax
shll %eax
imull -0x20(%rbp), %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%rbp), %rdi
callq 0x11d0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x97ca
movq 0x1e847(%rip), %rax     # 0x27ff0
movq (%rax), %rdi
movl -0x3c(%rbp), %edx
leaq 0x196f0(%rip), %rsi     # 0x22ea6
movb $0x0, %al
callq 0x1170
movq $0x0, -0x8(%rbp)
jmp 0x9967
cmpl $0x4, -0x24(%rbp)
jne 0x98c5
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x1c(%rbp), %eax
imull -0x20(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x50(%rbp), %eax
cmpl %ecx, %eax
jge 0x98c3
movq -0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movzbl (%rax,%rcx), %eax
sarl $0x4, %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0xf, %eax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
shll %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x30(%rbp), %rax
movl -0x48(%rbp), %ecx
shll $0x2, %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x30(%rbp), %rax
movl -0x48(%rbp), %ecx
shll $0x2, %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
movl -0x44(%rbp), %ecx
shll %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x30(%rbp), %rax
movl -0x48(%rbp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
movl -0x44(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x30(%rbp), %rax
movl -0x48(%rbp), %ecx
shll $0x2, %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x97db
jmp 0x993e
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl -0x1c(%rbp), %ecx
imull -0x20(%rbp), %ecx
cmpl %ecx, %eax
jge 0x993c
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
shll %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x30(%rbp), %rax
movl -0x4c(%rbp), %ecx
shll %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq -0x30(%rbp), %rax
movl -0x4c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x98cc
jmp 0x993e
movq -0x30(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x20(%rbp), %edx
movl $0x10, %ecx
callq 0x9520
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1040
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)