// File: /workspace/github_c_makefile_star2_O2/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, %r13
movq %rsi, %rbp
movq %rdi, %rbx
callq 0xb106
movq %rax, %r14
leaq 0x8b1a(%rip), %rsi      # 0x14c31
leaq 0x8490(%rip), %rdx      # 0x145ae
pushq $0x3
popq %rdi
callq 0x4227
testl %eax, %eax
je 0xc144
leaq 0x8b13(%rip), %rdi      # 0x14c44
movq %rbx, %rsi
movq %r13, %rdx
movl %r12d, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0x430a
leaq 0x90(%rsp), %rsi
movq %r13, %rdi
callq 0x31b0
testl %eax, %eax
je 0xc198
callq 0x9306
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 0x8ad5(%rip), %rdx      # 0x14c54
movq %rbx, %rdi
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc060
leaq 0x8ab2(%rip), %rsi      # 0x14c51
movq %r13, %rdi
callq 0x3370
movq %rax, (%r14)
testq %rax, %rax
je 0xc158
callq 0x483c
cvttsd2si %xmm0, %rax
movq %rax, 0x38(%rsp)
andq $0x0, 0x20(%rsp)
andq $0x0, 0x18(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x8a82(%rip), %rsi      # 0x14c60
movq %rbp, %rdi
callq 0xb03c
movb $0x0, 0x40(%rsp)
movl $0xc8, 0xc(%rsp)
testq %rax, %rax
movq %r15, 0x30(%rsp)
movq %r12, 0x28(%rsp)
je 0xc318
movq %rax, %r13
movq 0x8(%rax), %r12
leaq 0x1(%r12), %rdi
callq 0x33e0
testq %rax, %rax
je 0xc26a
movq %rax, %r15
movq (%r13), %rsi
movq %rax, %rdi
movq %r12, %rdx
callq 0x3380
movb $0x0, (%r15,%r12)
leaq 0x92e2(%rip), %rsi      # 0x1551c
leaq 0x20(%rsp), %r12
leaq 0x18(%rsp), %rcx
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x3420
movl %eax, %r13d
movq %r15, %rdi
callq 0x3060
movq (%r12), %rcx
leaq 0x18(%rsp), %rax
movq (%rax), %r8
jmp 0xc272
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %r13d, %r13d
testl %r13d, %r13d
setg %al
testq %rcx, %rcx
setns %dl
andb %al, %dl
testq %r8, %r8
setns %al
andb %dl, %al
cmpb $0x1, %al
jne 0xc318
cmpl $0x1, %r13d
movq 0x10(%rsp), %r9
jne 0xc2a4
leaq -0x1(%r9), %r8
movq %r8, 0x18(%rsp)
movq %r8, %r15
subq %rcx, %r15
setge %al
cmpq %r9, %r8
setl %dl
testb %dl, %al
je 0xc2ee
incq %r15
leaq 0x89c2(%rip), %rdx      # 0x14c83
leaq 0x40(%rsp), %rdi
pushq $0x46
popq %rsi
xorl %eax, %eax
callq 0x31e0
movq (%r14), %rdi
movq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x3490
movl $0xce, 0xc(%rsp)
movq %r15, 0x10(%rsp)
jmp 0xc318
leaq 0x8971(%rip), %rdx      # 0x14c66
leaq 0x40(%rsp), %rdi
pushq $0x46
popq %rsi
movq %r9, %rcx
xorl %eax, %eax
callq 0x31e0
movl $0x1a0, 0xc(%rsp)       # imm = 0x1A0
movq $0x0, 0x10(%rsp)
leaq 0x210(%rsp), %r12
leaq 0x897f(%rip), %rsi      # 0x14ca6
movq %rbp, %rdi
callq 0xb03c
testq %rax, %rax
je 0xc345
leaq 0x8976(%rip), %rsi      # 0x14cb1
movq %rax, %rdi
callq 0x68d7
jmp 0xc358
addq $0x40, %rbp
leaq 0x951b(%rip), %rsi      # 0x1586b
movq %rbp, %rdi
callq 0x6899
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movl %ecx, 0x18(%r14)
leaq 0x1a0(%rsp), %rbp
pushq $0x32
popq %rsi
leaq 0x90(%rsp), %rdx
movq %rbp, %rdi
callq 0xc43d
leaq 0x160(%rsp), %r13
leaq 0x38(%rsp), %rsi
movq %r13, %rdi
callq 0xc453
leaq 0xe8(%rsp), %rsi
leaq 0x120(%rsp), %r15
movq %r15, %rdi
callq 0xc453
movq (%r12), %rdx
movq 0x8(%r12), %rcx
movq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0xbe80
cmpl $0x0, 0x18(%r14)
leaq 0x885f(%rip), %rax      # 0x14c2b
leaq 0x88de(%rip), %r10      # 0x14cb1
cmoveq %rax, %r10
leaq 0x40(%rsp), %r11
leaq 0x88d9(%rip), %rsi      # 0x14cbc
movq %rbx, %rdi
movq %r13, %rdx
movq %r15, %rcx
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r9
xorl %eax, %eax
pushq %rbp
pushq %r11
movq 0x20(%rsp), %r15
pushq %r15
pushq %r10
callq 0x7bce
addq $0x20, %rsp
movq %r15, 0x8(%r14)
movl $0x1, 0x1c(%r14)
movq %rbx, %rdi
callq 0xb973
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq