// File: /workspace/github_c_makefile_star2_O1/coyove[P]chann/./lib/mongoose/mongoose.c
// Function: open_file_endpoint
static void open_file_endpoint(struct connection *conn, const char *path,
                               file_stat_t *st, const char *extra_headers) {
  char date[64], lm[64], etag[64], range[64], headers[1000];
  const char *msg = "OK", *hdr;
  time_t t, curtime = time(NULL);
  int64_t r1, r2;
  struct vec mime_vec;
  int n;

  conn->endpoint_type = EP_FILE;
  ns_set_close_on_exec(conn->endpoint.fd);
  conn->mg_conn.status_code = 200;

  get_mime_type(conn->server, path, &mime_vec);
  conn->cl = st->st_size;
  range[0] = '\0';

  // If Range: header specified, act accordingly
  r1 = r2 = 0;
  hdr = mg_get_header(&conn->mg_conn, "Range");
  if (hdr != NULL && (n = parse_range_header(hdr, &r1, &r2)) > 0 &&
      r1 >= 0 && r2 >= 0) {
    conn->mg_conn.status_code = 206;
    conn->cl = n == 2 ? (r2 > conn->cl ? conn->cl : r2) - r1 + 1: conn->cl - r1;
    mg_snprintf(range, sizeof(range), "Content-Range: bytes "
                "%" INT64_FMT "-%" INT64_FMT "/%" INT64_FMT "\r\n",
                r1, r1 + conn->cl - 1, (int64_t) st->st_size);
    msg = "Partial Content";
    lseek(conn->endpoint.fd, r1, SEEK_SET);
  }

  // Prepare Etag, Date, Last-Modified headers. Must be in UTC, according to
  // http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
  gmt_time_string(date, sizeof(date), &curtime);
  t = st->st_mtime; // store in local variable for NDK compile
  gmt_time_string(lm, sizeof(lm), &t);
  construct_etag(etag, sizeof(etag), st);

  n = mg_snprintf(headers, sizeof(headers),
                  "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%s%s\r\n",
                  conn->mg_conn.status_code, msg, date, lm, etag,
                  (int) mime_vec.len, mime_vec.ptr, conn->cl,
                  suggest_connection_header(&conn->mg_conn),
                  range, extra_headers == NULL ? "" : extra_headers,
                  MONGOOSE_USE_EXTRA_HTTP_HEADERS);
  ns_send(conn->ns_conn, headers, n);

  if (!strcmp(conn->mg_conn.request_method, "HEAD")) {
    conn->ns_conn->flags |= NSF_FINISHED_SENDING_DATA;
    close(conn->endpoint.fd);
    conn->endpoint_type = EP_NONE;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x548, %rsp            # imm = 0x548
movq %rcx, -0x50(%rbp)
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
xorl %r14d, %r14d
xorl %edi, %edi
callq 0xc820
movq %rax, -0x60(%rbp)
movl $0x1, 0x2b8(%rbx)
movl 0x2b0(%rbx), %edi
movl $0x2, %esi
movl $0x1, %edx
xorl %eax, %eax
callq 0xc220
leaq 0x8(%rbx), %r13
movl $0xc8, 0x284(%rbx)
movq 0x2a8(%rbx), %rdi
leaq -0x70(%rbp), %rdx
movq %r12, %rsi
callq 0x6e762
movq 0x30(%r15), %rax
movq %rax, 0x2d8(%rbx)
movb $0x0, -0xc0(%rbp)
movq %r14, -0x58(%rbp)
movq %r14, -0x30(%rbp)
leaq 0xeb0d(%rip), %rsi      # 0x7b7f3
movq %r13, -0x48(%rbp)
movq %r13, %rdi
callq 0x69863
leaq 0xd800(%rip), %r14      # 0x7a4f9
testq %rax, %rax
je 0x6cdb2
leaq 0xe720(%rip), %rsi      # 0x7b429
leaq -0x30(%rbp), %r12
leaq -0x58(%rbp), %r13
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xc100
testl %eax, %eax
setg %dl
movq (%r12), %rcx
testq %rcx, %rcx
setns %dil
andb %dl, %dil
movq (%r13), %rsi
testq %rsi, %rsi
setns %dl
andb %dil, %dl
cmpb $0x1, %dl
jne 0x6cdb2
movl $0xce, 0x284(%rbx)
movq 0x2d8(%rbx), %rdx
cmpl $0x2, %eax
jne 0x6cd6b
cmpq %rdx, %rsi
cmovlq %rsi, %rdx
subq %rcx, %rdx
incq %rdx
jmp 0x6cd6e
subq %rcx, %rdx
movq %rdx, 0x2d8(%rbx)
leaq (%rcx,%rdx), %r8
decq %r8
movq 0x30(%r15), %r9
leaq 0xe5f0(%rip), %rdx      # 0x7b377
leaq -0xc0(%rbp), %rdi
movl $0x40, %esi
xorl %eax, %eax
callq 0x6c630
movl 0x2b0(%rbx), %edi
movq -0x30(%rbp), %rsi
xorl %edx, %edx
callq 0xca40
leaq 0xd792(%rip), %r14      # 0x7a544
leaq -0x60(%rbp), %rdi
callq 0xc2f0
leaq 0x63ac(%rip), %r13      # 0x7316e
leaq -0x180(%rbp), %rdi
movl $0x40, %esi
movq %r13, %rdx
movq %rax, %rcx
callq 0xc9d0
movq 0x58(%r15), %rax
leaq -0x78(%rbp), %rdi
movq %rax, (%rdi)
callq 0xc2f0
leaq -0x140(%rbp), %rdi
movl $0x40, %esi
movq %r13, %rdx
movq %rax, %rcx
callq 0xc9d0
movq 0x30(%r15), %r8
movq 0x58(%r15), %rcx
leaq 0xe4f3(%rip), %rdx      # 0x7b302
leaq -0x100(%rbp), %rdi
movl $0x40, %esi
xorl %eax, %eax
callq 0x6c630
movl 0x284(%rbx), %r13d
movl -0x68(%rbp), %eax
movq %rax, -0x40(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x38(%rbp)
movq 0x2d8(%rbx), %r12
movq -0x48(%rbp), %rdi
callq 0x6ed8f
testl %eax, %eax
leaq 0xc56c(%rip), %rax      # 0x793bd
leaq 0xe5df(%rip), %r10      # 0x7b437
cmoveq %rax, %r10
movq -0x50(%rbp), %r15
testq %r15, %r15
leaq 0xc7fd(%rip), %r11      # 0x79667
cmoveq %r11, %r15
subq $0x8, %rsp
leaq 0xe521(%rip), %rdx      # 0x7b39a
leaq -0x570(%rbp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movl %r13d, %ecx
movq %r14, %r8
leaq -0x180(%rbp), %r9
xorl %eax, %eax
pushq %r11
pushq %r15
leaq -0xc0(%rbp), %r11
pushq %r11
pushq %r10
pushq %r12
pushq -0x38(%rbp)
pushq -0x40(%rbp)
leaq -0x100(%rbp), %r10
pushq %r10
leaq -0x140(%rbp), %r10
pushq %r10
callq 0x6c630
addq $0x50, %rsp
movq (%rbx), %rdi
movslq %eax, %rdx
leaq -0x570(%rbp), %rsi
callq 0x681c0
movq 0x8(%rbx), %rdi
leaq 0x991b(%rip), %rsi      # 0x767fe
callq 0xc950
testl %eax, %eax
jne 0x6cf0b
movq (%rbx), %rax
orl $0x1, 0xa0(%rax)
movl 0x2b0(%rbx), %edi
callq 0xc2b0
movl $0x0, 0x2b8(%rbx)
addq $0x548, %rsp            # imm = 0x548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq