// File: /workspace/github_c_makefile_star2_O3/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_http_serve_file
void mg_http_serve_file(struct mg_connection *nc, struct http_message *hm,
                        const char *path, const struct mg_str mime_type,
                        const struct mg_str extra_headers) {
  struct mg_http_proto_data *pd = mg_http_get_proto_data(nc);
  cs_stat_t st;
  LOG(LL_DEBUG, ("%p [%s] %.*s", nc, path, (int) mime_type.len, mime_type.p));
  if (mg_stat(path, &st) != 0 || (pd->file.fp = mg_fopen(path, "rb")) == NULL) {
    int code, err = mg_get_errno();
    switch (err) {
      case EACCES:
        code = 403;
        break;
      case ENOENT:
        code = 404;
        break;
      default:
        code = 500;
    };
    mg_http_send_error(nc, code, "Open failed");
  } else {
    char etag[50], current_time[50], last_modified[50], range[70];
    time_t t = (time_t) mg_time();
    int64_t r1 = 0, r2 = 0, cl = st.st_size;
    struct mg_str *range_hdr = mg_get_http_header(hm, "Range");
    int n, status_code = 200;

    /* Handle Range header */
    range[0] = '\0';
    if (range_hdr != NULL &&
        (n = mg_http_parse_range_header(range_hdr, &r1, &r2)) > 0 && r1 >= 0 &&
        r2 >= 0) {
      /* If range is specified like "400-", set second limit to content len */
      if (n == 1) {
        r2 = cl - 1;
      }
      if (r1 > r2 || r2 >= cl) {
        status_code = 416;
        cl = 0;
        snprintf(range, sizeof(range),
                 "Content-Range: bytes */%" INT64_FMT "\r\n",
                 (int64_t) st.st_size);
      } else {
        status_code = 206;
        cl = r2 - r1 + 1;
        snprintf(range, sizeof(range), "Content-Range: bytes %" INT64_FMT
                                       "-%" INT64_FMT "/%" INT64_FMT "\r\n",
                 r1, r1 + cl - 1, (int64_t) st.st_size);
#if _FILE_OFFSET_BITS == 64 || _POSIX_C_SOURCE >= 200112L || \
    _XOPEN_SOURCE >= 600
        fseeko(pd->file.fp, r1, SEEK_SET);
#else
        fseek(pd->file.fp, (long) r1, SEEK_SET);
#endif
      }
    }

#if !MG_DISABLE_HTTP_KEEP_ALIVE
    {
      struct mg_str *conn_hdr = mg_get_http_header(hm, "Connection");
      if (conn_hdr != NULL) {
        pd->file.keepalive = (mg_vcasecmp(conn_hdr, "keep-alive") == 0);
      } else {
        pd->file.keepalive = (mg_vcmp(&hm->proto, "HTTP/1.1") == 0);
      }
    }
#endif

    mg_http_construct_etag(etag, sizeof(etag), &st);
    mg_gmt_time_string(current_time, sizeof(current_time), &t);
    mg_gmt_time_string(last_modified, sizeof(last_modified), &st.st_mtime);
    /*
     * Content length casted to size_t because:
     * 1) that's the maximum buffer size anyway
     * 2) ESP8266 RTOS SDK newlib vprintf cannot contain a 64bit arg at non-last
     *    position
     * TODO(mkm): fix ESP8266 RTOS SDK
     */
    mg_send_response_line_s(nc, status_code, extra_headers);
    mg_printf(nc,
              "Date: %s\r\n"
              "Last-Modified: %s\r\n"
              "Accept-Ranges: bytes\r\n"
              "Content-Type: %.*s\r\n"
              "Connection: %s\r\n"
              "Content-Length: %" SIZE_T_FMT
              "\r\n"
              "%sEtag: %s\r\n\r\n",
              current_time, last_modified, (int) mime_type.len, mime_type.p,
              (pd->file.keepalive ? "keep-alive" : "close"), (size_t) cl, range,
              etag);

    pd->file.cl = cl;
    pd->file.type = DATA_FILE;
    mg_http_transfer_file_data(nc);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %r8, %r12
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %rbx
movq 0x88(%rdi), %r14
testq %r14, %r14
jne 0xcc47
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq %rax, %r14
movq %rax, 0x88(%rbx)
leaq 0x64be(%rip), %rax      # 0x130fe
movq %rax, 0x90(%rbx)
leaq 0x9000(%rip), %rsi      # 0x15c4e
leaq 0x8979(%rip), %rdx      # 0x155ce
movl $0x3, %edi
callq 0x4447
testl %eax, %eax
je 0xcc7d
leaq 0x8ff7(%rip), %rdi      # 0x15c61
movq %rbx, %rsi
movq %rbp, %rdx
movl %r12d, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0x4539
leaq 0xd0(%rsp), %rsi
movq %rbp, %rdi
callq 0x31b0
testl %eax, %eax
je 0xccd3
callq 0x30a0
movl (%rax), %eax
cmpl $0x2, %eax
movl $0x194, %ecx            # imm = 0x194
movl $0x1f4, %edx            # imm = 0x1F4
cmovel %ecx, %edx
cmpl $0xd, %eax
movl $0x193, %esi            # imm = 0x193
cmovnel %edx, %esi
leaq 0x8fb7(%rip), %rdx      # 0x15c71
movq %rbx, %rdi
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xcb3c
leaq 0x8f94(%rip), %rsi      # 0x15c6e
movq %rbp, %rdi
callq 0x3370
movq %rax, (%r14)
testq %rax, %rax
je 0xcc91
movq %r15, 0x30(%rsp)
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq 0x100(%rsp), %rbp
leaq 0x8f64(%rip), %rsi      # 0x15c7d
movq %r13, %rdi
callq 0xb896
movb $0x0, 0x40(%rsp)
movl $0xc8, 0xc(%rsp)
testq %rax, %rax
movq %r12, 0x28(%rsp)
je 0xce32
movq %rax, %r15
movq %rbp, 0x10(%rsp)
movq 0x8(%rax), %r12
leaq 0x1(%r12), %rdi
callq 0x33e0
testq %rax, %rax
je 0xce2d
movq %rax, %rbp
movq (%r15), %rsi
movq %rax, %rdi
movq %r12, %rdx
callq 0x3380
movb $0x0, (%rbp,%r12)
leaq 0x97c0(%rip), %rsi      # 0x16539
leaq 0x18(%rsp), %rdx
leaq 0x20(%rsp), %r12
movq %rbp, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x3420
movl %eax, %r15d
movq %rbp, %rdi
callq 0x3060
movl %r15d, %edi
leaq 0x18(%rsp), %rax
movq (%rax), %rcx
movq (%r12), %r8
testl %r15d, %r15d
setg %al
testq %rcx, %rcx
setns %dl
testq %r8, %r8
setns %sil
andb %dl, %sil
andb %al, %sil
movq 0x10(%rsp), %rbp
movq %rbp, %r15
cmpb $0x1, %sil
jne 0xce35
cmpl $0x1, %edi
jne 0xcddf
leaq -0x1(%rbp), %r8
movq %r8, 0x20(%rsp)
movq %r8, %r15
subq %rcx, %r15
setge %al
cmpq %rbp, %r8
setl %dl
testb %dl, %al
je 0xcf8d
incq %r15
leaq 0x8ea0(%rip), %rdx      # 0x15ca0
leaq 0x40(%rsp), %rdi
movl $0x46, %esi
movq %rbp, %r9
xorl %eax, %eax
callq 0x31e0
movq (%r14), %rdi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x3490
movl $0xce, 0xc(%rsp)
jmp 0xce35
movq 0x10(%rsp), %rbp
movq %rbp, %r15
leaq 0x8e87(%rip), %rsi      # 0x15cc3
movq %r13, %rdi
callq 0xb896
testq %rax, %rax
movq %r15, 0x10(%rsp)
je 0xce5f
leaq 0x8e79(%rip), %rsi      # 0x15cce
movq %rax, %rdi
callq 0x6b88
jmp 0xce72
addq $0x40, %r13
leaq 0x9a1e(%rip), %rsi      # 0x16888
movq %r13, %rdi
callq 0x6b4a
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movl %ecx, 0x18(%r14)
leaq 0x128(%rsp), %r13
movq (%r13), %rcx
leaq 0x96b7(%rip), %rdx      # 0x16547
leaq 0x90(%rsp), %rdi
movl $0x32, %esi
movq %rbp, %r8
xorl %eax, %eax
callq 0x31e0
leaq 0x38(%rsp), %rdi
callq 0x3530
leaq 0x9699(%rip), %r15      # 0x16551
leaq 0x1a0(%rsp), %rbp
movl $0x32, %esi
movq %rbp, %rdi
movq %r15, %rdx
movq %rax, %rcx
callq 0x34e0
movq %r13, %rdi
callq 0x3530
leaq 0x160(%rsp), %r12
movl $0x32, %esi
movq %r12, %rdi
movq %r15, %rdx
movq %rax, %rcx
callq 0x34e0
leaq 0x210(%rsp), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rcx
movq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0xc8f6
cmpl $0x0, 0x18(%r14)
leaq 0x8d2b(%rip), %rax      # 0x15c48
leaq 0x8daa(%rip), %r10      # 0x15cce
cmoveq %rax, %r10
leaq 0x40(%rsp), %r11
leaq 0x8da5(%rip), %rsi      # 0x15cd9
movq %rbx, %rdi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r9
xorl %eax, %eax
leaq 0x90(%rsp), %r15
pushq %r15
pushq %r11
movq 0x20(%rsp), %r15
pushq %r15
pushq %r10
callq 0x7ff5
addq $0x20, %rsp
movq %r15, 0x8(%r14)
movl $0x1, 0x1c(%r14)
movq %rbx, %rdi
callq 0xc220
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8cef(%rip), %rdx      # 0x15c83
xorl %r15d, %r15d
leaq 0x40(%rsp), %rdi
movl $0x46, %esi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x31e0
movl $0x1a0, 0xc(%rsp)       # imm = 0x1A0
jmp 0xce35