// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movss %xmm1, 0x1c(%rsp)
movaps %xmm0, 0x90(%rsp)
movq %rdx, %r14
movq %rsi, 0x58(%rsp)
movq %rdi, %r15
movsd 0x1ae1(%rip), %xmm0     # 0x7220
leaq 0x10(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x1ae0(%rip), %xmm0     # 0x7230
leaq 0x38(%rsp), %r13
movsd %xmm0, (%r13)
movq %rdx, %rdi
callq 0x3b60
movl %eax, %ebx
movq %r14, 0x60(%rsp)
movq %r14, %rdi
callq 0x3b64
movd %ebx, %xmm0
movd %eax, %xmm1
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
pcmpeqd %xmm1, %xmm1
paddd %xmm0, %xmm1
cvtdq2ps %xmm1, %xmm0
leaq 0x78(%rsp), %rax
movlps %xmm0, (%rax)
xorl %eax, %eax
cmpq $0x3, %rax
je 0x5867
movq %rax, 0x8(%rsp)
imulq $0xc, %rax, %r14
addq %r15, %r14
xorl %ebx, %ebx
cmpq $0x2, %rbx
je 0x585a
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x60b6
movq %rax, %rbp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3902
movss (%rax), %xmm0
minss (%rbp), %xmm0
maxss 0x1988(%rip), %xmm0     # 0x716c
movss %xmm0, 0x4(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x60b6
movss 0x4(%rsp), %xmm0
movss %xmm0, (%rax)
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
callq 0x60b6
movq %rax, %rbp
movq %r13, %rdi
movq %rbx, %rsi
callq 0x60b6
movq %rax, %r12
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3902
movss (%rax), %xmm0
maxss (%r12), %xmm0
minss (%rbp), %xmm0
movss %xmm0, 0x4(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x60b6
movss 0x4(%rsp), %xmm0
movss %xmm0, (%rax)
incq %rbx
jmp 0x57ab
movq 0x8(%rsp), %rax
incq %rax
jmp 0x5793
movss 0x10(%rsp), %xmm2
leaq 0x8(%r15), %r14
xorps %xmm6, %xmm6
leaq 0x68(%rsp), %r12
xorps %xmm7, %xmm7
movss 0x38(%rsp), %xmm0
ucomiss %xmm2, %xmm0
jb 0x5a39
movss %xmm2, %xmm6            # xmm6 = xmm2[0],xmm6[1,2,3]
movss 0x14(%rsp), %xmm1
movaps %xmm6, %xmm2
movaps %xmm1, %xmm6
movlhps %xmm2, %xmm6            # xmm6 = xmm6[0],xmm2[0]
shufps $0xe2, %xmm2, %xmm6     # xmm6 = xmm6[2,0],xmm2[2,3]
movss 0x3c(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x5a2c
movaps %xmm2, 0x80(%rsp)
movaps %xmm1, 0x40(%rsp)
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, 0x20(%rsp)
movss %xmm7, 0x4(%rsp)
callq 0x5594
xorps %xmm2, %xmm2
movlps %xmm0, 0x68(%rsp)
movss %xmm1, 0x70(%rsp)
ucomiss %xmm0, %xmm2
ja 0x5a0f
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
ucomiss %xmm0, %xmm2
ja 0x5a0f
ucomiss %xmm1, %xmm2
movaps 0x20(%rsp), %xmm6
movss 0x4(%rsp), %xmm7
ja 0x5a1a
xorps %xmm0, %xmm0
movq %r14, %r13
xorl %ebx, %ebx
movss %xmm0, 0x4(%rsp)
cmpq $0x3, %rbx
je 0x5971
movss (%r13), %xmm0
movss %xmm0, 0x8(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3902
movss 0x8(%rsp), %xmm1
mulss (%rax), %xmm1
movss 0x4(%rsp), %xmm0
addss %xmm1, %xmm0
incq %rbx
addq $0xc, %r13
jmp 0x5931
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0x3b60
cvtsi2ss %eax, %xmm0
movaps 0x40(%rsp), %xmm2
mulss %xmm2, %xmm0
movaps 0x80(%rsp), %xmm1
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
cltq
movq 0x58(%rsp), %r13
movss 0x4(%rsp), %xmm7
ucomiss (%r13,%rax,4), %xmm7
jbe 0x5a08
movq %rbx, %rdi
callq 0x3b60
cvtsi2ss %eax, %xmm0
movaps 0x40(%rsp), %xmm1
mulss %xmm1, %xmm0
movaps 0x80(%rsp), %xmm2
addss %xmm2, %xmm0
cvttss2si %xmm0, %eax
cltq
movss 0x4(%rsp), %xmm0
movss %xmm0, (%r13,%rax,4)
cvttss2si %xmm2, %esi
cvttss2si %xmm1, %edx
movq %rbx, %rdi
movaps 0x90(%rsp), %xmm0
movss 0x1c(%rsp), %xmm1
callq 0x3c90
movss 0x4(%rsp), %xmm7
movaps 0x20(%rsp), %xmm6
jmp 0x5a1a
movaps 0x20(%rsp), %xmm6
movss 0x4(%rsp), %xmm7
movaps 0x40(%rsp), %xmm1
addss 0x15dd(%rip), %xmm1     # 0x7004
jmp 0x5895
addss 0x15d0(%rip), %xmm2     # 0x7004
jmp 0x587c
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq