// File: /workspace/github_cpp_makefile_star2_O2/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 $0xf8, %rsp
cmpl $0x2, %edi
pushq $0x1
popq %rbp
cmovll %ebp, %edi
decl %edi
xorl %eax, %eax
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpq %rax, %rdi
je 0x3dee
movq 0x8(%rsi,%rax,8), %rcx
cmpb $0x2d, (%rcx)
jne 0x3de1
cmpb $0x74, 0x1(%rcx)
jne 0x3dcb
cmpb $0x0, 0x2(%rcx)
je 0x3de9
cmpb $0x62, 0x1(%rcx)
jne 0x3de1
cmpb $0x0, 0x2(%rcx)
sete %dl
cmovneq %rcx, %rbx
orb %dl, %r15b
jmp 0x3de4
movq %rcx, %rbx
incq %rax
jmp 0x3db0
movb $0x1, %r12b
jmp 0x3de4
testq %rbx, %rbx
je 0x4212
andl $0x0, 0x8(%rsp)
andl $0x0, 0x4(%rsp)
leaq 0x68(%rsp), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x20c0
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rdx
movq %rbx, %rsi
callq 0x479a
leaq 0x68(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq 0x2dfa
movl %eax, %r14d
leaq 0x40(%rsp), %rdi
callq 0x20a0
testb %r14b, %r14b
je 0x4233
movq 0xa0(%rsp), %rsi
movq 0x98(%rsp), %rcx
xorl %eax, %eax
cmpq %rcx, %rsi
setne %al
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %r8
cmpq %r8, %rdi
pushq $0x2
popq %rdx
cmovel %eax, %edx
movq 0xc8(%rsp), %r9
testb $0x1, %r12b
pushq $0x3
popq %rax
cmovel %edx, %eax
cmpq %r9, 0xd0(%rsp)
cmovel %edx, %eax
movq 0xe0(%rsp), %r10
testb $0x1, %r15b
pushq $0x4
popq %rbp
cmovel %eax, %ebp
cmpq %r10, 0xe8(%rsp)
cmovel %eax, %ebp
movl $0x46424d53, 0x10(%rsp) # imm = 0x46424D53
movw $0x100, 0x16(%rsp)      # imm = 0x100
movb %bpl, 0x14(%rsp)
movq 0x88(%rsp), %rax
movq 0x70(%rsp), %r14
movq 0x80(%rsp), %r11
subq %r11, %rax
pushq $0xc
popq %r15
cqto
idivq %r15
movl %eax, 0x18(%rsp)
subq 0x68(%rsp), %r14
shrq $0x3, %r14
movl %r14d, 0x1c(%rsp)
andq $0x0, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
andq %rax, %r15
je 0x3f8d
imulq $0x30, %r15, %r12
movq %r12, %rdi
callq 0x2130
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
callq 0x42e5
xorl %edi, %edi
xorl %esi, %esi
callq 0x42d6
movq %r14, 0x20(%rsp)
addq %r14, %r12
movq %r12, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq 0x80(%rsp), %r11
movq 0x98(%rsp), %rcx
movq 0xa0(%rsp), %rsi
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %r8
movq 0xc8(%rsp), %r9
movq 0xe0(%rsp), %r10
jmp 0x3f90
xorl %r14d, %r14d
imulq $0xc, %r15, %rax
xorl %edx, %edx
cmpq %rdx, %rax
je 0x4054
movss (%r11,%rdx), %xmm0
movss %xmm0, (%r14,%rdx,4)
movss 0x4(%r11,%rdx), %xmm0
movss %xmm0, 0x4(%r14,%rdx,4)
movss 0x8(%r11,%rdx), %xmm0
movss %xmm0, 0x8(%r14,%rdx,4)
cmpq %rcx, %rsi
je 0x3ff2
movss (%rcx,%rdx), %xmm0
movss %xmm0, 0xc(%r14,%rdx,4)
movss 0x4(%rcx,%rdx), %xmm0
movss %xmm0, 0x10(%r14,%rdx,4)
movss 0x8(%rcx,%rdx), %xmm0
movss %xmm0, 0x14(%r14,%rdx,4)
cmpq %r8, %rdi
je 0x4012
movss (%r8,%rdx), %xmm0
movss %xmm0, 0x18(%r14,%rdx,4)
movss 0x4(%r8,%rdx), %xmm0
movss %xmm0, 0x1c(%r14,%rdx,4)
movss (%r9,%rdx), %xmm0
movss %xmm0, 0x20(%r14,%rdx,4)
movss 0x4(%r9,%rdx), %xmm0
movss %xmm0, 0x24(%r14,%rdx,4)
movss 0x8(%r9,%rdx), %xmm0
movss %xmm0, 0x28(%r14,%rdx,4)
movss (%r10), %xmm0
movss %xmm0, 0x2c(%r14,%rdx,4)
addq $0xc, %rdx
addq $0x4, %r10
jmp 0x3f96
pushq $0x2e
popq %rsi
movq %rbx, %rdi
callq 0x2150
leaq 0x50(%rsp), %rcx
movq %rcx, -0x10(%rcx)
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x5116
leaq 0x211e(%rip), %rsi      # 0x619d
leaq 0x40(%rsp), %rdi
callq 0x2260
leaq 0x2113(%rip), %rdx      # 0x61a3
leaq 0x38(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x239c
movq 0x38(%rsp), %rbx
testq %rbx, %rbx
je 0x4250
leaq 0x10(%rsp), %rdi
pushq $0x10
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2270
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %r12
movl %ebp, %ebp
leaq 0x1fd9(%rip), %r13      # 0x60b0
cmpq %r12, %r15
je 0x40f5
movq (%r13,%rbp,8), %rsi
movq %r15, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2270
addq $0x30, %r15
jmp 0x40d7
movq %rbp, 0x60(%rsp)
movq 0x68(%rsp), %rbp
movq 0x70(%rsp), %r13
leaq 0xc(%rsp), %r14
pushq $0x4
popq %r15
pushq $0x1
popq %r12
cmpq %r13, %rbp
je 0x4134
movl (%rbp), %eax
movl %eax, 0xc(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x2270
addq $0x8, %rbp
jmp 0x4111
movq 0x40(%rsp), %rsi
leaq 0x2086(%rip), %rdi      # 0x61c6
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2030
leaq 0x2138(%rip), %rdi      # 0x6288
callq 0x21e0
movl 0x1c(%rsp), %esi
leaq 0x2073(%rip), %rdi      # 0x61d3
xorl %eax, %eax
callq 0x2030
leaq 0x2124(%rip), %rdi      # 0x6292
callq 0x21e0
movl 0x8(%rsp), %esi
leaq 0x2062(%rip), %rdi      # 0x61e0
xorl %eax, %eax
callq 0x2030
movl 0x4(%rsp), %esi
leaq 0x205f(%rip), %rdi      # 0x61ef
xorl %eax, %eax
callq 0x2030
leaq 0x2104(%rip), %rdi      # 0x62a2
callq 0x21e0
movl 0x18(%rsp), %esi
leaq 0x2050(%rip), %rdi      # 0x61fe
xorl %eax, %eax
callq 0x2030
leaq 0x1edc(%rip), %rax      # 0x6098
movq 0x60(%rsp), %rcx
movslq (%rax,%rcx,4), %rsi
addq %rax, %rsi
leaq 0x203d(%rip), %rdi      # 0x620c
xorl %eax, %eax
callq 0x2030
leaq 0x38(%rsp), %rdi
callq 0x2386
leaq 0x40(%rsp), %rdi
callq 0x20a0
leaq 0x20(%rsp), %rdi
callq 0x42bc
leaq 0x68(%rsp), %rdi
callq 0x47d6
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5dc7(%rip), %rax      # 0x9fe0
movq (%rax), %rcx
leaq 0x1f2f(%rip), %rdi      # 0x6152
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x2270
pushq $0x1
popq %rbp
jmp 0x41fe
movq 0x5da6(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
leaq 0x1f26(%rip), %rsi      # 0x616a
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21c0
jmp 0x41f4
movq 0x5d89(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
movq 0x40(%rsp), %rdx
leaq 0x1f3f(%rip), %rsi      # 0x61a5
xorl %eax, %eax
callq 0x21c0
pushq $0x1
popq %rbp
jmp 0x41d6
jmp 0x4288
jmp 0x4279
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x20a0
jmp 0x428b
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x42bc
jmp 0x42a9
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x20a0
jmp 0x42a9
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x47d6
movq %rbx, %rdi
callq 0x2240
nop