// File: /workspace/github_c_makefile_star2_O2/nkanaev[P]tipsy/src/tipsy.c
// Function: draw_surface
void draw_surface(Tigr *scr, Obj *obj, Surface sf, State state) {
  Face f = *(Face*)(list_get(obj->f, sf.idx));

  Tigr *texture = NULL;
  if (f.mtl) texture = f.mtl->map_Ka ? f.mtl->map_Ka : f.mtl->map_Kd;
  if (texture == NULL) return;

  int shading = -1, has_normals = 0;
  Vec vn1, vn2, vn3;

  if (state.shading == SHADING_FLAT) {
    Vec bc = {0.333, 0.333, 0.333};
    shading = shade(sf.nrm, bc);
  }
  if (state.shading == SHADING_GOURAUD) {
    has_normals = f.vn1 > 0 && f.vn2 > 0 && f.vn3 > 0;
    if (has_normals) {
      vn1 = vec_nrm(VREF(list_get(obj->vn, f.vn1-1)));
      vn2 = vec_nrm(VREF(list_get(obj->vn, f.vn2-1)));
      vn3 = vec_nrm(VREF(list_get(obj->vn, f.vn3-1)));
      vn1 = perspective(vn1, state.x, state.y, state.z);
      vn2 = perspective(vn2, state.x, state.y, state.z);
      vn3 = perspective(vn3, state.x, state.y, state.z);
    }
  }

  Vec vt1 = VREF(list_get(obj->vt, f.vt1-1)),
      vt2 = VREF(list_get(obj->vt, f.vt2-1)),
      vt3 = VREF(list_get(obj->vt, f.vt3-1));

  int minX = fminf(fminf(sf.v1.x, sf.v2.x), sf.v3.x),
      maxX = fmaxf(fmaxf(sf.v1.x, sf.v2.x), sf.v3.x)+1,
      minY = fminf(fminf(sf.v1.y, sf.v2.y), sf.v3.y),
      maxY = fmaxf(fmaxf(sf.v1.y, sf.v2.y), sf.v3.y)+1;

  for (int y = minY; y < maxY; y++) {
    for (int x = minX; x < maxX; x++) {
      Vec p = {x, y, 0};
      Vec bc = barycenter(p, sf.v1, sf.v2, sf.v3);
      float err = -0.0001;
      if (bc.x >= err && bc.y >= err && bc.z >= err) {
        if (state.use_zbuffer) {
          float z = bc.x*sf.v1.z + bc.y*sf.v2.z + bc.z*sf.v3.z;
          int zbuff_idx = y * WIDTH + x;
          if (z > state.zbuff[zbuff_idx]) continue;
          state.zbuff[zbuff_idx] = z;
        }

        float u, v;

        if (state.use_pcorrect) {
          Vec bcc = bc;
          bcc.x = bc.x/(sf.v1.z);
          bcc.y = bc.y/(sf.v2.z);
          bcc.z = bc.z/(sf.v3.z);
          float bd = bcc.x+bcc.y+bcc.z;
          bcc.x = bcc.x/bd;
          bcc.y = bcc.y/bd;
          bcc.z = bcc.z/bd;

          u = bcc.x*vt1.x + bcc.y*vt2.x + bcc.z*vt3.x;
          v = 1.0-(bcc.x*vt1.y + bcc.y*vt2.y + bcc.z*vt3.y);
        } else {
          u = bc.x*vt1.x + bc.y*vt2.x + bc.z*vt3.x;
          v = 1.0-(bc.x*vt1.y + bc.y*vt2.y + bc.z*vt3.y);
        }

        int tx = texture->w * u;
        int ty = texture->h * v;

        TPixel texel = tigrGet(texture, tx % texture->w, ty % texture->h);

        if (state.shading == SHADING_GOURAUD && has_normals) {
          int s1 = shade(vn1, bc);
          int s2 = shade(vn2, bc);
          int s3 = shade(vn3, bc);
          shading = bc.x*s1 + bc.y*s2 + bc.z*s3;
        }

        if (shading >= 0) {
          texel.r = (texel.r * shading) >> 8;
          texel.g = (texel.g * shading) >> 8;
          texel.b = (texel.b * shading) >> 8;
        }
        tigrPlot(scr, x, y, texel);
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rsi, %r8
movq %rdi, 0x108(%rsp)
movq 0x18(%rsi), %rax
movslq 0x204(%rsp), %rcx
movq (%rax), %rdx
movslq 0x10(%rax), %rsi
imulq %rcx, %rsi
movq 0x28(%rdx,%rsi), %rcx
testq %rcx, %rcx
je 0x8ff0
movslq 0xc(%rdx,%rsi), %rbx
movslq 0x10(%rdx,%rsi), %r13
movslq 0x14(%rdx,%rsi), %r12
movl 0x18(%rdx,%rsi), %eax
movl 0x1c(%rdx,%rsi), %r14d
movl 0x20(%rdx,%rsi), %r15d
movq 0x8(%rcx), %rdx
movq %rdx, 0xb8(%rsp)
testq %rdx, %rdx
jne 0x8adc
movq 0x10(%rcx), %rcx
movq %rcx, 0xb8(%rsp)
testq %rcx, %rcx
je 0x8ff0
leaq 0x208(%rsp), %rcx
leaq 0x1b0(%rsp), %rsi
movl 0x14(%rcx), %ecx
pushq $-0x1
popq %rbp
cmpl $0x2, %ecx
je 0x9002
cmpl $0x1, %ecx
jne 0x8b2c
movsd 0x48(%rsi), %xmm0
movss 0x50(%rsi), %xmm1
movaps 0x329f(%rip), %xmm2     # 0xbdb0
movss 0x321b(%rip), %xmm3     # 0xbd34
movq %rsi, %r14
movq %r8, %r15
callq 0x8981
movq %r15, %r8
movq %r14, %rsi
movl %eax, %ebp
xorl %ecx, %ecx
xorl %eax, %eax
andb %al, %cl
movl %ecx, 0xc(%rsp)
movq 0x10(%r8), %rax
decq %rbx
movq (%rax), %rcx
movslq 0x10(%rax), %rax
imulq %rax, %rbx
movss (%rcx,%rbx), %xmm0
movss %xmm0, 0xd0(%rsp)
movss 0x4(%rcx,%rbx), %xmm0
movss %xmm0, 0x4(%rsp)
decq %r13
imulq %rax, %r13
movss (%rcx,%r13), %xmm0
movss %xmm0, 0x9c(%rsp)
movss 0x4(%rcx,%r13), %xmm0
movss %xmm0, 0x98(%rsp)
decq %r12
imulq %rax, %r12
movss (%rcx,%r12), %xmm0
movss %xmm0, 0x94(%rsp)
movss 0x4(%rcx,%r12), %xmm0
movss %xmm0, 0x90(%rsp)
movss 0xc(%rsi), %xmm1
movss %xmm1, 0x10(%rsp)
movss (%rsi), %xmm2
movss %xmm2, 0x40(%rsp)
movss 0x4(%rsi), %xmm0
movss %xmm0, 0x20(%rsp)
movaps %xmm2, %xmm0
movq %rsi, %r12
callq 0x3290
movss 0x18(%r12), %xmm1
movss %xmm1, 0xa0(%rsp)
callq 0x3290
cvttss2si %xmm0, %ebx
movss 0x40(%rsp), %xmm0
movss 0x10(%rsp), %xmm1
callq 0x33f0
movss 0xa0(%rsp), %xmm1
callq 0x33f0
addss 0x13eb(%rip), %xmm0     # 0xa004
cvttss2si %xmm0, %r14d
movss 0x10(%r12), %xmm1
movss %xmm1, 0x10(%rsp)
movss 0x20(%rsp), %xmm0
callq 0x3290
movss 0x1c(%r12), %xmm1
movss %xmm1, 0x40(%rsp)
callq 0x3290
cvttss2si %xmm0, %r15d
movss 0x20(%rsp), %xmm0
movss 0x10(%rsp), %xmm1
callq 0x33f0
movss 0x40(%rsp), %xmm1
callq 0x33f0
addss 0x1393(%rip), %xmm0     # 0xa004
cvttss2si %xmm0, %eax
movsd (%r12), %xmm0
movaps %xmm0, 0x10(%rsp)
movss 0x8(%r12), %xmm0
movss %xmm0, 0x38(%rsp)
movsd 0xc(%r12), %xmm0
movaps %xmm0, 0x40(%rsp)
movss 0x14(%r12), %xmm0
movsd 0x18(%r12), %xmm1
movaps %xmm1, 0xa0(%rsp)
movss 0x20(%r12), %xmm1
leaq 0x208(%rsp), %rdx
movl 0x4(%rdx), %ecx
movl %ecx, 0x8(%rsp)
movl 0x8(%rdx), %ecx
movl %ecx, 0x8c(%rsp)
movslq %ebx, %rcx
movq %rcx, 0xf8(%rsp)
movslq %r14d, %r14
movslq %r15d, %r13
cltq
movq %rax, 0x100(%rsp)
movaps %xmm1, 0x150(%rsp)
movaps %xmm0, 0x160(%rsp)
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps %xmm1, 0x140(%rsp)
imulq $0x500, %r13, %rax      # imm = 0x500
addq 0x40(%rdx), %rax
movl $0xff, %r15d
cmpq 0x100(%rsp), %r13
jge 0x8ff0
movq %rax, 0xc0(%rsp)
cvtsi2ss %r13d, %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0xf8(%rsp), %r12
movl 0xc(%rsp), %ebx
movq %r13, 0x78(%rsp)
cmpq %r14, %r12
jge 0x8fda
cvtsi2ss %r12d, %xmm0
unpcklps 0x20(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movaps 0x10(%rsp), %xmm2
movaps 0x40(%rsp), %xmm4
movaps 0xa0(%rsp), %xmm6
callq 0x7df8
movss 0x2fbf(%rip), %xmm2     # 0xbd38
ucomiss %xmm2, %xmm0
jb 0x8fd2
movaps %xmm0, %xmm4
shufps $0x55, %xmm0, %xmm4     # xmm4 = xmm4[1,1],xmm0[1,1]
ucomiss %xmm2, %xmm4
jb 0x8fd2
ucomiss %xmm2, %xmm1
jb 0x8fd2
cmpl $0x0, 0x8(%rsp)
je 0x8de4
movaps 0x160(%rsp), %xmm3
mulss %xmm4, %xmm3
movaps %xmm0, %xmm2
mulss 0x38(%rsp), %xmm2
addss %xmm3, %xmm2
movaps %xmm1, %xmm3
mulss 0x150(%rsp), %xmm3
addss %xmm2, %xmm3
movq 0xc0(%rsp), %rax
ucomiss (%rax,%r12,4), %xmm3
ja 0x8fd2
movss %xmm3, (%rax,%r12,4)
movaps %xmm0, %xmm5
movss %xmm1, %xmm5            # xmm5 = xmm1[0],xmm5[1,2,3]
cmpl $0x0, 0x8c(%rsp)
movaps %xmm1, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm4, 0xe0(%rsp)
movaps %xmm0, %xmm1
je 0x8e32
divss 0x38(%rsp), %xmm1
divps 0x140(%rsp), %xmm5
movaps %xmm5, %xmm2
shufps $0x55, %xmm5, %xmm2     # xmm2 = xmm2[1,1],xmm5[1,1]
addss %xmm1, %xmm2
addss %xmm5, %xmm2
divss %xmm2, %xmm1
shufps $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
divps %xmm2, %xmm5
movaps %xmm5, %xmm2
shufps $0x55, %xmm5, %xmm2     # xmm2 = xmm2[1,1],xmm5[1,1]
movss 0x9c(%rsp), %xmm3
mulss %xmm2, %xmm3
movaps %xmm1, %xmm4
mulss 0xd0(%rsp), %xmm4
addss %xmm3, %xmm4
movaps %xmm5, %xmm3
mulss 0x94(%rsp), %xmm3
addss %xmm4, %xmm3
mulss 0x98(%rsp), %xmm2
mulss 0x4(%rsp), %xmm1
addss %xmm2, %xmm1
mulss 0x90(%rsp), %xmm5
addss %xmm1, %xmm5
movq 0xb8(%rsp), %rdi
movl (%rdi), %esi
cvtsi2ss %esi, %xmm1
movss 0x1168(%rip), %xmm2     # 0xa004
mulss %xmm3, %xmm1
cvttss2si %xmm1, %eax
movl 0x4(%rdi), %r8d
cvtsi2ss %r8d, %xmm1
subss %xmm5, %xmm2
mulss %xmm2, %xmm1
cvttss2si %xmm1, %ecx
cltd
idivl %esi
movl %edx, %esi
movl %ecx, %eax
cltd
idivl %r8d
callq 0x42c6
movl %eax, %r13d
testb %bl, %bl
je 0x8f6f
movaps 0x130(%rsp), %xmm0
movss 0x88(%rsp), %xmm1
movaps 0x50(%rsp), %xmm2
movaps 0x60(%rsp), %xmm3
callq 0x8981
movl %eax, %ebp
movaps 0x120(%rsp), %xmm0
movss 0x84(%rsp), %xmm1
movaps 0x50(%rsp), %xmm2
movaps 0x60(%rsp), %xmm3
callq 0x8981
movl %eax, %ebx
movaps 0x110(%rsp), %xmm0
movss 0x80(%rsp), %xmm1
movaps 0x50(%rsp), %xmm2
movaps 0x60(%rsp), %xmm3
callq 0x8981
cvtsi2ss %ebp, %xmm0
cvtsi2ss %ebx, %xmm1
movl 0xc(%rsp), %ebx
movaps 0xe0(%rsp), %xmm2
mulss %xmm1, %xmm2
mulss 0x50(%rsp), %xmm0
addss %xmm2, %xmm0
cvtsi2ss %eax, %xmm1
movaps 0x60(%rsp), %xmm2
mulss %xmm1, %xmm2
addss %xmm0, %xmm2
cvttss2si %xmm2, %ebp
movl %r13d, %edx
shrl $0x8, %edx
movl %r13d, %eax
shrl $0x10, %eax
movl %r13d, %ecx
testl %ebp, %ebp
js 0x8f9d
andl %r15d, %ecx
imull %ebp, %ecx
shrl $0x8, %ecx
andl %r15d, %edx
imull %ebp, %edx
shrl $0x8, %edx
andl %r15d, %eax
imull %ebp, %eax
shrl $0x8, %eax
andl $0xff000000, %r13d      # imm = 0xFF000000
andl %r15d, %eax
shll $0x10, %eax
orl %r13d, %eax
shll $0x8, %edx
movzwl %dx, %edx
andl %r15d, %ecx
orl %edx, %ecx
orl %eax, %ecx
movq 0x108(%rsp), %rdi
movl %r12d, %esi
movq 0x78(%rsp), %r13
movl %r13d, %edx
callq 0x3c2e
incq %r12
jmp 0x8d47
incq %r13
movq 0xc0(%rsp), %rax
addq $0x500, %rax            # imm = 0x500
jmp 0x8d16
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %eax, %eax
setg %cl
testl %r14d, %r14d
setg %dl
andb %cl, %dl
testl %r15d, %r15d
setg %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x91cf
movq 0x8(%r8), %rcx
decl %eax
movq (%rcx), %rdx
movl 0x10(%rcx), %ecx
movl %ecx, 0x20(%rsp)
imull %ecx, %eax
cltq
movq %rdx, 0x10(%rsp)
movsd (%rdx,%rax), %xmm0
movss 0x8(%rdx,%rax), %xmm1
movq %r8, 0x38(%rsp)
callq 0x7c88
movaps %xmm0, 0xd0(%rsp)
movss %xmm1, 0x4(%rsp)
decl %r14d
imull 0x20(%rsp), %r14d
movslq %r14d, %rax
movq 0x10(%rsp), %rcx
movsd (%rcx,%rax), %xmm0
movss 0x8(%rcx,%rax), %xmm1
movq %rcx, %r14
callq 0x7c88
movaps %xmm0, 0xc0(%rsp)
movss %xmm1, 0x78(%rsp)
decl %r15d
imull 0x20(%rsp), %r15d
movslq %r15d, %rax
movsd (%r14,%rax), %xmm0
movss 0x8(%r14,%rax), %xmm1
callq 0x7c88
movaps %xmm0, 0xe0(%rsp)
movss %xmm1, 0x8(%rsp)
leaq 0x208(%rsp), %rax
movsd 0x18(%rax), %xmm2
movaps %xmm2, 0x20(%rsp)
movss 0x20(%rax), %xmm3
movss %xmm3, 0x10(%rsp)
movsd 0x24(%rax), %xmm4
movaps %xmm4, 0x40(%rsp)
movss 0x2c(%rax), %xmm5
movss %xmm5, 0xa0(%rsp)
movsd 0x30(%rax), %xmm6
movaps %xmm6, 0x60(%rsp)
movss 0x38(%rax), %xmm7
movss %xmm7, 0x50(%rsp)
movaps 0xd0(%rsp), %xmm0
movss 0x4(%rsp), %xmm1
callq 0x7d08
movaps %xmm0, 0x130(%rsp)
movss %xmm1, 0x88(%rsp)
movaps 0xc0(%rsp), %xmm0
movss 0x78(%rsp), %xmm1
movaps 0x20(%rsp), %xmm2
movss 0x10(%rsp), %xmm3
movaps 0x40(%rsp), %xmm4
movss 0xa0(%rsp), %xmm5
movaps 0x60(%rsp), %xmm6
movss 0x50(%rsp), %xmm7
callq 0x7d08
movaps %xmm0, 0x120(%rsp)
movss %xmm1, 0x84(%rsp)
movaps 0xe0(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
movaps 0x20(%rsp), %xmm2
movss 0x10(%rsp), %xmm3
movaps 0x40(%rsp), %xmm4
movss 0xa0(%rsp), %xmm5
movaps 0x60(%rsp), %xmm6
movss 0x50(%rsp), %xmm7
callq 0x7d08
movq 0x38(%rsp), %r8
leaq 0x1b0(%rsp), %rsi
movaps %xmm0, 0x110(%rsp)
movss %xmm1, 0x80(%rsp)
movb $0x1, %cl
movb $0x1, %al
jmp 0x8b30
movb $0x1, %cl
xorl %eax, %eax
jmp 0x8b30