// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdi, %r14
movq %rsi, %rdi
callq 0x57a0
testq %rax, %rax
je 0x13656
movq %rax, %rbx
leaq 0x314a(%rip), %rsi      # 0x16770
leaq 0x2c86(%rip), %rdx      # 0x162b3
xorl %r12d, %r12d
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8fd
movq 0x28(%r14), %rax
testq %rax, %rax
je 0x1368f
xorl %ecx, %ecx
cmpb $0x64, 0x1(%rax)
setne %cl
leal (%rcx,%rcx,2), %r13d
addl $0x61, %r13d
jmp 0x13695
callq 0x5060
movl (%rax), %edi
callq 0x51a0
leaq 0x2c1d(%rip), %rdx      # 0x16286
leaq 0x2c2c(%rip), %rcx      # 0x1629c
leaq 0x10d0(%rsp), %r8
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq %rax, %r9
xorl %eax, %eax
callq 0x12ea8
jmp 0x13b2c
movl $0x64, %r13d
movq %rbx, %rdi
callq 0x57c0
testq %rax, %rax
movq %r14, 0x20(%rsp)
je 0x13819
movq %rax, %rbp
movl %r13d, 0x38(%rsp)
movq %r15, 0x48(%rsp)
movl $0x80, 0x28(%rsp)
leaq 0x2c3c(%rip), %r12      # 0x16303
leaq 0x2c34(%rip), %r13      # 0x16302
leaq 0x29d0(%rip), %r15      # 0x160a5
movq $0x0, 0x18(%rsp)
xorl %r14d, %r14d
addq $0x13, %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x56b0
testl %eax, %eax
je 0x13712
movq %rbp, %rdi
movq %r13, %rsi
callq 0x56b0
testl %eax, %eax
je 0x13712
movq %rbp, %rdi
movq %r15, %rsi
callq 0x56b0
testl %eax, %eax
jne 0x13727
movq %rbx, %rdi
callq 0x57c0
movq %rax, %rbp
testq %rax, %rax
jne 0x136e1
jmp 0x13805
testq %r14, %r14
setne %al
movl 0x28(%rsp), %edx
movslq %edx, %rcx
cmpq %rcx, 0x18(%rsp)
setl %cl
testb %cl, %al
jne 0x1375c
addl %edx, %edx
movl %edx, 0x28(%rsp)
movslq %edx, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
movq %r14, %rdi
callq 0x5890
movq %rax, %r14
testq %r14, %r14
je 0x13b3e
movq %rbp, (%rsp)
movl $0x1000, %edx           # imm = 0x1000
movq 0x20(%rsp), %rdi
leaq 0x10d0(%rsp), %rsi
leaq 0x2a0e(%rip), %rcx      # 0x16190
movq 0x48(%rsp), %r8
movl $0x2f, %r9d
xorl %eax, %eax
callq 0x131fd
movq 0x18(%rsp), %rax
leaq (%rax,%rax,4), %rax
movq %r14, 0x30(%rsp)
movq %rax, 0x40(%rsp)
leaq (%r14,%rax,8), %r14
addq $0x10, %r14
leaq 0x10d0(%rsp), %rdi
movq %r14, %rsi
callq 0x12847
testl %eax, %eax
je 0x137d4
xorpd %xmm0, %xmm0
movupd %xmm0, (%r14)
movq $0x0, 0x10(%r14)
movq 0x40(%rsp), %rax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rax,8), %r14
movq 0x20(%rsp), %rax
movq %rax, (%r14)
movq %rbp, %rdi
callq 0xfe19
movq %rax, 0x8(%r14)
movq 0x30(%rsp), %r14
incq 0x18(%rsp)
jmp 0x13712
movq %r14, %r15
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r12
movl 0x38(%rsp), %r13d
jmp 0x1381c
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x5860
movq 0x18(%r14), %rcx
movl %r13d, (%rsp)
leaq 0x2af0(%rip), %rsi      # 0x16323
movq %r14, %rdi
movq %rcx, %rdx
movl %r13d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0xe8fd
cltq
addq %rax, 0x4b0(%r14)
movq 0x18(%r14), %rdx
leaq 0x2723(%rip), %r9       # 0x15f7d
movq %r9, (%rsp)
leaq 0x2bd6(%rip), %rsi      # 0x1643b
leaq 0x2a96(%rip), %rcx      # 0x16302
leaq 0x2c19(%rip), %r8       # 0x1648c
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8fd
cltq
addq %rax, 0x4b0(%r14)
movslq %r12d, %rbx
leaq 0xe9a(%rip), %rcx       # 0x1472a
movl $0x28, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5980
testl %ebx, %ebx
jle 0x13af7
movl %r12d, %eax
movq %rax, 0x28(%rsp)
leaq 0x90(%rsp), %rbx
leaq 0x50(%rsp), %r14
xorl %ecx, %ecx
movq %r15, 0x30(%rsp)
leaq (%rcx,%rcx,4), %rax
leaq (%r15,%rax,8), %r15
cmpl $0x0, 0x10(%r15)
movq %rcx, 0x38(%rsp)
je 0x138f3
movq (%r15), %rdi
movl $0x40, %edx
movq %rbx, %rsi
leaq 0x2e86(%rip), %rcx      # 0x16770
leaq 0x2bc6(%rip), %r8       # 0x164b7
jmp 0x13916
movq 0x18(%r15), %r8
cmpq $0x3ff, %r8             # imm = 0x3FF
jg 0x13a82
movq (%r15), %rdi
movl $0x40, %edx
movq %rbx, %rsi
leaq 0x2bad(%rip), %rcx      # 0x164c3
xorl %eax, %eax
callq 0x131fd
leaq 0x20(%r15), %rdi
callq 0x5340
movl $0x40, %esi
movq %r14, %rdi
leaq 0x2ba4(%rip), %rdx      # 0x164d9
movq %rax, %rcx
callq 0x53a0
movq 0x8(%r15), %rax
movq %rax, 0x18(%rsp)
movb (%rax), %r13b
leaq 0xd0(%rsp), %r12
testb %r13b, %r13b
je 0x139fa
callq 0x5520
movq %rax, %r14
leaq 0xd0(%rsp), %r12
movq 0x18(%rsp), %rbx
xorl %ebp, %ebp
movq (%r14), %rax
movzbl %r13b, %esi
testb $0x8, (%rax,%rsi,2)
jne 0x13994
movl $0xa, %edx
leaq 0x2bb3(%rip), %rdi      # 0x1653d
callq 0x5540
testq %rax, %rax
je 0x139c1
movb %r13b, (%r12)
leaq (%rsp,%rbp), %r12
addq $0xd1, %r12
movb 0x1(%rbx), %r13b
testb %r13b, %r13b
je 0x139fa
incq %rbx
leaq 0x1(%rbp), %rax
cmpq $0xffe, %rbp            # imm = 0xFFE
movq %rax, %rbp
jl 0x13971
jmp 0x139fa
cmpq $0xffc, %rbp            # imm = 0xFFC
jg 0x13998
movb $0x25, (%r12)
movzbl (%rbx), %eax
shrl $0x4, %eax
leaq 0x212f(%rip), %rcx      # 0x15b0b
movb (%rax,%rcx), %al
movb %al, 0x1(%r12)
movzbl (%rbx), %eax
andl $0xf, %eax
movb (%rax,%rcx), %al
movb %al, 0xd2(%rsp,%rbp)
addq $0x2, %rbp
jmp 0x13998
movb $0x0, (%r12)
movq (%r15), %rdi
cmpl $0x0, 0x10(%r15)
movq 0x18(%rdi), %rdx
leaq 0x24fb(%rip), %r8       # 0x15f0d
leaq 0x2829(%rip), %rax      # 0x16242
cmoveq %rax, %r8
leaq 0x90(%rsp), %rbx
movq %rbx, 0x10(%rsp)
leaq 0x50(%rsp), %r14
movq %r14, 0x8(%rsp)
movq %r8, (%rsp)
leaq 0x2aa9(%rip), %rsi      # 0x164e8
leaq 0xd0(%rsp), %rcx
movq 0x18(%rsp), %r9
xorl %eax, %eax
callq 0xe8fd
cltq
movq (%r15), %rcx
addq %rax, 0x4b0(%rcx)
movq 0x8(%r15), %rdi
callq 0x55d0
movq 0x38(%rsp), %rcx
incq %rcx
cmpq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r15
jne 0x138c4
jmp 0x13af7
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
cmpq $0xfffff, %r8           # imm = 0xFFFFF
jg 0x13aaf
movq (%r15), %rdi
mulsd 0x1cc2(%rip), %xmm0     # 0x15760
movl $0x40, %edx
movq %rbx, %rsi
leaq 0x2a1a(%rip), %rcx      # 0x164c7
jmp 0x13aeb
movq (%r15), %rdi
cmpq $0x3fffffff, %r8        # imm = 0x3FFFFFFF
jg 0x13ad4
mulsd 0x1c95(%rip), %xmm0     # 0x15758
movl $0x40, %edx
movq %rbx, %rsi
leaq 0x29fb(%rip), %rcx      # 0x164cd
jmp 0x13aeb
mulsd 0x1c74(%rip), %xmm0     # 0x15750
movl $0x40, %edx
movq %rbx, %rsi
leaq 0x29e8(%rip), %rcx      # 0x164d3
movb $0x1, %al
callq 0x131fd
jmp 0x1391d
movq %r15, %rdi
callq 0x55d0
leaq 0x2c6a(%rip), %rsi      # 0x16770
leaq 0x2990(%rip), %rdx      # 0x1649d
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8fd
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 0x2739(%rip), %rdx      # 0x16286
leaq 0x2c1c(%rip), %rcx      # 0x16770
leaq 0x27aa(%rip), %r8       # 0x16305
movq 0x20(%rsp), %rdi
movl $0x1f4, %esi            # imm = 0x1F4
xorl %eax, %eax
callq 0x12ea8
jmp 0x13b2c