// File: /workspace/github_cpp_makefile_star2_O1/graphitemaster[P]smbf/smbf.cpp
// Function: main
int main(int argc, char **argv) {
    --argc;
    ++argv;
    bool tangents = false;
    bool bitangents = false;
    const char *file = nullptr;

    for (int i = 0; i < argc; i++) {
        if (!strcmp(argv[i], "-t"))
            tangents = true;
        else if (!strcmp(argv[i], "-b"))
            bitangents = true;
        else
            file = argv[i];
    }

    if (!file) {
        fprintf(stderr, "expected OBJ file name\n");
        return 1;
    }

    int before = 0;
    int after = 0;
    obj o;
    if (!o.load(file, before, after)) {
        fprintf(stderr, "failed to load OBJ model `%s'\n", file);
        return 1;
    }

    int format = SMBF_FORMAT_P;
    if (o.m_normals.size())
        format = SMBF_FORMAT_PN;
    if (o.m_coordinates.size())
        format = SMBF_FORMAT_PNC;
    if (o.m_tangents.size() && tangents)
        format = SMBF_FORMAT_PNCT;
    if (o.m_bitangents.size() && bitangents)
        format = SMBF_FORMAT_PNCTB;


    const char *formats[] = {
        "P", "PN", "PNC", "PNCT", "PNCTB"
    };

    header h;
    h.magic[0] = 'S';
    h.magic[1] = 'M';
    h.magic[2] = 'B';
    h.magic[3] = 'F';
    h.version = SMBF_VERSION;
    h.format = format;
    h.count = o.m_positions.size();
    h.indices = o.m_indices.size();

    // Interleave the data
    union data {
        smbfP asP;
        smbfPN asPN;
        smbfPNC asPNC;
        smbfPNCT asPNCT;
        smbfPNCTB asPNCTB;
    };
    u::vector<data> inter;
    inter.resize(h.count);
    for (size_t i = 0; i < h.count; i++) {
        data &x = inter[i];
        smbfPNCTB &s = x.asPNCTB;
        s.px = o.m_positions[i].x;
        s.py = o.m_positions[i].y;
        s.pz = o.m_positions[i].z;
        if (o.m_normals.size()) {
            s.nx = o.m_normals[i].x;
            s.ny = o.m_normals[i].y;
            s.nz = o.m_normals[i].z;
        }
        if (o.m_coordinates.size()) {
            s.tu = o.m_coordinates[i].x;
            s.tv = o.m_coordinates[i].y;
        }
        s.tx = o.m_tangents[i].x;
        s.ty = o.m_tangents[i].y;
        s.tz = o.m_tangents[i].z;
        s.b = o.m_bitangents[i];
    }

    u::string name(file, strrchr(file, '.'));
    name += ".smbf";
    u::file write = u::fopen(name, "w");
    if (!write) {
        fprintf(stderr, "failed to open `%s' for writing\n", name.c_str());
        return 1;
    }

    fwrite(&h, sizeof(header), 1, write);
    for (auto &it : inter) {
        switch (format) {
        case SMBF_FORMAT_P:
            fwrite(&it.asP, sizeof(smbfP), 1, write);
            break;
        case SMBF_FORMAT_PN:
            fwrite(&it.asPN, sizeof(smbfPN), 1, write);
            break;
        case SMBF_FORMAT_PNC:
            fwrite(&it.asPNC, sizeof(smbfPNC), 1, write);
            break;
        case SMBF_FORMAT_PNCT:
            fwrite(&it.asPNCT, sizeof(smbfPNCT), 1, write);
            break;
        case SMBF_FORMAT_PNCTB:
            fwrite(&it.asPNCTB, sizeof(smbfPNCTB), 1, write);
            break;
        }
    }

    for (auto &it : o.m_indices) {
        uint32_t convert = it;
        fwrite(&convert, sizeof(uint32_t), 1, write);
    }

    printf("Wrote `%s':\n", name.c_str());
    printf(" Indices:\n");
    printf("  Count: %i\n", int(h.indices));
    printf("  Cache misses:\n");
    printf("   Before: %i\n", before);
    printf("   After:  %i\n", after);
    printf(" Vertices:\n");
    printf("  Count:  %i\n", int(h.count));
    printf("  Format: %s\n", formats[format]);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
cmpl $0x2, %edi
jl 0x49b1
movq %rsi, %rbx
movl %edi, %r14d
decl %r14d
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x8(%rbx,%r15,8), %rbp
movq %rbp, %rdi
leaq 0x27fd(%rip), %rsi      # 0x7168
callq 0x2190
testl %eax, %eax
je 0x4998
movq %rbp, %rdi
leaq 0x27ed(%rip), %rsi      # 0x716b
callq 0x2190
testl %eax, %eax
cmovneq %rbp, %r12
movzbl %r13b, %r13d
movl $0x1, %eax
cmovel %eax, %r13d
jmp 0x499f
movb $0x1, %al
movq %rax, 0x10(%rsp)
incq %r15
cmpq %r15, %r14
jne 0x495c
movq %r12, %r15
movq 0x10(%rsp), %r12
jmp 0x49ba
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
testq %r15, %r15
je 0x4ee4
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x20a0
leaq 0x30(%rsp), %r14
movq %r14, -0x10(%r14)
movq %r15, %rdi
callq 0x2080
leaq (%rax,%r15), %rdx
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x5b7e
leaq 0x58(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x3216
movl %eax, %ebx
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x4a32
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2120
testb %bl, %bl
je 0x4ef1
movq 0x90(%rsp), %rax
xorl %ecx, %ecx
cmpq 0x88(%rsp), %rax
setne %cl
movq 0xa8(%rsp), %rax
cmpq 0xa0(%rsp), %rax
movl $0x2, %eax
cmovel %ecx, %eax
movq 0xc0(%rsp), %rcx
testb $0x1, %r12b
movl $0x3, %edx
cmovel %eax, %edx
cmpq 0xb8(%rsp), %rcx
cmovel %eax, %edx
movq 0xd8(%rsp), %rax
testb $0x1, %r13b
movl $0x4, %ebp
cmovel %edx, %ebp
cmpq 0xd0(%rsp), %rax
cmovel %edx, %ebp
movl $0x46424d53, 0x48(%rsp) # imm = 0x46424D53
movw $0x100, 0x4e(%rsp)      # imm = 0x100
movb %bpl, 0x4c(%rsp)
movq 0x60(%rsp), %rax
movq 0x78(%rsp), %rcx
subq 0x70(%rsp), %rcx
sarq $0x2, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
movl %edx, 0x50(%rsp)
subq 0x58(%rsp), %rax
shrq $0x3, %rax
movl %eax, 0x54(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
andq %rdx, %r12
je 0x4b57
movq %r12, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r14
movq %r14, %rdi
callq 0x2110
movq %rax, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
cmpl $0x1, %r12d
je 0x4b47
movl $0x30, %eax
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rbx,%rax)
movups %xmm1, 0x10(%rbx,%rax)
movups %xmm0, (%rbx,%rax)
addq $0x30, %rax
cmpq %rax, %r14
jne 0x4b25
leaq (%r12,%r12,2), %rax
shlq $0x4, %rax
movq %rbx, %r14
addq %rax, %r14
jmp 0x4b5c
xorl %r14d, %r14d
xorl %ebx, %ebx
testq %r12, %r12
je 0x4c61
movq 0x70(%rsp), %rax
movq 0x88(%rsp), %rcx
movq 0x90(%rsp), %rdx
movq 0xa8(%rsp), %rsi
movq 0xa0(%rsp), %rdi
movq 0xb8(%rsp), %r8
movq 0xd0(%rsp), %r9
shlq $0x2, %r12
leaq (%r12,%r12,2), %r10
xorl %r11d, %r11d
movss (%rax,%r11), %xmm0
movss %xmm0, (%rbx,%r11,4)
movss 0x4(%rax,%r11), %xmm0
movss %xmm0, 0x4(%rbx,%r11,4)
movss 0x8(%rax,%r11), %xmm0
movss %xmm0, 0x8(%rbx,%r11,4)
cmpq %rcx, %rdx
je 0x4bfb
movss (%rcx,%r11), %xmm0
movss %xmm0, 0xc(%rbx,%r11,4)
movss 0x4(%rcx,%r11), %xmm0
movss %xmm0, 0x10(%rbx,%r11,4)
movss 0x8(%rcx,%r11), %xmm0
movss %xmm0, 0x14(%rbx,%r11,4)
cmpq %rdi, %rsi
je 0x4c1b
movss (%rdi,%r11), %xmm0
movss %xmm0, 0x18(%rbx,%r11,4)
movss 0x4(%rdi,%r11), %xmm0
movss %xmm0, 0x1c(%rbx,%r11,4)
movss (%r8,%r11), %xmm0
movss %xmm0, 0x20(%rbx,%r11,4)
movss 0x4(%r8,%r11), %xmm0
movss %xmm0, 0x24(%rbx,%r11,4)
movss 0x8(%r8,%r11), %xmm0
movss %xmm0, 0x28(%rbx,%r11,4)
movss (%r9), %xmm0
movss %xmm0, 0x2c(%rbx,%r11,4)
addq $0xc, %r11
addq $0x4, %r9
cmpq %r11, %r10
jne 0x4ba5
movq %r15, %rdi
movl $0x2e, %esi
callq 0x2130
leaq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %r12
movq %r12, (%rdi)
movq %r15, %rsi
movq %rax, %rdx
callq 0x5b7e
leaq 0x252c(%rip), %rsi      # 0x71b9
leaq 0x20(%rsp), %rdi
callq 0x2250
leaq 0x2521(%rip), %rdx      # 0x71bf
leaq 0x18(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x247e
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
je 0x4f17
leaq 0x48(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x2260
movl %ebp, %ebp
cmpq %r14, %rbx
je 0x4d05
movq 0x18(%rsp), %r15
leaq 0x23de(%rip), %rax      # 0x70c0
movq (%rax,%rbp,8), %r12
movq %rbx, %r13
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x2260
addq $0x30, %r13
cmpq %r14, %r13
jne 0x4ce9
movq %rbp, 0x10(%rsp)
movq 0x58(%rsp), %r13
movq 0x60(%rsp), %rbp
cmpq %rbp, %r13
je 0x4d49
movq 0x18(%rsp), %r15
leaq 0x44(%rsp), %r12
movl (%r13), %eax
movl %eax, 0x44(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x2260
addq $0x8, %r13
cmpq %rbp, %r13
jne 0x4d23
movq 0x20(%rsp), %rsi
leaq 0x248d(%rip), %rdi      # 0x71e2
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2030
leaq 0x253e(%rip), %rdi      # 0x72a4
callq 0x21c0
movl 0x54(%rsp), %esi
leaq 0x2479(%rip), %rdi      # 0x71ef
xorl %eax, %eax
callq 0x2030
leaq 0x252a(%rip), %rdi      # 0x72ae
callq 0x21c0
movl 0xc(%rsp), %esi
leaq 0x2468(%rip), %rdi      # 0x71fc
xorl %eax, %eax
callq 0x2030
movl 0x8(%rsp), %esi
leaq 0x2465(%rip), %rdi      # 0x720b
xorl %eax, %eax
callq 0x2030
leaq 0x250a(%rip), %rdi      # 0x72be
callq 0x21c0
movl 0x50(%rsp), %esi
leaq 0x2456(%rip), %rdi      # 0x721a
xorl %eax, %eax
callq 0x2030
leaq 0x22d6(%rip), %rax      # 0x70a8
movq 0x10(%rsp), %rcx
movslq (%rax,%rcx,4), %rsi
addq %rax, %rsi
leaq 0x2443(%rip), %rdi      # 0x7228
xorl %eax, %eax
callq 0x2030
leaq 0x30(%rsp), %r12
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x4e00
callq 0x2100
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x4e17
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2120
testq %rbx, %rbx
je 0x4e2a
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2120
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x4e47
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x4e64
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x4e81
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x4e9e
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x4eb8
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4ecf
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movl %r15d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x22be
movl $0x1, %r15d
jmp 0x4ecf
movq 0x50e8(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
leaq 0x2284(%rip), %rsi      # 0x7186
movq %r15, %rdx
xorl %eax, %eax
callq 0x21a0
movl $0x1, %r15d
jmp 0x4e2a
leaq 0x20(%rsp), %rdi
callq 0x229a
movl $0x1, %r15d
jmp 0x4df1
movq %rax, %r15
xorl %r14d, %r14d
xorl %ebx, %ebx
jmp 0x4f5c
jmp 0x4f38
movq %rax, %r15
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x4f5c
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2120
jmp 0x4f5c
movq %rax, %r15
testq %rbx, %rbx
je 0x4f8b
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x4f81
movq %rax, %r15
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x4f8b
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2120
jmp 0x4f8b
movq %rax, %r15
leaq 0x58(%rsp), %rdi
callq 0x541e
movq %r15, %rdi
callq 0x2220