// File: shinerd[P]BriefRenderer/main.cpp
// Function: main
int main(int argc, char** argv) {
    if (argc==2) {
        model = new Model(argv[1]);
    } else {
        model = new Model("obj/african_head.obj");
    }
    
    TGAImage image(width, height, TGAImage::RGB);
    
    for (int i=0; i<model->nfaces(); i++) {
        std::vector<int> face = model->face(i);
        Vec2i screen_coords[3];
        for (int j=0; j<3; j++) {
            Vec3f world_coords = model->vert(face[j]);
            screen_coords[j] = Vec2i((world_coords.x+1.)*width/2., (world_coords.y+1.)*height/2.);
        }
        triangle(screen_coords[0], screen_coords[1], screen_coords[2], image, TGAColor(rand()%255, rand()%255, rand()%255, 255));
    }
    
    image.flip_vertically();
    image.write_tga_file("output.tga");
    
    delete model;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jne 0x2b52
movl $0x30, %edi
callq 0x21d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x32d0
jmp 0x2b25
movq -0xc0(%rbp), %rax
movq %rax, 0x96cd(%rip)      # 0xc200
jmp 0x2ba4
movq -0xc0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x21b0
jmp 0x2f12
movl $0x30, %edi
callq 0x21d0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xc8(%rbp)
leaq 0x54b0(%rip), %rsi      # 0x8020
callq 0x32d0
jmp 0x2b77
movq -0xc8(%rbp), %rax
movq %rax, 0x967b(%rip)      # 0xc200
jmp 0x2ba4
movq -0xc8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x21b0
jmp 0x2f12
leaq -0x38(%rbp), %rdi
movl $0xc8, %edx
movl $0x3, %ecx
movl %edx, %esi
callq 0x6370
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0xd0(%rbp)
movq 0x9630(%rip), %rdi      # 0xc200
callq 0x38f0
movl %eax, -0xcc(%rbp)
jmp 0x2bdd
movl -0xd0(%rbp), %eax
movl -0xcc(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2e81
movq 0x9608(%rip), %rsi      # 0xc200
movl -0x3c(%rbp), %edx
leaq -0x58(%rbp), %rdi
callq 0x3910
jmp 0x2c06
leaq -0x70(%rbp), %rax
movq %rax, %rcx
addq $0x18, %rcx
movq %rcx, -0xe0(%rbp)
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x3030
jmp 0x2c34
movq -0xe0(%rbp), %rcx
movq -0xe8(%rbp), %rax
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0xd8(%rbp)
jne 0x2c1f
movl $0x0, -0x74(%rbp)
cmpl $0x3, -0x74(%rbp)
jge 0x2d81
movq 0x9596(%rip), %rax      # 0xc200
movq %rax, -0xf8(%rbp)
movslq -0x74(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x3050
movq %rax, -0xf0(%rbp)
jmp 0x2c87
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rax
movl (%rax), %esi
callq 0x3960
movss %xmm1, -0x114(%rbp)
movaps %xmm0, -0x110(%rbp)
jmp 0x2cad
movss -0x114(%rbp), %xmm0
movaps -0x110(%rbp), %xmm1
movlpd %xmm1, -0x90(%rbp)
movss %xmm0, -0x88(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x80(%rbp)
movss -0x80(%rbp), %xmm1
movss -0x7c(%rbp), %xmm0
cvtss2sd %xmm1, %xmm4
movsd 0x531a(%rip), %xmm3     # 0x8010
addsd %xmm3, %xmm4
movsd 0x5316(%rip), %xmm2     # 0x8018
mulsd %xmm2, %xmm4
movsd 0x52fa(%rip), %xmm1     # 0x8008
mulsd %xmm1, %xmm4
cvttsd2si %xmm4, %esi
cvtss2sd %xmm0, %xmm0
addsd %xmm3, %xmm0
mulsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
leaq -0x98(%rbp), %rdi
callq 0x3010
jmp 0x2d38
movslq -0x74(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rcx, -0x70(%rbp,%rax,8)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x2c59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x2f05
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x3070
jmp 0x2e7c
movq -0x70(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xb0(%rbp)
callq 0x20c0
movslq %eax, %rcx
imulq $-0x7f7f7f7f, %rcx, %rcx # imm = 0x80808081
shrq $0x20, %rcx
addl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
shrl $0x7, %ecx
addl %edx, %ecx
addl %ecx, %eax
movl %eax, -0x11c(%rbp)
callq 0x20c0
movslq %eax, %rcx
imulq $-0x7f7f7f7f, %rcx, %rcx # imm = 0x80808081
shrq $0x20, %rcx
addl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
shrl $0x7, %ecx
addl %edx, %ecx
addl %ecx, %eax
movl %eax, -0x118(%rbp)
callq 0x20c0
movl -0x11c(%rbp), %edx
movl -0x118(%rbp), %ecx
movslq %eax, %rsi
imulq $-0x7f7f7f7f, %rsi, %rsi # imm = 0x80808081
shrq $0x20, %rsi
addl %eax, %esi
movl %esi, %edi
shrl $0x1f, %edi
shrl $0x7, %esi
addl %edi, %esi
addl %esi, %eax
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
leaq -0xb8(%rbp), %rdi
movl $0xff, %r8d
callq 0x2f30
jmp 0x2e3a
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0xb8(%rbp), %r8
callq 0x2880
jmp 0x2e61
leaq -0x58(%rbp), %rdi
callq 0x3070
jmp 0x2e6c
jmp 0x2e6e
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2bc0
jmp 0x2f05
leaq -0x38(%rbp), %rdi
callq 0x6d30
jmp 0x2e8c
leaq 0x51a2(%rip), %rsi      # 0x8035
leaq -0x38(%rbp), %rdi
movl $0x1, %edx
callq 0x6f50
jmp 0x2ea3
movq 0x9356(%rip), %rax      # 0xc200
movq %rax, -0x128(%rbp)
cmpq $0x0, %rax
je 0x2ed1
movq -0x128(%rbp), %rdi
callq 0x3870
jmp 0x2ec5
movq -0x128(%rbp), %rdi
callq 0x21b0
movl $0x0, -0x4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6460
movl -0x4(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x128(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x21b0
leaq -0x38(%rbp), %rdi
callq 0x6460
jmp 0x2f10
jmp 0x2f12
movq -0x18(%rbp), %rdi
callq 0x2350
movq %rax, %rdi
callq 0x30f0
nopw %cs:(%rax,%rax)
nopl (%rax)