// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %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, (%r15)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3270
movl %ebx, 0x10(%rax)
movq %rax, 0x8(%r15)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3270
movl %ebx, 0x10(%rax)
movq %rax, 0x10(%r15)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3270
movl $0x30, 0x10(%rax)
movq %r15, 0x8(%rsp)
movq %rax, 0x18(%r15)
leaq 0x4259(%rip), %rsi      # 0xcbd3
movq %r14, 0x48(%rsp)
movq %r14, %rdi
callq 0x3400
testq %rax, %rax
je 0x8d6e
movq %rax, %rbx
xorl %eax, %eax
leaq 0x30(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x60(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, %rdx
callq 0x87f4
cmpl $-0x1, %eax
je 0x8d45
leaq 0x1c(%rsp), %r15
leaq 0x20(%rsp), %r12
leaq 0x445c(%rip), %r14      # 0xce23
movq %rsp, %r13
movq $0x0, 0x50(%rsp)
movq %rbx, 0x58(%rsp)
movl $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x30(%rsp), %rbp
movl $0x2, %edx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3160
testl %eax, %eax
je 0x8ac1
movl $0x3, %edx
movq %rbp, %rdi
leaq 0x441c(%rip), %rsi      # 0xce31
callq 0x3160
testl %eax, %eax
je 0x8aea
movl $0x3, %edx
movq %rbp, %rdi
leaq 0x4410(%rip), %rsi      # 0xce41
callq 0x3160
testl %eax, %eax
je 0x8b14
movl $0x2, %edx
movq %rbp, %rdi
leaq 0x4404(%rip), %rsi      # 0xce51
callq 0x3160
testl %eax, %eax
je 0x8b67
movl $0x7, %edx
movq %rbp, %rdi
leaq 0x43ff(%rip), %rsi      # 0xce68
callq 0x3160
testl %eax, %eax
je 0x8ced
movl $0x7, %edx
movq %rbp, %rdi
leaq 0x43eb(%rip), %rsi      # 0xce70
callq 0x3160
testl %eax, %eax
jne 0x8b47
addq $0x7, %rbp
movq 0x8(%rsp), %rax
movq 0x20(%rax), %r15
testq %r15, %r15
je 0x8d26
movq (%r15), %rdi
movq %rbp, %rsi
callq 0x3590
testl %eax, %eax
je 0x8d26
movq 0x18(%r15), %r15
jmp 0x8a9f
movq %rbp, %rdi
leaq 0x435b(%rip), %rsi      # 0xce26
leaq 0x18(%rsp), %r13
movq %r13, %rdx
movq %r15, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x3340
movq 0x8(%rsp), %rax
movq (%rax), %rdi
jmp 0x8b3c
movq %rbp, %rdi
leaq 0x4341(%rip), %rsi      # 0xce35
leaq 0x18(%rsp), %r13
movq %r13, %rdx
movq %r15, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x3340
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
jmp 0x8b3c
movq %rbp, %rdi
leaq 0x4327(%rip), %rsi      # 0xce45
leaq 0x18(%rsp), %r13
movq %r13, %rdx
movq %r15, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x3340
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq %r13, %rsi
movq %rsp, %r13
callq 0x821f
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %rbx, %rdx
callq 0x87f4
cmpl $-0x1, %eax
jne 0x89d8
jmp 0x8d45
movl $0x2, %r12d
xorl %r15d, %r15d
leaq 0x4(%rsp), %rbx
xorl %eax, %eax
movl %eax, 0x4(%rsp)
movl %eax, 0x14(%rsp)
movl %eax, 0x10(%rsp)
movl %eax, (%rsp)
movslq %r12d, %r14
addq %rbp, %r14
movq %r14, %rdi
leaq 0x42be(%rip), %rsi      # 0xce54
movq %rbx, %rdx
leaq 0x14(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r13, %r9
xorl %eax, %eax
callq 0x3340
cmpl $0x2, %eax
jg 0x8c13
movq %r14, %rdi
leaq 0x429b(%rip), %rsi      # 0xce57
movq %rbx, %rdx
leaq 0x14(%rsp), %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x3340
cmpl $0x1, %eax
jg 0x8c13
movq %r14, %rdi
leaq 0x4282(%rip), %rsi      # 0xce5f
movq %rbx, %rdx
leaq 0x10(%rsp), %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x3340
cmpl $0x1, %eax
jg 0x8c13
movq %r14, %rdi
leaq 0x4265(%rip), %rsi      # 0xce63
movq %rbx, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x3340
testl %eax, %eax
jle 0x8cce
addl (%rsp), %r12d
movl 0x4(%rsp), %r13d
movl 0x14(%rsp), %ebx
movl 0x10(%rsp), %r14d
testl %r15d, %r15d
je 0x8ca9
cmpl $0x1, %r15d
je 0x8c97
movl 0x3c(%rsp), %eax
movl %eax, 0x68(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x6c(%rsp)
movl %r13d, 0x70(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x78(%rsp)
movl %ebx, 0x7c(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x80(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x84(%rsp)
movl %r14d, 0x88(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0x68(%rsp), %rsi
callq 0x821f
xorl %eax, %eax
movl %r13d, 0x24(%rsp)
movl %ebx, 0x2c(%rsp)
movl %r14d, 0x28(%rsp)
jmp 0x8cb9
xorl %eax, %eax
movl %ebx, 0x44(%rsp)
movl %r14d, 0x40(%rsp)
movl %r13d, 0x3c(%rsp)
leaq 0x4(%rsp), %rbx
movq %rsp, %r13
incl %r15d
testb %al, %al
je 0x8b75
jmp 0x8cd2
movb $0x1, %al
jmp 0x8cc1
movq 0x58(%rsp), %rbx
leaq 0x1c(%rsp), %r15
leaq 0x20(%rsp), %r12
leaq 0x413b(%rip), %r14      # 0xce23
jmp 0x8b47
addq $0x7, %rbp
movq %rbp, %rdi
movq 0x48(%rsp), %rsi
callq 0x8134
movq %rax, %r14
movq %rax, %rdi
callq 0x8582
movq 0x8(%rsp), %rcx
movq %rax, 0x20(%rcx)
movq %r14, %rdi
leaq 0x4107(%rip), %r14      # 0xce23
callq 0x3410
jmp 0x8b47
movq %r15, 0x50(%rsp)
testq %r15, %r15
leaq 0x1c(%rsp), %r15
jne 0x8b47
leaq 0x4138(%rip), %rdi      # 0xce78
movq %rbp, %rsi
jmp 0x8d7a
movq 0x30(%rsp), %rdi
callq 0x3410
movq %rbx, %rdi
callq 0x3350
movq 0x8(%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4092(%rip), %rdi      # 0xce07
movq 0x48(%rsp), %rsi
xorl %eax, %eax
callq 0x874c