// File: /workspace/github_c_makefile_star2_O2/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 0x1d7(%rip), %rdx       # 0x5a69b
movq %r12, %rcx
callq 0x5895c
testq %rax, %rax
je 0x5a522
movq %rax, %r14
orb $-0x80, 0xa2(%rax)
movl $0x6, 0x2b0(%rbx)
movq %rax, 0x2a8(%rbx)
movq (%rbx), %r13
leaq 0xdea9(%rip), %rsi      # 0x6839f
movq %r13, %rdi
callq 0xc970
testl %eax, %eax
je 0x5a534
movq 0x8(%rbx), %r12
leaq 0xde9a(%rip), %rsi      # 0x683a7
pushq $0x7
popq %rdx
movq %r12, %rdi
callq 0xc800
testl %eax, %eax
jne 0x5a555
addq $0x7, %r12
jmp 0x5a551
movq (%r12), %rax
orl $0x10, 0xa0(%rax)
xorl %eax, %eax
jmp 0x5a68c
movq (%r12), %rax
movl 0x20(%rax), %edi
leaq 0xdb3e(%rip), %rsi      # 0x68080
pushq $0x13
popq %rdx
xorl %ecx, %ecx
callq 0xc3e0
jmp 0x5a689
movq %r12, 0x8(%rbx)
movzbl (%r12), %eax
testl %eax, %eax
je 0x5a568
cmpl $0x2f, %eax
je 0x5a568
incq %r12
jmp 0x5a551
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %r9
testq %r9, %r9
leaq 0xd0d8(%rip), %rcx      # 0x67652
leaq 0xb135(%rip), %r8       # 0x656b6
cmoveq %rcx, %r8
cmoveq %rcx, %r9
movq %rax, (%rsp)
leaq 0xe63d(%rip), %rsi      # 0x68bd1
movq %r14, %r15
movq %r14, %rdi
movq %r13, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x57946
movq %rbx, -0x38(%rbp)
leaq 0x90(%rbx), %r12
leaq 0xa2e0(%rip), %rbx      # 0x64899
xorl %r14d, %r14d
movl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %rax
movslq 0x84(%rax), %rax
cmpq %rax, %r14
jge 0x5a632
movq -0x8(%r12), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x58e28
testl %eax, %eax
je 0x5a601
movq (%r12), %rcx
movq %r15, %rdi
leaq 0xdce5(%rip), %rsi      # 0x682da
movq %r13, %rdx
xorl %eax, %eax
callq 0x57946
jmp 0x5a622
movq %r15, %rdi
leaq 0xdccf(%rip), %rsi      # 0x682da
movq %rbx, %rdx
leaq 0xcd93(%rip), %rcx      # 0x673a8
xorl %eax, %eax
callq 0x57946
pushq $0x1
popq %rax
movl %eax, -0x2c(%rbp)
incq %r14
addq $0x10, %r12
leaq 0xa269(%rip), %rbx      # 0x64899
jmp 0x5a5c3
cmpl $0x0, -0x2c(%rbp)
jne 0x5a657
leaq 0xdc9b(%rip), %rsi      # 0x682da
leaq 0xa253(%rip), %rdx      # 0x64899
leaq 0xcd5b(%rip), %rcx      # 0x673a8
movq %r15, %rdi
xorl %eax, %eax
callq 0x57946
leaq 0xbcd1(%rip), %rsi      # 0x6632f
leaq 0xdd05(%rip), %rdx      # 0x6836a
movq %r15, %rdi
xorl %eax, %eax
callq 0x57946
movq -0x38(%rbp), %rax
movq 0x268(%rax), %rsi
movq 0x270(%rax), %rdx
movq %r15, %rdi
callq 0x57905
pushq $0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq