// File: /workspace/github_c_makefile_star2_O1/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 0x205(%rip), %rdx       # 0x6b350
movq %r12, %rcx
callq 0x693ba
testq %rax, %rax
je 0x6b313
movq %rax, %r14
orb $-0x80, 0xa2(%rax)
movl $0x6, 0x2b0(%rbx)
movq %rax, 0x2a8(%rbx)
movq (%rbx), %r15
leaq 0xf23e(%rip), %rsi      # 0x7a3bf
movq %r15, %rdi
callq 0xc950
testl %eax, %eax
je 0x6b322
movq 0x8(%rbx), %rax
movl $0x70747468, %ecx       # imm = 0x70747468
xorl (%rax), %ecx
movl $0x2f2f3a70, %edx       # imm = 0x2F2F3A70
xorl 0x3(%rax), %edx
orl %ecx, %edx
jne 0x6b1b0
addq $0x7, %rax
movq %rax, 0x8(%rbx)
movq 0x8(%rbx), %rcx
movzbl (%rcx), %eax
testl %eax, %eax
je 0x6b1c9
cmpl $0x2f, %eax
je 0x6b1c9
incq %rcx
movq %rcx, 0x8(%rbx)
jmp 0x6b1b4
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %r9
testq %r9, %r9
leaq 0xe48c(%rip), %rdx      # 0x79667
leaq 0xc4e9(%rip), %r8       # 0x776cb
cmoveq %rdx, %r8
cmoveq %rdx, %r9
movq %rax, (%rsp)
leaq 0xf9fc(%rip), %rsi      # 0x7abf1
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x68203
cmpl $0x0, 0x84(%rbx)
jle 0x6b2c4
movq %r14, -0x38(%rbp)
leaq 0x88(%rbx), %r13
callq 0xc4a0
movq %rax, %r15
leaq 0xb688(%rip), %r12      # 0x768b1
xorl %r14d, %r14d
movl $0x0, -0x2c(%rbp)
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 0x6b267
incq %rsi
cmpl %r8d, %edi
je 0x6b248
cmpl %r8d, %edi
jne 0x6b291
movq -0x38(%rbp), %rdi
leaq 0xf083(%rip), %rsi      # 0x7a2fa
movq %r12, %rdx
leaq 0xe13c(%rip), %rcx      # 0x793bd
xorl %eax, %eax
callq 0x68203
movl $0x1, -0x2c(%rbp)
jmp 0x6b2a7
movq 0x8(%rax), %rcx
movq -0x38(%rbp), %rdi
leaq 0xf05a(%rip), %rsi      # 0x7a2fa
xorl %eax, %eax
callq 0x68203
incq %r14
movslq 0x84(%rbx), %rax
cmpq %rax, %r14
jl 0x6b233
cmpl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %r14
jne 0x6b2e3
leaq 0xf02f(%rip), %rsi      # 0x7a2fa
leaq 0xb5df(%rip), %rdx      # 0x768b1
leaq 0xe0e4(%rip), %rcx      # 0x793bd
movq %r14, %rdi
xorl %eax, %eax
callq 0x68203
leaq 0xd05a(%rip), %rsi      # 0x78344
leaq 0xf099(%rip), %rdx      # 0x7a38a
movq %r14, %rdi
xorl %eax, %eax
callq 0x68203
movq 0x268(%rbx), %rsi
movq 0x270(%rbx), %rdx
movq %r14, %rdi
callq 0x681c0
jmp 0x6b33c
movq (%r12), %rax
orl $0x10, 0xa0(%rax)
xorl %eax, %eax
jmp 0x6b341
movq (%r12), %rax
movl 0x20(%rax), %edi
leaq 0xed70(%rip), %rsi      # 0x7a0a0
movl $0x13, %edx
xorl %ecx, %ecx
callq 0xc3d0
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq