// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
leaq 0x10(%rbp), %rax
movq %rax, -0x220(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x10(%rbp)
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq -0x20(%rbp), %rdi
callq 0x109b0
movq %rax, -0x38(%rbp)
movl $0x3, %edi
leaq 0xdc79(%rip), %rsi      # 0x1fbcd
leaq 0xd5ef(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x11f85
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x10(%rbp), %rax
movl %eax, %ecx
movq -0x18(%rbp), %r8
leaq 0xdc62(%rip), %rdi      # 0x1fbe0
movb $0x0, %al
callq 0x47a0
jmp 0x11f87
movq -0x30(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x12330
cmpl $0x0, %eax
jne 0x11fb9
movq -0x30(%rbp), %rdi
leaq 0xdc46(%rip), %rsi      # 0x1fbed
callq 0x12360
movq -0x38(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x12021
callq 0xd6a0
movl %eax, -0xd0(%rbp)
movl -0xd0(%rbp), %eax
movl %eax, -0x224(%rbp)
subl $0x2, %eax
je 0x11ff0
jmp 0x11fd7
movl -0x224(%rbp), %eax
subl $0xd, %eax
jne 0x11ffc
jmp 0x11fe4
movl $0x193, -0xcc(%rbp)     # imm = 0x193
jmp 0x12006
movl $0x194, -0xcc(%rbp)     # imm = 0x194
jmp 0x12006
movl $0x1f4, -0xcc(%rbp)     # imm = 0x1F4
movq -0x20(%rbp), %rdi
movl -0xcc(%rbp), %esi
leaq 0xdbd9(%rip), %rdx      # 0x1fbf0
callq 0x11e30
jmp 0x12321
callq 0xb6e0
cvttsd2si %xmm0, %rax
movq %rax, -0x1e8(%rbp)
movq $0x0, -0x1f0(%rbp)
movq $0x0, -0x1f8(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x200(%rbp)
movq -0x28(%rbp), %rdi
leaq 0xdb9b(%rip), %rsi      # 0x1fbfc
callq 0x10830
movq %rax, -0x208(%rbp)
movl $0xc8, -0x210(%rbp)
movb $0x0, -0x1e0(%rbp)
cmpq $0x0, -0x208(%rbp)
je 0x121b8
movq -0x208(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
leaq -0x1f8(%rbp), %rdx
callq 0x12390
movl %eax, -0x20c(%rbp)
cmpl $0x0, %eax
jle 0x121b8
cmpq $0x0, -0x1f0(%rbp)
jl 0x121b8
cmpq $0x0, -0x1f8(%rbp)
jl 0x121b8
cmpl $0x1, -0x20c(%rbp)
jne 0x120ec
movq -0x200(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x1f8(%rbp)
movq -0x1f0(%rbp), %rax
cmpq -0x1f8(%rbp), %rax
jg 0x1210c
movq -0x1f8(%rbp), %rax
cmpq -0x200(%rbp), %rax
jl 0x12144
movl $0x1a0, -0x210(%rbp)    # imm = 0x1A0
movq $0x0, -0x200(%rbp)
leaq -0x1e0(%rbp), %rdi
movq -0x98(%rbp), %rcx
movl $0x46, %esi
leaq 0xdac7(%rip), %rdx      # 0x1fc02
movb $0x0, %al
callq 0x31f0
jmp 0x121b6
movl $0xce, -0x210(%rbp)
movq -0x1f8(%rbp), %rax
subq -0x1f0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x200(%rbp)
leaq -0x1e0(%rbp), %rdi
movq -0x1f0(%rbp), %rcx
movq -0x1f0(%rbp), %r8
addq -0x200(%rbp), %r8
subq $0x1, %r8
movq -0x98(%rbp), %r9
movl $0x46, %esi
leaq 0xda85(%rip), %rdx      # 0x1fc1f
movb $0x0, %al
callq 0x31f0
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq -0x1f0(%rbp), %rsi
xorl %edx, %edx
callq 0x34c0
jmp 0x121b8
movq -0x28(%rbp), %rdi
leaq 0xda7f(%rip), %rsi      # 0x1fc42
callq 0x10830
movq %rax, -0x218(%rbp)
cmpq $0x0, -0x218(%rbp)
je 0x12200
movq -0x218(%rbp), %rdi
leaq 0xda66(%rip), %rsi      # 0x1fc4d
callq 0x8b60
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x12226
movq -0x28(%rbp), %rdi
addq $0x40, %rdi
leaq 0xe643(%rip), %rsi      # 0x20852
callq 0x8ad0
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x18(%rax)
leaq -0x110(%rbp), %rdi
movl $0x32, %esi
leaq -0xc8(%rbp), %rdx
callq 0x12430
leaq -0x150(%rbp), %rdi
movl $0x32, %esi
leaq -0x1e8(%rbp), %rdx
callq 0x12470
leaq -0x190(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
addq $0x58, %rdx
movl $0x32, %esi
callq 0x12470
movq -0x220(%rbp), %rax
movq -0x20(%rbp), %rdi
movl -0x210(%rbp), %esi
movq (%rax), %rdx
movq 0x8(%rax), %rcx
callq 0x11ba0
movq -0x20(%rbp), %rdi
leaq -0x150(%rbp), %rdx
leaq -0x190(%rbp), %rcx
movq -0x10(%rbp), %rax
movl %eax, %r8d
movq -0x18(%rbp), %r9
movq -0x38(%rbp), %rax
movl 0x18(%rax), %esi
leaq 0xd90d(%rip), %rbx      # 0x1fbc7
leaq 0xd98c(%rip), %rax      # 0x1fc4d
cmpl $0x0, %esi
cmovneq %rax, %rbx
movq -0x200(%rbp), %r11
leaq -0x1e0(%rbp), %r10
leaq -0x110(%rbp), %rax
leaq 0xd974(%rip), %rsi      # 0x1fc58
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movb $0x0, %al
callq 0xb2f0
movq -0x200(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movl $0x1, 0x1c(%rax)
movq -0x20(%rbp), %rdi
callq 0x11200
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %rbp
retq
nopl (%rax,%rax)