// File: /workspace/github_c_makefile_star2_O2/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 0x9d06
movq 0x8(%rsi), %rdi
callq 0x81c3
movq %rax, %r12
movq %rax, %rdi
callq 0x8651
pushq $0x58
popq %rdi
callq 0x7b51
movq %rax, %r14
movq 0x18(%r12), %r15
xorl %ebp, %ebp
leaq 0xd0(%rsp), %rbx
movl 0x8(%r15), %edx
cmpl %edx, %ebp
jge 0x964d
movl %ebp, 0x124(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7b79
incl %ebp
jmp 0x962f
movq (%r12), %rax
movl 0x8(%rax), %esi
leaq 0x284c(%rip), %rdi      # 0xbea7
xorl %eax, %eax
callq 0x3060
leaq 0x2855(%rip), %rdx      # 0xbebe
pushq $0x10
popq %rcx
movl $0x140, %edi            # imm = 0x140
movl $0xf0, %esi
callq 0x5ecc
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 0x3510
movq %rax, %rbx
movq %rax, 0x40(%r13)
xorps %xmm0, %xmm0
movss %xmm0, 0x4c(%rsp)
movss 0x939(%rip), %xmm2      # 0xa004
pushq $0x1
popq %rax
movss %xmm0, 0x48(%rsp)
movl $0x0, 0x68(%rsp)
movl $0x0, 0x78(%rsp)
xorl %r13d, %r13d
movl %eax, 0x50(%rsp)
movl %eax, 0x9c(%rsp)
movl $0x0, 0x6c(%rsp)
movl $0x0, 0x98(%rsp)
movl $0x0, 0x94(%rsp)
movl $0x0, 0x7c(%rsp)
movss %xmm2, 0x80(%rsp)
movq %r15, %rdi
callq 0x66c9
testl %eax, %eax
jne 0x9cd2
movq %r15, %rdi
movl $0xa4, %esi
callq 0x66f4
testl %eax, %eax
jne 0x9cd2
xorl %eax, %eax
callq 0x70b5
movss 0x80(%rsp), %xmm2
addss %xmm0, %xmm2
cvtss2sd %xmm2, %xmm0
movsd 0x265a(%rip), %xmm1     # 0xbdc0
ucomisd %xmm0, %xmm1
ja 0x9718
movl %ebp, 0x80(%rsp)
movq %r15, %rdi
movl $0xaa, %esi
callq 0x6746
movl %eax, %ebp
testl %eax, %eax
movl %r13d, 0x64(%rsp)
je 0x979f
movss 0x48(%rsp), %xmm0
addss 0x25a3(%rip), %xmm0     # 0xbd3c
movss %xmm0, 0x48(%rsp)
movq %r15, %rdi
movl $0xac, %esi
callq 0x6746
movl %eax, %r13d
testl %eax, %eax
movq %rbx, 0xa0(%rsp)
je 0x97cf
movss 0x48(%rsp), %xmm0
addss 0x2577(%rip), %xmm0     # 0xbd40
movss %xmm0, 0x48(%rsp)
movq %r15, %rdi
movl $0xad, %esi
callq 0x6746
movl %eax, %ebx
testl %eax, %eax
je 0x97f6
movss 0x4c(%rsp), %xmm0
addss 0x254c(%rip), %xmm0     # 0xbd3c
movss %xmm0, 0x4c(%rsp)
orl %ebp, %r13d
movq %r15, %rdi
movl $0xab, %esi
callq 0x6746
orl %eax, %ebx
orl %r13d, %ebx
pushq $0x1
popq %rcx
movl 0x50(%rsp), %edx
cmovnel %ecx, %edx
testl %eax, %eax
movq %r12, 0xa8(%rsp)
movl %edx, 0x50(%rsp)
je 0x9839
movss 0x4c(%rsp), %xmm0
addss 0x250d(%rip), %xmm0     # 0xbd40
movss %xmm0, 0x4c(%rsp)
movq %r15, %rdi
pushq $0x57
popq %rsi
callq 0x66f4
movl %eax, %ebp
testl %eax, %eax
setne 0x60(%rsp)
movq %r15, %rdi
pushq $0x5a
popq %rsi
callq 0x66f4
orl %eax, %ebp
testl %eax, %eax
setne 0x74(%rsp)
movq %r15, %rdi
pushq $0x50
popq %rsi
callq 0x66f4
orl %eax, %ebp
testl %eax, %eax
setne 0xc0(%rsp)
movq %r15, %rdi
pushq $0x43
popq %rsi
callq 0x66f4
movl %eax, %ebx
xorl %r13d, %r13d
testl %eax, %eax
setne 0x70(%rsp)
movq %r15, %rdi
pushq $0x4a
popq %rsi
callq 0x66f4
orl %eax, %ebx
xorl %r12d, %r12d
testl %eax, %eax
setne 0x5f(%rsp)
orl %ebp, %ebx
pushq $0x1
popq %rbx
movl 0x50(%rsp), %ebp
cmovnel %ebx, %ebp
movq %r15, %rdi
pushq $0x46
popq %rsi
callq 0x66f4
testl %eax, %eax
je 0x98d0
movq 0xa8(%rsp), %rdi
callq 0x8865
movl %ebx, %ebp
movl %ebp, 0x50(%rsp)
movq %r15, %rdi
pushq $0x52
popq %rsi
callq 0x66f4
movl %eax, %ebp
testl %eax, %eax
je 0x98f4
xorps %xmm0, %xmm0
movss %xmm0, 0x48(%rsp)
movss %xmm0, 0x4c(%rsp)
xorl %eax, %eax
movb 0x60(%rsp), %cl
movb %cl, %al
xorl %eax, 0x80(%rsp)
xorl %ecx, %ecx
movb 0x74(%rsp), %al
movb %al, %cl
movl 0x6c(%rsp), %eax
movl %ecx, 0x74(%rsp)
xorl %ecx, %eax
movl %eax, 0x60(%rsp)
xorl %eax, %eax
movb 0xc0(%rsp), %cl
movb %cl, %al
xorl %eax, 0x98(%rsp)
movb 0x70(%rsp), %al
movb %al, %r13b
xorl %r13d, 0x94(%rsp)
movb 0x5f(%rsp), %al
movb %al, %r12b
xorl %r12d, 0x9c(%rsp)
movq %r15, %rdi
pushq $0x31
popq %rsi
callq 0x66f4
movl %eax, %ebx
xorl %eax, %eax
testl %ebx, %ebx
movl 0x7c(%rsp), %r12d
cmovnel %eax, %r12d
movq %r15, %rdi
pushq $0x32
popq %rsi
callq 0x66f4
movl %eax, %r13d
testl %eax, %eax
pushq $0x1
popq %rax
cmovnel %eax, %r12d
movq %r15, %rdi
pushq $0x33
popq %rsi
callq 0x66f4
orl %eax, %r13d
testl %eax, %eax
pushq $0x2
popq %rax
cmovnel %eax, %r12d
movl %r12d, 0x7c(%rsp)
orl %ebx, %r13d
orl %ebp, %r13d
sete %bl
movq %r15, %rdi
leaq 0xbc(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
leaq 0xb4(%rsp), %rcx
callq 0x7109
testb $0x1, 0xb4(%rsp)
jne 0x99e0
leaq 0xd0(%rsp), %r13
movl 0x80(%rsp), %ebp
xorl %r12d, %r12d
jmp 0x9a71
movl 0xbc(%rsp), %eax
movl 0xb8(%rsp), %ecx
cmpl $0x0, 0x68(%rsp)
leaq 0xd0(%rsp), %r13
je 0x9a5e
movl 0x78(%rsp), %edx
subl %eax, %edx
cvtsi2ss %edx, %xmm0
movss 0x2331(%rip), %xmm1     # 0xbd40
mulss %xmm1, %xmm0
movss 0x48(%rsp), %xmm2
subss %xmm0, %xmm2
movss %xmm2, 0x48(%rsp)
movl 0x64(%rsp), %edx
subl %ecx, %edx
cvtsi2ss %edx, %xmm0
mulss %xmm1, %xmm0
movss 0x4c(%rsp), %xmm1
addss %xmm0, %xmm1
movaps %xmm1, %xmm0
pushq $0x1
popq %rdx
movl %edx, 0x68(%rsp)
movl %eax, 0x78(%rsp)
movl %ecx, 0x64(%rsp)
movq 0xa0(%rsp), %rbx
movl 0x80(%rsp), %ebp
jmp 0x9acd
movl %ecx, 0x64(%rsp)
movl %eax, 0x78(%rsp)
pushq $0x1
popq %r12
movl 0x80(%rsp), %ebp
cmpl $0x0, 0x50(%rsp)
sete %al
andb %al, %bl
cmpb $0x1, %bl
jne 0x9aba
movq %r15, %rdi
callq 0x6970
xorps %xmm2, %xmm2
movl $0x0, 0x50(%rsp)
movl %r12d, 0x68(%rsp)
movl 0x60(%rsp), %eax
movl %eax, 0x6c(%rsp)
movq 0xa8(%rsp), %r12
movq 0xa0(%rsp), %rbx
movl 0x64(%rsp), %r13d
jmp 0x9718
movl %r12d, 0x68(%rsp)
movq 0xa0(%rsp), %rbx
movss 0x4c(%rsp), %xmm0
movl 0x60(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl 0x98(%rsp), %eax
movl %eax, 0xd8(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0xdc(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0xe4(%rsp)
movl %ebp, 0xd0(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, 0xe0(%rsp)
maxss 0x2228(%rip), %xmm0     # 0xbd44
minss 0x2224(%rip), %xmm0     # 0xbd48
movss %xmm0, 0x4c(%rsp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
callq 0x3770
movsd %xmm0, 0x80(%rsp)
cvtss2sd 0x48(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
callq 0x3080
mulsd 0x80(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movsd 0x50(%rsp), %xmm0
callq 0x3080
cvtsd2ss %xmm0, %xmm0
xorps 0x221f(%rip), %xmm0     # 0xbda0
movaps 0x80(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps %xmm1, 0x80(%rsp)
movsd 0x50(%rsp), %xmm0
callq 0x3770
movsd %xmm0, 0x50(%rsp)
movsd 0xc0(%rsp), %xmm0
callq 0x3770
mulsd 0x50(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm3
movss %xmm3, 0x50(%rsp)
xorps %xmm1, %xmm1
movsd 0x1de2(%rip), %xmm0     # 0xb9b0
movaps 0x80(%rsp), %xmm2
callq 0x7cc4
callq 0x7c88
movaps %xmm0, %xmm2
movaps %xmm1, %xmm3
movaps 0x80(%rsp), %xmm0
movss 0x50(%rsp), %xmm1
movaps %xmm2, 0xc0(%rsp)
movss %xmm3, 0x70(%rsp)
callq 0x7cc4
movl 0x74(%rsp), %eax
cmpl %eax, 0x6c(%rsp)
movq 0xa8(%rsp), %r12
jne 0x9cb8
movaps 0xc0(%rsp), %xmm2
movlps %xmm2, 0xe8(%rsp)
movss 0x70(%rsp), %xmm2
movss %xmm2, 0xf0(%rsp)
movlps %xmm0, 0xf4(%rsp)
movss %xmm1, 0xfc(%rsp)
movaps 0x80(%rsp), %xmm0
movlps %xmm0, 0x100(%rsp)
movss 0x50(%rsp), %xmm0
movss %xmm0, 0x108(%rsp)
movq %r15, %rdi
movl $0xff000000, %esi       # imm = 0xFF000000
callq 0x3aae
pushq $0x9
popq %rcx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x91d8
movq %r15, %rdi
callq 0x6970
xorps %xmm2, %xmm2
movl $0x0, 0x50(%rsp)
movl 0x60(%rsp), %eax
movl %eax, 0x6c(%rsp)
movl 0x64(%rsp), %r13d
jmp 0x9718
xorl %eax, %eax
cmpq $0x12c00, %rax          # imm = 0x12C00
je 0x9c1d
movl $0x7f7fffff, (%rbx,%rax,4) # imm = 0x7F7FFFFF
incq %rax
jmp 0x9cba
movq %r15, %rdi
callq 0x5afa
movq %r12, %rdi
callq 0x8618
movq %r14, %rdi
callq 0x7b64
movq %rbx, %rdi
callq 0x3430
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 0x2181(%rip), %rdi      # 0xbe91
xorl %eax, %eax
callq 0x8031