// File: /workspace/github_c_makefile_star2_O1/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 $0x298, %rsp            # imm = 0x298
movq %rdx, %r12
movl %esi, %r14d
movq %rdi, %rbx
movq %rcx, -0x108(%rbp)
movq %r8, -0x100(%rbp)
movq %r9, -0xf8(%rbp)
testb %al, %al
je 0x6c7a8
movaps %xmm0, -0xf0(%rbp)
movaps %xmm1, -0xe0(%rbp)
movaps %xmm2, -0xd0(%rbp)
movaps %xmm3, -0xc0(%rbp)
movaps %xmm4, -0xb0(%rbp)
movaps %xmm5, -0xa0(%rbp)
movaps %xmm6, -0x90(%rbp)
movaps %xmm7, -0x80(%rbp)
movl %esi, %edi
callq 0x69c65
movq %rax, -0x30(%rbp)
movq 0x2a8(%rbx), %rax
movq 0xc8(%rax), %r15
leaq 0x8(%rbx), %r13
movl %r14d, 0x284(%rbx)
testq %rbx, %rbx
sete %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x6c80d
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x6c80d
movq %r13, %rdi
movl $0x6d, %esi
callq *%rax
cmpl $0x1, %eax
jne 0x6c80d
movq %rbx, %rdi
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6a615
leaq -0x70(%rbp), %rsi
leaq -0x48(%rbp), %rdx
movq %r15, %rdi
callq 0x6d92e
testq %rax, %rax
je 0x6c888
movq %rax, %r15
movq -0x70(%rbp), %rdi
callq 0xc370
testl %eax, %eax
setle %cl
cmpl %r14d, %eax
setne %al
orb %cl, %al
jne 0x6c80d
movl $0x12e, 0x284(%rbx)     # imm = 0x12E
movq -0x48(%rbp), %r8
movq -0x40(%rbp), %rcx
movq 0x20(%rbx), %rax
testq %rax, %rax
leaq 0xce0a(%rip), %r10      # 0x79667
cmovneq %rax, %r10
leaq 0xe3aa(%rip), %rsi      # 0x7ac12
movq %r13, %rdi
movl $0x12e, %edx            # imm = 0x12E
movl %r14d, %r9d
xorl %eax, %eax
pushq %r10
pushq 0x10(%rbx)
callq 0x69675
addq $0x10, %rsp
jmp 0x6c990
leaq 0xe3cd(%rip), %rdx      # 0x7ac5c
xorl %r13d, %r13d
leaq -0x1f0(%rbp), %rdi
movl $0xc8, %esi
movl %r14d, %ecx
movq -0x30(%rbp), %r8
xorl %eax, %eax
callq 0x6c630
movl %eax, %r15d
testq %r12, %r12
je 0x6c92b
leaq -0x120(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq 0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movabsq $0x3000000018, %rax     # imm = 0x3000000018
movq %rax, -0x60(%rbp)
cmpl $0xc8, %r15d
je 0x6c926
movslq %r15d, %rax
movl $0xc8, %r13d
subq %rax, %r13
leaq (%rax,%rbp), %rdi
addq $-0x1f0, %rdi           # imm = 0xFE10
movq %rdi, -0x38(%rbp)
leaq -0x60(%rbp), %rcx
movq %r13, %rsi
movq %r12, %rdx
callq 0xcd48
leal -0x1(%r13), %ecx
cmpl %r13d, %eax
cmovll %eax, %ecx
xorl %r13d, %r13d
testl %eax, %eax
cmovnsl %ecx, %r13d
movslq %r13d, %rax
movq -0x38(%rbp), %rcx
movb $0x0, (%rax,%rcx)
addl %r15d, %r13d
jmp 0x6c92e
movl %r15d, %r13d
leal -0x12c(%r14), %eax
xorl %ecx, %ecx
cmpl $0x64, %eax
cmovbl %ecx, %r13d
cmpl $0xcc, %r14d
cmovel %ecx, %r13d
leaq 0xe313(%rip), %rdx      # 0x7ac63
leaq -0x2c0(%rbp), %r15
movl $0xc8, %esi
movq %r15, %rdi
movl %r14d, %ecx
movq -0x30(%rbp), %r8
movl %r13d, %r9d
xorl %eax, %eax
callq 0x6c630
movq (%rbx), %rdi
movslq %eax, %rdx
movq %r15, %rsi
callq 0x681c0
movq (%rbx), %rdi
movslq %r13d, %rdx
leaq -0x1f0(%rbp), %rsi
callq 0x681c0
movq %rbx, %rdi
callq 0x6a615
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq