// File: /workspace/github_c_makefile_star2_O0/moefh[P]hed/src/file_sel.c
// Function: read_dir_list
static struct file_item *read_dir_list(const char *dir_name)
{
  DIR *dir = opendir(dir_name);
  if (! dir) {
    show_msg("Can't read directory '%s'", dir_name);
    return NULL;
  }
  char *name_buf = NULL;
  size_t name_buf_len = 0;
  bool is_root = strcmp(dir_name, "/") == 0;
  struct stat st;

  struct file_item *list = NULL;
  while (true) {
    struct dirent *ent = readdir(dir);
    if (! ent)
      break;
    if (strcmp(ent->d_name, ".") == 0 || (is_root && strcmp(ent->d_name, "..") == 0))
      continue;
    if (stat_at(dir_name, ent->d_name, &name_buf, &name_buf_len, &st) < 0)
      continue;
    
    size_t file_name_len = strlen(ent->d_name);
    struct file_item *file = malloc(sizeof(struct file_item) + file_name_len + 1);
    if (! file)
      break;

    memcpy(file->filename, ent->d_name, file_name_len);
    file->filename[file_name_len] = '\0';
    
    file->is_file = (st.st_mode & S_IFMT) == S_IFREG;
    file->size = st.st_size;

    file->prev = NULL;
    file->next = list;
    if (list)
      list->prev = file;
    list = file;
  }
  
  free(name_buf);
  closedir(dir);

  sort_file_list(&list);
  return list;
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x20c0
movq %rax, 0xd0(%rsp)
cmpq $0x0, 0xd0(%rsp)
jne 0x5796
movq 0xd8(%rsp), %rsi
leaq 0x5d03(%rip), %rdi      # 0xb481
movb $0x0, %al
callq 0x3610
movq $0x0, 0xe0(%rsp)
jmp 0x596a
movq $0x0, 0xc8(%rsp)
movq $0x0, 0xc0(%rsp)
movq 0xd8(%rsp), %rdi
leaq 0x5cde(%rip), %rsi      # 0xb49b
callq 0x2140
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movb %al, 0xbf(%rsp)
movq $0x0, 0x20(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x21b0
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x57f9
jmp 0x5939
movq 0x18(%rsp), %rdi
addq $0x13, %rdi
leaq 0x5c95(%rip), %rsi      # 0xb49e
callq 0x2140
cmpl $0x0, %eax
je 0x5837
testb $0x1, 0xbf(%rsp)
je 0x5839
movq 0x18(%rsp), %rdi
addq $0x13, %rdi
leaq 0x5c70(%rip), %rsi      # 0xb49d
callq 0x2140
cmpl $0x0, %eax
jne 0x5839
jmp 0x57da
movq 0xd8(%rsp), %rdi
movq 0x18(%rsp), %rsi
addq $0x13, %rsi
leaq 0xc8(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
leaq 0x28(%rsp), %r8
callq 0x59c0
cmpl $0x0, %eax
jge 0x586e
jmp 0x57da
movq 0x18(%rsp), %rdi
addq $0x13, %rdi
callq 0x20d0
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
addq $0x28, %rdi
addq $0x1, %rdi
callq 0x21c0
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x58a5
jmp 0x5939
movq 0x8(%rsp), %rdi
addq $0x28, %rdi
movq 0x18(%rsp), %rsi
addq $0x13, %rsi
movq 0x10(%rsp), %rdx
callq 0x21a0
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movb $0x0, 0x28(%rax,%rcx)
movl 0x40(%rsp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
sete %cl
movq 0x8(%rsp), %rax
andb $0x1, %cl
movb %cl, 0x18(%rax)
movq 0x58(%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x8(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x20(%rsp)
je 0x592a
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x57da
movq 0xc8(%rsp), %rdi
callq 0x2030
movq 0xd0(%rsp), %rdi
callq 0x2110
leaq 0x20(%rsp), %rdi
callq 0x5ac0
movq 0x20(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
addq $0xe8, %rsp
retq
nopw (%rax,%rax)