// File: /workspace/github_cpp_makefile_star2_O3/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 $0xb8, %rsp
movss %xmm1, 0x2c(%rsp)
movaps %xmm0, 0xa0(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq %rdx, %rdi
callq 0x3e92
movl %eax, %ebp
movq %rbx, %rdi
callq 0x3e96
movd %ebp, %xmm0
movd %eax, %xmm1
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
pcmpeqd %xmm1, %xmm1
paddd %xmm0, %xmm1
cvtdq2ps %xmm1, %xmm1
pxor %xmm0, %xmm0
punpcklqdq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0]
movapd 0x14ba(%rip), %xmm4     # 0x71f0
xorl %eax, %eax
movsd (%r15,%rax), %xmm1
movapd %xmm4, %xmm2
movsd %xmm1, %xmm2            # xmm2 = xmm1[0],xmm2[1]
movapd %xmm4, %xmm3
unpcklpd %xmm1, %xmm3            # xmm3 = xmm3[0],xmm1[0]
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
cmpltps %xmm3, %xmm2
andpd %xmm2, %xmm1
andnpd %xmm4, %xmm2
orpd %xmm1, %xmm2
movdqa %xmm0, %xmm1
cmpltps %xmm2, %xmm1
movaps %xmm2, %xmm4
cmpltps %xmm0, %xmm4
movsd %xmm1, %xmm4            # xmm4 = xmm1[0],xmm4[1]
andpd %xmm4, %xmm2
andnpd %xmm0, %xmm4
orpd %xmm2, %xmm4
addq $0xc, %rax
cmpq $0x24, %rax
jne 0x5d38
movapd %xmm4, %xmm0
unpckhpd %xmm4, %xmm0            # xmm0 = xmm0[1],xmm4[1]
movapd %xmm0, 0x80(%rsp)
ucomiss %xmm4, %xmm0
jb 0x5fa1
movaps %xmm4, %xmm0
movaps %xmm4, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
movaps %xmm4, %xmm1
shufps $0xff, %xmm0, %xmm1     # xmm1 = xmm1[3,3],xmm0[3,3]
movaps %xmm1, 0x60(%rsp)
xorps %xmm4, %xmm4
movaps %xmm0, %xmm1
xorps %xmm7, %xmm7
movaps %xmm2, 0x30(%rsp)
movaps 0x60(%rsp), %xmm3
ucomiss %xmm2, %xmm3
movss %xmm1, %xmm4            # xmm4 = xmm1[0],xmm4[1,2,3]
movaps %xmm0, 0x90(%rsp)
movss %xmm1, %xmm0            # xmm0 = xmm1[0],xmm0[1,2,3]
jb 0x5f7a
movaps %xmm2, %xmm3
movaps %xmm3, 0x10(%rsp)
movaps %xmm4, 0x70(%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, 0x40(%rsp)
movss %xmm7, 0xc(%rsp)
callq 0x5ba8
movlps %xmm0, 0x50(%rsp)
movss %xmm1, 0x58(%rsp)
xorps %xmm2, %xmm2
ucomiss %xmm0, %xmm2
ja 0x5f45
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
ucomiss %xmm0, %xmm2
ja 0x5f45
ucomiss %xmm1, %xmm2
movaps 0x30(%rsp), %xmm2
movaps 0x40(%rsp), %xmm4
movss 0xc(%rsp), %xmm7
movaps 0x10(%rsp), %xmm3
ja 0x5f5a
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 0x50(%rsp,%rcx), %xmm0
addss %xmm0, %xmm1
addq $0xc, %rax
cmpq $0x24, %rax
jne 0x5e71
movq %rbx, %rdi
movss %xmm1, 0xc(%rsp)
callq 0x3e92
movss 0xc(%rsp), %xmm7
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps 0x10(%rsp), %xmm3
mulss %xmm3, %xmm0
movaps 0x70(%rsp), %xmm1
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
cltq
ucomiss (%r14,%rax,4), %xmm7
jbe 0x5f39
movq %rbx, %rdi
callq 0x3e92
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x10(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
cltq
movss 0xc(%rsp), %xmm0
movss %xmm0, (%r14,%rax,4)
cvttss2si %xmm1, %esi
cvttss2si 0x10(%rsp), %edx
movq %rbx, %rdi
movaps 0xa0(%rsp), %xmm0
movss 0x2c(%rsp), %xmm1
callq 0x40a4
movaps 0x10(%rsp), %xmm3
movss 0xc(%rsp), %xmm7
movaps 0x30(%rsp), %xmm2
movaps 0x40(%rsp), %xmm4
jmp 0x5f5a
movaps 0x30(%rsp), %xmm2
movaps 0x40(%rsp), %xmm4
movss 0xc(%rsp), %xmm7
movaps 0x10(%rsp), %xmm3
addss 0x10de(%rip), %xmm3     # 0x7040
movaps %xmm3, %xmm0
movlhps %xmm4, %xmm0            # xmm0 = xmm0[0],xmm4[0]
shufps $0xe2, %xmm4, %xmm0     # xmm0 = xmm0[2,0],xmm4[2,3]
movaps 0x60(%rsp), %xmm1
ucomiss %xmm3, %xmm1
jae 0x5dea
addss 0x10be(%rip), %xmm4     # 0x7040
movaps 0x80(%rsp), %xmm1
ucomiss %xmm4, %xmm1
movaps %xmm4, %xmm1
movaps %xmm0, %xmm4
movaps 0x90(%rsp), %xmm0
jae 0x5dc9
addq $0xb8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq