// File: /workspace/github_c_makefile_star2_O3/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_send_directory_listing
static void mg_send_directory_listing(struct mg_connection *nc, const char *dir,
                                      struct http_message *hm,
                                      struct mg_serve_http_opts *opts) {
  static const char *sort_js_code =
      "<script>function srt(tb, sc, so, d) {"
      "var tr = Array.prototype.slice.call(tb.rows, 0),"
      "tr = tr.sort(function (a, b) { var c1 = a.cells[sc], c2 = b.cells[sc],"
      "n1 = c1.getAttribute('name'), n2 = c2.getAttribute('name'), "
      "t1 = a.cells[2].getAttribute('name'), "
      "t2 = b.cells[2].getAttribute('name'); "
      "return so * (t1 < 0 && t2 >= 0 ? -1 : t2 < 0 && t1 >= 0 ? 1 : "
      "n1 ? parseInt(n2) - parseInt(n1) : "
      "c1.textContent.trim().localeCompare(c2.textContent.trim())); });";
  static const char *sort_js_code2 =
      "for (var i = 0; i < tr.length; i++) tb.appendChild(tr[i]); "
      "if (!d) window.location.hash = ('sc=' + sc + '&so=' + so); "
      "};"
      "window.onload = function() {"
      "var tb = document.getElementById('tb');"
      "var m = /sc=([012]).so=(1|-1)/.exec(window.location.hash) || [0, 2, 1];"
      "var sc = m[1], so = m[2]; document.onclick = function(ev) { "
      "var c = ev.target.rel; if (c) {if (c == sc) so *= -1; srt(tb, c, so); "
      "sc = c; ev.preventDefault();}};"
      "srt(tb, sc, so, true);"
      "}"
      "</script>";

  mg_send_response_line(nc, 200, opts->extra_headers);
  mg_printf(nc, "%s: %s\r\n%s: %s\r\n\r\n", "Transfer-Encoding", "chunked",
            "Content-Type", "text/html; charset=utf-8");

  mg_printf_http_chunk(
      nc,
      "<html><head><title>Index of %.*s</title>%s%s"
      "<style>th,td {text-align: left; padding-right: 1em; "
      "font-family: monospace; }</style></head>\n"
      "<body><h1>Index of %.*s</h1>\n<table cellpadding=0><thead>"
      "<tr><th><a href=# rel=0>Name</a></th><th>"
      "<a href=# rel=1>Modified</a</th>"
      "<th><a href=# rel=2>Size</a></th></tr>"
      "<tr><td colspan=3><hr></td></tr>\n"
      "</thead>\n"
      "<tbody id=tb>",
      (int) hm->uri.len, hm->uri.p, sort_js_code, sort_js_code2,
      (int) hm->uri.len, hm->uri.p);
  mg_scan_directory(nc, dir, opts, mg_print_dir_entry);
  mg_printf_http_chunk(nc,
                       "</tbody><tr><td colspan=3><hr></td></tr>\n"
                       "</table>\n"
                       "<address>%s</address>\n"
                       "</body></html>",
                       mg_version_header);
  mg_send_http_chunk(nc, "", 0);
  /* TODO(rojer): Remove when cesanta/dev/issues/197 is fixed. */
  nc->flags |= MG_F_SEND_AND_CLOSE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2138, %rsp           # imm = 0x2138
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r14
movq 0x78(%rcx), %rdi
callq 0x6b26
movq %rdx, %rcx
movq %r14, %rdi
movl $0xc8, %esi
movq %rax, %rdx
callq 0xc8f6
leaq 0x3a3d(%rip), %rsi      # 0x16e4a
leaq 0x2607(%rip), %rdx      # 0x15a1b
leaq 0x2612(%rip), %rcx      # 0x15a2d
leaq 0x3532(%rip), %r8       # 0x16954
leaq 0x3a34(%rip), %r9       # 0x16e5d
movq %r14, %rdi
xorl %eax, %eax
callq 0x7ff5
movl 0x38(%rbx), %edx
movq 0x30(%rbx), %rcx
leaq 0x3a35(%rip), %rsi      # 0x16e76
leaq 0x3679(%rip), %r8       # 0x16ac1
leaq 0x3837(%rip), %r9       # 0x16c86
movq %r14, %rdi
xorl %eax, %eax
pushq %rcx
pushq %rdx
callq 0xd31a
addq $0x10, %rsp
leaq 0x3bd0(%rip), %rsi      # 0x17036
leaq 0x2161(%rip), %rdx      # 0x155ce
movl $0x3, %edi
callq 0x4447
testl %eax, %eax
je 0x1348f
leaq 0x337a(%rip), %rdi      # 0x167fc
movq %r14, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x4539
movq %rbp, %rdi
callq 0x3170
testq %rax, %rax
je 0x13724
movq %rax, %r12
movq %r14, 0x8(%rsp)
movq %rax, %rdi
callq 0x3510
testq %rax, %rax
je 0x13715
movq %rax, %r14
movq %rbp, 0x18(%rsp)
leaq 0x13(%r14), %rbx
movq 0x10(%r15), %rsi
movq 0x58(%r15), %rdx
movq %rbx, %rdi
movl $0x1, %ecx
callq 0x13321
testl %eax, %eax
jne 0x13701
movl $0x1000, %esi           # imm = 0x1000
leaq 0x1130(%rsp), %r13
movq %r13, %rdi
leaq 0x3356(%rip), %rdx      # 0x1684f
movq %rbp, %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x31e0
movq %r13, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x31b0
testl %eax, %eax
jne 0x13701
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x1355f
movabsq $0x4f5443455249445b, %rax # imm = 0x4F5443455249445B
movq %rax, 0x20(%rsp)
movl $0x5d5952, 0x28(%rsp)   # imm = 0x5D5952
movq $-0x1, 0x10(%rsp)
leaq 0x2bbc(%rip), %rbp      # 0x16116
jmp 0x13608
movq 0xd0(%rsp), %rcx
cmpq $0x3ff, %rcx            # imm = 0x3FF
movq %rcx, 0x10(%rsp)
jg 0x1358f
movl $0x40, %esi
leaq 0x20(%rsp), %rdi
leaq 0x2e73(%rip), %rdx      # 0x163f9
xorl %eax, %eax
callq 0x31e0
jmp 0x13601
cmpq $0xfffff, %rcx          # imm = 0xFFFFF
ja 0x135b8
cvtsi2sd %rcx, %xmm0
mulsd 0x1d8b(%rip), %xmm0     # 0x15330
movl $0x40, %esi
leaq 0x20(%rsp), %rdi
leaq 0x3ab3(%rip), %rdx      # 0x17069
jmp 0x135fa
cvtsi2sd %rcx, %xmm0
cmpq $0x3fffffff, %rcx       # imm = 0x3FFFFFFF
ja 0x135e1
mulsd 0x1d5a(%rip), %xmm0     # 0x15328
movl $0x40, %esi
leaq 0x20(%rsp), %rdi
leaq 0x3a90(%rip), %rdx      # 0x1706f
jmp 0x135fa
mulsd 0x1d37(%rip), %xmm0     # 0x15320
movl $0x40, %esi
leaq 0x20(%rsp), %rdi
leaq 0x3a7b(%rip), %rdx      # 0x17075
movb $0x1, %al
callq 0x31e0
leaq 0x31e5(%rip), %rbp      # 0x167ed
leaq 0xf8(%rsp), %rdi
callq 0x3080
movl $0x40, %esi
leaq 0x60(%rsp), %rdi
leaq 0x3a55(%rip), %rdx      # 0x1707b
movq %rax, %rcx
callq 0x34e0
movb (%rbx), %al
testb %al, %al
je 0x13696
addq $0x14, %r14
xorl %r13d, %r13d
leaq (%rsp,%r13), %rdi
addq $0x130, %rdi            # imm = 0x130
cmpb $0x3c, %al
jne 0x1366c
movl $0x1000, %esi           # imm = 0x1000
subq %r13, %rsi
leaq 0x29ba(%rip), %rdx      # 0x16013
leaq 0x26fa(%rip), %rcx      # 0x15d5a
xorl %eax, %eax
callq 0x31e0
movslq %eax, %rdx
jmp 0x13673
movb %al, (%rdi)
movl $0x1, %edx
leaq (%rdx,%r13), %rcx
movb (%r14), %al
testb %al, %al
je 0x13698
incq %r14
addq %r13, %rdx
addq $0x5, %rdx
movq %rcx, %r13
cmpq $0x1000, %rdx           # imm = 0x1000
jb 0x1363b
jmp 0x13698
xorl %ecx, %ecx
movb $0x0, 0x130(%rsp,%rcx)
movq %rbx, %rdi
callq 0x6b26
movq %rax, %rdi
movq %rdx, %rsi
callq 0x10ec3
movq %rax, %rbx
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
leaq 0x39c4(%rip), %rsi      # 0x1708a
movq %rax, %rdx
movq %rbp, %rcx
leaq 0x138(%rsp), %r8
movq %rbp, %r9
xorl %eax, %eax
leaq 0x28(%rsp), %r10
pushq %r10
pushq 0x20(%rsp)
leaq 0x78(%rsp), %r10
pushq %r10
callq 0xd31a
addq $0x20, %rsp
movq %rbx, %rdi
callq 0x3060
movq 0x18(%rsp), %rbp
movq %r12, %rdi
callq 0x3510
movq %rax, %r14
testq %rax, %rax
jne 0x134c1
movq %r12, %rdi
callq 0x3290
movq 0x8(%rsp), %r14
jmp 0x1375b
leaq 0x390b(%rip), %rsi      # 0x17036
leaq 0x1e9c(%rip), %rdx      # 0x155ce
movl $0x3, %edi
callq 0x4447
testl %eax, %eax
je 0x1375b
callq 0x30a0
movl (%rax), %ecx
leaq 0x38fa(%rip), %rdi      # 0x17048
movq %r14, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x4539
leaq 0x387d(%rip), %rsi      # 0x16fdf
leaq 0x2dc2(%rip), %rdx      # 0x1652b
movq %r14, %rdi
xorl %eax, %eax
callq 0xd31a
leaq 0x3073(%rip), %rsi      # 0x167ed
movq %r14, %rdi
xorl %edx, %edx
callq 0xd247
orb $0x4, 0xc9(%r14)
addq $0x2138, %rsp           # imm = 0x2138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq