// File: /workspace/github_c_makefile_star2_O1/nkanaev[P]tipsy/src/tipsy.c
// Function: mtl_readfile
Mtl* mtl_readfile(const char *filepath) {
  FILE *f = fopen(filepath, "r");
  if (f == NULL) error("failed to open mtl file: %s", filepath);

  char *line = NULL, *tline = NULL;
  size_t len = 0;

  Mtl *m = NULL;

  while (readline(&line, &len, f) != -1) {
    tline = trim(line);
    if (strncmp(tline, "newmtl ", 7) == 0) {
      char *mtlname = &line[7];
      Mtl *newm = calloc(1, sizeof(Mtl));
      newm->name = strdup(mtlname);
      newm->next = m;
      m = newm;
    } else if (strncmp(tline, "map_Ka ", 7) == 0 && m != NULL) {
      char *imgname = strdup(tline+7);
      char *imgpath = relpath(imgname, filepath);
      Tigr *img = tigrLoadImage(imgpath);
      if (img == NULL) error("failed to open image: {%s}", imgpath);
      m->map_Ka = img;
      free(imgpath);
      free(imgname);
    } else if (strncmp(tline, "map_Kd ", 7) == 0 && m != NULL) {
      char *imgname = strdup(tline+7);
      char *imgpath = relpath(imgname, filepath);
      Tigr *img = tigrLoadImage(imgpath);
      if (img == NULL) error("failed to open image: {%s}", imgpath);
      m->map_Kd = img;
      free(imgpath);
      free(imgname);
    }
  }
  free(line);
  fclose(f);
  return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0x463c(%rip), %rsi      # 0xcbd3
movq %rdi, (%rsp)
callq 0x3400
testq %rax, %rax
je 0x873f
movq %rax, %r14
xorl %r15d, %r15d
leaq 0x8(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x10(%rsp), %rsi
movq %r15, (%rsi)
movq %rax, %rdx
callq 0x87f4
cmpl $-0x1, %eax
je 0x870a
xorl %r15d, %r15d
leaq 0x8(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x81bc
movq %rax, %rbx
movl $0x7, %edx
movq %rax, %rdi
leaq 0x47d8(%rip), %rsi      # 0xcdd4
callq 0x3160
testl %eax, %eax
je 0x8663
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x47c8(%rip), %rsi      # 0xcddc
callq 0x3160
testl %eax, %eax
sete %al
testq %r15, %r15
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x868d
addq $0x7, %rbx
movq %rbx, %rdi
callq 0x3740
movq %rax, %rbx
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x8134
movq %rax, %r12
movq %rax, %rdi
callq 0x4a35
testq %rax, %rax
je 0x872e
movq %rax, 0x8(%r15)
jmp 0x86e3
addq $0x7, %r12
movl $0x1, %edi
movl $0x20, %esi
callq 0x3270
movq %rax, %rbx
movq %r12, %rdi
callq 0x3740
movq %rax, (%rbx)
movq %r15, 0x18(%rbx)
movq %rbx, %r15
jmp 0x86f3
testq %r15, %r15
setne %r12b
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x475c(%rip), %rsi      # 0xcdff
callq 0x3160
testl %eax, %eax
sete %al
andb %r12b, %al
cmpb $0x1, %al
jne 0x86f3
addq $0x7, %rbx
movq %rbx, %rdi
callq 0x3740
movq %rax, %rbx
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x8134
movq %rax, %r12
movq %rax, %rdi
callq 0x4a35
testq %rax, %rax
je 0x872e
movq %rax, 0x10(%r15)
movq %r12, %rdi
callq 0x3410
movq %rbx, %rdi
callq 0x3410
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x87f4
cmpl $-0x1, %eax
jne 0x85dd
movq 0x8(%rsp), %rdi
callq 0x3410
movq %r14, %rdi
callq 0x3350
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x46af(%rip), %rdi      # 0xcde4
movq %r12, %rsi
xorl %eax, %eax
callq 0x874c
leaq 0x4672(%rip), %rdi      # 0xcdb8
movq (%rsp), %rsi
jmp 0x8738