// File: graphitemaster[P]smbf/smbf.cpp
// Function: createTangents(std::vector<m::vec3, std::allocator<m::vec3>> const&, std::vector<m::vec3, std::allocator<m::vec3>> const&, std::vector<m::vec3, std::allocator<m::vec3>> const&, std::vector<unsigned long, std::allocator<unsigned long>> const&, std::vector<m::vec3, std::allocator<m::vec3>>&, std::vector<float, std::allocator<float>>&)
static void createTangents(const u::vector<m::vec3> &vertices,
                           const u::vector<m::vec3> &coordinates,
                           const u::vector<m::vec3> &normals,
                           const u::vector<size_t> &indices,
                           u::vector<m::vec3> &tangents_,
                           u::vector<float> &bitangents_)
{
    // Computing Tangent Space Basis Vectors for an Arbitrary Mesh (Lengyel’s Method)
    // Section 7.8 (or in Section 6.8 of the second edition).
    const size_t vertexCount = vertices.size();
    u::vector<m::vec3> tangents;
    u::vector<m::vec3> bitangents;

    tangents.resize(vertexCount);
    bitangents.resize(vertexCount);

    m::vec3 tangent;
    m::vec3 bitangent;

    for (size_t i = 0; i < indices.size(); i += 3) {
        const size_t x = indices[i+0];
        const size_t y = indices[i+1];
        const size_t z = indices[i+2];

        calculateTangent(vertices, coordinates, x, y, z, tangent, bitangent);

        tangents[x] += tangent;
        tangents[y] += tangent;
        tangents[z] += tangent;
        bitangents[x] += bitangent;
        bitangents[y] += bitangent;
        bitangents[z] += bitangent;
    }

    for (size_t i = 0; i < vertexCount; i++) {
        // Gram-Schmidt orthogonalize
        // http://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process
        const m::vec3 &n = normals[i];
        m::vec3 t = tangents[i];
        const m::vec3 v = (t - n * (n * t));
        tangents_[i] = v.isNullEpsilon() ? v : v.normalized();

        if (!tangents_[i].isNormalized()) {
            // Couldn't calculate vertex tangent for vertex, so we fill it in along
            // the x axis.
            tangents_[i] = m::vec3::xAxis;
            t = tangents_[i];
        }

        // bitangents are only stored by handness in the W component (-1.0f or 1.0f).
        bitangents_[i] = (((n ^ t) * bitangents[i]) < 0.0f) ? -1.0f : 1.0f;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rdi
callq 0x7dd0
movq %rax, -0x38(%rbp)
leaq -0x50(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x78b0
leaq -0x68(%rbp), %rdi
callq 0x78b0
movq -0x158(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x7c60
jmp 0x4bdb
movq -0x38(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x7c60
jmp 0x4bea
leaq -0x80(%rbp), %rdi
callq 0xa9c0
jmp 0x4bf5
leaq -0x8c(%rbp), %rdi
callq 0xa9c0
jmp 0x4c03
movq $0x0, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x20(%rbp), %rdi
callq 0x6e30
movq %rax, %rcx
movq -0x160(%rbp), %rax
cmpq %rcx, %rax
jae 0x4dbb
movq -0x20(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0xa9f0
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x20(%rbp), %rdi
movq -0x98(%rbp), %rsi
incq %rsi
callq 0xa9f0
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x20(%rbp), %rdi
movq -0x98(%rbp), %rsi
addq $0x2, %rsi
callq 0xa9f0
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0xa0(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movq -0xb0(%rbp), %r8
movq %rsp, %rax
leaq -0x8c(%rbp), %r9
movq %r9, (%rax)
leaq -0x80(%rbp), %r9
callq 0x5e20
jmp 0x4cc2
movq -0xa0(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x7db0
movq %rax, %rdi
leaq -0x80(%rbp), %rsi
callq 0xaa10
jmp 0x4ce0
movq -0xa8(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x7db0
movq %rax, %rdi
leaq -0x80(%rbp), %rsi
callq 0xaa10
jmp 0x4cfe
movq -0xb0(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x7db0
movq %rax, %rdi
leaq -0x80(%rbp), %rsi
callq 0xaa10
jmp 0x4d1c
movq -0xa0(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x7db0
movq %rax, %rdi
leaq -0x8c(%rbp), %rsi
callq 0xaa10
jmp 0x4d3d
movq -0xa8(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x7db0
movq %rax, %rdi
leaq -0x8c(%rbp), %rsi
callq 0xaa10
jmp 0x4d5e
movq -0xb0(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x7db0
movq %rax, %rdi
leaq -0x8c(%rbp), %rsi
callq 0xaa10
jmp 0x4d7f
jmp 0x4d81
movq -0x98(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x98(%rbp)
jmp 0x4c0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x7fe0
leaq -0x50(%rbp), %rdi
callq 0x7fe0
jmp 0x5127
movq $0x0, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x510c
movq -0x18(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0xaa60
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x7db0
movl 0x8(%rax), %ecx
movl %ecx, -0xc8(%rbp)
movq (%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0xc0(%rbp), %rdi
movq %rdi, -0x170(%rbp)
leaq -0xd0(%rbp), %rsi
callq 0xab50
movss %xmm0, -0x164(%rbp)
jmp 0x4e35
movss -0x164(%rbp), %xmm0
movq -0x170(%rbp), %rdi
callq 0xaaf0
movss %xmm1, -0x184(%rbp)
movaps %xmm0, -0x180(%rbp)
jmp 0x4e5a
movss -0x184(%rbp), %xmm0
movaps -0x180(%rbp), %xmm1
movlpd %xmm1, -0x100(%rbp)
movss %xmm0, -0xf8(%rbp)
movl -0xf8(%rbp), %eax
movl %eax, -0xe8(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xaa80
movss %xmm1, -0x1a4(%rbp)
movaps %xmm0, -0x1a0(%rbp)
jmp 0x4eb7
movss -0x1a4(%rbp), %xmm0
movaps -0x1a0(%rbp), %xmm1
movlpd %xmm1, -0x110(%rbp)
movss %xmm0, -0x108(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0xd8(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0xe0(%rbp), %rdi
movss 0xb11d(%rip), %xmm0     # 0x1001c
callq 0xabb0
movb %al, -0x1a5(%rbp)
jmp 0x4f0c
movb -0x1a5(%rbp), %al
testb $0x1, %al
jne 0x4f18
jmp 0x4f34
movq -0xe0(%rbp), %rax
movq %rax, -0x11c(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0x114(%rbp)
jmp 0x4f8a
leaq -0xe0(%rbp), %rdi
callq 0xabe0
movss %xmm1, -0x1c4(%rbp)
movaps %xmm0, -0x1c0(%rbp)
jmp 0x4f51
movss -0x1c4(%rbp), %xmm0
movaps -0x1c0(%rbp), %xmm1
movlpd %xmm1, -0x130(%rbp)
movss %xmm0, -0x128(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x11c(%rbp)
movl -0x128(%rbp), %eax
movl %eax, -0x114(%rbp)
movq -0x28(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x7db0
movl -0x114(%rbp), %ecx
movl %ecx, 0x8(%rax)
movq -0x11c(%rbp), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x7db0
movq %rax, %rdi
callq 0xac60
movb %al, -0x1c5(%rbp)
jmp 0x4fcd
movb -0x1c5(%rbp), %al
testb $0x1, %al
jne 0x501f
jmp 0x4fd9
movq -0x28(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x7db0
movq 0xb054(%rip), %rcx      # 0x10044
movq %rcx, (%rax)
movl 0xb053(%rip), %ecx      # 0x1004c
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x7db0
movq (%rax), %rcx
movq %rcx, -0xd0(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0xaca0
movss %xmm1, -0x1e4(%rbp)
movaps %xmm0, -0x1e0(%rbp)
jmp 0x5043
movss -0x1e4(%rbp), %xmm0
movaps -0x1e0(%rbp), %xmm1
movlpd %xmm1, -0x150(%rbp)
movss %xmm0, -0x148(%rbp)
movl -0x148(%rbp), %eax
movl %eax, -0x138(%rbp)
movq -0x150(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0xb8(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x7db0
movq %rax, %rsi
leaq -0x140(%rbp), %rdi
callq 0xab50
movss %xmm0, -0x1e8(%rbp)
jmp 0x50a5
movss -0x1e8(%rbp), %xmm0
movss 0xaf63(%rip), %xmm3     # 0x10018
movss 0xaf4f(%rip), %xmm2     # 0x1000c
xorps %xmm1, %xmm1
cmpltss %xmm1, %xmm0
movaps %xmm0, %xmm1
andps %xmm3, %xmm1
andnps %xmm2, %xmm0
orps %xmm1, %xmm0
movss %xmm0, -0x1ec(%rbp)
movq -0x30(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x8170
movss -0x1ec(%rbp), %xmm0
movss %xmm0, (%rax)
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0x4dc6
leaq -0x68(%rbp), %rdi
callq 0x7fe0
leaq -0x50(%rbp), %rdi
callq 0x7fe0
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x2330