// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %rbx
subq $0x88, %rsp
movss %xmm1, 0x2c(%rsp)
movaps %xmm0, 0x60(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq %rdx, %rdi
callq 0x3ea6
movl %eax, %ebp
movq %rbx, %rdi
callq 0x3eaa
movss 0x1431(%rip), %xmm5     # 0x71c0
movss 0x142d(%rip), %xmm0     # 0x71c4
xorl %ecx, %ecx
xorps %xmm1, %xmm1
movaps %xmm0, %xmm6
movaps %xmm5, %xmm8
leaq (%rcx,%rcx,2), %rdx
leaq (%r15,%rdx,4), %rdx
movl $0x1, %esi
cmpl $0x1, %esi
movl %eax, %edi
cmovel %ebp, %edi
decl %edi
xorl %r8d, %r8d
movaps %xmm0, %xmm3
subl $0x1, %esi
setae %r8b
leaq 0x4(,%r8,4), %r9
movl $0x0, %r8d
cmovneq %r9, %r8
je 0x5dde
movaps %xmm6, %xmm3
movss (%rdx,%r8), %xmm2
movaps %xmm2, %xmm4
minss %xmm3, %xmm4
maxss %xmm1, %xmm4
je 0x5dfa
movaps %xmm8, %xmm3
movaps %xmm4, %xmm6
jmp 0x5e00
movaps %xmm5, %xmm3
movaps %xmm4, %xmm0
maxss %xmm3, %xmm2
xorps %xmm3, %xmm3
cvtsi2ss %edi, %xmm3
minss %xmm3, %xmm2
je 0x5e19
movaps %xmm2, %xmm8
je 0x5db0
jmp 0x5e1e
movaps %xmm2, %xmm5
je 0x5db0
incq %rcx
cmpq $0x3, %rcx
jne 0x5da3
ucomiss %xmm0, %xmm5
jb 0x6020
xorps %xmm1, %xmm1
xorps %xmm7, %xmm7
ucomiss %xmm6, %xmm8
movss %xmm0, %xmm1            # xmm1 = xmm0[0],xmm1[1,2,3]
unpcklps %xmm6, %xmm0            # xmm0 = xmm0[0],xmm6[0],xmm0[1],xmm6[1]
jb 0x6006
movaps %xmm6, %xmm2
movss %xmm5, 0xc(%rsp)
movaps %xmm6, 0x40(%rsp)
movss %xmm8, 0x8(%rsp)
movaps %xmm2, 0x10(%rsp)
movaps %xmm1, 0x50(%rsp)
movaps %xmm0, %xmm6
movsd (%r15), %xmm0
movss 0x8(%r15), %xmm1
movsd 0xc(%r15), %xmm2
movss 0x14(%r15), %xmm3
movsd 0x18(%r15), %xmm4
movss 0x20(%r15), %xmm5
movaps %xmm6, 0x70(%rsp)
movss %xmm7, 0x4(%rsp)
callq 0x5c24
movlps %xmm0, 0x30(%rsp)
movss %xmm1, 0x38(%rsp)
xorps %xmm0, %xmm0
ucomiss 0x30(%rsp), %xmm0
ja 0x5fc8
ucomiss 0x34(%rsp), %xmm0
ja 0x5fc8
ucomiss %xmm1, %xmm0
movss 0xc(%rsp), %xmm5
movaps 0x40(%rsp), %xmm6
movss 0x8(%rsp), %xmm8
movss 0x4(%rsp), %xmm7
movaps 0x10(%rsp), %xmm2
ja 0x5fe5
xorps %xmm1, %xmm1
xorl %eax, %eax
movss 0x8(%r15,%rax), %xmm0
xorl %ecx, %ecx
cmpq $0xc, %rax
setne %cl
testq %rax, %rax
leaq 0x4(,%rcx,4), %rcx
cmoveq %rax, %rcx
mulss 0x30(%rsp,%rcx), %xmm0
addss %xmm0, %xmm1
addq $0xc, %rax
cmpq $0x24, %rax
jne 0x5eef
movq %rbx, %rdi
movss %xmm1, 0x4(%rsp)
callq 0x3ea6
movss 0x4(%rsp), %xmm7
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps 0x10(%rsp), %xmm2
mulss %xmm2, %xmm0
movaps 0x50(%rsp), %xmm1
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
cltq
ucomiss (%r14,%rax,4), %xmm7
jbe 0x5fb4
movq %rbx, %rdi
callq 0x3ea6
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x10(%rsp), %xmm0
movaps 0x50(%rsp), %xmm1
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
cltq
movss 0x4(%rsp), %xmm0
movss %xmm0, (%r14,%rax,4)
cvttss2si %xmm1, %esi
cvttss2si 0x10(%rsp), %edx
movq %rbx, %rdi
movaps 0x60(%rsp), %xmm0
movss 0x2c(%rsp), %xmm1
callq 0x40b8
movaps 0x10(%rsp), %xmm2
movss 0x4(%rsp), %xmm7
movss 0xc(%rsp), %xmm5
movaps 0x40(%rsp), %xmm6
movss 0x8(%rsp), %xmm8
jmp 0x5fe5
movss 0xc(%rsp), %xmm5
movaps 0x40(%rsp), %xmm6
movss 0x8(%rsp), %xmm8
movss 0x4(%rsp), %xmm7
movaps 0x10(%rsp), %xmm2
addss 0x1017(%rip), %xmm2     # 0x7004
movaps %xmm2, %xmm0
movaps 0x70(%rsp), %xmm1
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
shufps $0xe2, %xmm1, %xmm0     # xmm0 = xmm0[2,0],xmm1[2,3]
ucomiss %xmm2, %xmm8
jae 0x5e60
movaps %xmm1, %xmm2
movaps %xmm0, %xmm1
addss 0xff0(%rip), %xmm2      # 0x7004
ucomiss %xmm2, %xmm5
movaps %xmm2, %xmm0
jae 0x5e3a
addq $0x88, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq