// File: nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: handle_file_request
static void handle_file_request(struct mg_connection *conn, const char *path,
                                struct mgstat *stp) {
  char date[64], lm[64], etag[64], range[64];
  const char *fmt = "%a, %d %b %Y %H:%M:%S %Z", *msg = "OK", *hdr;
  time_t curtime = time(NULL);
  int64_t cl, r1, r2;
  struct vec mime_vec;
  FILE *fp;
  int n;

  get_mime_type(conn->ctx, path, &mime_vec);
  cl = stp->size;
  conn->request_info.status_code = 200;
  range[0] = '\0';

  if ((fp = mg_fopen(path, "rb")) == NULL) {
    send_http_error(conn, 500, http_500_error,
        "fopen(%s): %s", path, strerror(ERRNO));
    return;
  }
  set_close_on_exec(fileno(fp));

  // If Range: header specified, act accordingly
  r1 = r2 = 0;
  hdr = mg_get_header(conn, "Range");
  if (hdr != NULL && (n = parse_range_header(hdr, &r1, &r2)) > 0) {
    conn->request_info.status_code = 206;
    (void) fseeko(fp, (off_t) r1, SEEK_SET);
    cl = n == 2 ? r2 - r1 + 1: cl - r1;
    (void) mg_snprintf(conn, range, sizeof(range),
        "Content-Range: bytes "
        "%" INT64_FMT "-%"
        INT64_FMT "/%" INT64_FMT "\r\n",
        r1, r1 + cl - 1, stp->size);
    msg = "Partial Content";
  }

  // Prepare Etag, Date, Last-Modified headers
  (void) strftime(date, sizeof(date), fmt, localtime(&curtime));
  (void) strftime(lm, sizeof(lm), fmt, localtime(&stp->mtime));
  (void) mg_snprintf(conn, etag, sizeof(etag), "%lx.%lx",
      (unsigned long) stp->mtime, (unsigned long) stp->size);

  (void) mg_printf(conn,
      "HTTP/1.1 %d %s\r\n"
      "Date: %s\r\n"
      "Last-Modified: %s\r\n"
      "Etag: \"%s\"\r\n"
      "Content-Type: %.*s\r\n"
      "Content-Length: %" INT64_FMT "\r\n"
      "Connection: %s\r\n"
      "Accept-Ranges: bytes\r\n"
      "%s\r\n",
      conn->request_info.status_code, msg, date, lm, etag,
      mime_vec.len, mime_vec.ptr, cl, suggest_connection_header(conn), range);

  if (strcmp(conn->request_info.request_method, "HEAD") != 0) {
    send_file_data(conn, fp, cl);
  }
  (void) fclose(fp);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x220, %rsp            # imm = 0x220
movq %rdi, 0x218(%rsp)
movq %rsi, 0x210(%rsp)
movq %rdx, 0x208(%rsp)
leaq 0x4e39(%rip), %rax      # 0x239b4
movq %rax, 0xf8(%rsp)
leaq 0x3a81(%rip), %rax      # 0x2260b
movq %rax, 0xf0(%rsp)
xorl %eax, %eax
movl %eax, %edi
callq 0x54f0
movq %rax, 0xe0(%rsp)
movq 0x218(%rsp), %rax
movq 0x458(%rax), %rdi
movq 0x210(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0x213e0
movq 0x208(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0xd8(%rsp)
movq 0x218(%rsp), %rax
movl $0xc8, 0x4c(%rax)
movb $0x0, 0x100(%rsp)
movq 0x210(%rsp), %rdi
leaq 0x496c(%rip), %rsi      # 0x2356d
callq 0x56b0
movq %rax, 0xb0(%rsp)
cmpq $0x0, %rax
jne 0x1ec82
movq 0x218(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0xce35(%rip), %rax      # 0x2ba60
movq %rax, 0x98(%rsp)
movq 0x210(%rsp), %rax
movq %rax, 0xa0(%rsp)
callq 0x5060
movl (%rax), %edi
callq 0x5190
movq 0x90(%rsp), %rdi
movq 0x98(%rsp), %rdx
movq 0xa0(%rsp), %r8
movq %rax, %r9
movl $0x1f4, %esi            # imm = 0x1F4
leaq 0x4c80(%rip), %rcx      # 0x238f6
movb $0x0, %al
callq 0x1bc40
jmp 0x1efaa
movq 0xb0(%rsp), %rdi
callq 0x55c0
movl %eax, %edi
callq 0x1ae20
movq $0x0, 0xc8(%rsp)
movq $0x0, 0xd0(%rsp)
movq 0x218(%rsp), %rdi
leaq 0x45bd(%rip), %rsi      # 0x2327a
callq 0x16b70
movq %rax, 0xe8(%rsp)
cmpq $0x0, 0xe8(%rsp)
je 0x1edda
movq 0xe8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
leaq 0xc8(%rsp), %rdx
callq 0x1f940
movl %eax, 0xac(%rsp)
cmpl $0x0, %eax
jle 0x1edda
movq 0x218(%rsp), %rax
movl $0xce, 0x4c(%rax)
movq 0xb0(%rsp), %rdi
movq 0xd0(%rsp), %rsi
xorl %edx, %edx
callq 0x5300
cmpl $0x2, 0xac(%rsp)
jne 0x1ed54
movq 0xc8(%rsp), %rax
subq 0xd0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x88(%rsp)
jmp 0x1ed6c
movq 0xd8(%rsp), %rax
subq 0xd0(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x218(%rsp), %rdi
leaq 0x100(%rsp), %rsi
movq 0xd0(%rsp), %r8
movq 0xd0(%rsp), %r9
addq 0xd8(%rsp), %r9
subq $0x1, %r9
movq 0x208(%rsp), %rax
movq 0x8(%rax), %rax
movl $0x40, %edx
leaq 0x4c0d(%rip), %rcx      # 0x239cd
movq %rax, (%rsp)
movb $0x0, %al
callq 0x1cc90
leaq 0x4c1e(%rip), %rax      # 0x239f0
movq %rax, 0xf0(%rsp)
leaq 0x1c0(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x53c0
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rdx
movq %rax, %rcx
movl $0x40, %esi
callq 0x5440
leaq 0x180(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x208(%rsp), %rdi
addq $0x10, %rdi
callq 0x53c0
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rdx
movq %rax, %rcx
movl $0x40, %esi
callq 0x5440
movq 0x218(%rsp), %rdi
leaq 0x140(%rsp), %rsi
movq 0x208(%rsp), %rax
movq 0x10(%rax), %r8
movq 0x208(%rsp), %rax
movq 0x8(%rax), %r9
movl $0x40, %edx
leaq 0x4b72(%rip), %rcx      # 0x23a00
movb $0x0, %al
callq 0x1cc90
movq 0x218(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x218(%rsp), %rax
movl 0x4c(%rax), %eax
movl %eax, 0x64(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x1c0(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x180(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x140(%rsp), %r15
movq 0xc0(%rsp), %r14
movq 0xb8(%rsp), %rbx
movq 0xd8(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x218(%rsp), %rdi
callq 0x1cd70
movq 0x58(%rsp), %rdi
movl 0x64(%rsp), %edx
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %r8
movq 0x78(%rsp), %r9
movq 0x80(%rsp), %r11
movq %rax, %r10
leaq 0x100(%rsp), %rax
leaq 0x4ac9(%rip), %rsi      # 0x23a08
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movb $0x0, %al
callq 0x17030
movq 0x218(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x3faa(%rip), %rsi      # 0x22f20
callq 0x5810
cmpl $0x0, %eax
je 0x1ef9d
movq 0x218(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
callq 0x207c0
movq 0xb0(%rsp), %rdi
callq 0x5560
addq $0x220, %rsp            # imm = 0x220
popq %rbx
popq %r14
popq %r15
retq
nopw (%rax,%rax)