// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
cmpq $0x0, 0x88(%rdi)
jne 0xce5d
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq %rax, 0x88(%rbx)
leaq 0x5dcb(%rip), %rax      # 0x12c21
movq %rax, 0x90(%rbx)
movq 0x88(%rbx), %rbp
leaq 0x8dc2(%rip), %rsi      # 0x15c2d
leaq 0x8738(%rip), %rdx      # 0x155aa
movl $0x3, %edi
callq 0x44b4
testl %eax, %eax
je 0xce9a
leaq 0x8db9(%rip), %rdi      # 0x15c40
movq %rbx, %rsi
movq %r13, %rdx
movl %r15d, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0x45a0
leaq 0x90(%rsp), %rsi
movq %r13, %rdi
callq 0x31b0
testl %eax, %eax
je 0xcef0
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 0x8d79(%rip), %rdx      # 0x15c50
movq %rbx, %rdi
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xcd85
leaq 0x8d56(%rip), %rsi      # 0x15c4d
movq %r13, %rdi
callq 0x3370
movq %rax, (%rbp)
testq %rax, %rax
je 0xceae
movq %r15, 0x28(%rsp)
movq %r14, 0x30(%rsp)
callq 0x4b23
cvttsd2si %xmm0, %rax
movq %rax, 0x38(%rsp)
xorl %r13d, %r13d
movq %r13, 0x18(%rsp)
movq %r13, 0x10(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x8d1a(%rip), %rsi      # 0x15c5c
movq %r12, %rdi
callq 0xbaa7
movb $0x0, 0x40(%rsp)
movl $0xc8, 0x4(%rsp)
testq %rax, %rax
je 0xd07e
movq %rax, %r15
movq %rbp, 0x20(%rsp)
movq 0x8(%rax), %r14
leaq 0x1(%r14), %rdi
callq 0x33e0
testq %rax, %rax
je 0xcfb7
movq %rax, %rbp
movq (%r15), %rsi
movq %rax, %rdi
movq %r14, %rdx
callq 0x3380
movb $0x0, (%rbp,%r14)
leaq 0x9583(%rip), %rsi      # 0x1651b
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %rbp, %rdi
xorl %eax, %eax
callq 0x3420
movl %eax, %r13d
movq %rbp, %rdi
callq 0x3060
testl %r13d, %r13d
movq 0x20(%rsp), %rbp
jle 0xd07e
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
js 0xd07e
cmpq $0x0, 0x10(%rsp)
js 0xd07e
cmpl $0x1, %r13d
movq 0x8(%rsp), %rdx
jne 0xcff3
leaq -0x1(%rdx), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %r8
movq %r8, %r14
subq %rcx, %r14
setge %al
cmpq %rdx, %r8
setl %dl
testb %dl, %al
je 0xd04d
incq %r14
movq 0xc0(%rsp), %r9
leaq 0x8c62(%rip), %rdx      # 0x15c7f
leaq 0x40(%rsp), %rdi
movl $0x46, %esi
xorl %eax, %eax
callq 0x31e0
movq (%rbp), %rdi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x3490
movl $0xce, 0x4(%rsp)
movq %r14, 0x8(%rsp)
jmp 0xd07e
movq 0xc0(%rsp), %rcx
leaq 0x8c06(%rip), %rdx      # 0x15c62
movq $0x0, 0x8(%rsp)
leaq 0x40(%rsp), %rdi
movl $0x46, %esi
xorl %eax, %eax
callq 0x31e0
movl $0x1a0, 0x4(%rsp)       # imm = 0x1A0
leaq 0x8c1d(%rip), %rsi      # 0x15ca2
movq %r12, %rdi
callq 0xbaa7
testq %rax, %rax
je 0xd0a3
leaq 0x8c14(%rip), %rsi      # 0x15cad
movq %rax, %rdi
callq 0x6c4a
jmp 0xd0b6
addq $0x40, %r12
leaq 0x97bc(%rip), %rsi      # 0x1686a
movq %r12, %rdi
callq 0x6c0c
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movl %ecx, 0x18(%rbp)
leaq 0xe8(%rsp), %r15
movq -0x28(%r15), %r8
movq (%r15), %rcx
leaq 0x9453(%rip), %rdx      # 0x16529
leaq 0x1a0(%rsp), %r12
movl $0x32, %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x31e0
leaq 0x38(%rsp), %rdi
callq 0x3530
leaq 0x9435(%rip), %r14      # 0x16533
leaq 0x160(%rsp), %r13
movl $0x32, %esi
movq %r13, %rdi
movq %r14, %rdx
movq %rax, %rcx
callq 0x34e0
movq %r15, %rdi
callq 0x3530
leaq 0x120(%rsp), %r15
movl $0x32, %esi
movq %r15, %rdi
movq %r14, %rdx
movq %rax, %rcx
callq 0x34e0
leaq 0x210(%rsp), %rax
movq (%rax), %rdx
movq 0x8(%rax), %rcx
movq %rbx, %rdi
movl 0x4(%rsp), %esi
callq 0xcb88
cmpl $0x0, 0x18(%rbp)
leaq 0x8ac5(%rip), %rax      # 0x15c27
leaq 0x8b44(%rip), %r10      # 0x15cad
cmoveq %rax, %r10
leaq 0x40(%rsp), %r11
leaq 0x8b3f(%rip), %rsi      # 0x15cb8
movq %rbx, %rdi
movq %r13, %rdx
movq %r15, %rcx
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r9
xorl %eax, %eax
pushq %r12
pushq %r11
movq 0x18(%rsp), %r14
pushq %r14
pushq %r10
callq 0x80d3
addq $0x20, %rsp
movq %r14, 0x8(%rbp)
movl $0x1, 0x1c(%rbp)
movq %rbx, %rdi
callq 0xc491
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq