// File: /workspace/github_c_makefile_star2_O0/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 $0x618, %rsp            # imm = 0x618
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
leaq 0x12575(%rip), %rax     # 0xc6de0
movq %rax, -0x548(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0xd980
movq %rax, -0x560(%rbp)
movq -0x30(%rbp), %rax
movl $0x1, 0x2b8(%rax)
movq -0x30(%rbp), %rax
movl 0x2b0(%rax), %edi
callq 0xaf170
movq -0x30(%rbp), %rax
movl $0xc8, 0x284(%rax)
movq -0x30(%rbp), %rax
movq 0x2a8(%rax), %rdi
movq -0x38(%rbp), %rsi
leaq -0x580(%rbp), %rdx
callq 0xb83a0
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x2d8(%rax)
movb $0x0, -0x150(%rbp)
movq $0x0, -0x570(%rbp)
movq $0x0, -0x568(%rbp)
movq -0x30(%rbp), %rdi
addq $0x8, %rdi
leaq 0x137ec(%rip), %rsi     # 0xc80f3
callq 0xb0fe0
movq %rax, -0x550(%rbp)
cmpq $0x0, -0x550(%rbp)
je 0xb4a5e
movq -0x550(%rbp), %rdi
leaq -0x568(%rbp), %rsi
leaq -0x570(%rbp), %rdx
callq 0xb8e80
movl %eax, -0x584(%rbp)
cmpl $0x0, %eax
jle 0xb4a5e
cmpq $0x0, -0x568(%rbp)
jl 0xb4a5e
cmpq $0x0, -0x570(%rbp)
jl 0xb4a5e
movq -0x30(%rbp), %rax
movl $0xce, 0x284(%rax)
cmpl $0x2, -0x584(%rbp)
jne 0xb49ce
movq -0x570(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x2d8(%rcx), %rax
jle 0xb49a5
movq -0x30(%rbp), %rax
movq 0x2d8(%rax), %rax
movq %rax, -0x590(%rbp)
jmp 0xb49b3
movq -0x570(%rbp), %rax
movq %rax, -0x590(%rbp)
movq -0x590(%rbp), %rax
subq -0x568(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x598(%rbp)
jmp 0xb49e7
movq -0x30(%rbp), %rax
movq 0x2d8(%rax), %rax
subq -0x568(%rbp), %rax
movq %rax, -0x598(%rbp)
movq -0x598(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x2d8(%rax)
leaq -0x150(%rbp), %rdi
movq -0x568(%rbp), %rcx
movq -0x568(%rbp), %r8
movq -0x30(%rbp), %rax
addq 0x2d8(%rax), %r8
subq $0x1, %r8
movq -0x40(%rbp), %rax
movq 0x30(%rax), %r9
movl $0x40, %esi
leaq 0x13246(%rip), %rdx     # 0xc7c77
movb $0x0, %al
callq 0xb3c50
leaq 0x123ec(%rip), %rax     # 0xc6e2b
movq %rax, -0x548(%rbp)
movq -0x30(%rbp), %rax
movl 0x2b0(%rax), %edi
movq -0x568(%rbp), %rsi
xorl %edx, %edx
callq 0xdbd0
leaq -0x90(%rbp), %rdi
movl $0x40, %esi
leaq -0x560(%rbp), %rdx
callq 0xb8ec0
movq -0x40(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x558(%rbp)
leaq -0xd0(%rbp), %rdi
movl $0x40, %esi
leaq -0x558(%rbp), %rdx
callq 0xb8ec0
leaq -0x110(%rbp), %rdi
movq -0x40(%rbp), %rdx
movl $0x40, %esi
callq 0xb8c00
leaq -0x540(%rbp), %rax
movq %rax, -0x5f0(%rbp)
movq -0x30(%rbp), %rax
movl 0x284(%rax), %eax
movl %eax, -0x5e4(%rbp)
movq -0x548(%rbp), %rax
movq %rax, -0x5e0(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0x5d8(%rbp)
leaq -0xd0(%rbp), %rax
movq %rax, -0x5d0(%rbp)
leaq -0x110(%rbp), %rax
movq %rax, -0x5c8(%rbp)
movq -0x578(%rbp), %rax
movl %eax, -0x5bc(%rbp)
movq -0x580(%rbp), %rax
movq %rax, -0x5b8(%rbp)
movq -0x30(%rbp), %rax
movq 0x2d8(%rax), %rax
movq %rax, -0x5b0(%rbp)
movq -0x30(%rbp), %rdi
addq $0x8, %rdi
callq 0xb8f10
movq %rax, -0x5a8(%rbp)
leaq -0x150(%rbp), %rax
movq %rax, -0x5a0(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xb4b6e
leaq 0x10fe6(%rip), %rax     # 0xc5b4b
movq %rax, -0x5f8(%rbp)
jmp 0xb4b79
movq -0x48(%rbp), %rax
movq %rax, -0x5f8(%rbp)
movq -0x5a8(%rbp), %rbx
movq -0x5b0(%rbp), %r14
movq -0x5b8(%rbp), %r15
movl -0x5bc(%rbp), %r12d
movq -0x5c8(%rbp), %r13
movq -0x5d0(%rbp), %r11
movq -0x5d8(%rbp), %r9
movq -0x5e0(%rbp), %r8
movl -0x5e4(%rbp), %ecx
movq -0x5f0(%rbp), %rdi
movq -0x5f8(%rbp), %r10
movl $0x3e8, %esi            # imm = 0x3E8
leaq 0x130c9(%rip), %rdx     # 0xc7c9a
leaq 0x10f73(%rip), %rax     # 0xc5b4b
movq %r11, (%rsp)
movq -0x5a0(%rbp), %r11
movq %r13, 0x8(%rsp)
movl %r12d, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movb $0x0, %al
callq 0xb3c50
movl %eax, -0x584(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rdi
leaq -0x540(%rbp), %rsi
movslq -0x584(%rbp), %rdx
callq 0xafce0
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xe096(%rip), %rsi      # 0xc2cd7
callq 0xdab0
cmpl $0x0, %eax
jne 0xb4c7e
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl 0xa0(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0xa0(%rax)
movq -0x30(%rbp), %rax
movl 0x2b0(%rax), %edi
callq 0xd350
movq -0x30(%rbp), %rax
movl $0x0, 0x2b8(%rax)
addq $0x618, %rsp            # imm = 0x618
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq