// File: /workspace/github_c_makefile_star2_O3/nkanaev[P]tipsy/src/tipsy.c
// Function: obj_readfile
Obj* obj_readfile(char *filepath) {
  Obj *o = calloc(1, sizeof(Obj));
  o->v = list_new(sizeof(Vec));
  o->vn = list_new(sizeof(Vec));
  o->vt = list_new(sizeof(Vec));
  o->f = list_new(sizeof(Face));

  FILE *f = fopen(filepath, "r");
  if (f == NULL) error("failed to open obj file: %s", filepath);

  char *line = NULL;
  size_t len = 0;

  Mtl *mtl = NULL;

  while (readline(&line, &len, f) != -1) {
    Vec v = {0, 0, 0};
    if (strncmp(line, "v ", 2) == 0) {
      sscanf(line, "v %f %f %f", &v.x, &v.y, &v.z);
      list_add(o->v, &v);
    } else if (strncmp(line, "vn ", 3) == 0) {
      sscanf(line, "vn %f %f %f", &v.x, &v.y, &v.z);
      list_add(o->vn, &v);
    } else if (strncmp(line, "vt ", 3) == 0) {
      sscanf(line, "vt %f %f %f", &v.x, &v.y, &v.z);
      list_add(o->vt, &v);
    } else if (strncmp(line, "f ", 2) == 0) {
      int offset = 2;
      int i = 0, v1, v2, v3, vt1, vt2, vt3, vn1, vn2, vn3;
      while (++i) {
        int v = 0, vt = 0, vn = 0, nread = 0;
        if (!(
          sscanf(line+offset, "%d/%d/%d%n", &v, &vt, &vn, &nread) >= 3 ||
          sscanf(line+offset, "%d/%d%n", &v, &vt, &nread) >= 2 ||
          sscanf(line+offset, "%d//%d%n", &v, &vn, &nread) >= 2 ||
          sscanf(line+offset, "%d%n", &v, &nread) >= 1
        )) break;
        offset += nread;

        if (i == 1) { v1 = v; vt1 = vt; vn1 = vn; continue; }
        v2 = v3; v3 = v;
        vt2 = vt3; vt3 = vt;
        vn2 = vn3; vn3 = vn;

        if (i >= 3) {
          Face f = {
            .v1=v1, .v2=v2, .v3=v3, .vt1=vt1, .vt2=vt2, .vt3=vt3,
            .vn1=vn1, .vn2=vn2, .vn3=vn3, .mtl=mtl
          };
          list_add(o->f, &f);
        }
      }
    } else if (strncmp(line, "mtllib ", 7) == 0) {
      char *mtlname = line+7;
      char *mtlpath = relpath(mtlname, filepath);
      o->mtl = mtl_readfile(mtlpath);
      free(mtlpath);
    } else if (strncmp(line, "usemtl ", 7) == 0) {
      char *name = line+7;
      mtl = mtl_get(o->mtl, name);
      if (mtl == NULL) error("failed to find mtl: %s", name);
    }
  }
  free(line);
  fclose(f);

  return o;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %r14
movl $0x1, %edi
movl $0x28, %esi
callq 0x3270
movq %rax, %r15
movl $0x1, %edi
movl $0x18, %esi
callq 0x3270
movl $0xc, %ebx
movl %ebx, 0x10(%rax)
movq %rax, 0x90(%rsp)
movq %rax, (%r15)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3270
movl %ebx, 0x10(%rax)
movq %rax, 0x80(%rsp)
movq %rax, 0x8(%r15)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3270
movl %ebx, 0x10(%rax)
movq %rax, 0x78(%rsp)
movq %rax, 0x10(%r15)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3270
movl $0x30, 0x10(%rax)
movq %r15, 0x68(%rsp)
movq %rax, 0x98(%rsp)
movq %rax, 0x18(%r15)
leaq 0x4175(%rip), %rsi      # 0xcc1b
movq %r14, 0x60(%rsp)
movq %r14, %rdi
callq 0x3400
testq %rax, %rax
je 0x8ea7
movq %rax, %rbx
xorl %eax, %eax
leaq 0x48(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, %rdx
callq 0x8901
cmpl $-0x1, %eax
je 0x8e6d
leaq 0x3c(%rsp), %r13
leaq 0x38(%rsp), %rbp
leaq 0x20(%rsp), %r15
movq $0x0, 0x40(%rsp)
xorl %r12d, %r12d
movq %rbx, 0x88(%rsp)
movl $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x48(%rsp), %rdi
movzbl (%rdi), %eax
cmpl $0x66, %eax
je 0x8b77
cmpl $0x76, %eax
jne 0x8db3
cmpb $0x20, 0x1(%rdi)
jne 0x8d21
leaq 0x4366(%rip), %rsi      # 0xcea3
movq %rbx, %r14
movq %r12, %rbx
leaq 0x10(%rsp), %r12
movq %r12, %rdx
leaq 0x14(%rsp), %rcx
leaq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x3340
movq 0x90(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %r12
movq %r14, %rbx
callq 0x8383
jmp 0x8e54
cmpb $0x20, 0x1(%rdi)
jne 0x8db3
movq %rdi, 0xa0(%rsp)
movq %r12, 0x70(%rsp)
movl $0x2, %r14d
movl $0x0, 0x4(%rsp)
xorl %eax, %eax
movl %eax, 0x3c(%rsp)
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
movl %eax, 0x38(%rsp)
movslq %r14d, %r12
addq 0xa0(%rsp), %r12
movq %r12, %rdi
leaq 0x4303(%rip), %rsi      # 0xcec6
movq %r13, %rdx
leaq 0xc(%rsp), %rcx
leaq 0x8(%rsp), %r8
movq %rbp, %r9
xorl %eax, %eax
callq 0x3340
cmpl $0x2, %eax
jg 0x8c40
movq %r12, %rdi
leaq 0x42e0(%rip), %rsi      # 0xcec9
movq %r13, %rdx
leaq 0xc(%rsp), %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x3340
cmpl $0x1, %eax
jg 0x8c40
movq %r12, %rdi
leaq 0x42c7(%rip), %rsi      # 0xced1
movq %r13, %rdx
leaq 0x8(%rsp), %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x3340
cmpl $0x1, %eax
jg 0x8c40
movq %r12, %rdi
leaq 0x42aa(%rip), %rsi      # 0xced5
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x3340
testl %eax, %eax
jle 0x8d60
movq %rbp, %r15
movq %r13, %rbp
addl 0x38(%rsp), %r14d
movl 0x3c(%rsp), %r13d
movl 0xc(%rsp), %ebx
movl 0x8(%rsp), %r12d
movl 0x4(%rsp), %eax
testl %eax, %eax
je 0x8cf0
cmpl $0x1, %eax
je 0x8ce9
movl 0x54(%rsp), %eax
movl %eax, 0xa8(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0xac(%rsp)
movl %r13d, 0xb0(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0xb4(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0xb8(%rsp)
movl %ebx, 0xbc(%rsp)
movl 0x58(%rsp), %eax
movl %eax, 0xc0(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0xc4(%rsp)
movl %r12d, 0xc8(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x8383
movl 0x4(%rsp), %eax
movl %r13d, 0x2c(%rsp)
jmp 0x8d07
movl %ebx, 0x5c(%rsp)
movl 0x30(%rsp), %ebx
movl %r12d, 0x58(%rsp)
movl %r13d, 0x54(%rsp)
movl 0x34(%rsp), %r12d
incl %eax
movl %eax, 0x4(%rsp)
movl %ebx, 0x30(%rsp)
movl %r12d, 0x34(%rsp)
movq %rbp, %r13
movq %r15, %rbp
jmp 0x8b9c
cmpb $0x6e, 0x1(%rdi)
jne 0x8d77
cmpb $0x20, 0x2(%rdi)
jne 0x8d77
leaq 0x417a(%rip), %rsi      # 0xceae
movq %rbx, %r14
movq %r12, %rbx
leaq 0x10(%rsp), %r12
movq %r12, %rdx
leaq 0x14(%rsp), %rcx
leaq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x3340
movq 0x80(%rsp), %rdi
jmp 0x8b64
movq 0x88(%rsp), %rbx
leaq 0x20(%rsp), %r15
movq 0x70(%rsp), %r12
jmp 0x8e54
cmpb $0x74, 0x1(%rdi)
jne 0x8db3
cmpb $0x20, 0x2(%rdi)
jne 0x8db3
leaq 0x4130(%rip), %rsi      # 0xceba
movq %rbx, %r14
movq %r12, %rbx
leaq 0x10(%rsp), %r12
movq %r12, %rdx
leaq 0x14(%rsp), %rcx
leaq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x3340
movq 0x78(%rsp), %rdi
jmp 0x8b64
movq %rdi, %r15
movq %r12, %r14
movl $0x7, %edx
leaq 0x4115(%rip), %rsi      # 0xceda
callq 0x3160
testl %eax, %eax
je 0x8e17
movl $0x7, %edx
movq %r15, %rdi
leaq 0x4105(%rip), %rsi      # 0xcee2
callq 0x3160
testl %eax, %eax
jne 0x8e4c
addq $0x7, %r15
cmpq $0x0, 0x40(%rsp)
je 0x8e96
movq 0x40(%rsp), %r12
movq (%r12), %rdi
movq %r15, %rsi
callq 0x3590
testl %eax, %eax
je 0x8e4f
movq 0x18(%r12), %r12
testq %r12, %r12
jne 0x8dfb
jmp 0x8e96
addq $0x7, %r15
movq %r15, %rdi
movq 0x60(%rsp), %rsi
callq 0x8298
movq %rax, %r12
movq %rax, %rdi
callq 0x868f
movq %rax, %rcx
movq 0x68(%rsp), %rax
movq %rcx, 0x40(%rsp)
movq %rcx, 0x20(%rax)
movq %r12, %rdi
callq 0x3410
movq %r14, %r12
leaq 0x20(%rsp), %r15
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x8901
cmpl $-0x1, %eax
jne 0x8b05
movq 0x48(%rsp), %rdi
callq 0x3410
movq %rbx, %rdi
callq 0x3350
movq 0x68(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x404d(%rip), %rdi      # 0xceea
movq %r15, %rsi
xorl %eax, %eax
callq 0x8859
leaq 0x3fd9(%rip), %rdi      # 0xce87
movq 0x60(%rsp), %rsi
jmp 0x8ea0