// File: /workspace/github_c_makefile_star2_O1/FH0[P]tun_direct/lwip-2.1.2/src/core/tcp.c
// Function: tcp_connect
err_t
tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port,
            tcp_connected_fn connected)
{
  struct netif *netif = NULL;
  err_t ret;
  u32_t iss;
  u16_t old_local_port;

  LWIP_ASSERT_CORE_LOCKED();

  LWIP_ERROR("tcp_connect: invalid pcb", pcb != NULL, return ERR_ARG);
  LWIP_ERROR("tcp_connect: invalid ipaddr", ipaddr != NULL, return ERR_ARG);

  LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);

  LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
  ip_addr_set(&pcb->remote_ip, ipaddr);
  pcb->remote_port = port;

  if (pcb->netif_idx != NETIF_NO_INDEX) {
    netif = netif_get_by_index(pcb->netif_idx);
  } else {
    /* check if we have a route to the remote host */
    netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
  }
  if (netif == NULL) {
    /* Don't even try to send a SYN packet if we have no route since that will fail. */
    return ERR_RTE;
  }

  /* check if local IP has been assigned to pcb, if not, get one */
  if (ip_addr_isany(&pcb->local_ip)) {
    const ip_addr_t *local_ip = ip_netif_get_local_ip(netif, ipaddr);
    if (local_ip == NULL) {
      return ERR_RTE;
    }
    ip_addr_copy(pcb->local_ip, *local_ip);
  }

#if LWIP_IPV6 && LWIP_IPV6_SCOPES
  /* If the given IP address should have a zone but doesn't, assign one now.
   * Given that we already have the target netif, this is easy and cheap. */
  if (IP_IS_V6(&pcb->remote_ip) &&
      ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNICAST)) {
    ip6_addr_assign_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNICAST, netif);
  }
#endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */

  old_local_port = pcb->local_port;
  if (pcb->local_port == 0) {
    pcb->local_port = tcp_new_port();
    if (pcb->local_port == 0) {
      return ERR_BUF;
    }
  } else {
#if SO_REUSE
    if (ip_get_option(pcb, SOF_REUSEADDR)) {
      /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
         now that the 5-tuple is unique. */
      struct tcp_pcb *cpcb;
      int i;
      /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */
      for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
        for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
          if ((cpcb->local_port == pcb->local_port) &&
              (cpcb->remote_port == port) &&
              ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
              ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
            /* linux returns EISCONN here, but ERR_USE should be OK for us */
            return ERR_USE;
          }
        }
      }
    }
#endif /* SO_REUSE */
  }

  iss = tcp_next_iss(pcb);
  pcb->rcv_nxt = 0;
  pcb->snd_nxt = iss;
  pcb->lastack = iss - 1;
  pcb->snd_wl2 = iss - 1;
  pcb->snd_lbb = iss - 1;
  /* Start with a window that does not need scaling. When window scaling is
     enabled and used, the window is enlarged when both sides agree on scaling. */
  pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
  pcb->rcv_ann_right_edge = pcb->rcv_nxt;
  pcb->snd_wnd = TCP_WND;
  /* As initial send MSS, we use TCP_MSS but limit it to 536.
     The send MSS is updated when an MSS option is received. */
  pcb->mss = INITIAL_MSS;
#if TCP_CALCULATE_EFF_SEND_MSS
  pcb->mss = tcp_eff_send_mss_netif(pcb->mss, netif, &pcb->remote_ip);
#endif /* TCP_CALCULATE_EFF_SEND_MSS */
  pcb->cwnd = 1;
#if LWIP_CALLBACK_API
  pcb->connected = connected;
#else /* LWIP_CALLBACK_API */
  LWIP_UNUSED_ARG(connected);
#endif /* LWIP_CALLBACK_API */

  /* Send a SYN together with the MSS option. */
  ret = tcp_enqueue_flags(pcb, TCP_SYN);
  if (ret == ERR_OK) {
    /* SYN segment was enqueued, changed the pcbs state now */
    pcb->state = SYN_SENT;
    if (old_local_port != 0) {
      TCP_RMV(&tcp_bound_pcbs, pcb);
    }
    TCP_REG_ACTIVE(pcb);
    MIB2_STATS_INC(mib2.tcpactiveopens);

    tcp_output(pcb);
  }
  return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x7e56
movq %rsi, %r13
testq %rsi, %rsi
je 0x7e72
movq %rdi, %rbx
cmpl $0x0, 0x48(%rdi)
jne 0x7e8e
movq %rcx, %r14
movb 0x14(%r13), %al
movb %al, 0x2c(%rbx)
cmpb $0x6, 0x14(%r13)
movl (%r13), %ecx
movl %ecx, 0x18(%rbx)
jne 0x7c3f
movl 0x4(%r13), %ecx
movl %ecx, 0x1c(%rbx)
movl 0x8(%r13), %ecx
movl %ecx, 0x20(%rbx)
movl 0xc(%r13), %ecx
movl %ecx, 0x24(%rbx)
movb 0x10(%r13), %cl
movb %cl, 0x28(%rbx)
jmp 0x7c49
xorl %ecx, %ecx
movq %rcx, 0x21(%rbx)
movq %rcx, 0x1c(%rbx)
leaq 0x18(%rbx), %r15
movw %dx, 0x50(%rbx)
movb 0x30(%rbx), %cl
testb %cl, %cl
je 0x7c62
movzbl %cl, %edi
callq 0x4dfd
jmp 0x7c7b
cmpb $0x6, %al
jne 0x7c73
movq %rbx, %rdi
movq %r15, %rsi
callq 0xfae4
jmp 0x7c7b
movq %r15, %rdi
callq 0xe3b4
movq %rax, %r12
movb $-0x4, %bpl
testq %rax, %rax
je 0x7e36
cmpb $0x6, 0x14(%rbx)
movl (%rbx), %eax
jne 0x7cad
testl %eax, %eax
jne 0x7cb5
cmpl $0x0, 0x4(%rbx)
jne 0x7cb5
cmpl $0x0, 0x8(%rbx)
jne 0x7cb5
cmpl $0x0, 0xc(%rbx)
jne 0x7cb5
jmp 0x7daa
testl %eax, %eax
je 0x7daa
cmpb $0x6, 0x2c(%rbx)
jne 0x7cdd
cmpb $0x0, 0x28(%rbx)
jne 0x7cdd
movl $0xc0ff, %eax           # imm = 0xC0FF
andl (%r15), %eax
cmpl $0x80fe, %eax           # imm = 0x80FE
jne 0x7cdd
movb 0xee(%r12), %al
incb %al
movb %al, 0x28(%rbx)
movzwl 0x4e(%rbx), %r13d
testw %r13w, %r13w
jne 0x7cfa
callq 0x7866
movw %ax, 0x4e(%rbx)
testw %ax, %ax
je 0x7dbe
movq %rbx, %rdi
callq 0x7ebb
movl $0x0, 0x5c(%rbx)
movl %eax, 0x88(%rbx)
decl %eax
movl %eax, 0x7c(%rbx)
movl %eax, 0x90(%rbx)
movl %eax, 0x94(%rbx)
movl $0x80008000, %eax       # imm = 0x80008000
movq %rax, 0x60(%rbx)
movw $0x8000, 0x98(%rbx)     # imm = 0x8000
movw $0x218, 0x6a(%rbx)      # imm = 0x218
movl $0x218, %edi            # imm = 0x218
movq %r12, %rsi
movq %r15, %rdx
callq 0x7f01
movw %ax, 0x6a(%rbx)
movw $0x1, 0x80(%rbx)
movq %r14, 0xe0(%rbx)
movq %rbx, %rdi
movl $0x2, %esi
callq 0xbe1b
movl %eax, %ebp
testb %al, %al
jne 0x7e36
movl $0x2, 0x48(%rbx)
testw %r13w, %r13w
je 0x7e10
movq 0x15b75(%rip), %rcx     # 0x1d900
cmpq %rbx, %rcx
je 0x7dfa
testq %rcx, %rcx
je 0x7e08
movq %rcx, %rax
movq 0x38(%rcx), %rcx
cmpq %rbx, %rcx
jne 0x7d90
addq $0x38, %rax
movq %rbx, %rcx
jmp 0x7e01
cmpb $0x6, 0x14(%r13)
jne 0x7dc3
movq %r12, %rdi
movq %r13, %rsi
callq 0xfaef
jmp 0x7dc8
movb $-0x2, %bpl
jmp 0x7e36
leaq 0x8(%r12), %rax
testq %rax, %rax
je 0x7e36
movb 0x14(%rax), %cl
movb %cl, 0x14(%rbx)
cmpb $0x6, 0x14(%rax)
movl (%rax), %ecx
movl %ecx, (%rbx)
jne 0x7e47
movl 0x4(%rax), %ecx
movl %ecx, 0x4(%rbx)
movl 0x8(%rax), %ecx
movl %ecx, 0x8(%rbx)
movl 0xc(%rax), %ecx
movl %ecx, 0xc(%rbx)
movb 0x10(%rax), %al
movb %al, 0x10(%rbx)
jmp 0x7cb5
leaq 0x15aff(%rip), %rax     # 0x1d900
movq 0x38(%rcx), %rcx
movq %rcx, (%rax)
movq $0x0, 0x38(%rbx)
movq 0x15af1(%rip), %rax     # 0x1d908
movq %rax, 0x38(%rbx)
movq %rbx, 0x15ae6(%rip)     # 0x1d908
callq 0xce20
movb $0x1, 0x15aeb(%rip)     # 0x1d919
movq %rbx, %rdi
callq 0xbfab
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
movq %rax, 0x9(%rbx)
movq %rax, 0x4(%rbx)
jmp 0x7cb5
leaq 0xd46f(%rip), %rdi      # 0x152cc
leaq 0xdfee(%rip), %rsi      # 0x15e52
leaq 0xde34(%rip), %rcx      # 0x15c9f
movl $0x435, %edx            # imm = 0x435
jmp 0x7ea8
leaq 0xd453(%rip), %rdi      # 0x152cc
leaq 0xdfeb(%rip), %rsi      # 0x15e6b
leaq 0xde18(%rip), %rcx      # 0x15c9f
movl $0x436, %edx            # imm = 0x436
jmp 0x7ea8
leaq 0xd437(%rip), %rdi      # 0x152cc
leaq 0xdfeb(%rip), %rsi      # 0x15e87
leaq 0xddfc(%rip), %rcx      # 0x15c9f
movl $0x438, %edx            # imm = 0x438
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050