// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rdi
pushq $0x28
popq %rsi
callq 0x3270
movq %rax, %r15
pushq $0xc
popq %rbx
movl %ebx, %edi
callq 0x7b51
movq %rax, 0x88(%rsp)
movq %rax, (%r15)
movl %ebx, %edi
callq 0x7b51
movq %rax, 0x78(%rsp)
movq %rax, 0x8(%r15)
movl %ebx, %edi
callq 0x7b51
movq %rax, 0x70(%rsp)
movq %rax, 0x10(%r15)
pushq $0x30
popq %rdi
callq 0x7b51
movq %r15, 0x58(%rsp)
movq %rax, 0x90(%rsp)
movq %rax, 0x18(%r15)
leaq 0x3979(%rip), %rsi      # 0xbbb3
movq %r14, 0x50(%rsp)
movq %r14, %rdi
callq 0x3420
testq %rax, %rax
je 0x860a
movq %rax, %r15
leaq 0x38(%rsp), %rax
andq $0x0, (%rax)
leaq 0xa0(%rsp), %r13
andq $0x0, (%r13)
leaq 0x34(%rsp), %rbp
leaq 0x30(%rsp), %r14
movq $0x0, 0x60(%rsp)
movq $0x0, 0x68(%rsp)
movq %r15, 0x80(%rsp)
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x80d5
cmpl $-0x1, %eax
je 0x85d0
andl $0x0, 0x20(%rsp)
andq $0x0, 0x18(%rsp)
movq 0x38(%rsp), %rbx
movzbl (%rbx), %eax
cmpl $0x66, %eax
je 0x8313
cmpl $0x76, %eax
pushq $0x7
popq %r12
jne 0x8545
cmpb $0x20, 0x1(%rbx)
jne 0x84b8
movq %rbx, %rdi
leaq 0x3b54(%rip), %rsi      # 0xbe33
movq %r15, %r12
leaq 0x18(%rsp), %r15
movq %r15, %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x20(%rsp), %r8
xorl %eax, %eax
callq 0x3350
movq 0x88(%rsp), %rdi
movq %r15, %rsi
movq %r12, %r15
callq 0x7b79
jmp 0x828d
cmpb $0x20, 0x1(%rbx)
pushq $0x7
popq %r12
jne 0x8545
movq %rbx, 0x98(%rsp)
movl $0x0, 0xc(%rsp)
pushq $0x2
popq %r13
andl $0x0, 0x34(%rsp)
andl $0x0, 0x14(%rsp)
andl $0x0, 0x10(%rsp)
andl $0x0, 0x30(%rsp)
movslq %r13d, %rbx
addq 0x98(%rsp), %rbx
movq %rbx, %rdi
leaq 0x3af8(%rip), %rsi      # 0xbe56
movq %rbp, %rdx
leaq 0x14(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x3350
cmpl $0x2, %eax
jg 0x83db
movq %rbx, %rdi
leaq 0x3ad5(%rip), %rsi      # 0xbe59
movq %rbp, %rdx
leaq 0x14(%rsp), %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x3350
cmpl $0x1, %eax
jg 0x83db
movq %rbx, %rdi
leaq 0x3abc(%rip), %rsi      # 0xbe61
movq %rbp, %rdx
leaq 0x10(%rsp), %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x3350
cmpl $0x1, %eax
jg 0x83db
movq %rbx, %rdi
leaq 0x3a9f(%rip), %rsi      # 0xbe65
movq %rbp, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x3350
testl %eax, %eax
jle 0x84f4
movq %r14, %r15
movq %rbp, %r14
addl 0x30(%rsp), %r13d
movl 0x34(%rsp), %ebp
movl 0x14(%rsp), %r12d
movl 0x10(%rsp), %ebx
movl 0xc(%rsp), %eax
testl %eax, %eax
je 0x8488
cmpl $0x1, %eax
je 0x8482
movl 0x44(%rsp), %eax
movl %eax, 0xa8(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0xac(%rsp)
movl %ebp, 0xb0(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0xb4(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0xb8(%rsp)
movl %r12d, 0xbc(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0xc0(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0xc4(%rsp)
movl %ebx, 0xc8(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0x90(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x7b79
movl 0xc(%rsp), %eax
movl %ebp, 0x24(%rsp)
jmp 0x849e
movl %r12d, 0x4c(%rsp)
movl 0x2c(%rsp), %r12d
movl %ebx, 0x48(%rsp)
movl %ebp, 0x44(%rsp)
movl 0x28(%rsp), %ebx
incl %eax
movl %eax, 0xc(%rsp)
movl %r12d, 0x2c(%rsp)
movl %ebx, 0x28(%rsp)
movq %r14, %rbp
movq %r15, %r14
jmp 0x8335
cmpb $0x6e, 0x1(%rbx)
jne 0x8509
cmpb $0x20, 0x2(%rbx)
jne 0x8509
movq %rbx, %rdi
leaq 0x3970(%rip), %rsi      # 0xbe3e
movq %r15, %r12
leaq 0x18(%rsp), %r15
movq %r15, %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x20(%rsp), %r8
xorl %eax, %eax
callq 0x3350
movq 0x78(%rsp), %rdi
jmp 0x8303
movq 0x80(%rsp), %r15
leaq 0xa0(%rsp), %r13
jmp 0x828d
cmpb $0x74, 0x1(%rbx)
jne 0x8545
cmpb $0x20, 0x2(%rbx)
jne 0x8545
movq %rbx, %rdi
leaq 0x392b(%rip), %rsi      # 0xbe4a
movq %r15, %r12
leaq 0x18(%rsp), %r15
movq %r15, %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x20(%rsp), %r8
xorl %eax, %eax
callq 0x3350
movq 0x70(%rsp), %rdi
jmp 0x8303
movq %rbx, %rdi
leaq 0x391b(%rip), %rsi      # 0xbe6a
movq %r12, %rdx
callq 0x3160
testl %eax, %eax
je 0x8596
movq %rbx, %rdi
leaq 0x390d(%rip), %rsi      # 0xbe72
movq %r12, %rdx
callq 0x3160
testl %eax, %eax
jne 0x828d
addq $0x7, %rbx
movq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x814b
movq %rax, 0x68(%rsp)
testq %rax, %rax
jne 0x828d
jmp 0x85f9
addq $0x7, %rbx
movq %rbx, %rdi
movq 0x50(%rsp), %rsi
callq 0x7aa0
movq %rax, %rbx
movq %rax, %rdi
callq 0x7e80
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x60(%rsp)
movq %rcx, 0x20(%rax)
movq %rbx, %rdi
callq 0x3430
jmp 0x828d
movq 0x38(%rsp), %rdi
callq 0x3430
movq %r15, %rdi
callq 0x3360
movq 0x58(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x387a(%rip), %rdi      # 0xbe7a
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8031
leaq 0x3806(%rip), %rdi      # 0xbe17
movq 0x50(%rsp), %rsi
jmp 0x8603