// File: /workspace/github_c_makefile_star2_O3/nkanaev[P]tipsy/src/tipsy.c
// Function: main
int main(int argc, char **argv) {
  if (argc != 2) error("usage: %s path/to/obj", argv[0]);

  char *filepath = argv[1];
  Obj *obj = obj_readfile(filepath);
  obj_normalize(obj);

  list *sfaces = list_new(sizeof(Surface));
  for (int i = 0; i < obj->f->len; i++) {
    Surface sf;
    sf.idx = i;
    list_add(sfaces, &sf);
  }

  printf("%d vertices, %d faces\n", obj->v->len, obj->f->len);

  Tigr *screen = tigrWindow(WIDTH, HEIGHT, "tipsy", TIGR_FIXED | TIGR_RETINA);
  TPixel colorBlack = tigrRGB(0, 0, 0);

  State state = {
    .draw_wireframe=(obj->mtl == NULL),
    .use_zbuffer=0, .use_pcorrect=0,
    .inv_bculling=0, .jitter=1,
  };
  state.zbuff = malloc(sizeof(float) * (WIDTH * HEIGHT));

  Vec upward = {0, 1, 0};
  float rotX = 0, rotY = 0, sensitivity = 0.05;
  int mouseX, mouseY, mouseBtn, mousePrev = 0, mousePrevX = 0, mousePrevY = 0;

  float elapsed = 1;
  int input = 1;

  while (!tigrClosed(screen) && !tigrKeyDown(screen, TK_ESCAPE)) {
    // FPS cap
    elapsed += tigrTime();
    if (elapsed < 1.0/FPS) { continue; } else { elapsed = 0; }

    if (tigrKeyHeld(screen, TK_LEFT)  && (input = 1)) rotY -= sensitivity;
    if (tigrKeyHeld(screen, TK_RIGHT) && (input = 1)) rotY += sensitivity;
    if (tigrKeyHeld(screen, TK_DOWN)  && (input = 1)) rotX -= sensitivity;
    if (tigrKeyHeld(screen, TK_UP)    && (input = 1)) rotX += sensitivity;
    if (tigrKeyDown(screen, 'W') && (input = 1)) state.draw_wireframe ^= 1;
    if (tigrKeyDown(screen, 'Z') && (input = 1)) state.use_zbuffer ^= 1;
    if (tigrKeyDown(screen, 'P') && (input = 1)) state.use_pcorrect ^= 1;
    if (tigrKeyDown(screen, 'C') && (input = 1)) state.inv_bculling ^= 1;
    if (tigrKeyDown(screen, 'J') && (input = 1)) state.jitter ^= 1;
    if (tigrKeyDown(screen, 'F') && (input = 1)) obj_flip(obj);
    if (tigrKeyDown(screen, 'R') && (input = 1)) rotX = rotY = 0;
    if (tigrKeyDown(screen, '1') && (input = 1)) state.shading = SHADING_NONE;
    if (tigrKeyDown(screen, '2') && (input = 1)) state.shading = SHADING_FLAT;
    if (tigrKeyDown(screen, '3') && (input = 1)) state.shading = SHADING_GOURAUD;

    tigrMouse(screen, &mouseX, &mouseY, &mouseBtn);
    if (mouseBtn & 1) {
      if (mousePrev) {
        rotY -= (mousePrevX-mouseX)*sensitivity;
        rotX += (mousePrevY-mouseY)*sensitivity;
        input = 1;
      }
      mousePrevX = mouseX;
      mousePrevY = mouseY;
      mousePrev = 1;
    } else {
      mousePrev = 0;
    }

    if (!input) { tigrUpdate(screen); continue; } else { input = 0; }

    rotX = fminf(PI/2, fmaxf(rotX, -PI/2));

    Vec z = {cos(rotX)*sin(rotY), -sin(rotX), cos(rotX)*cos(rotY)};
    Vec x = vec_nrm(vec_cross(upward, z));
    Vec y = vec_cross(z, x);

    if (state.use_zbuffer)
      for (int i = 0; i < WIDTH*HEIGHT; state.zbuff[i++] = FLT_MAX);

    state.x = x; state.y = y; state.z = z;
    tigrClear(screen, colorBlack);
    draw(screen, obj, state, sfaces);
    tigrUpdate(screen);
  }

  tigrFree(screen);
  obj_del(obj);
  list_del(sfaces);
  free(state.zbuff);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
cmpl $0x2, %edi
jne 0xaac7
movq 0x8(%rsi), %rdi
callq 0x89f7
movq %rax, %r12
movq %rax, %rdi
callq 0x8f23
movl $0x1, %edi
movl $0x18, %esi
callq 0x3270
movq %rax, %r14
movl $0x58, 0x10(%rax)
movq 0x18(%r12), %r15
movl 0x8(%r15), %edx
testl %edx, %edx
jle 0xa366
xorl %ebp, %ebp
leaq 0xd0(%rsp), %rbx
movl %ebp, 0x124(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8383
incl %ebp
movl 0x8(%r15), %edx
cmpl %edx, %ebp
jl 0xa34a
movq (%r12), %rax
movl 0x8(%rax), %esi
leaq 0x2ba3(%rip), %rdi      # 0xcf17
xorl %eax, %eax
callq 0x3060
leaq 0x2bac(%rip), %rdx      # 0xcf2e
movl $0x140, %edi            # imm = 0x140
movl $0xf0, %esi
movl $0x10, %ecx
callq 0x5e49
movq %rax, %r15
xorps %xmm0, %xmm0
leaq 0xd0(%rsp), %r13
movaps %xmm0, 0x30(%r13)
movaps %xmm0, 0x20(%r13)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, (%r13)
xorl %ebp, %ebp
cmpq $0x0, 0x20(%r12)
sete %bpl
movl $0x4b000, %edi          # imm = 0x4B000
callq 0x34f0
movq %rax, %rbx
movq %rax, 0x40(%r13)
movss 0xce3(%rip), %xmm2      # 0xb0c0
xorpd %xmm1, %xmm1
movss %xmm1, 0x54(%rsp)
movss %xmm1, 0x50(%rsp)
movl $0x0, 0x74(%rsp)
movl $0x0, 0x6c(%rsp)
xorl %r13d, %r13d
movl $0x1, 0x58(%rsp)
movl $0x1, 0x8c(%rsp)
movl $0x0, 0x78(%rsp)
movl $0x0, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
movl $0x0, 0x80(%rsp)
movss %xmm2, 0x70(%rsp)
movq %r15, %rdi
callq 0x67b1
testl %eax, %eax
jne 0xaa8b
movq %r15, %rdi
movl $0xa4, %esi
callq 0x67fa
testl %eax, %eax
jne 0xaa8b
xorl %eax, %eax
callq 0x732b
movss 0x70(%rsp), %xmm2
addss %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
movsd 0x29a9(%rip), %xmm1     # 0xce30
ucomisd %xmm0, %xmm1
ja 0xa43c
movl %ebp, 0x70(%rsp)
movq %r15, %rdi
movl $0xaa, %esi
callq 0x6868
movl %eax, %ebp
testl %eax, %eax
movq %r12, 0x90(%rsp)
movq %rbx, 0xb0(%rsp)
movl %r13d, 0x68(%rsp)
je 0xa4cd
movss 0x50(%rsp), %xmm0
addss 0x28e5(%rip), %xmm0     # 0xcdac
movss %xmm0, 0x50(%rsp)
movq %r15, %rdi
movl $0xac, %esi
callq 0x6868
movl %eax, %r13d
testl %eax, %eax
je 0xa4f5
movss 0x50(%rsp), %xmm0
addss 0x28c1(%rip), %xmm0     # 0xcdb0
movss %xmm0, 0x50(%rsp)
movq %r15, %rdi
movl $0xad, %esi
callq 0x6868
movl %eax, %ebx
testl %eax, %eax
je 0xa51c
movss 0x54(%rsp), %xmm0
addss 0x2896(%rip), %xmm0     # 0xcdac
movss %xmm0, 0x54(%rsp)
orl %ebp, %r13d
movq %r15, %rdi
movl $0xab, %esi
callq 0x6868
orl %eax, %ebx
orl %r13d, %ebx
movl $0x1, %ecx
movl 0x58(%rsp), %edx
cmovnel %ecx, %edx
testl %eax, %eax
movl %edx, 0x58(%rsp)
je 0xa559
movss 0x54(%rsp), %xmm0
addss 0x285d(%rip), %xmm0     # 0xcdb0
movss %xmm0, 0x54(%rsp)
movq %r15, %rdi
movl $0x57, %esi
callq 0x67fa
movl %eax, %ebp
testl %eax, %eax
setne 0x64(%rsp)
movq %r15, %rdi
movl $0x5a, %esi
callq 0x67fa
orl %eax, %ebp
testl %eax, %eax
setne 0x7c(%rsp)
movq %r15, %rdi
movl $0x50, %esi
callq 0x67fa
orl %eax, %ebp
testl %eax, %eax
setne 0xa8(%rsp)
movq %r15, %rdi
movl $0x43, %esi
callq 0x67fa
movl %eax, %ebx
xorl %r13d, %r13d
testl %eax, %eax
setne 0xa0(%rsp)
movq %r15, %rdi
movl $0x4a, %esi
callq 0x67fa
orl %eax, %ebx
xorl %r12d, %r12d
testl %eax, %eax
setne 0x98(%rsp)
orl %ebp, %ebx
movl $0x1, %eax
movl 0x58(%rsp), %ebx
cmovnel %eax, %ebx
movq %r15, %rdi
movl $0x46, %esi
callq 0x67fa
testl %eax, %eax
je 0xa607
movq 0x90(%rsp), %rdi
callq 0x9144
movl $0x1, %ebx
movl %ebx, 0x58(%rsp)
movq %r15, %rdi
movl $0x52, %esi
callq 0x67fa
movl %eax, %ebp
testl %eax, %eax
je 0xa62d
xorps %xmm0, %xmm0
movss %xmm0, 0x50(%rsp)
movss %xmm0, 0x54(%rsp)
xorl %eax, %eax
movb 0x64(%rsp), %cl
movb %cl, %al
xorl %eax, 0x70(%rsp)
xorl %ecx, %ecx
movb 0x7c(%rsp), %al
movb %al, %cl
movl 0x78(%rsp), %eax
movl %ecx, 0x7c(%rsp)
xorl %ecx, %eax
movl %eax, 0x64(%rsp)
xorl %eax, %eax
movb 0xa8(%rsp), %cl
movb %cl, %al
xorl %eax, 0x88(%rsp)
movb 0xa0(%rsp), %al
movb %al, %r13b
xorl %r13d, 0x84(%rsp)
movb 0x98(%rsp), %al
movb %al, %r12b
xorl %r12d, 0x8c(%rsp)
movq %r15, %rdi
movl $0x31, %esi
callq 0x67fa
movl %eax, %ebx
xorl %eax, %eax
testl %ebx, %ebx
movl 0x80(%rsp), %r12d
cmovnel %eax, %r12d
movq %r15, %rdi
movl $0x32, %esi
callq 0x67fa
movl %eax, %r13d
testl %eax, %eax
movl $0x1, %eax
cmovnel %eax, %r12d
movq %r15, %rdi
movl $0x33, %esi
callq 0x67fa
orl %eax, %r13d
testl %eax, %eax
movl $0x2, %eax
cmovnel %eax, %r12d
movl %r12d, 0x80(%rsp)
orl %ebx, %r13d
orl %ebp, %r13d
sete %r13b
movq %r15, %rdi
leaq 0xc4(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
leaq 0xbc(%rsp), %rcx
callq 0x7382
testb $0x1, 0xbc(%rsp)
jne 0xa726
movl 0x68(%rsp), %ebx
movl 0x6c(%rsp), %ebp
xorl %r12d, %r12d
jmp 0xa7b5
movl 0xc4(%rsp), %ebp
movl 0xc0(%rsp), %ebx
cmpl $0x0, 0x74(%rsp)
je 0xa7af
movl 0x6c(%rsp), %eax
subl %ebp, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0x2660(%rip), %xmm1     # 0xcdb0
mulss %xmm1, %xmm0
movss 0x50(%rsp), %xmm2
subss %xmm0, %xmm2
movss %xmm2, 0x50(%rsp)
movl 0x68(%rsp), %eax
subl %ebx, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss %xmm1, %xmm0
movss 0x54(%rsp), %xmm1
addss %xmm0, %xmm1
movaps %xmm1, %xmm0
movl $0x1, 0x74(%rsp)
movl %ebp, 0x6c(%rsp)
movl %ebx, 0x68(%rsp)
movq 0x90(%rsp), %r12
movq 0xb0(%rsp), %rbx
leaq 0xd0(%rsp), %r13
jmp 0xa831
movl $0x1, %r12d
cmpl $0x0, 0x58(%rsp)
sete %al
andb %al, %r13b
cmpb $0x1, %r13b
jne 0xa806
movq %r15, %rdi
callq 0x6ad0
xorps %xmm2, %xmm2
movl $0x0, 0x58(%rsp)
movl %r12d, 0x74(%rsp)
movl %ebp, 0x6c(%rsp)
movl %ebx, %r13d
movl 0x64(%rsp), %eax
movl %eax, 0x78(%rsp)
movq 0x90(%rsp), %r12
movq 0xb0(%rsp), %rbx
movl 0x70(%rsp), %ebp
jmp 0xa43c
movl %r12d, 0x74(%rsp)
movl %ebp, 0x6c(%rsp)
movl %ebx, 0x68(%rsp)
movq 0x90(%rsp), %r12
movq 0xb0(%rsp), %rbx
leaq 0xd0(%rsp), %r13
movss 0x54(%rsp), %xmm0
movl 0x64(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl 0x88(%rsp), %eax
movl %eax, 0xd8(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0xdc(%rsp)
movl 0x80(%rsp), %eax
movl %eax, 0xe4(%rsp)
movl 0x70(%rsp), %ebp
movl %ebp, 0xd0(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0xe0(%rsp)
maxss 0x252d(%rip), %xmm0     # 0xcdb4
minss 0x2529(%rip), %xmm0     # 0xcdb8
movss %xmm0, 0x54(%rsp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
callq 0x3750
movsd %xmm0, 0xa8(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x50(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
callq 0x3080
movsd %xmm0, 0xa0(%rsp)
movsd 0x58(%rsp), %xmm0
callq 0x3080
movsd %xmm0, 0x98(%rsp)
movsd 0x58(%rsp), %xmm0
callq 0x3750
movsd %xmm0, 0x58(%rsp)
movsd 0xc8(%rsp), %xmm0
callq 0x3750
movl 0x7c(%rsp), %eax
cmpl %eax, 0x78(%rsp)
movapd 0x2510(%rip), %xmm1     # 0xce20
je 0xa925
xorl %eax, %eax
movupd %xmm1, (%rbx,%rax,4)
addq $0x4, %rax
cmpq $0x12c00, %rax          # imm = 0x12C00
jne 0xa914
movsd 0xa8(%rsp), %xmm1
mulsd 0xa0(%rsp), %xmm1
cvtsd2ss %xmm1, %xmm1
cvtsd2ss 0x98(%rsp), %xmm4
movsd 0x58(%rsp), %xmm2
mulsd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsd2ss %xmm2, %xmm0
movaps %xmm4, %xmm2
xorps %xmm6, %xmm6
mulss %xmm6, %xmm2
addss %xmm0, %xmm2
movaps %xmm0, %xmm5
movss 0x244e(%rip), %xmm7     # 0xcdbc
mulss %xmm7, %xmm5
movaps %xmm1, %xmm3
mulss %xmm6, %xmm3
addss %xmm5, %xmm3
movaps %xmm4, %xmm6
mulss %xmm7, %xmm6
subss %xmm1, %xmm6
movaps %xmm3, %xmm5
mulss %xmm3, %xmm5
movaps %xmm2, %xmm7
mulss %xmm2, %xmm7
addss %xmm5, %xmm7
movaps %xmm6, %xmm5
mulss %xmm6, %xmm5
addss %xmm7, %xmm5
xorps %xmm7, %xmm7
sqrtss %xmm5, %xmm7
movss 0x70c(%rip), %xmm5      # 0xb0c0
divss %xmm7, %xmm5
mulss %xmm5, %xmm2
movaps %xmm2, %xmm7
movaps %xmm0, %xmm8
mulss %xmm2, %xmm8
mulss %xmm4, %xmm2
xorps 0x65d(%rip), %xmm4      # 0xb030
mulss %xmm5, %xmm3
unpcklps %xmm3, %xmm7            # xmm7 = xmm7[0],xmm3[0],xmm7[1],xmm3[1]
mulss %xmm6, %xmm5
movaps %xmm0, %xmm6
mulss %xmm3, %xmm6
movaps %xmm5, %xmm9
mulss %xmm1, %xmm9
mulss %xmm1, %xmm3
unpcklps %xmm4, %xmm1            # xmm1 = xmm1[0],xmm4[0],xmm1[1],xmm4[1]
mulss %xmm5, %xmm4
subss %xmm6, %xmm4
subss %xmm9, %xmm8
unpcklps %xmm8, %xmm4            # xmm4 = xmm4[0],xmm8[0],xmm4[1],xmm8[1]
addss %xmm2, %xmm3
movlps %xmm7, 0xe8(%rsp)
movss %xmm5, 0xf0(%rsp)
movlps %xmm4, 0xf4(%rsp)
movss %xmm3, 0xfc(%rsp)
movlps %xmm1, 0x100(%rsp)
movss %xmm0, 0x108(%rsp)
movq %r15, %rdi
movl $0xff000000, %esi       # imm = 0xFF000000
callq 0x3aaf
movl $0x9, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x9c8e
movq %r15, %rdi
callq 0x6ad0
xorps %xmm2, %xmm2
movl $0x0, 0x58(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x78(%rsp)
movl 0x68(%rsp), %r13d
jmp 0xa43c
movq %r15, %rdi
callq 0x5a5d
movq %r12, %rdi
callq 0x8eb5
movq (%r14), %rdi
callq 0x3410
movq %r14, %rdi
callq 0x3410
movq %rbx, %rdi
callq 0x3410
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsi), %rsi
leaq 0x2430(%rip), %rdi      # 0xcf01
xorl %eax, %eax
callq 0x8859