// File: /workspace/github_cpp_makefile_star2_O3/nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: handle_directory_request
static void handle_directory_request(struct mg_connection *conn,
                                     const char *dir) {
  struct dirent *dp;
  DIR *dirp;
  struct de *entries = NULL;
  char path[PATH_MAX];
  int i, sort_direction, num_entries = 0, arr_size = 128;

  if ((dirp = opendir(dir)) == NULL) {
    send_http_error(conn, 500, "Cannot open directory",
        "Error: opendir(%s): %s", path, strerror(ERRNO));
    return;
  }

  (void) mg_printf(conn, "%s",
      "HTTP/1.1 200 OK\r\n"
      "Connection: close\r\n"
      "Content-Type: text/html; charset=utf-8\r\n\r\n");

  sort_direction = conn->request_info.query_string != NULL &&
    conn->request_info.query_string[1] == 'd' ? 'a' : 'd';

  while ((dp = readdir(dirp)) != NULL) {

    // Do not show current dir and passwords file
    if (!strcmp(dp->d_name, ".") ||
        !strcmp(dp->d_name, "..") ||
        !strcmp(dp->d_name, PASSWORDS_FILE_NAME))
      continue;

    if (entries == NULL || num_entries >= arr_size) {
      arr_size *= 2;
      entries = (struct de *) realloc(entries,
          arr_size * sizeof(entries[0]));
    }

    if (entries == NULL) {
      closedir(dirp);
      send_http_error(conn, 500, "Cannot open directory",
          "%s", "Error: cannot allocate memory");
      return;
    }

    mg_snprintf(conn, path, sizeof(path), "%s%c%s", dir, DIRSEP, dp->d_name);

    // If we don't memset stat structure to zero, mtime will have
    // garbage and strftime() will segfault later on in
    // print_dir_entry(). memset is required only if mg_stat()
    // fails. For more details, see
    // http://code.google.com/p/mongoose/issues/detail?id=79
    if (mg_stat(path, &entries[num_entries].st) != 0) {
      memset(&entries[num_entries].st, 0, sizeof(entries[num_entries].st));
    }

    entries[num_entries].conn = conn;
    entries[num_entries].file_name = mg_strdup(dp->d_name);
    num_entries++;
  }
  (void) closedir(dirp);

  conn->num_bytes_sent += mg_printf(conn,
      "<html><head><title>Index of %s</title>"
      "<style>th {text-align: left;}</style></head>"
      "<body><h1>Index of %s</h1><pre><table cellpadding=\"0\">"
      "<tr><th><a href=\"?n%c\">Name</a></th>"
      "<th><a href=\"?d%c\">Modified</a></th>"
      "<th><a href=\"?s%c\">Size</a></th></tr>"
      "<tr><td colspan=\"3\"><hr></td></tr>",
      conn->request_info.uri, conn->request_info.uri,
      sort_direction, sort_direction, sort_direction);

  // Print first entry - link to a parent directory
  conn->num_bytes_sent += mg_printf(conn,
      "<tr><td><a href=\"%s%s\">%s</a></td>"
      "<td>&nbsp;%s</td><td>&nbsp;&nbsp;%s</td></tr>\n",
      conn->request_info.uri, "..", "Parent directory", "-", "-");

  // Sort and print directory entries
  qsort(entries, (size_t)num_entries, sizeof(entries[0]), compare_dir_entries);
  for (i = 0; i < num_entries; i++) {
    print_dir_entry(&entries[i]);
    free(entries[i].file_name);
  }
  free(entries);

  conn->num_bytes_sent += mg_printf(conn, "%s", "</table></body></html>");
  conn->request_info.status_code = 200;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20d8, %rsp           # imm = 0x20D8
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0x57a0
testq %rax, %rax
je 0x12b42
movq %rax, %rbx
leaq 0x3c60(%rip), %rsi      # 0x1676d
leaq 0x379c(%rip), %rdx      # 0x162b0
xorl %r15d, %r15d
movq %r14, %rdi
xorl %eax, %eax
callq 0xe860
movq %r14, 0x20(%rsp)
movq 0x28(%r14), %rax
testq %rax, %rax
je 0x12b7b
xorl %ecx, %ecx
cmpb $0x64, 0x1(%rax)
setne %cl
leal (%rcx,%rcx,2), %r14d
addl $0x61, %r14d
jmp 0x12b81
callq 0x5060
movl (%rax), %edi
callq 0x51a0
leaq 0x372e(%rip), %rdx      # 0x16283
leaq 0x373d(%rip), %rcx      # 0x16299
leaq 0x10d0(%rsp), %r8
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq %rax, %r9
xorl %eax, %eax
callq 0x12224
jmp 0x13004
movl $0x64, %r14d
movq %rbx, %rdi
callq 0x57c0
testq %rax, %rax
je 0x12cd9
movq %rax, %r13
movq %r12, 0x40(%rsp)
movl $0x80, 0x28(%rsp)
leaq 0x34fc(%rip), %r12      # 0x160a5
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpb $0x2e, 0x13(%r13)
jne 0x12bca
cmpb $0x0, 0x14(%r13)
je 0x12bdd
cmpb $0x2e, 0x14(%r13)
jne 0x12bca
cmpb $0x0, 0x15(%r13)
je 0x12bdd
addq $0x13, %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x56b0
testl %eax, %eax
jne 0x12bf2
movq %rbx, %rdi
callq 0x57c0
movq %rax, %r13
testq %rax, %rax
jne 0x12bae
jmp 0x12cdb
testq %rbp, %rbp
setne %al
movl 0x28(%rsp), %edx
movslq %edx, %rcx
cmpq %rcx, %r15
setl %cl
testb %cl, %al
jne 0x12c2e
addl %edx, %edx
movl %edx, 0x28(%rsp)
movslq %edx, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
movq %rbp, %rdi
callq 0x5890
movq %rax, %rbp
testq %rax, %rax
je 0x13016
movl %r14d, 0x38(%rsp)
movq %r13, (%rsp)
movl $0x1000, %edx           # imm = 0x1000
movq 0x20(%rsp), %rdi
leaq 0x10d0(%rsp), %rsi
leaq 0x3540(%rip), %rcx      # 0x16190
movq 0x40(%rsp), %r8
movl $0x2f, %r9d
xorl %eax, %eax
callq 0x12579
movq %rbp, %rax
leaq (%r15,%r15,4), %rbp
movq %rax, 0x30(%rsp)
leaq (%rax,%rbp,8), %r14
addq $0x10, %r14
leaq 0x10d0(%rsp), %rdi
movq %r14, %rsi
callq 0x11bdf
testl %eax, %eax
je 0x12c9b
xorpd %xmm0, %xmm0
movupd %xmm0, (%r14)
movq $0x0, 0x10(%r14)
movq 0x30(%rsp), %rax
leaq (%rax,%rbp,8), %r14
movq %rax, %rbp
movq 0x20(%rsp), %rax
movq %rax, (%r14)
movq %r13, %rdi
callq 0xfd09
movq %rax, 0x8(%r14)
incq %r15
movq %rbx, %rdi
callq 0x57c0
movq %rax, %r13
testq %rax, %rax
movl 0x38(%rsp), %r14d
jne 0x12bae
jmp 0x12cdb
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x5860
movq 0x20(%rsp), %rbx
movq 0x18(%rbx), %rcx
movl %r14d, (%rsp)
leaq 0x3629(%rip), %rsi      # 0x16320
movq %rbx, %rdi
movq %rcx, %rdx
movl %r14d, %r8d
movl %r14d, %r9d
xorl %eax, %eax
callq 0xe860
cltq
addq %rax, 0x4b0(%rbx)
movq 0x18(%rbx), %rdx
leaq 0x325f(%rip), %r9       # 0x15f7d
movq %r9, (%rsp)
leaq 0x370f(%rip), %rsi      # 0x16438
leaq 0x35cf(%rip), %rcx      # 0x162ff
leaq 0x3752(%rip), %r8       # 0x16489
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe860
cltq
addq %rax, 0x4b0(%rbx)
movslq %r15d, %rbx
leaq 0x1800(%rip), %rcx      # 0x14554
movl $0x28, %edx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x5980
testl %ebx, %ebx
jle 0x12fcf
movl %r15d, %eax
movq %rax, 0x48(%rsp)
leaq 0x90(%rsp), %rbx
leaq 0x50(%rsp), %r14
xorl %ecx, %ecx
movq %rbp, 0x30(%rsp)
leaq (%rcx,%rcx,4), %rax
leaq (,%rax,8), %r15
addq %rbp, %r15
cmpl $0x0, 0x10(%r15)
movq %rcx, 0x40(%rsp)
je 0x12dbe
movq (%r15), %rdi
movl $0x40, %edx
movq %rbx, %rsi
leaq 0x39b8(%rip), %rcx      # 0x1676d
leaq 0x36f8(%rip), %r8       # 0x164b4
jmp 0x12de1
movq 0x18(%r15), %r8
cmpq $0x3ff, %r8             # imm = 0x3FF
jg 0x12f5a
movq (%r15), %rdi
movl $0x40, %edx
movq %rbx, %rsi
leaq 0x36df(%rip), %rcx      # 0x164c0
xorl %eax, %eax
callq 0x12579
leaq 0x20(%r15), %rdi
callq 0x5340
movl $0x40, %esi
movq %r14, %rdi
leaq 0x36d6(%rip), %rdx      # 0x164d6
movq %rax, %rcx
callq 0x53a0
movq %r15, 0x38(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0x28(%rsp)
movb (%rax), %r15b
leaq 0xd0(%rsp), %r13
testb %r15b, %r15b
je 0x12ecd
callq 0x5520
movq (%rax), %r12
movq 0x28(%rsp), %rax
leaq 0x1(%rax), %rbp
leaq 0xd0(%rsp), %r13
xorl %ebx, %ebx
movzbl %r15b, %r14d
testb $0x8, (%r12,%r14,2)
jne 0x12e69
movl $0xa, %edx
leaq 0x36de(%rip), %rdi      # 0x1653a
movl %r14d, %esi
callq 0x5540
testq %rax, %rax
je 0x12e96
movb %r15b, (%r13)
leaq (%rsp,%rbx), %r13
addq $0xd1, %r13
movb (%rbp), %r15b
testb %r15b, %r15b
je 0x12ecd
leaq 0x1(%rbx), %rax
incq %rbp
cmpq $0xffe, %rbx            # imm = 0xFFE
movq %rax, %rbx
jl 0x12e45
jmp 0x12ecd
cmpq $0xffc, %rbx            # imm = 0xFFC
jg 0x12e6d
movb $0x25, (%r13)
movl %r14d, %eax
shrl $0x4, %eax
leaq 0x2c5a(%rip), %rcx      # 0x15b0b
movb (%rax,%rcx), %al
movb %al, 0x1(%r13)
andl $0xf, %r14d
movb (%r14,%rcx), %al
movb %al, 0xd2(%rsp,%rbx)
addq $0x2, %rbx
jmp 0x12e6d
movb $0x0, (%r13)
movq 0x38(%rsp), %r15
movq (%r15), %rdi
cmpl $0x0, 0x10(%r15)
movq 0x18(%rdi), %rdx
leaq 0x3023(%rip), %r8       # 0x15f0d
leaq 0x334e(%rip), %rax      # 0x1623f
cmoveq %rax, %r8
leaq 0x90(%rsp), %rbx
movq %rbx, 0x10(%rsp)
leaq 0x50(%rsp), %r14
movq %r14, 0x8(%rsp)
movq %r8, (%rsp)
leaq 0x35ce(%rip), %rsi      # 0x164e5
leaq 0xd0(%rsp), %rcx
movq 0x28(%rsp), %r9
xorl %eax, %eax
callq 0xe860
cltq
movq (%r15), %rcx
addq %rax, 0x4b0(%rcx)
movq 0x8(%r15), %rdi
callq 0x55d0
movq 0x40(%rsp), %rcx
incq %rcx
cmpq 0x48(%rsp), %rcx
movq 0x30(%rsp), %rbp
jne 0x12d88
jmp 0x12fcf
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
cmpq $0xfffff, %r8           # imm = 0xFFFFF
ja 0x12f87
movq (%r15), %rdi
mulsd 0x27ea(%rip), %xmm0     # 0x15760
movl $0x40, %edx
movq %rbx, %rsi
leaq 0x353f(%rip), %rcx      # 0x164c4
jmp 0x12fc3
movq (%r15), %rdi
cmpq $0x3fffffff, %r8        # imm = 0x3FFFFFFF
ja 0x12fac
mulsd 0x27bd(%rip), %xmm0     # 0x15758
movl $0x40, %edx
movq %rbx, %rsi
leaq 0x3520(%rip), %rcx      # 0x164ca
jmp 0x12fc3
mulsd 0x279c(%rip), %xmm0     # 0x15750
movl $0x40, %edx
movq %rbx, %rsi
leaq 0x350d(%rip), %rcx      # 0x164d0
movb $0x1, %al
callq 0x12579
jmp 0x12de8
movq %rbp, %rdi
callq 0x55d0
leaq 0x378f(%rip), %rsi      # 0x1676d
leaq 0x34b5(%rip), %rdx      # 0x1649a
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe860
cltq
addq %rax, 0x4b0(%rbx)
movl $0xc8, 0x4c(%rbx)
addq $0x20d8, %rsp           # imm = 0x20D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x5860
leaq 0x325e(%rip), %rdx      # 0x16283
leaq 0x3741(%rip), %rcx      # 0x1676d
leaq 0x32cf(%rip), %r8       # 0x16302
movq 0x20(%rsp), %rdi
movl $0x1f4, %esi            # imm = 0x1F4
xorl %eax, %eax
callq 0x12224
jmp 0x13004