// File: /workspace/github_c_makefile_star2_O1/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 0x6be8
movq %rdx, %rcx
movq %r14, %rdi
movl $0xc8, %esi
movq %rax, %rdx
callq 0xcb88
leaq 0x3f03(%rip), %rsi      # 0x16e2c
leaq 0x2aca(%rip), %rdx      # 0x159fa
leaq 0x2ad5(%rip), %rcx      # 0x15a0c
leaq 0x39f8(%rip), %r8       # 0x16936
leaq 0x3efa(%rip), %r9       # 0x16e3f
movq %r14, %rdi
xorl %eax, %eax
callq 0x80d3
movl 0x38(%rbx), %edx
movq 0x30(%rbx), %rcx
leaq 0x3efb(%rip), %rsi      # 0x16e58
leaq 0x3b3f(%rip), %r8       # 0x16aa3
leaq 0x3cfd(%rip), %r9       # 0x16c68
movq %r14, %rdi
xorl %eax, %eax
pushq %rcx
pushq %rdx
callq 0xd4cb
addq $0x10, %rsp
leaq 0x4096(%rip), %rsi      # 0x17018
leaq 0x2621(%rip), %rdx      # 0x155aa
movl $0x3, %edi
callq 0x44b4
testl %eax, %eax
je 0x12fab
leaq 0x3840(%rip), %rdi      # 0x167de
movq %r14, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x45a0
movq %rbp, %rdi
callq 0x3170
testq %rax, %rax
je 0x13241
movq %rax, %r12
movq %r14, 0x8(%rsp)
movq %rax, %rdi
callq 0x3510
testq %rax, %rax
je 0x13232
movq %rax, %r14
movq %rbp, 0x18(%rsp)
leaq 0x13(%r14), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x12e30
testl %eax, %eax
jne 0x1321e
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %r13
leaq 0x1130(%rsp), %r15
movq %r15, %rdi
leaq 0x381e(%rip), %rdx      # 0x16831
movq %rbp, %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x31e0
movq %r15, %rdi
movq %r13, %r15
leaq 0xa0(%rsp), %rsi
callq 0x31b0
testl %eax, %eax
jne 0x1321e
movl 0xb8(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x1307c
movabsq $0x4f5443455249445b, %rax # imm = 0x4F5443455249445B
movq %rax, 0x20(%rsp)
movl $0x5d5952, 0x28(%rsp)   # imm = 0x5D5952
movq $-0x1, 0x10(%rsp)
leaq 0x3081(%rip), %rbp      # 0x160f8
jmp 0x13125
movq 0xd0(%rsp), %rcx
cmpq $0x3ff, %rcx            # imm = 0x3FF
movq %rcx, 0x10(%rsp)
jg 0x130ac
movl $0x40, %esi
leaq 0x20(%rsp), %rdi
leaq 0x3338(%rip), %rdx      # 0x163db
xorl %eax, %eax
callq 0x31e0
jmp 0x1311e
cmpq $0xfffff, %rcx          # imm = 0xFFFFF
jg 0x130d5
cvtsi2sd %rcx, %xmm0
mulsd 0x226e(%rip), %xmm0     # 0x15330
movl $0x40, %esi
leaq 0x20(%rsp), %rdi
leaq 0x3f78(%rip), %rdx      # 0x1704b
jmp 0x13117
cvtsi2sd %rcx, %xmm0
cmpq $0x3fffffff, %rcx       # imm = 0x3FFFFFFF
jg 0x130fe
mulsd 0x223d(%rip), %xmm0     # 0x15328
movl $0x40, %esi
leaq 0x20(%rsp), %rdi
leaq 0x3f55(%rip), %rdx      # 0x17051
jmp 0x13117
mulsd 0x221a(%rip), %xmm0     # 0x15320
movl $0x40, %esi
leaq 0x20(%rsp), %rdi
leaq 0x3f40(%rip), %rdx      # 0x17057
movb $0x1, %al
callq 0x31e0
leaq 0x36aa(%rip), %rbp      # 0x167cf
leaq 0xf8(%rsp), %rdi
callq 0x3080
movl $0x40, %esi
leaq 0x60(%rsp), %rdi
leaq 0x3f1a(%rip), %rdx      # 0x1705d
movq %rax, %rcx
callq 0x34e0
movb (%rbx), %al
testb %al, %al
je 0x131b3
addq $0x14, %r14
xorl %r13d, %r13d
leaq (%rsp,%r13), %rdi
addq $0x130, %rdi            # imm = 0x130
cmpb $0x3c, %al
jne 0x13189
movl $0x1000, %esi           # imm = 0x1000
subq %r13, %rsi
leaq 0x2e7c(%rip), %rdx      # 0x15ff2
leaq 0x2bbc(%rip), %rcx      # 0x15d39
xorl %eax, %eax
callq 0x31e0
movslq %eax, %rdx
jmp 0x13190
movb %al, (%rdi)
movl $0x1, %edx
leaq (%rdx,%r13), %rcx
movb (%r14), %al
testb %al, %al
je 0x131b5
incq %r14
addq %r13, %rdx
addq $0x5, %rdx
movq %rcx, %r13
cmpq $0x1000, %rdx           # imm = 0x1000
jb 0x13158
jmp 0x131b5
xorl %ecx, %ecx
movb $0x0, 0x130(%rsp,%rcx)
movq %rbx, %rdi
callq 0x6be8
movq %rax, %rdi
movq %rdx, %rsi
callq 0x10eb9
movq %rax, %rbx
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
leaq 0x3e89(%rip), %rsi      # 0x1706c
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 0xd4cb
addq $0x20, %rsp
movq %rbx, %rdi
callq 0x3060
movq 0x18(%rsp), %rbp
movq %r12, %rdi
callq 0x3510
movq %rax, %r14
testq %rax, %rax
jne 0x12fdd
movq %r12, %rdi
callq 0x3290
movq 0x8(%rsp), %r14
jmp 0x13278
leaq 0x3dd0(%rip), %rsi      # 0x17018
leaq 0x235b(%rip), %rdx      # 0x155aa
movl $0x3, %edi
callq 0x44b4
testl %eax, %eax
je 0x13278
callq 0x30a0
movl (%rax), %ecx
leaq 0x3dbf(%rip), %rdi      # 0x1702a
movq %r14, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x45a0
leaq 0x3d42(%rip), %rsi      # 0x16fc1
leaq 0x3287(%rip), %rdx      # 0x1650d
movq %r14, %rdi
xorl %eax, %eax
callq 0xd4cb
leaq 0x3538(%rip), %rsi      # 0x167cf
movq %r14, %rdi
xorl %edx, %edx
callq 0xd461
orb $0x4, 0xc9(%r14)
addq $0x2138, %rsp           # imm = 0x2138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq