// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/mongoose/mongoose.c
// Function: send_http_error
static void send_http_error(struct connection *conn, int code,
                            const char *fmt, ...) {
  const char *message = status_code_to_str(code);
  const char *rewrites = conn->server->config_options[URL_REWRITES];
  char headers[200], body[200];
  struct vec a, b;
  va_list ap;
  int body_len, headers_len, match_code;

  conn->mg_conn.status_code = code;

  // Invoke error handler if it is set
  if (call_user(conn, MG_HTTP_ERROR) == MG_TRUE) {
    close_local_endpoint(conn);
    return;
  }

  // Handle error code rewrites
  while ((rewrites = next_option(rewrites, &a, &b)) != NULL) {
    if ((match_code = atoi(a.ptr)) > 0 && match_code == code) {
      struct mg_connection *c = &conn->mg_conn;
      c->status_code = 302;
      mg_printf(c, "HTTP/1.1 %d Moved\r\n"
                "Location: %.*s?code=%d&orig_uri=%s&query_string=%s\r\n\r\n",
                c->status_code, b.len, b.ptr, code, c->uri,
                c->query_string == NULL ? "" : c->query_string);
      close_local_endpoint(conn);
      return;
    }
  }

  body_len = mg_snprintf(body, sizeof(body), "%d %s\n", code, message);
  if (fmt != NULL) {
    va_start(ap, fmt);
    body_len += mg_vsnprintf(body + body_len, sizeof(body) - body_len, fmt, ap);
    va_end(ap);
  }
  if ((code >= 300 && code <= 399) || code == 204) {
    // 3xx errors do not have body
    body_len = 0;
  }
  headers_len = mg_snprintf(headers, sizeof(headers),
                            "HTTP/1.1 %d %s\r\nContent-Length: %d\r\n"
                            "Content-Type: text/plain\r\n\r\n",
                            code, message, body_len);
  ns_send(conn->ns_conn, headers, headers_len);
  ns_send(conn->ns_conn, body, body_len);
  close_local_endpoint(conn);  // This will write to the log file
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdx, %r12
movl %esi, %r14d
movq %rdi, %rbx
movq %rcx, -0x118(%rbp)
movq %r8, -0x110(%rbp)
movq %r9, -0x108(%rbp)
testb %al, %al
je 0x5bddf
movaps %xmm0, -0x100(%rbp)
movaps %xmm1, -0xf0(%rbp)
movaps %xmm2, -0xe0(%rbp)
movaps %xmm3, -0xd0(%rbp)
movaps %xmm4, -0xc0(%rbp)
movaps %xmm5, -0xb0(%rbp)
movaps %xmm6, -0xa0(%rbp)
movaps %xmm7, -0x90(%rbp)
movl %esi, %edi
callq 0x591d6
movq %rax, %r15
movq 0x2a8(%rbx), %rax
movq 0xc8(%rax), %r13
movl %r14d, 0x284(%rbx)
pushq $0x6d
popq %rsi
movq %rbx, %rdi
callq 0x5d354
cmpl $0x1, %eax
jne 0x5be27
movq %rbx, %rdi
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x59b05
movq %r12, -0x40(%rbp)
movq %r15, -0x30(%rbp)
leaq 0x8(%rbx), %rax
movq %rax, -0x38(%rbp)
leaq -0x60(%rbp), %r15
leaq -0x50(%rbp), %r12
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x5c8fa
testq %rax, %rax
je 0x5beb7
movq %rax, %r13
movq -0x60(%rbp), %rdi
callq 0xc380
testl %eax, %eax
setle %cl
cmpl %r14d, %eax
setne %al
orb %cl, %al
jne 0x5be3f
movl $0x12e, 0x284(%rbx)     # imm = 0x12E
movq -0x50(%rbp), %r8
movq -0x48(%rbp), %rcx
movq 0x20(%rbx), %rax
testq %rax, %rax
leaq 0xb7c5(%rip), %r10      # 0x67652
cmovneq %rax, %r10
leaq 0xcd5a(%rip), %rsi      # 0x68bf2
movq -0x38(%rbp), %rdi
movl $0x12e, %edx            # imm = 0x12E
movl %r14d, %r9d
xorl %eax, %eax
pushq %r10
pushq 0x10(%rbx)
callq 0x58c06
popq %rcx
popq %rdx
jmp 0x5bf8d
leaq 0xcd7e(%rip), %rdx      # 0x68c3c
xorl %r12d, %r12d
leaq -0x200(%rbp), %rdi
movl $0xc8, %r15d
movl $0xc8, %esi
movl %r14d, %ecx
movq -0x30(%rbp), %r8
xorl %eax, %eax
callq 0x5bc9e
movl %eax, %r13d
movq -0x40(%rbp), %rdx
testq %rdx, %rdx
je 0x5bf2d
leaq -0x130(%rbp), %rax
leaq -0x80(%rbp), %rcx
movq %rax, 0x10(%rcx)
leaq 0x10(%rbp), %rax
movq %rax, 0x8(%rcx)
movabsq $0x3000000018, %rax     # imm = 0x3000000018
movq %rax, (%rcx)
movslq %r13d, %rax
leaq (%rax,%rbp), %rdi
addq $-0x200, %rdi           # imm = 0xFE00
subq %rax, %r15
movq %r15, %rsi
callq 0x5d31b
addl %eax, %r13d
leal -0x12c(%r14), %eax
cmpl $0x64, %eax
cmovbl %r12d, %r13d
cmpl $0xcc, %r14d
cmovel %r12d, %r13d
leaq 0xccf6(%rip), %rdx      # 0x68c43
leaq -0x2d0(%rbp), %r15
movl $0xc8, %esi
movq %r15, %rdi
movl %r14d, %ecx
movq -0x30(%rbp), %r8
movl %r13d, %r9d
xorl %eax, %eax
callq 0x5bc9e
movq (%rbx), %rdi
movslq %eax, %rdx
movq %r15, %rsi
callq 0x57905
movq (%rbx), %rdi
movslq %r13d, %rdx
leaq -0x200(%rbp), %rsi
callq 0x57905
movq %rbx, %rdi
callq 0x59b05
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq