// File: /workspace/github_c_makefile_star2_O1/DavidSM64[P]n64graphics_ci/n64graphics_ci.c
// Function: main
int main(int argc, char* argv[])
{
    graphics_config config = default_config;
    rgba *imgr;
    ia   *imgi;
    FILE *fp;
    uint8_t *raw;
    int raw_size;
    int length = 0;
    int flength;
    int res;
    FILE *fp_pal; // for ci palette
    uint8_t *pal;
    int pal_len;
    char* pal_bin_filename;

    int valid = parse_arguments(argc, argv, &config);
    if (!valid || !config.bin_filename || !config.bin_filename || !config.img_filename) {
        print_usage();
        exit(EXIT_FAILURE);
    }

    if (config.mode == MODE_IMPORT) {
        printf("%s\n", config.bin_filename);
        if (config.truncate) {
            fp = fopen(config.bin_filename, "w");
        }
        else {
            fp = fopen(config.bin_filename, "r+");
        }
        if (!fp) {
            ERROR("Error opening binary file \"%s\"\n", config.bin_filename);
            return -1;
        }
        if (!config.truncate) {
            fseek(fp, config.offset, SEEK_SET);
        }
        switch (config.format) {
        case IMG_FORMAT_CI:
            imgr = png2rgba(config.img_filename, &config.width, &config.height);
            raw_size = config.width * config.height * config.depth / 8;
            raw = malloc(raw_size);
            if (!raw) {
                ERROR("Error allocating %u bytes\n", raw_size);
            }
            if (config.depth == 4) {
                pal_len = 16 * 2; // CI4
            }
            else {
                pal_len = 256 * 2; // CI8
            }
            pal = malloc(pal_len);
            if (!pal) {
                ERROR("Error allocating %u bytes for palette\n", pal_len);
            }
            length = rgba2rawci(raw, pal, &pal_len, imgr, config.width, config.height, config.depth);
            //length = rgba2raw(raw, imgr, config.width, config.height, config.depth);
            break;
        }
        if (length <= 0) {
            ERROR("Error converting to raw format\n");
            return EXIT_FAILURE;
        }
        INFO("Writing 0x%X bytes to offset 0x%X of \"%s\"\n", length, config.offset, config.bin_filename);
        flength = fwrite(raw, 1, length, fp);
        if (flength != length) {
            ERROR("Error writing %d bytes to \"%s\"\n", length, config.bin_filename);
        }
        fclose(fp);

        if (config.format == IMG_FORMAT_CI) {
            pal_bin_filename = getPaletteFilename(config.bin_filename);

            fp_pal = fopen(pal_bin_filename, "w");
            INFO("Writing 0x%X bytes to palette file \"%s\"\n", pal_len, pal_bin_filename);
            flength = fwrite(pal, 1, pal_len, fp_pal);
            if (flength != pal_len) {
                ERROR("Error writing %d bytes to \"%s\"\n", pal_len, pal_bin_filename);
            }
            fclose(fp_pal);
            free(pal_bin_filename);
        }
    } else { // Export
        if (config.width <= 0 || config.height <= 0 || config.depth <= 0) {
            ERROR("Error: must set position width and height for export\n");
            return EXIT_FAILURE;
        }
        fp = fopen(config.bin_filename, "r");
        if (!fp) {
            ERROR("Error opening \"%s\"\n", config.bin_filename);
            return -1;
        }
        raw_size = config.width * config.height * config.depth / 8;
        raw = malloc(raw_size);
        if (config.offset > 0) {
            fseek(fp, config.offset, SEEK_SET);
        }
        flength = fread(raw, 1, raw_size, fp);
        if (flength != raw_size) {
            ERROR("Error reading %d bytes from \"%s\"\n", raw_size, config.bin_filename);
        }
        switch (config.format) {
        case IMG_FORMAT_CI:
            // Read Palette file
            pal_bin_filename = getPaletteFilename(config.bin_filename);
            fp_pal = fopen(pal_bin_filename, "r");
            if (!fp_pal) {
                ERROR("Error opening \"%s\"\n", pal_bin_filename);
                return -1;
            }
            if (config.depth == 4) {
                pal_len = 16 * 2; // CI4
            }
            else {
                pal_len = 256 * 2; // CI8
            }

            pal = malloc(pal_len);
            if (config.offset > 0) {
                fseek(fp, config.offset, SEEK_SET);
            }
            flength = fread(pal, 1, pal_len, fp_pal);

            imgr = rawci2rgba(raw, pal, config.width, config.height, config.depth);
            res = rgba2png(config.img_filename, imgr, config.width, config.height);

            free(pal_bin_filename);
            break;
        default:
            return EXIT_FAILURE;
        }
        if (!res) {
            ERROR("Error writing to \"%s\"\n", config.img_filename);
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x80(%rbp)
movl $0x8, -0x64(%rbp)
movabsq $0x2000000020, %rax     # imm = 0x2000000020
movq %rax, -0x60(%rbp)
movl $0x1, -0x58(%rbp)
cmpl $0x2, %edi
setl %r15b
jl 0x78e0
movl -0x60(%rbp), %eax
movq %rax, -0x98(%rbp)
movl -0x6c(%rbp), %eax
movq %rax, -0x90(%rbp)
movl -0x58(%rbp), %eax
movq %rax, -0x48(%rbp)
movl -0x5c(%rbp), %eax
movq %rax, -0xa0(%rbp)
movq -0x80(%rbp), %r14
movq -0x78(%rbp), %r12
movl -0x70(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x84(%rbp)
movq 0x8(%rsi), %rax
cmpb $0x2d, (%rax)
jne 0x7898
movl %edi, %r13d
movl $0x1, %ebx
xorl %r15d, %r15d
leaq 0xbbb4(%rip), %rdx      # 0x13268
movq %rsi, -0x30(%rbp)
movzbl 0x1(%rax), %eax
leal -0x65(%rax), %ecx
cmpl $0x12, %ecx
ja 0x7d72
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
incl %ebx
cmpl %r13d, %ebx
jge 0x789b
movslq %ebx, %rax
movq (%rsi,%rax,8), %r12
movl $0x0, -0x38(%rbp)
jmp 0x787b
incl %ebx
cmpl %r13d, %ebx
jge 0x789b
movslq %ebx, %rax
movq (%rsi,%rax,8), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1240
leaq 0xbb57(%rip), %rdx      # 0x13268
movq -0x30(%rbp), %rsi
movq %rax, -0x98(%rbp)
jmp 0x787b
incl %ebx
cmpl %r13d, %ebx
jge 0x789b
movslq %ebx, %rax
movq (%rsi,%rax,8), %r12
movl $0x1, -0x38(%rbp)
jmp 0x787b
incl %ebx
cmpl %r13d, %ebx
jge 0x789b
movslq %ebx, %rax
movq (%rsi,%rax,8), %rdi
movq $0x0, -0x48(%rbp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x1240
leaq 0xbaff(%rip), %rdx      # 0x13268
movq -0x30(%rbp), %rsi
movq %rax, -0x90(%rbp)
jmp 0x787b
incl %ebx
cmpl %r13d, %ebx
jge 0x789b
movslq %ebx, %rax
movq (%rsi,%rax,8), %r14
jmp 0x787b
incl %ebx
cmpl %r13d, %ebx
jge 0x789b
movslq %ebx, %rax
movq (%rsi,%rax,8), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1240
leaq 0xbab6(%rip), %rdx      # 0x13268
movq -0x30(%rbp), %rsi
movq %rax, -0xa0(%rbp)
jmp 0x787b
incl %ebx
cmpl %r13d, %ebx
jge 0x789b
movq %r12, -0xb0(%rbp)
movq %r14, -0xa8(%rbp)
movslq %ebx, %rax
movq (%rsi,%rax,8), %r14
movq %r14, %rdi
leaq 0xe9fa(%rip), %rsi      # 0x161e6
callq 0x1050
testl %eax, %eax
je 0x782c
movb $0x1, %cl
movl %ecx, %r12d
testb $0x1, %cl
je 0x785c
movq %r14, %rdi
leaq 0xe9e1(%rip), %rsi      # 0x161ea
callq 0x1050
xorl %ecx, %ecx
testl %eax, %eax
jne 0x77f7
leaq 0x125a5(%rip), %rax     # 0x19dc0
jmp 0x7836
leaq 0x129b4(%rip), %rax     # 0x1a1d8
movl $0x1, (%rax)
jmp 0x787b
movb $0x1, %r12b
leaq 0x1257a(%rip), %rax     # 0x19db0
movq -0xa8(%rbp), %r14
movq -0x30(%rbp), %rsi
leaq 0xba20(%rip), %rdx      # 0x13268
movl 0x8(%rax), %ecx
movl %ecx, -0x88(%rbp)
movl 0xc(%rax), %eax
movl %eax, -0x84(%rbp)
jmp 0x786e
movq -0xa8(%rbp), %r14
movq -0x30(%rbp), %rsi
leaq 0xb9fa(%rip), %rdx      # 0x13268
testb $0x1, %r12b
movq -0xb0(%rbp), %r12
je 0x789b
incl %ebx
cmpl %r13d, %ebx
setge %r15b
jge 0x789b
movslq %ebx, %rax
movq (%rsi,%rax,8), %rax
cmpb $0x2d, (%rax)
je 0x76b8
jmp 0x789b
xorl %r15d, %r15d
movq -0x98(%rbp), %rax
movl %eax, -0x60(%rbp)
movq -0x90(%rbp), %rax
movl %eax, -0x6c(%rbp)
movq -0x48(%rbp), %rax
movl %eax, -0x58(%rbp)
movq -0xa0(%rbp), %rax
movl %eax, -0x5c(%rbp)
movq %r14, -0x80(%rbp)
movq %r12, -0x78(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x64(%rbp)
testb $0x1, %r15b
je 0x7dcc
movq -0x78(%rbp), %r12
testq %r12, %r12
je 0x7dcc
movq -0x80(%rbp), %r15
testq %r15, %r15
je 0x7dcc
cmpl $0x1, -0x70(%rbp)
jne 0x7960
movq %r12, %rdi
callq 0x1090
movl -0x58(%rbp), %ebx
testl %ebx, %ebx
leaq 0xdc94(%rip), %rax      # 0x155b2
leaq 0xdc8b(%rip), %rsi      # 0x155b0
cmoveq %rax, %rsi
movq %r12, %rdi
callq 0x1220
testq %rax, %rax
je 0x7c52
movq %rax, %r14
testl %ebx, %ebx
jne 0x794e
movl -0x6c(%rbp), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x11e0
cmpl $0x0, -0x68(%rbp)
je 0x7aeb
xorl %r12d, %r12d
jmp 0x7b86
movl -0x60(%rbp), %eax
movl %eax, -0x38(%rbp)
testl %eax, %eax
jle 0x7c65
movl -0x5c(%rbp), %ebx
testl %ebx, %ebx
jle 0x7c65
movl -0x64(%rbp), %r13d
testl %r13d, %r13d
jle 0x7c65
leaq 0xde71(%rip), %rsi      # 0x157fe
movq %r12, %rdi
callq 0x1220
testq %rax, %rax
je 0x7c6c
movq %rax, %r14
movq %r15, -0x48(%rbp)
movl %ebx, -0xa0(%rbp)
imull -0x38(%rbp), %ebx
imull %r13d, %ebx
shrl $0x3, %ebx
movq %rbx, %rdi
callq 0x11c0
movq %rax, -0x30(%rbp)
movl -0x6c(%rbp), %r15d
testq %r15, %r15
je 0x79d8
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x11e0
movl $0x1, %esi
movq -0x30(%rbp), %rdi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x10a0
cmpl %eax, %ebx
jne 0x7cac
movl $0x1, %ebx
cmpl $0x0, -0x68(%rbp)
jne 0x7c98
movq %r12, %rdi
callq 0x7598
movq %rax, %rbx
leaq 0xdde9(%rip), %rsi      # 0x157fe
movq %rax, %rdi
callq 0x1220
testq %rax, %rax
je 0x7d36
movq %rax, -0x90(%rbp)
movq %rbx, -0x98(%rbp)
cmpl $0x4, %r13d
movl $0x20, %eax
movl $0x200, %r12d           # imm = 0x200
cmovel %eax, %r12d
movl %r12d, -0x3c(%rbp)
movq %r12, %rdi
callq 0x11c0
movq %rax, %rbx
testq %r15, %r15
je 0x7a68
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x11e0
movl %r12d, %edx
movl $0x1, %esi
movq %rbx, %rdi
movq -0x90(%rbp), %rcx
callq 0x10a0
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movl -0x38(%rbp), %ebx
movl %ebx, %edx
movl -0xa0(%rbp), %r14d
movl %r14d, %ecx
movl %r13d, %r8d
callq 0x6f91
movq -0x48(%rbp), %rdi
movq %rax, %rsi
movl %ebx, %edx
movl %r14d, %ecx
callq 0x746e
movl %eax, %r14d
movq -0x98(%rbp), %rdi
callq 0x1040
xorl %ebx, %ebx
testl %r14d, %r14d
jne 0x7c98
movq 0x12521(%rip), %rax     # 0x19ff0
movq (%rax), %rdi
leaq 0xdc23(%rip), %rsi      # 0x156fc
xorl %ebx, %ebx
movq -0x48(%rbp), %rdx
xorl %eax, %eax
callq 0x1160
jmp 0x7c98
leaq -0x60(%rbp), %rsi
leaq -0x5c(%rbp), %rbx
movq %r15, %rdi
movq %rbx, %rdx
callq 0x723e
movq %rax, -0x30(%rbp)
movl -0x8(%rbx), %r12d
movl -0x4(%rbx), %r13d
movl (%rbx), %eax
movl %eax, -0x48(%rbp)
imull %r13d, %eax
imull %r12d, %eax
leal 0x7(%rax), %r15d
testl %eax, %eax
cmovnsl %eax, %r15d
sarl $0x3, %r15d
movslq %r15d, %rdi
callq 0x11c0
movq %rax, %rbx
testq %rax, %rax
je 0x7cf2
cmpl $0x4, %r12d
movl $0x20, %eax
movl $0x200, %r15d           # imm = 0x200
cmovel %eax, %r15d
movl %r15d, -0x3c(%rbp)
movq %r15, %rdi
callq 0x11c0
testq %rax, %rax
movq %rax, -0x38(%rbp)
je 0x7d12
movl %r12d, (%rsp)
leaq -0x3c(%rbp), %rdx
movq %rbx, %rdi
movq %rax, %rsi
movq -0x30(%rbp), %rcx
movl %r13d, %r8d
movl -0x48(%rbp), %r9d
callq 0x709a
movl %eax, %r12d
testl %r12d, %r12d
jle 0x7c8e
leaq 0x12642(%rip), %r15     # 0x1a1d8
cmpl $0x0, (%r15)
je 0x7bb4
movl -0x6c(%rbp), %edx
movq -0x78(%rbp), %rcx
leaq 0xda72(%rip), %rdi      # 0x1561c
movl %r12d, %esi
xorl %eax, %eax
callq 0x10e0
movl %r12d, %edx
movl $0x1, %esi
movq %rbx, %rdi
movq %r14, %rcx
callq 0x1270
cmpl %eax, %r12d
jne 0x7cce
movq %r14, %rdi
callq 0x10b0
xorl %ebx, %ebx
cmpl $0x0, -0x68(%rbp)
jne 0x7c98
movq -0x78(%rbp), %rdi
callq 0x7598
movq %rax, %r14
leaq 0xd9b9(%rip), %rsi      # 0x155b0
movq %rax, %rdi
callq 0x1220
movq %rax, %r12
cmpl $0x0, (%r15)
je 0x7c1c
movl -0x3c(%rbp), %esi
leaq 0xda55(%rip), %rdi      # 0x15667
movq %r14, %rdx
xorl %eax, %eax
callq 0x10e0
movl -0x3c(%rbp), %r13d
movslq %r13d, %r15
movl $0x1, %esi
movq -0x38(%rbp), %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x1270
cmpl %eax, %r15d
jne 0x7d4f
movq %r12, %rdi
callq 0x10b0
movq %r14, %rdi
callq 0x1040
jmp 0x7c98
movq 0x12397(%rip), %rax     # 0x19ff0
movq (%rax), %rdi
leaq 0xd952(%rip), %rsi      # 0x155b5
jmp 0x7c7d
callq 0x1384
jmp 0x7c93
movq 0x1237d(%rip), %rax     # 0x19ff0
movq (%rax), %rdi
leaq 0xda49(%rip), %rsi      # 0x156c6
movq %r12, %rdx
xorl %eax, %eax
callq 0x1160
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x7c98
callq 0x13b3
movl $0x1, %ebx
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1233d(%rip), %rax     # 0x19ff0
movq (%rax), %rdi
leaq 0xda1d(%rip), %rsi      # 0x156da
movl %ebx, %edx
movq %r12, %rcx
xorl %eax, %eax
callq 0x1160
jmp 0x79f4
movq 0x1231b(%rip), %rax     # 0x19ff0
movq (%rax), %rdi
movq -0x78(%rbp), %rcx
leaq 0xd964(%rip), %rsi      # 0x15647
movl %r12d, %edx
xorl %eax, %eax
callq 0x1160
jmp 0x7bd0
movq 0x122f7(%rip), %rax     # 0x19ff0
movq (%rax), %rdi
leaq 0xd79e(%rip), %rsi      # 0x154a1
movl %r15d, %edx
xorl %eax, %eax
callq 0x1160
jmp 0x7b39
movq 0x122d7(%rip), %rax     # 0x19ff0
movq (%rax), %rdi
leaq 0xd8b2(%rip), %rsi      # 0x155d5
movl %r15d, %edx
xorl %eax, %eax
callq 0x1160
movq -0x38(%rbp), %rax
jmp 0x7b65
movq 0x122b3(%rip), %rax     # 0x19ff0
movq (%rax), %rdi
leaq 0xd97f(%rip), %rsi      # 0x156c6
movq %rbx, %rdx
jmp 0x7c80
movq 0x1229a(%rip), %rax     # 0x19ff0
movq (%rax), %rdi
leaq 0xd8e7(%rip), %rsi      # 0x15647
movl %r13d, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x1160
jmp 0x7c40
cmpl $0x56, %eax
jne 0x789b
movq -0x98(%rbp), %rax
movl %eax, -0x60(%rbp)
movq -0x90(%rbp), %rax
movl %eax, -0x6c(%rbp)
movq -0x48(%rbp), %rax
movl %eax, -0x58(%rbp)
movq -0xa0(%rbp), %rax
movl %eax, -0x5c(%rbp)
movq %r14, -0x80(%rbp)
movq %r12, -0x78(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x64(%rbp)
callq 0x132a
xorl %edi, %edi
callq 0x1260
callq 0x12d7
movl $0x1, %edi
callq 0x1260