// File: /workspace/github_cpp_makefile_star2_O3/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 0x4d4a
decl %edi
xorl %eax, %eax
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x8(%rsi,%rax,8), %rcx
cmpb $0x2d, (%rcx)
jne 0x4812
cmpb $0x74, 0x1(%rcx)
jne 0x47fc
cmpb $0x0, 0x2(%rcx)
je 0x481f
cmpb $0x62, 0x1(%rcx)
jne 0x4812
cmpb $0x0, 0x2(%rcx)
sete %dl
cmovneq %rcx, %r15
orb %dl, %r14b
jmp 0x4815
movq %rcx, %r15
incq %rax
cmpq %rax, %rdi
jne 0x47e6
jmp 0x4824
movb $0x1, %r12b
jmp 0x4815
testq %r15, %r15
je 0x4d4a
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x20a0
leaq 0x20(%rsp), %r13
movq %r13, -0x10(%r13)
movq %r15, %rdi
callq 0x2080
leaq (%rax,%r15), %rdx
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x59e6
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x31fc
movl %eax, %ebx
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x489c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2120
testb %bl, %bl
je 0x4d57
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, %r14b
movl $0x4, %ebp
cmovel %edx, %ebp
cmpq 0xd0(%rsp), %rax
cmovel %edx, %ebp
movl $0x46424d53, 0x38(%rsp) # imm = 0x46424D53
movw $0x100, 0x3e(%rsp)      # imm = 0x100
movb %bpl, 0x3c(%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, 0x40(%rsp)
subq 0x58(%rsp), %rax
shrq $0x3, %rax
movl %eax, 0x44(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
andq %rdx, %r12
je 0x499a
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)
leaq (%r12,%r12,2), %rax
cmpl $0x1, %r12d
jne 0x49a4
shlq $0x4, %rax
movq %rbx, %r14
addq %rax, %r14
jmp 0x49de
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x4ada
movl $0x30, %ecx
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rbx,%rcx)
movups %xmm1, 0x10(%rbx,%rcx)
movups %xmm0, (%rbx,%rcx)
addq $0x30, %rcx
cmpq %rcx, %r14
jne 0x49a9
shlq $0x4, %rax
movq %rbx, %r14
addq %rax, %r14
testq %r12, %r12
je 0x4ada
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 0x4a74
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 0x4a94
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 0x4a1e
movq %r15, %rdi
movl $0x2e, %esi
callq 0x2130
leaq 0x10(%rsp), %rdi
movq %r13, (%rdi)
movq %r15, %rsi
movq %rax, %rdx
callq 0x59e6
leaq 0x26cc(%rip), %rsi      # 0x71cd
leaq 0x10(%rsp), %rdi
callq 0x2220
leaq 0x26c1(%rip), %rdx      # 0x71d3
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x244e
movq 0x50(%rsp), %r15
testq %r15, %r15
je 0x4d7d
leaq 0x38(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2230
movl %ebp, %ebp
cmpq %r14, %rbx
je 0x4b77
leaq 0x258c(%rip), %rax      # 0x70e0
movq (%rax,%rbp,8), %r12
movq %rbx, %r13
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x2230
addq $0x30, %r13
cmpq %r14, %r13
jne 0x4b5b
movq %rbp, 0x48(%rsp)
movq 0x58(%rsp), %r13
movq 0x60(%rsp), %rbp
cmpq %rbp, %r13
je 0x4bb6
leaq 0x34(%rsp), %r12
movl (%r13), %eax
movl %eax, 0x34(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x2230
addq $0x8, %r13
cmpq %rbp, %r13
jne 0x4b90
movq 0x10(%rsp), %rsi
leaq 0x2634(%rip), %rdi      # 0x71f6
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x2030
leaq 0x26e5(%rip), %rdi      # 0x72b8
callq 0x21a0
movl 0x44(%rsp), %esi
leaq 0x2620(%rip), %rdi      # 0x7203
xorl %eax, %eax
callq 0x2030
leaq 0x26d1(%rip), %rdi      # 0x72c2
callq 0x21a0
movl 0xc(%rsp), %esi
leaq 0x260f(%rip), %rdi      # 0x7210
xorl %eax, %eax
callq 0x2030
movl 0x8(%rsp), %esi
leaq 0x260c(%rip), %rdi      # 0x721f
xorl %eax, %eax
callq 0x2030
leaq 0x26b1(%rip), %rdi      # 0x72d2
callq 0x21a0
movl 0x40(%rsp), %esi
leaq 0x25fd(%rip), %rdi      # 0x722e
xorl %eax, %eax
callq 0x2030
leaq 0x2489(%rip), %rax      # 0x70c8
movq 0x48(%rsp), %rcx
movslq (%rax,%rcx,4), %rsi
addq %rax, %rsi
leaq 0x25ea(%rip), %rdi      # 0x723c
xorl %eax, %eax
callq 0x2030
movq %r15, %rdi
callq 0x2100
leaq 0x20(%rsp), %r13
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x4c7d
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2120
testq %rbx, %rbx
je 0x4c90
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2120
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x4cad
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x4cca
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x4ce7
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x4d04
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x4d1e
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4d35
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movl %r12d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x228e
movl $0x1, %r12d
jmp 0x4d35
movq 0x5282(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
leaq 0x2432(%rip), %rsi      # 0x719a
movq %r15, %rdx
xorl %eax, %eax
callq 0x2180
movl $0x1, %r12d
jmp 0x4c90
leaq 0x10(%rsp), %rdi
callq 0x226a
movl $0x1, %r12d
jmp 0x4c66
jmp 0x4de1
jmp 0x4d96
movq %rax, %r15
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x4db5
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2120
jmp 0x4db5
movq %rax, %r15
testq %rbx, %rbx
je 0x4de4
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x4dda
movq %rax, %r15
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x4de4
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2120
jmp 0x4de4
movq %rax, %r15
leaq 0x58(%rsp), %rdi
callq 0x527e
movq %r15, %rdi
callq 0x2200