// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0xe0b4
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x27fa(%rip), %rdi      # 0x108a7
movb $0x0, %al
callq 0xb810
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0xba90
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0xbfa0
movl $0x58, %edi
callq 0xaaa0
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x20(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpl 0x8(%rcx), %eax
jge 0xe11c
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x84(%rbp), %rsi
callq 0xab10
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xe0eb
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0x8(%rax), %esi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x8(%rax), %edx
leaq 0x2785(%rip), %rdi      # 0x108bd
movb $0x0, %al
callq 0x3060
movl $0x140, %edi            # imm = 0x140
movl $0xf0, %esi
leaq 0x2784(%rip), %rdx      # 0x108d4
movl $0x10, %ecx
callq 0x6eb0
movq %rax, -0x90(%rbp)
xorl %edx, %edx
movl %edx, %edi
movl %edx, %esi
callq 0xe050
movl %eax, -0x94(%rbp)
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x48, %edx
callq 0x31d0
movq -0x20(%rbp), %rax
cmpq $0x0, 0x20(%rax)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0xe0(%rbp)
movl $0x1, -0xd0(%rbp)
movl $0x4b000, %edi          # imm = 0x4B000
callq 0x34f0
movq %rax, -0xa0(%rbp)
movq 0x274e(%rip), %rax      # 0x1090c
movq %rax, -0xec(%rbp)
movl 0x2749(%rip), %eax      # 0x10914
movl %eax, -0xe4(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0xf0(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0xf4(%rbp)
movss 0x25ad(%rip), %xmm0     # 0x1079c
movss %xmm0, -0xf8(%rbp)
movl $0x0, -0x108(%rbp)
movl $0x0, -0x10c(%rbp)
movl $0x0, -0x110(%rbp)
movss 0xde7(%rip), %xmm0      # 0xf004
movss %xmm0, -0x114(%rbp)
movl $0x1, -0x118(%rbp)
movq -0x90(%rbp), %rdi
callq 0x7990
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x1d5(%rbp)
jne 0xe269
movq -0x90(%rbp), %rdi
movl $0xa4, %esi
callq 0x79f0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x1d5(%rbp)
movb -0x1d5(%rbp), %al
testb $0x1, %al
jne 0xe278
jmp 0xea1f
movb $0x0, %al
callq 0x8b40
addss -0x114(%rbp), %xmm0
movss %xmm0, -0x114(%rbp)
movss -0x114(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x2515(%rip), %xmm0     # 0x107b8
ucomisd %xmm1, %xmm0
jbe 0xe2ab
jmp 0xe22f
xorps %xmm0, %xmm0
movss %xmm0, -0x114(%rbp)
movq -0x90(%rbp), %rdi
movl $0xaa, %esi
callq 0x7a80
cmpl $0x0, %eax
je 0xe2fa
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe2de
jmp 0xe2fa
movss -0xf8(%rbp), %xmm1
movss -0xf4(%rbp), %xmm0
subss %xmm1, %xmm0
movss %xmm0, -0xf4(%rbp)
movq -0x90(%rbp), %rdi
movl $0xac, %esi
callq 0x7a80
cmpl $0x0, %eax
je 0xe33a
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe322
jmp 0xe33a
movss -0xf8(%rbp), %xmm0
addss -0xf4(%rbp), %xmm0
movss %xmm0, -0xf4(%rbp)
movq -0x90(%rbp), %rdi
movl $0xad, %esi
callq 0x7a80
cmpl $0x0, %eax
je 0xe37e
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe362
jmp 0xe37e
movss -0xf8(%rbp), %xmm1
movss -0xf0(%rbp), %xmm0
subss %xmm1, %xmm0
movss %xmm0, -0xf0(%rbp)
movq -0x90(%rbp), %rdi
movl $0xab, %esi
callq 0x7a80
cmpl $0x0, %eax
je 0xe3be
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe3a6
jmp 0xe3be
movss -0xf8(%rbp), %xmm0
addss -0xf0(%rbp), %xmm0
movss %xmm0, -0xf0(%rbp)
movq -0x90(%rbp), %rdi
movl $0x57, %esi
callq 0x79f0
cmpl $0x0, %eax
je 0xe3f5
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe3e6
jmp 0xe3f5
movl -0xe0(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0xe0(%rbp)
movq -0x90(%rbp), %rdi
movl $0x5a, %esi
callq 0x79f0
cmpl $0x0, %eax
je 0xe42c
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe41d
jmp 0xe42c
movl -0xdc(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0xdc(%rbp)
movq -0x90(%rbp), %rdi
movl $0x50, %esi
callq 0x79f0
cmpl $0x0, %eax
je 0xe463
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe454
jmp 0xe463
movl -0xd8(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0xd8(%rbp)
movq -0x90(%rbp), %rdi
movl $0x43, %esi
callq 0x79f0
cmpl $0x0, %eax
je 0xe49a
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe48b
jmp 0xe49a
movl -0xd4(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0xd4(%rbp)
movq -0x90(%rbp), %rdi
movl $0x4a, %esi
callq 0x79f0
cmpl $0x0, %eax
je 0xe4d1
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe4c2
jmp 0xe4d1
movl -0xd0(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0xd0(%rbp)
movq -0x90(%rbp), %rdi
movl $0x46, %esi
callq 0x79f0
cmpl $0x0, %eax
je 0xe502
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe4f9
jmp 0xe502
movq -0x20(%rbp), %rdi
callq 0xc3d0
movq -0x90(%rbp), %rdi
movl $0x52, %esi
callq 0x79f0
cmpl $0x0, %eax
je 0xe540
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe52a
jmp 0xe540
xorps %xmm0, %xmm0
movss %xmm0, -0xf4(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0xf0(%rbp)
movq -0x90(%rbp), %rdi
movl $0x31, %esi
callq 0x79f0
cmpl $0x0, %eax
je 0xe572
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe568
jmp 0xe572
movl $0x0, -0xcc(%rbp)
movq -0x90(%rbp), %rdi
movl $0x32, %esi
callq 0x79f0
cmpl $0x0, %eax
je 0xe5a4
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe59a
jmp 0xe5a4
movl $0x1, -0xcc(%rbp)
movq -0x90(%rbp), %rdi
movl $0x33, %esi
callq 0x79f0
cmpl $0x0, %eax
je 0xe5d6
movl $0x1, -0x118(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0xe5cc
jmp 0xe5d6
movl $0x2, -0xcc(%rbp)
movq -0x90(%rbp), %rdi
leaq -0xfc(%rbp), %rsi
leaq -0x100(%rbp), %rdx
leaq -0x104(%rbp), %rcx
callq 0x8bd0
movl -0x104(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xe6ad
cmpl $0x0, -0x108(%rbp)
je 0xe689
movl -0x10c(%rbp), %eax
subl -0xfc(%rbp), %eax
cvtsi2ss %eax, %xmm0
movss -0xf8(%rbp), %xmm2
movss -0xf4(%rbp), %xmm1
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0xf4(%rbp)
movl -0x110(%rbp), %eax
subl -0x100(%rbp), %eax
cvtsi2ss %eax, %xmm0
movss -0xf8(%rbp), %xmm2
movss -0xf0(%rbp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0xf0(%rbp)
movl $0x1, -0x118(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, -0x10c(%rbp)
movl -0x100(%rbp), %eax
movl %eax, -0x110(%rbp)
movl $0x1, -0x108(%rbp)
jmp 0xe6b7
movl $0x0, -0x108(%rbp)
cmpl $0x0, -0x118(%rbp)
jne 0xe6d1
movq -0x90(%rbp), %rdi
callq 0x8220
jmp 0xe22f
movl $0x0, -0x118(%rbp)
movss -0xf0(%rbp), %xmm0
movss 0x20b5(%rip), %xmm1     # 0x107a0
maxss %xmm1, %xmm0
movss 0x20ad(%rip), %xmm1     # 0x107a4
minss %xmm1, %xmm0
movss %xmm0, -0xf0(%rbp)
movss -0xf0(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x3760
movsd %xmm0, -0x1e8(%rbp)
movss -0xf4(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x3080
movaps %xmm0, %xmm1
movsd -0x1e8(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x124(%rbp)
movss -0xf0(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x3080
movaps 0x2060(%rip), %xmm1     # 0x107c0
pxor %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x120(%rbp)
movss -0xf0(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x3760
movsd %xmm0, -0x1e0(%rbp)
movss -0xf4(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x3760
movaps %xmm0, %xmm1
movsd -0x1e0(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x11c(%rbp)
movl -0xe4(%rbp), %eax
movl %eax, -0x148(%rbp)
movq -0xec(%rbp), %rax
movq %rax, -0x150(%rbp)
movsd -0x150(%rbp), %xmm0
movss -0x148(%rbp), %xmm1
movl -0x11c(%rbp), %eax
movl %eax, -0x158(%rbp)
movq -0x124(%rbp), %rax
movq %rax, -0x160(%rbp)
movsd -0x160(%rbp), %xmm2
movss -0x158(%rbp), %xmm3
callq 0xb060
movss %xmm1, -0x168(%rbp)
movlpd %xmm0, -0x170(%rbp)
movl -0x168(%rbp), %eax
movl %eax, -0x138(%rbp)
movq -0x170(%rbp), %rax
movq %rax, -0x140(%rbp)
movl -0x138(%rbp), %eax
movl %eax, -0x178(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x180(%rbp)
movsd -0x180(%rbp), %xmm0
movss -0x178(%rbp), %xmm1
callq 0xaf60
movss %xmm1, -0x188(%rbp)
movlpd %xmm0, -0x190(%rbp)
movl -0x188(%rbp), %eax
movl %eax, -0x128(%rbp)
movq -0x190(%rbp), %rax
movq %rax, -0x130(%rbp)
movl -0x11c(%rbp), %eax
movl %eax, -0x1a8(%rbp)
movq -0x124(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movsd -0x1b0(%rbp), %xmm0
movss -0x1a8(%rbp), %xmm1
movl -0x128(%rbp), %eax
movl %eax, -0x1b8(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movsd -0x1c0(%rbp), %xmm2
movss -0x1b8(%rbp), %xmm3
callq 0xb060
movss %xmm1, -0x1c8(%rbp)
movlpd %xmm0, -0x1d0(%rbp)
movq -0x1d0(%rbp), %rax
movq %rax, -0x19c(%rbp)
movl -0x1c8(%rbp), %eax
movl %eax, -0x194(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xe961
movl $0x0, -0x1d4(%rbp)
cmpl $0x12c00, -0x1d4(%rbp)  # imm = 0x12C00
jge 0xe95f
jmp 0xe935
movq -0xa0(%rbp), %rax
movl -0x1d4(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x1d4(%rbp)
movslq %ecx, %rcx
movss 0x1e50(%rip), %xmm0     # 0x107a8
movss %xmm0, (%rax,%rcx,4)
jmp 0xe927
jmp 0xe961
movq -0x130(%rbp), %rax
movq %rax, -0xc8(%rbp)
movl -0x128(%rbp), %eax
movl %eax, -0xc0(%rbp)
movq -0x19c(%rbp), %rax
movq %rax, -0xbc(%rbp)
movl -0x194(%rbp), %eax
movl %eax, -0xb4(%rbp)
movq -0x124(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x11c(%rbp), %eax
movl %eax, -0xa8(%rbp)
movq -0x90(%rbp), %rdi
movl -0x94(%rbp), %esi
callq 0x3c30
movq -0x90(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0xa0(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x40(%rax)
movups -0xe0(%rbp), %xmm0
movups -0xd0(%rbp), %xmm1
movups -0xc0(%rbp), %xmm2
movups -0xb0(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0xd6e0
movq -0x90(%rbp), %rdi
callq 0x8220
jmp 0xe22f
movq -0x90(%rbp), %rdi
callq 0x6890
movq -0x20(%rbp), %rdi
callq 0xbf40
movq -0x28(%rbp), %rdi
callq 0xaae0
movq -0xa0(%rbp), %rdi
callq 0x3410
xorl %eax, %eax
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq