// File: /workspace/github_c_makefile_star2_O3/coyove[P]chann/./lib/mongoose/mongoose.c
// Function: mg_forward
int mg_forward(struct mg_connection *c, const char *addr) {
  static const char ok[] = "HTTP/1.1 200 OK\r\n\r\n";
  struct connection *conn = MG_CONN_2_CONN(c);
  struct ns_connection *pc;

  if ((pc = ns_connect(&conn->server->ns_mgr, addr,
      mg_ev_handler, conn)) == NULL) {
    conn->ns_conn->flags |= NSF_CLOSE_IMMEDIATELY;
    return 0;
  }

  // Interlink two connections
  pc->flags |= MG_PROXY_CONN;
  conn->endpoint_type = EP_PROXY;
  conn->endpoint.nc = pc;
  DBG(("%p [%s] [%s] -> %p %p", conn, c->uri, addr, pc, conn->ns_conn->ssl));

  if (strcmp(c->request_method, "CONNECT") == 0) {
    // For CONNECT request, reply with 200 OK. Tunnel is established.
    // TODO(lsm): check for send() failure
    (void) send(conn->ns_conn->sock, ok, sizeof(ok) - 1, 0);
  } else {
    // Strip "http://host:port" part from the URI
    if (memcmp(c->uri, "http://", 7) == 0) c->uri += 7;
    while (*c->uri != '\0' && *c->uri != '/') c->uri++;
    proxy_request(pc, c);
  }
  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq -0x8(%rdi), %r12
movq 0x2a0(%rdi), %rdi
leaq 0x20d(%rip), %rdx       # 0x69768
movq %r12, %rcx
callq 0x677cf
testq %rax, %rax
je 0x695b6
movq %rax, %r14
orb $-0x80, 0xa2(%rax)
movl $0x6, 0x2b0(%rbx)
movq %rax, 0x2a8(%rbx)
movq (%rbx), %r15
leaq 0xee32(%rip), %rsi      # 0x783bf
movq %r15, %rdi
callq 0xc950
testl %eax, %eax
je 0x695c8
movq 0x8(%rbx), %rcx
movl $0x70747468, %eax       # imm = 0x70747468
xorl (%rcx), %eax
movl $0x2f2f3a70, %edx       # imm = 0x2F2F3A70
xorl 0x3(%rcx), %edx
orl %eax, %edx
jne 0x695eb
addq $0x7, %rcx
jmp 0x695e7
movq (%r12), %rax
orl $0x10, 0xa0(%rax)
xorl %eax, %eax
jmp 0x69759
movq (%r12), %rax
movl 0x20(%rax), %edi
leaq 0xeaca(%rip), %rsi      # 0x780a0
movl $0x13, %edx
xorl %ecx, %ecx
callq 0xc3d0
jmp 0x69754
movq %rcx, 0x8(%rbx)
movzbl (%rcx), %eax
testl %eax, %eax
je 0x695fc
cmpl $0x2f, %eax
je 0x695fc
incq %rcx
jmp 0x695e7
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %r9
testq %r9, %r9
leaq 0xe064(%rip), %rdx      # 0x77672
leaq 0xc0c1(%rip), %r8       # 0x756d6
cmoveq %rdx, %r8
cmoveq %rdx, %r9
movq %rax, (%rsp)
leaq 0xf5c9(%rip), %rsi      # 0x78bf1
movq %r14, -0x38(%rbp)
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x6665d
cmpl $0x0, 0x84(%rbx)
jle 0x69702
leaq 0x88(%rbx), %r13
movb $0x1, %al
movl %eax, -0x2c(%rbp)
callq 0xc4a0
movq %rax, %r15
leaq 0xb258(%rip), %r12      # 0x748b9
xorl %r14d, %r14d
movq %r14, %rcx
shlq $0x4, %rcx
leaq (%rcx,%r13), %rax
movq (%r13,%rcx), %rdx
movq (%r15), %rcx
xorl %esi, %esi
movzbl (%rdx,%rsi), %r9d
movl (%rcx,%r9,4), %edi
movzbl (%rsi,%r12), %r8d
movl (%rcx,%r8,4), %r8d
testl %r9d, %r9d
je 0x69698
incq %rsi
cmpl %r8d, %edi
je 0x69679
cmpl %r8d, %edi
je 0x696c4
movq 0x8(%rax), %rcx
movq -0x38(%rbp), %rdi
leaq 0xec4e(%rip), %rsi      # 0x782fa
xorl %eax, %eax
callq 0x6665d
incq %r14
movslq 0x84(%rbx), %rax
cmpq %rax, %r14
jl 0x69664
jmp 0x696fc
movl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
leaq 0xec24(%rip), %rsi      # 0x782fa
movq %r12, %rdx
leaq 0xdce8(%rip), %rcx      # 0x773c8
xorl %eax, %eax
callq 0x6665d
incq %r14
movslq 0x84(%rbx), %rax
cmpq %rax, %r14
jl 0x69664
jmp 0x69722
testb $0x1, -0x2c(%rbp)
je 0x69722
leaq 0xebf1(%rip), %rsi      # 0x782fa
leaq 0xb1a9(%rip), %rdx      # 0x748b9
leaq 0xdcb1(%rip), %rcx      # 0x773c8
movq -0x38(%rbp), %rdi
xorl %eax, %eax
callq 0x6665d
leaq 0xcc26(%rip), %rsi      # 0x7634f
leaq 0xec5a(%rip), %rdx      # 0x7838a
movq -0x38(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x6665d
movq 0x268(%rbx), %rsi
movq 0x270(%rbx), %rdx
movq %r14, %rdi
callq 0x6661a
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq