// File: /workspace/github_c_makefile_star2_O3/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 0x7d43
movq %rsi, %r13
testq %rsi, %rsi
je 0x7d5f
movq %rdi, %rbx
cmpl $0x0, 0x48(%rdi)
jne 0x7d7b
leaq 0x18(%rbx), %r15
movb 0x14(%r13), %al
movb %al, 0x2c(%rbx)
movl (%r13), %esi
movl %esi, 0x18(%rbx)
cmpb $0x6, %al
movq %rcx, (%rsp)
jne 0x7b4e
movl 0x4(%r13), %eax
movl %eax, 0x1c(%rbx)
movl 0x8(%r13), %eax
movl %eax, 0x20(%rbx)
movl 0xc(%r13), %eax
movl %eax, 0x24(%rbx)
movb 0x10(%r13), %al
leaq 0x28(%rbx), %r14
movb %al, 0x28(%rbx)
movw %dx, 0x50(%rbx)
movb 0x30(%rbx), %al
testb %al, %al
jne 0x7b6a
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf744
jmp 0x7b7c
leaq 0x1c(%rbx), %rax
leaq 0x28(%rbx), %r14
xorl %esi, %esi
movq %rsi, 0x5(%rax)
movq %rsi, (%rax)
movw %dx, 0x50(%rbx)
movb 0x30(%rbx), %al
testb %al, %al
je 0x7b74
movzbl %al, %edi
callq 0x4d9d
jmp 0x7b7c
movq %r15, %rdi
callq 0xe120
movq %rax, %r12
movb $-0x4, %bpl
testq %rax, %rax
je 0x7d32
cmpb $0x6, 0x14(%rbx)
movl (%rbx), %eax
jne 0x7bab
testl %eax, %eax
jne 0x7c06
cmpl $0x0, 0x4(%rbx)
jne 0x7c06
cmpl $0x0, 0x8(%rbx)
jne 0x7c06
cmpl $0x0, 0xc(%rbx)
jne 0x7c06
jmp 0x7baf
testl %eax, %eax
jne 0x7c06
cmpb $0x6, 0x14(%r13)
jne 0x7bcb
movq %r12, %rdi
movq %r13, %rsi
callq 0xf74f
testq %rax, %rax
jne 0x7bd0
jmp 0x7d32
leaq 0x8(%r12), %rax
movb 0x14(%rax), %cl
movb %cl, 0x14(%rbx)
movl (%rax), %edx
movl %edx, (%rbx)
cmpb $0x6, %cl
jne 0x7bf9
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 0x7c06
leaq 0x4(%rbx), %rax
xorl %ecx, %ecx
movq %rcx, 0x5(%rax)
movq %rcx, (%rax)
cmpb $0x6, 0x2c(%rbx)
jne 0x7c2e
cmpb $0x0, (%r14)
jne 0x7c2e
movl $0xc0ff, %eax           # imm = 0xC0FF
andl (%r15), %eax
cmpl $0x80fe, %eax           # imm = 0x80FE
jne 0x7c2e
movb 0xee(%r12), %al
incb %al
movb %al, (%r14)
movzwl 0x4e(%rbx), %r14d
testw %r14w, %r14w
jne 0x7c4b
callq 0x7751
movw %ax, 0x4e(%rbx)
testw %ax, %ax
je 0x7cf7
movq %rbx, %rdi
callq 0x7da8
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 0x7dee
movw %ax, 0x6a(%rbx)
movw $0x1, 0x80(%rbx)
movq (%rsp), %rax
movq %rax, 0xe0(%rbx)
movq %rbx, %rdi
movl $0x2, %esi
callq 0xbb78
movl %eax, %ebp
testb %al, %al
jne 0x7d32
movl $0x2, 0x48(%rbx)
testw %r14w, %r14w
je 0x7d0a
movq 0x14c28(%rip), %rcx     # 0x1c900
cmpq %rbx, %rcx
je 0x7cfc
testq %rcx, %rcx
je 0x7d0a
movq %rcx, %rax
movq 0x38(%rcx), %rcx
cmpq %rbx, %rcx
jne 0x7cdd
addq $0x38, %rax
movq %rbx, %rcx
jmp 0x7d03
movb $-0x2, %bpl
jmp 0x7d32
leaq 0x14bfd(%rip), %rax     # 0x1c900
movq 0x38(%rcx), %rcx
movq %rcx, (%rax)
movq 0x14bf7(%rip), %rax     # 0x1c908
movq %rax, 0x38(%rbx)
movq %rbx, 0x14bec(%rip)     # 0x1c908
callq 0xcba0
movb $0x1, 0x14bf1(%rip)     # 0x1c919
movq %rbx, %rdi
callq 0xbd02
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc582(%rip), %rdi      # 0x142cc
leaq 0xd0ea(%rip), %rsi      # 0x14e3b
leaq 0xcf30(%rip), %rcx      # 0x14c88
movl $0x435, %edx            # imm = 0x435
jmp 0x7d95
leaq 0xc566(%rip), %rdi      # 0x142cc
leaq 0xd0e7(%rip), %rsi      # 0x14e54
leaq 0xcf14(%rip), %rcx      # 0x14c88
movl $0x436, %edx            # imm = 0x436
jmp 0x7d95
leaq 0xc54a(%rip), %rdi      # 0x142cc
leaq 0xd0e7(%rip), %rsi      # 0x14e70
leaq 0xcef8(%rip), %rcx      # 0x14c88
movl $0x438, %edx            # imm = 0x438
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050