// File: /workspace/github_c_makefile_star2_O3/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 0x4577(%rip), %rsi      # 0xcc1b
movq %rdi, (%rsp)
callq 0x3400
testq %rax, %rax
je 0x884c
movq %rax, %r14
xorl %r15d, %r15d
leaq 0x8(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x10(%rsp), %rsi
movq %r15, (%rsi)
movq %rax, %rdx
callq 0x8901
cmpl $-0x1, %eax
je 0x8817
xorl %r15d, %r15d
leaq 0x8(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x8320
movq %rax, %rbx
movl $0x7, %edx
movq %rax, %rdi
leaq 0x474b(%rip), %rsi      # 0xce54
callq 0x3160
testl %eax, %eax
je 0x8770
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x473b(%rip), %rsi      # 0xce5c
callq 0x3160
testl %eax, %eax
sete %al
testq %r15, %r15
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x879a
addq $0x7, %rbx
movq %rbx, %rdi
callq 0x3730
movq %rax, %rbx
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x8298
movq %rax, %r12
movq %rax, %rdi
callq 0x4b46
testq %rax, %rax
je 0x883b
movq %rax, 0x8(%r15)
jmp 0x87f0
addq $0x7, %r12
movl $0x1, %edi
movl $0x20, %esi
callq 0x3270
movq %rax, %rbx
movq %r12, %rdi
callq 0x3730
movq %rax, (%rbx)
movq %r15, 0x18(%rbx)
movq %rbx, %r15
jmp 0x8800
testq %r15, %r15
setne %r12b
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x46cf(%rip), %rsi      # 0xce7f
callq 0x3160
testl %eax, %eax
sete %al
andb %r12b, %al
cmpb $0x1, %al
jne 0x8800
addq $0x7, %rbx
movq %rbx, %rdi
callq 0x3730
movq %rax, %rbx
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x8298
movq %rax, %r12
movq %rax, %rdi
callq 0x4b46
testq %rax, %rax
je 0x883b
movq %rax, 0x10(%r15)
movq %r12, %rdi
callq 0x3410
movq %rbx, %rdi
callq 0x3410
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x8901
cmpl $-0x1, %eax
jne 0x86ea
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 0x4622(%rip), %rdi      # 0xce64
movq %r12, %rsi
xorl %eax, %eax
callq 0x8859
leaq 0x45e5(%rip), %rdi      # 0xce38
movq (%rsp), %rsi
jmp 0x8845