// File: SpiritLooper[P]RDV_OBJRender/./triangle.hpp
// Function: triangle(Vec3<float>*, float*, Image&, Vec3<float>)
void triangle(Vec3f *pts, float *zbuffer, Image &image, Vec3f color) {
    Vec2f bboxmin( std::numeric_limits<float>::max(),  std::numeric_limits<float>::max());
    Vec2f bboxmax(-std::numeric_limits<float>::max(), -std::numeric_limits<float>::max());
    Vec2f clamp(image.get_width()-1, image.get_height()-1);
    
    for (int i=0; i<3; i++) {
        for (int j=0; j<2; j++) {
            bboxmin[j] = std::max(0.f,      std::min(bboxmin[j], pts[i][j]));
            bboxmax[j] = std::min(clamp[j], std::max(bboxmax[j], pts[i][j]));
        }
    }
    
    Vec3f P;
    
    for (P.x=bboxmin.x; P.x<=bboxmax.x; P.x++) {
        for (P.y=bboxmin.y; P.y<=bboxmax.y; P.y++) {
            Vec3f bc_screen  = barycentric(pts[0], pts[1], pts[2], P);
            if (bc_screen.x<0 || bc_screen.y<0 || bc_screen.z<0) continue;
            P.z = 0;
            for (int i=0; i<3; i++) P.z += pts[i][2]*bc_screen[i];
            if (zbuffer[int(P.x+P.y*image.get_width())]<P.z) {
                zbuffer[int(P.x+P.y*image.get_width())] = P.z;
                image.setPixel(P.x, P.y, color);
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movlpd %xmm0, -0x1c(%rbp)
movss %xmm1, -0x14(%rbp)
movq -0x1c(%rbp), %rax
movq %rax, -0xc(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
callq 0xe830
movss %xmm0, -0x12c(%rbp)
callq 0xe830
movaps %xmm0, %xmm1
movss -0x12c(%rbp), %xmm0
leaq -0x40(%rbp), %rdi
callq 0xe840
callq 0xe830
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x128(%rbp)
callq 0xe830
movaps %xmm0, %xmm1
movss -0x128(%rbp), %xmm0
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
leaq -0x48(%rbp), %rdi
callq 0xe840
movq -0x38(%rbp), %rdi
callq 0x6600
subl $0x1, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x124(%rbp)
movq -0x38(%rbp), %rdi
callq 0x6620
movss -0x124(%rbp), %xmm0
subl $0x1, %eax
cvtsi2ss %eax, %xmm1
leaq -0x50(%rbp), %rdi
callq 0xe840
movl $0x0, -0x54(%rbp)
cmpl $0x3, -0x54(%rbp)
jge 0xd775
movl $0x0, -0x58(%rbp)
cmpl $0x2, -0x58(%rbp)
jge 0xd765
xorps %xmm0, %xmm0
movss %xmm0, -0x5c(%rbp)
movslq -0x58(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0xe870
movq %rax, -0x150(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x54(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
movslq -0x58(%rbp), %rsi
callq 0x5c40
movq -0x150(%rbp), %rdi
movq %rax, %rsi
callq 0x6fb0
movq %rax, %rsi
leaq -0x5c(%rbp), %rdi
callq 0x6f70
movss (%rax), %xmm0
movss %xmm0, -0x144(%rbp)
movslq -0x58(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0xe870
movss -0x144(%rbp), %xmm0
movss %xmm0, (%rax)
movslq -0x58(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0xe870
movq %rax, -0x138(%rbp)
movslq -0x58(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0xe870
movq %rax, -0x140(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x54(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
movslq -0x58(%rbp), %rsi
callq 0x5c40
movq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0x6f70
movq -0x138(%rbp), %rdi
movq %rax, %rsi
callq 0x6fb0
movss (%rax), %xmm0
movss %xmm0, -0x130(%rbp)
movslq -0x58(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0xe870
movss -0x130(%rbp), %xmm0
movss %xmm0, (%rax)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xd656
jmp 0xd767
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xd645
leaq -0x68(%rbp), %rdi
callq 0x5b90
movss -0x40(%rbp), %xmm0
movss %xmm0, -0x68(%rbp)
movss -0x68(%rbp), %xmm1
movss -0x48(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jb 0xdade
movss -0x3c(%rbp), %xmm0
movss %xmm0, -0x64(%rbp)
movss -0x64(%rbp), %xmm1
movss -0x44(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jb 0xdac5
movq -0x28(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x78(%rbp)
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rax
movl 0x14(%rax), %ecx
movl %ecx, -0x88(%rbp)
movq 0xc(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x28(%rbp), %rax
movl 0x20(%rax), %ecx
movl %ecx, -0x98(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0xa0(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0xa8(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0xb8(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xc0(%rbp)
movsd -0xc0(%rbp), %xmm0
movss -0xb8(%rbp), %xmm1
movl -0x88(%rbp), %eax
movl %eax, -0xc8(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xd0(%rbp)
movsd -0xd0(%rbp), %xmm2
movss -0xc8(%rbp), %xmm3
movl -0x98(%rbp), %eax
movl %eax, -0xd8(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movsd -0xe0(%rbp), %xmm4
movss -0xd8(%rbp), %xmm5
movl -0xa8(%rbp), %eax
movl %eax, -0xe8(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movsd -0xf0(%rbp), %xmm6
movss -0xe8(%rbp), %xmm7
callq 0xd240
movss %xmm1, -0xf8(%rbp)
movlpd %xmm0, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x74(%rbp)
movl -0xf8(%rbp), %eax
movl %eax, -0x6c(%rbp)
xorps %xmm0, %xmm0
ucomiss -0x74(%rbp), %xmm0
ja 0xd8f3
xorps %xmm0, %xmm0
ucomiss -0x70(%rbp), %xmm0
ja 0xd8f3
xorps %xmm0, %xmm0
ucomiss -0x6c(%rbp), %xmm0
jbe 0xd8f8
jmp 0xdaae
xorps %xmm0, %xmm0
movss %xmm0, -0x60(%rbp)
movl $0x0, -0x104(%rbp)
cmpl $0x3, -0x104(%rbp)
jge 0xd97a
movq -0x28(%rbp), %rdi
movslq -0x104(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
movl $0x2, %esi
callq 0x5c40
movss (%rax), %xmm0
movss %xmm0, -0x154(%rbp)
movslq -0x104(%rbp), %rsi
leaq -0x74(%rbp), %rdi
callq 0x5c40
movss -0x154(%rbp), %xmm0
movss (%rax), %xmm2
movss -0x60(%rbp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x60(%rbp)
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0xd90a
movq -0x30(%rbp), %rax
movq %rax, -0x160(%rbp)
movss -0x68(%rbp), %xmm0
movss %xmm0, -0x164(%rbp)
movss -0x64(%rbp), %xmm0
movss %xmm0, -0x168(%rbp)
movq -0x38(%rbp), %rdi
callq 0x6600
movss -0x168(%rbp), %xmm0
movss -0x164(%rbp), %xmm1
movl %eax, %ecx
movq -0x160(%rbp), %rax
cvtsi2ss %ecx, %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm1
movss -0x60(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xdaac
movss -0x60(%rbp), %xmm0
movss %xmm0, -0x174(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x170(%rbp)
movss -0x68(%rbp), %xmm0
movss %xmm0, -0x178(%rbp)
movss -0x64(%rbp), %xmm0
movss %xmm0, -0x17c(%rbp)
movq -0x38(%rbp), %rdi
callq 0x6600
movss -0x17c(%rbp), %xmm1
movss -0x178(%rbp), %xmm2
movss -0x174(%rbp), %xmm0
movl %eax, %ecx
movq -0x170(%rbp), %rax
cvtsi2ss %ecx, %xmm3
mulss %xmm3, %xmm1
addss %xmm2, %xmm1
cvttss2si %xmm1, %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
movq -0x38(%rbp), %rdi
cvttss2si -0x68(%rbp), %esi
cvttss2si -0x64(%rbp), %edx
movl -0x4(%rbp), %eax
movl %eax, -0x108(%rbp)
movq -0xc(%rbp), %rax
movq %rax, -0x110(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0x118(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x120(%rbp)
movsd -0x120(%rbp), %xmm0
movss -0x118(%rbp), %xmm1
callq 0x68d0
jmp 0xdaae
movss 0x154e(%rip), %xmm0     # 0xf004
addss -0x64(%rbp), %xmm0
movss %xmm0, -0x64(%rbp)
jmp 0xd7a5
jmp 0xdac7
movss 0x1535(%rip), %xmm0     # 0xf004
addss -0x68(%rbp), %xmm0
movss %xmm0, -0x68(%rbp)
jmp 0xd788
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopw (%rax,%rax)