// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movq %rsi, -0x30(%rbp)
movq $0x0, -0x78(%rbp)
movl %edi, -0x38(%rbp)
cmpl $0x2, %edi
jl 0x7d67
movl $0x1, -0x44(%rbp)
movl $0x20, %eax
movq %rax, -0x60(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x8, %r13d
leaq 0xabc1(%rip), %r8       # 0x12268
leaq 0x11702(%rip), %r15     # 0x18db0
xorl %r14d, %r14d
xorl %ebx, %ebx
movq $0x0, -0x58(%rbp)
movl $0x20, %eax
movq %rax, -0x68(%rbp)
movl $0x1, %r12d
movq -0x30(%rbp), %rsi
movl -0x38(%rbp), %edi
movslq %r12d, %rax
movq (%rsi,%rax,8), %rax
cmpb $0x2d, (%rax)
jne 0x7884
movzbl 0x1(%rax), %ecx
xorl %eax, %eax
leal -0x65(%rcx), %edx
cmpl $0x12, %edx
ja 0x7d2f
movslq (%r8,%rdx,4), %rcx
addq %r8, %rcx
jmpq *%rcx
incl %r12d
xorl %eax, %eax
cmpl %edi, %r12d
jge 0x7886
movslq %r12d, %rax
movq (%rsi,%rax,8), %r14
movq $0x0, -0x40(%rbp)
jmp 0x7874
incl %r12d
cmpl %edi, %r12d
jge 0x7884
movslq %r12d, %rax
movq (%rsi,%rax,8), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1250
leaq 0xab27(%rip), %r8       # 0x12268
movl -0x38(%rbp), %edi
movq -0x30(%rbp), %rsi
movq %rax, -0x68(%rbp)
jmp 0x7874
incl %r12d
cmpl %edi, %r12d
jge 0x7884
movslq %r12d, %rax
movq (%rsi,%rax,8), %r14
movl $0x1, %eax
movq %rax, -0x40(%rbp)
jmp 0x7874
incl %r12d
xorl %eax, %eax
cmpl %edi, %r12d
jge 0x7886
movslq %r12d, %rax
movq (%rsi,%rax,8), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1250
leaq 0xaad1(%rip), %r8       # 0x12268
movl -0x38(%rbp), %edi
movq -0x30(%rbp), %rsi
movq %rax, -0x58(%rbp)
movl $0x0, -0x44(%rbp)
jmp 0x7874
incl %r12d
cmpl %edi, %r12d
jge 0x7884
movslq %r12d, %rax
movq (%rsi,%rax,8), %rbx
jmp 0x7874
incl %r12d
cmpl %edi, %r12d
jge 0x7884
movslq %r12d, %rax
movq (%rsi,%rax,8), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1250
leaq 0xaa7f(%rip), %r8       # 0x12268
movl -0x38(%rbp), %edi
movq -0x30(%rbp), %rsi
movq %rax, -0x60(%rbp)
jmp 0x7874
incl %r12d
cmpl %edi, %r12d
jge 0x7884
movq %r14, -0xa8(%rbp)
movq %rbx, -0x50(%rbp)
movslq %r12d, %rax
movq (%rsi,%rax,8), %rbx
movb $0x1, %r13b
xorl %r14d, %r14d
shlq $0x4, %r14
movq (%r14,%r15), %rsi
movq %rbx, %rdi
callq 0x1050
testl %eax, %eax
je 0x7854
movl $0x1, %r14d
testb $0x1, %r13b
movl $0x0, %r13d
jne 0x781a
jmp 0x7d67
leaq 0x1197c(%rip), %rax     # 0x191c8
movl $0x1, (%rax)
jmp 0x7874
addq %r15, %r14
movl 0xc(%r14), %r13d
movq -0x50(%rbp), %rbx
movq -0xa8(%rbp), %r14
movq -0x30(%rbp), %rsi
movl -0x38(%rbp), %edi
leaq 0xa9f4(%rip), %r8       # 0x12268
incl %r12d
cmpl %edi, %r12d
jl 0x76d1
movb $0x1, %al
jmp 0x7886
xorl %eax, %eax
movq -0x68(%rbp), %rcx
movl %ecx, -0x80(%rbp)
movq -0x58(%rbp), %rcx
movl %ecx, -0x8c(%rbp)
movl -0x44(%rbp), %ecx
movl %ecx, -0x78(%rbp)
movq -0x60(%rbp), %rcx
movl %ecx, -0x7c(%rbp)
movq %rbx, -0x50(%rbp)
movq %rbx, -0xa0(%rbp)
movq %r14, -0x98(%rbp)
movq -0x40(%rbp), %rcx
movl %ecx, -0x90(%rbp)
movl $0x0, -0x88(%rbp)
movl %r13d, -0x84(%rbp)
testb %al, %al
je 0x7d67
movq %r14, %r15
testq %r14, %r14
je 0x7d67
cmpq $0x0, -0x50(%rbp)
je 0x7d67
cmpl $0x1, -0x40(%rbp)
jne 0x792f
movq %r15, %r14
movq %r15, %rdi
callq 0x1090
cmpl $0x0, -0x44(%rbp)
je 0x7a99
leaq 0xcc9a(%rip), %rsi      # 0x145ac
movq %r14, %rdi
callq 0x1230
movq %rax, %r15
testq %rax, %rax
movq -0x50(%rbp), %rbx
jne 0x7ac5
jmp 0x7c53
cmpl $0x0, -0x68(%rbp)
jle 0x7c75
cmpl $0x0, -0x60(%rbp)
jle 0x7c75
testl %r13d, %r13d
jle 0x7c75
leaq 0xcea7(%rip), %rsi      # 0x147fa
movq %r15, %rdi
callq 0x1230
testq %rax, %rax
je 0x7c3d
movq %rax, %r14
movq -0x60(%rbp), %rbx
imull -0x68(%rbp), %ebx
movl %r13d, -0x30(%rbp)
imull %r13d, %ebx
shrl $0x3, %ebx
movq %rbx, %rdi
callq 0x11d0
movq %rax, %r12
movq -0x58(%rbp), %rax
movl %eax, %r13d
testl %eax, %eax
je 0x799d
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x11f0
movq %r13, -0x40(%rbp)
movl $0x1, %esi
movq %r12, %rdi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x10a0
cmpl %eax, %ebx
jne 0x7ce6
movq %r15, %rdi
callq 0x75ea
movq %rax, %r15
leaq 0xce2c(%rip), %rsi      # 0x147fa
movq %rax, %rdi
callq 0x1230
movq %rax, %rbx
testq %rax, %rax
movl -0x30(%rbp), %eax
je 0x7c3d
movq %r15, -0x38(%rbp)
movq %r12, %r15
cmpl $0x4, %eax
movl $0x20, %eax
movl $0x200, %r12d           # imm = 0x200
cmoveq %rax, %r12
movq %r12, %rdi
callq 0x11d0
movq %rax, %r13
cmpl $0x0, -0x58(%rbp)
je 0x7a1d
movq %r14, %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x11f0
movl $0x1, %esi
movq %r13, %rdi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x10a0
movq %r15, %rdi
movq %r13, %rsi
movq -0x68(%rbp), %r14
movl %r14d, %edx
movq -0x60(%rbp), %rbx
movl %ebx, %ecx
movl -0x30(%rbp), %r8d
callq 0x7005
movq -0x50(%rbp), %r12
movq %r12, %rdi
movq %rax, %rsi
movl %r14d, %edx
movl %ebx, %ecx
callq 0x74c0
movl %eax, %r14d
movq -0x38(%rbp), %rdi
callq 0x1040
xorl %ebx, %ebx
testl %r14d, %r14d
jne 0x7cd2
movq 0x11572(%rip), %rax     # 0x18ff0
movq (%rax), %rdi
leaq 0xcc70(%rip), %rsi      # 0x146f8
xorl %ebx, %ebx
movq %r12, %rdx
xorl %eax, %eax
callq 0x1170
jmp 0x7cd2
leaq 0xcb0e(%rip), %rsi      # 0x145ae
movq %r14, %rdi
callq 0x1230
testq %rax, %rax
movq -0x50(%rbp), %rbx
je 0x7c53
movq %rax, %r15
movl -0x58(%rbp), %esi
movq %rax, %rdi
xorl %edx, %edx
callq 0x11f0
movq %r15, -0x38(%rbp)
movq %rbx, %rdi
leaq -0x80(%rbp), %rsi
leaq -0x7c(%rbp), %rdx
callq 0x72b2
movq %rax, -0x30(%rbp)
movl -0x84(%rbp), %ebx
movl -0x80(%rbp), %r15d
movl -0x7c(%rbp), %r13d
movl %r13d, %eax
imull %r15d, %eax
imull %ebx, %eax
leal 0x7(%rax), %r14d
testl %eax, %eax
cmovnsl %eax, %r14d
sarl $0x3, %r14d
movslq %r14d, %rdi
callq 0x11d0
movq %rax, %r12
testq %rax, %rax
je 0x7c7c
cmpl $0x4, %ebx
movl $0x20, %eax
movl $0x200, %r14d           # imm = 0x200
cmovel %eax, %r14d
movl %r14d, -0x6c(%rbp)
movq %r14, %rdi
callq 0x11d0
testq %rax, %rax
movq %rax, -0x40(%rbp)
je 0x7c9c
movl %ebx, (%rsp)
leaq -0x6c(%rbp), %rdx
movq %r12, %rdi
movq %rax, %rsi
movq -0x30(%rbp), %rcx
movl %r15d, %r8d
movl %r13d, %r9d
callq 0x710e
movl %eax, %r15d
testl %eax, %eax
jle 0x7cc8
leaq 0x11658(%rip), %rax     # 0x191c8
cmpl $0x0, (%rax)
je 0x7b93
movl -0x8c(%rbp), %edx
movq -0x98(%rbp), %rcx
leaq 0xca8f(%rip), %rdi      # 0x14618
movl %r15d, %esi
xorl %eax, %eax
callq 0x10e0
movl %r15d, %edx
movl $0x1, %esi
movq %r12, %rdi
movq -0x38(%rbp), %rbx
movq %rbx, %rcx
callq 0x1280
cmpl %eax, %r15d
jne 0x7d08
movq %rbx, %rdi
callq 0x10b0
xorl %ebx, %ebx
cmpl $0x0, -0x88(%rbp)
jne 0x7cd2
movq -0x98(%rbp), %rdi
callq 0x75ea
movq %rax, %r15
leaq 0xc9cc(%rip), %rsi      # 0x145ac
movq %rax, %rdi
callq 0x1230
movq %rax, %r12
leaq 0x115d6(%rip), %rax     # 0x191c8
cmpl $0x0, (%rax)
je 0x7c0b
leaq 0xca65(%rip), %rdi      # 0x14663
movl %r14d, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x10e0
movl %r14d, %edx
movl $0x1, %esi
movq -0x40(%rbp), %rdi
movq %r12, %rcx
callq 0x1280
cmpl %eax, %r14d
jne 0x7d44
movq %r12, %rdi
callq 0x10b0
movq %r15, %rdi
callq 0x1040
jmp 0x7cd2
movq 0x113ac(%rip), %rax     # 0x18ff0
movq (%rax), %rdi
leaq 0xca74(%rip), %rsi      # 0x146c2
movq %r15, %rdx
jmp 0x7c67
movq 0x11396(%rip), %rax     # 0x18ff0
movq (%rax), %rdi
leaq 0xc94d(%rip), %rsi      # 0x145b1
movq %r14, %rdx
xorl %eax, %eax
callq 0x1170
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x7cd2
callq 0x136d
jmp 0x7ccd
movq 0x1136d(%rip), %rax     # 0x18ff0
movq (%rax), %rdi
leaq 0xc810(%rip), %rsi      # 0x1449d
movl %r14d, %edx
xorl %eax, %eax
callq 0x1170
jmp 0x7b17
movq 0x1134d(%rip), %rax     # 0x18ff0
movq (%rax), %rdi
leaq 0xc924(%rip), %rsi      # 0x145d1
movl %r14d, %edx
xorl %eax, %eax
callq 0x1170
movl %ebx, (%rsp)
leaq -0x6c(%rbp), %rdx
movq %r12, %rdi
xorl %esi, %esi
jmp 0x7b4f
callq 0x139c
movl $0x1, %ebx
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x11303(%rip), %rax     # 0x18ff0
movq (%rax), %rdi
leaq 0xc9df(%rip), %rsi      # 0x146d6
movl %ebx, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x1170
jmp 0x79bc
movq 0x112e1(%rip), %rax     # 0x18ff0
movq (%rax), %rdi
movq -0x98(%rbp), %rcx
leaq 0xc923(%rip), %rsi      # 0x14643
movl %r15d, %edx
xorl %eax, %eax
callq 0x1170
jmp 0x7bb3
cmpl $0x56, %ecx
jne 0x7886
callq 0x1313
xorl %edi, %edi
callq 0x1270
movq 0x112a5(%rip), %rax     # 0x18ff0
movq (%rax), %rdi
leaq 0xc8ee(%rip), %rsi      # 0x14643
movl %r14d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x1170
jmp 0x7c28
callq 0x12e7
movl $0x1, %edi
callq 0x1270