// File: /workspace/github_c_makefile_star2_O1/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 $0x148, %rsp            # imm = 0x148
cmpl $0x2, %edi
jne 0xae26
movq 0x8(%rsi), %rdi
callq 0x88e8
movq %rax, %r13
movq %rax, %rdi
callq 0x8def
movl $0x1, %edi
movl $0x18, %esi
callq 0x3270
movq %rax, %r14
movl $0x58, 0x10(%rax)
movq 0x18(%r13), %r15
movl 0x8(%r15), %edx
testl %edx, %edx
jle 0xa651
xorl %ebp, %ebp
leaq 0xf0(%rsp), %rbx
movl %ebp, 0x144(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x821f
incl %ebp
movl 0x8(%r15), %edx
cmpl %edx, %ebp
jl 0xa635
movq (%r13), %rax
movl 0x8(%rax), %esi
leaq 0x2846(%rip), %rdi      # 0xcea5
xorl %eax, %eax
callq 0x3060
leaq 0x284f(%rip), %rdx      # 0xcebc
movl $0x140, %edi            # imm = 0x140
movl $0xf0, %esi
movl $0x10, %ecx
callq 0x5d3c
movq %rax, %r15
xorps %xmm0, %xmm0
leaq 0xf0(%rsp), %rbx
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
xorl %eax, %eax
cmpq $0x0, 0x20(%r13)
movq $0x0, 0x40(%rbx)
sete %al
movl %eax, (%rbx)
movl $0x1, 0x10(%rbx)
movl $0x4b000, %edi          # imm = 0x4B000
callq 0x34f0
movq %rax, %rbp
movq %rax, 0x40(%rbx)
movl (%rbx), %eax
movl %eax, 0x7c(%rsp)
movl 0x4(%rbx), %eax
movl %eax, 0x60(%rsp)
movl 0x10(%rbx), %eax
movl %eax, 0x78(%rsp)
movl 0x8(%rbx), %eax
movl %eax, 0x74(%rsp)
movl 0xc(%rbx), %eax
movl %eax, 0x70(%rsp)
movss 0x910(%rip), %xmm2      # 0xb004
movl 0x14(%rbx), %ebx
xorpd %xmm1, %xmm1
movss %xmm1, 0x50(%rsp)
movss %xmm1, 0x54(%rsp)
movl $0x0, 0x68(%rsp)
movl $0x0, 0x80(%rsp)
movl $0x0, 0x84(%rsp)
movl $0x1, %r12d
movss %xmm2, 0x64(%rsp)
movq %r15, %rdi
callq 0x66ab
testl %eax, %eax
jne 0xadac
movq %r15, %rdi
movl $0xa4, %esi
callq 0x66f4
testl %eax, %eax
jne 0xadac
xorl %eax, %eax
callq 0x7200
movss 0x64(%rsp), %xmm2
addss %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
movsd 0x263a(%rip), %xmm1     # 0xcdb0
ucomisd %xmm0, %xmm1
ja 0xa72b
movq %rbp, 0x90(%rsp)
movq %r15, %rdi
movl $0xaa, %esi
callq 0x6762
movl %eax, %ebp
testl %eax, %eax
movl %ebx, 0x5c(%rsp)
je 0xa7af
movss 0x54(%rsp), %xmm0
addss 0x25c3(%rip), %xmm0     # 0xcd6c
movss %xmm0, 0x54(%rsp)
movq %r15, %rdi
movl $0xac, %esi
callq 0x6762
movl %eax, %ebx
testl %eax, %eax
movq %r13, 0xa0(%rsp)
je 0xa7de
movss 0x54(%rsp), %xmm0
addss 0x2598(%rip), %xmm0     # 0xcd70
movss %xmm0, 0x54(%rsp)
movq %r15, %rdi
movl $0xad, %esi
callq 0x6762
movl %eax, %r13d
testl %eax, %eax
je 0xa806
movss 0x50(%rsp), %xmm0
addss 0x256c(%rip), %xmm0     # 0xcd6c
movss %xmm0, 0x50(%rsp)
orl %ebp, %ebx
movq %r15, %rdi
movl $0xab, %esi
callq 0x6762
orl %eax, %r13d
orl %ebx, %r13d
movl $0x1, %ecx
cmovnel %ecx, %r12d
testl %eax, %eax
je 0xa83c
movss 0x50(%rsp), %xmm0
addss 0x253a(%rip), %xmm0     # 0xcd70
movss %xmm0, 0x50(%rsp)
movq %r15, %rdi
movl $0x57, %esi
callq 0x66f4
movl %eax, %ebp
testl %eax, %eax
setne 0xb0(%rsp)
movq %r15, %rdi
movl $0x5a, %esi
callq 0x66f4
orl %eax, %ebp
testl %eax, %eax
setne 0xe0(%rsp)
movq %r15, %rdi
movl $0x50, %esi
callq 0x66f4
orl %eax, %ebp
testl %eax, %eax
setne 0xd0(%rsp)
movq %r15, %rdi
movl $0x43, %esi
callq 0x66f4
movl %eax, %ebx
testl %eax, %eax
setne 0x5b(%rsp)
movq %r15, %rdi
movl $0x4a, %esi
callq 0x66f4
orl %eax, %ebx
testl %eax, %eax
setne 0x5a(%rsp)
orl %ebp, %ebx
movl $0x1, %eax
cmovnel %eax, %r12d
movq %r15, %rdi
movl $0x46, %esi
callq 0x66f4
testl %eax, %eax
je 0xa8e2
movq 0xa0(%rsp), %rdi
callq 0x9109
movl $0x1, %r12d
movl %r12d, 0xc0(%rsp)
movq %r15, %rdi
movl $0x52, %esi
callq 0x66f4
movl %eax, %ebp
testl %eax, %eax
movl 0x5c(%rsp), %ebx
je 0xa910
xorps %xmm0, %xmm0
movss %xmm0, 0x54(%rsp)
movss %xmm0, 0x50(%rsp)
movq %r15, %rdi
movl $0x31, %esi
callq 0x66f4
orl %eax, %ebp
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, 0x64(%rsp)
cmovnel %ecx, %ebx
movq %r15, %rdi
movl $0x32, %esi
callq 0x66f4
movl %eax, %r13d
testl %eax, %eax
movl $0x1, %r12d
cmovnel %r12d, %ebx
movq %r15, %rdi
movl $0x33, %esi
callq 0x66f4
orl %eax, %r13d
testl %eax, %eax
movl $0x2, %eax
cmovnel %eax, %ebx
movl %ebx, 0x5c(%rsp)
orl %ebp, %r13d
movl 0xc0(%rsp), %r13d
cmovnel %r12d, %r13d
movq %r15, %rdi
leaq 0x8c(%rsp), %rsi
leaq 0x88(%rsp), %rdx
leaq 0xac(%rsp), %rcx
callq 0x7257
testb $0x1, 0xac(%rsp)
jne 0xa9c1
movq 0x90(%rsp), %rbp
movl 0x7c(%rsp), %eax
movl 0x78(%rsp), %ecx
movl 0x74(%rsp), %edx
movl 0x70(%rsp), %esi
xorl %r12d, %r12d
jmp 0xaa6a
cmpl $0x0, 0x68(%rsp)
movq 0x90(%rsp), %rbp
movl 0x7c(%rsp), %eax
movl 0x78(%rsp), %ecx
movl 0x74(%rsp), %edx
movl 0x70(%rsp), %esi
movl 0x84(%rsp), %edi
movl $0x0, %r12d
je 0xaa46
movl 0x80(%rsp), %r8d
subl 0x8c(%rsp), %r8d
xorps %xmm0, %xmm0
cvtsi2ss %r8d, %xmm0
subl 0x88(%rsp), %edi
movss 0x235c(%rip), %xmm2     # 0xcd70
mulss %xmm2, %xmm0
cvtsi2ss %edi, %xmm1
movss 0x54(%rsp), %xmm3
subss %xmm0, %xmm3
movss %xmm3, 0x54(%rsp)
mulss %xmm2, %xmm1
movss 0x50(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x50(%rsp)
movl $0x1, %r13d
movl 0x8c(%rsp), %edi
movl %edi, 0x80(%rsp)
movl $0x1, 0x64(%rsp)
movl 0x88(%rsp), %edi
movl %edi, 0x84(%rsp)
xorl %edi, %edi
movb 0xb0(%rsp), %r8b
movb %r8b, %dil
xorl %edi, %eax
movb 0xe0(%rsp), %dil
movb %dil, %r12b
movl 0x60(%rsp), %ebx
xorl %r12d, %ebx
xorl %edi, %edi
movb 0xd0(%rsp), %r8b
movb %r8b, %dil
xorl %edi, %edx
xorl %edi, %edi
movb 0x5b(%rsp), %r8b
movb %r8b, %dil
xorl %edi, %esi
xorl %edi, %edi
movb 0x5a(%rsp), %r8b
movb %r8b, %dil
xorl %edi, %ecx
testl %r13d, %r13d
movl %eax, 0x7c(%rsp)
movl %ecx, 0x78(%rsp)
movl %edx, 0x74(%rsp)
movl %esi, 0x70(%rsp)
je 0xac1e
movl %ebx, 0xf4(%rsp)
movl %edx, 0xf8(%rsp)
movl %esi, 0xfc(%rsp)
movl 0x5c(%rsp), %edx
movl %edx, 0x104(%rsp)
movl %eax, 0xf0(%rsp)
movl %ecx, 0x100(%rsp)
movss 0x50(%rsp), %xmm0
maxss 0x226d(%rip), %xmm0     # 0xcd74
minss 0x2269(%rip), %xmm0     # 0xcd78
movss %xmm0, 0x50(%rsp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
callq 0x3760
movsd %xmm0, 0x90(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x54(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
callq 0x3080
mulsd 0x90(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movsd 0x68(%rsp), %xmm0
callq 0x3080
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movsd 0x68(%rsp), %xmm0
callq 0x3760
movsd %xmm0, 0x68(%rsp)
movsd 0xb0(%rsp), %xmm0
callq 0x3760
movaps 0xc0(%rsp), %xmm6
movaps 0x90(%rsp), %xmm5
mulsd 0x68(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm7
movaps %xmm6, %xmm8
xorps %xmm2, %xmm2
mulss %xmm2, %xmm8
addss %xmm7, %xmm8
movaps %xmm7, %xmm0
movss 0x21b7(%rip), %xmm1     # 0xcd7c
mulss %xmm1, %xmm0
movaps %xmm5, %xmm9
mulss %xmm2, %xmm9
addss %xmm0, %xmm9
movaps %xmm6, %xmm10
mulss %xmm1, %xmm10
subss %xmm5, %xmm10
movaps %xmm9, %xmm0
mulss %xmm9, %xmm0
movaps %xmm8, %xmm1
mulss %xmm8, %xmm1
addss %xmm0, %xmm1
movaps %xmm10, %xmm0
mulss %xmm10, %xmm0
addss %xmm1, %xmm0
ucomiss %xmm2, %xmm0
movq 0xa0(%rsp), %r13
jb 0xac49
sqrtss %xmm0, %xmm0
jmp 0xaca0
movq %r15, %rdi
callq 0x69ca
xorps %xmm2, %xmm2
xorl %r12d, %r12d
movl 0x64(%rsp), %eax
movl %eax, 0x68(%rsp)
movl %ebx, 0x60(%rsp)
movq 0xa0(%rsp), %r13
movl 0x5c(%rsp), %ebx
jmp 0xa72b
movss %xmm7, 0x68(%rsp)
movaps %xmm8, 0xb0(%rsp)
movaps %xmm9, 0xe0(%rsp)
movaps %xmm10, 0xd0(%rsp)
callq 0x3730
movaps 0xd0(%rsp), %xmm10
movaps 0xe0(%rsp), %xmm9
movaps 0xb0(%rsp), %xmm8
movss 0x68(%rsp), %xmm7
movaps 0xc0(%rsp), %xmm6
movaps 0x90(%rsp), %xmm5
cmpl %r12d, 0x60(%rsp)
je 0xacbc
xorl %eax, %eax
movl $0x7f7fffff, (%rbp,%rax,4) # imm = 0x7F7FFFFF
incq %rax
cmpq $0x12c00, %rax          # imm = 0x12C00
jne 0xaca9
movaps %xmm6, %xmm1
xorps 0x20da(%rip), %xmm1     # 0xcda0
movaps %xmm5, %xmm2
unpcklps %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
movss 0x330(%rip), %xmm3      # 0xb004
divss %xmm0, %xmm3
mulss %xmm3, %xmm8
mulss %xmm3, %xmm9
movaps %xmm8, %xmm0
unpcklps %xmm9, %xmm0            # xmm0 = xmm0[0],xmm9[0],xmm0[1],xmm9[1]
mulss %xmm3, %xmm10
movaps %xmm7, %xmm3
mulss %xmm9, %xmm3
mulss %xmm10, %xmm1
subss %xmm3, %xmm1
movaps %xmm10, %xmm3
mulss %xmm5, %xmm3
movaps %xmm7, %xmm4
mulss %xmm8, %xmm4
subss %xmm3, %xmm4
unpcklps %xmm4, %xmm1            # xmm1 = xmm1[0],xmm4[0],xmm1[1],xmm4[1]
mulss %xmm6, %xmm8
mulss %xmm5, %xmm9
addss %xmm8, %xmm9
movlps %xmm0, 0x108(%rsp)
movss %xmm10, 0x110(%rsp)
movlps %xmm1, 0x114(%rsp)
movss %xmm9, 0x11c(%rsp)
movlps %xmm2, 0x120(%rsp)
movss %xmm7, 0x128(%rsp)
movq %r15, %rdi
movl $0xff000000, %esi       # imm = 0xFF000000
callq 0x3aa9
movl $0x9, %ecx
movq %rsp, %rdi
leaq 0xf0(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x9e5b
movq %r15, %rdi
callq 0x69ca
xorps %xmm2, %xmm2
xorl %r12d, %r12d
movl 0x64(%rsp), %eax
movl %eax, 0x68(%rsp)
movl %ebx, 0x60(%rsp)
movl 0x5c(%rsp), %ebx
jmp 0xa72b
movl 0x60(%rsp), %eax
movl %eax, 0xf4(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0xf8(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0xfc(%rsp)
movl %ebx, 0x104(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0xf0(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x100(%rsp)
movq %r15, %rdi
callq 0x5937
movq %r13, %rdi
callq 0x8d81
movq (%r14), %rdi
callq 0x3410
movq %r14, %rdi
callq 0x3410
movq %rbp, %rdi
callq 0x3410
xorl %eax, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsi), %rsi
leaq 0x205f(%rip), %rdi      # 0xce8f
xorl %eax, %eax
callq 0x874c