// File: /workspace/github_c_makefile_star2_O0/moefh[P]hed/src/file.c
// Function: hed_read_file
struct hed_file *hed_read_file(const char *filename)
{
  FILE *f = fopen(filename, "r");
  if (! f) {
    show_msg("ERROR: can't open file '%s'", filename);
    return NULL;
  }

  uint8_t *data = NULL;
  char *new_filename = malloc(strlen(filename) + 1);
  if (! new_filename) {
    show_msg("ERROR: out of memory");
    goto err;
  }
  strcpy(new_filename, filename);
  
  if (fseek(f, 0, SEEK_END) < 0) {
    show_msg("ERROR: can't determine file size");
    goto err;
  }
  long size = ftell(f);
  if (size < 0 || (unsigned long) size > SIZE_MAX) {
    show_msg("ERROR: file is too large");
    goto err;
  }
  if (fseek(f, 0, SEEK_SET) == -1) {
    show_msg("ERROR: can't seek to start of file");
    goto err;
  }
  data = malloc((size_t) size);
  if (! data) {
    show_msg("ERROR: not enough memory for %ld bytes", size);
    goto err;
  }
  
  if (fread(data, 1, size, f) != (size_t) size) {
    show_msg("ERROR: error reading file");
    goto err;
  }
  
  struct hed_file *file = new_file();
  if (! file) {
    show_msg("ERROR: error reading file");
    goto err;
  }
  file->data = data;
  file->data_len = size;
  file->filename = new_filename;
  fclose(f);
  return file;

 err:
  if (new_filename)
    free(new_filename);
  if (data)
    free(data);
  fclose(f);
  return NULL;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x755f(%rip), %rsi      # 0xb514
callq 0x2220
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0x3fe8
movq 0x28(%rsp), %rsi
leaq 0x73a9(%rip), %rdi      # 0xb37c
movb $0x0, %al
callq 0x3610
movq $0x0, 0x30(%rsp)
jmp 0x41ad
movq $0x0, 0x18(%rsp)
movq 0x28(%rsp), %rdi
callq 0x20d0
movq %rax, %rdi
addq $0x1, %rdi
callq 0x21c0
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x4027
leaq 0x737d(%rip), %rdi      # 0xb398
movb $0x0, %al
callq 0x3610
jmp 0x4176
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x2050
movq 0x20(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x21d0
cmpl $0x0, %eax
jge 0x4061
leaq 0x7358(%rip), %rdi      # 0xb3ad
movb $0x0, %al
callq 0x3610
jmp 0x4176
movq 0x20(%rsp), %rdi
callq 0x2170
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jl 0x4080
cmpq $-0x1, 0x8(%rsp)
jbe 0x4093
leaq 0x7347(%rip), %rdi      # 0xb3ce
movb $0x0, %al
callq 0x3610
jmp 0x4176
movq 0x20(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x21d0
cmpl $-0x1, %eax
jne 0x40bb
leaq 0x7338(%rip), %rdi      # 0xb3e7
movb $0x0, %al
callq 0x3610
jmp 0x4176
movq 0x8(%rsp), %rdi
callq 0x21c0
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x40ea
movq 0x8(%rsp), %rsi
leaq 0x732c(%rip), %rdi      # 0xb40a
movb $0x0, %al
callq 0x3610
jmp 0x4176
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl $0x1, %esi
callq 0x2080
cmpq 0x8(%rsp), %rax
je 0x411a
leaq 0x7320(%rip), %rdi      # 0xb431
movb $0x0, %al
callq 0x3610
jmp 0x4176
callq 0x3ed0
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
jne 0x413a
leaq 0x7300(%rip), %rdi      # 0xb431
movb $0x0, %al
callq 0x3610
jmp 0x4176
movq 0x18(%rsp), %rcx
movq (%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x8(%rsp), %rcx
movq (%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x10(%rsp), %rcx
movq (%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x20(%rsp), %rdi
callq 0x20b0
movq (%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x41ad
cmpq $0x0, 0x10(%rsp)
je 0x4188
movq 0x10(%rsp), %rdi
callq 0x2030
cmpq $0x0, 0x18(%rsp)
je 0x419a
movq 0x18(%rsp), %rdi
callq 0x2030
movq 0x20(%rsp), %rdi
callq 0x20b0
movq $0x0, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq $0x38, %rsp
retq
nopw (%rax,%rax)