// File: /workspace/github_c_makefile_star2_O2/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 0x7a6b
movq %rsi, %r13
testq %rsi, %rsi
je 0x7a87
movq %rdi, %rbx
cmpl $0x0, 0x48(%rdi)
jne 0x7aa3
movq %rcx, %r14
movb 0x14(%r13), %al
movb %al, 0x2c(%rbx)
movl (%r13), %ecx
movl %ecx, 0x18(%rbx)
cmpb $0x6, %al
jne 0x786d
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
jmp 0x7878
andl $0x0, 0x24(%rbx)
andq $0x0, 0x1c(%rbx)
xorl %ecx, %ecx
leaq 0x18(%rbx), %r15
movb %cl, 0x28(%rbx)
movw %dx, 0x50(%rbx)
movb 0x30(%rbx), %cl
testb %cl, %cl
je 0x7894
movzbl %cl, %edi
callq 0x4bee
jmp 0x78ad
cmpb $0x6, %al
jne 0x78a5
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf144
jmp 0x78ad
movq %r15, %rdi
callq 0xdb18
movq %rax, %r12
movb $-0x4, %bpl
testq %rax, %rax
je 0x7a5a
cmpb $0x6, 0x14(%rbx)
movl (%rbx), %eax
jne 0x78dc
testl %eax, %eax
jne 0x7937
cmpl $0x0, 0x4(%rbx)
jne 0x7937
cmpl $0x0, 0x8(%rbx)
jne 0x7937
cmpl $0x0, 0xc(%rbx)
jne 0x7937
jmp 0x78e0
testl %eax, %eax
jne 0x7937
cmpb $0x6, 0x14(%r13)
jne 0x78fc
movq %r12, %rdi
movq %r13, %rsi
callq 0xf14f
testq %rax, %rax
jne 0x7901
jmp 0x7a5a
leaq 0x8(%r12), %rax
movb 0x14(%rax), %cl
movb %cl, 0x14(%rbx)
movl (%rax), %edx
movl %edx, (%rbx)
cmpb $0x6, %cl
jne 0x792a
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 0x7937
leaq 0x4(%rbx), %rax
andq $0x0, 0x5(%rax)
andq $0x0, (%rax)
cmpb $0x6, 0x2c(%rbx)
jne 0x795f
cmpb $0x0, 0x28(%rbx)
jne 0x795f
movl $0xc0ff, %eax           # imm = 0xC0FF
andl (%r15), %eax
cmpl $0x80fe, %eax           # imm = 0x80FE
jne 0x795f
movb 0xee(%r12), %al
incb %al
movb %al, 0x28(%rbx)
movzwl 0x4e(%rbx), %r13d
testw %r13w, %r13w
jne 0x797c
callq 0x74a2
movw %ax, 0x4e(%rbx)
testw %ax, %ax
je 0x7a1f
movq %rbx, %rdi
callq 0x7ad0
andl $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 0x7b16
movw %ax, 0x6a(%rbx)
movw $0x1, 0x80(%rbx)
movq %r14, 0xe0(%rbx)
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0xb67d
movl %eax, %ebp
testb %al, %al
jne 0x7a5a
movl $0x2, 0x48(%rbx)
testw %r13w, %r13w
je 0x7a32
movq 0x15f00(%rip), %rcx     # 0x1d900
cmpq %rbx, %rcx
je 0x7a24
testq %rcx, %rcx
je 0x7a32
movq %rcx, %rax
movq 0x38(%rcx), %rcx
cmpq %rbx, %rcx
jne 0x7a05
addq $0x38, %rax
movq %rbx, %rcx
jmp 0x7a2b
movb $-0x2, %bpl
jmp 0x7a5a
leaq 0x15ed5(%rip), %rax     # 0x1d900
movq 0x38(%rcx), %rcx
movq %rcx, (%rax)
movq 0x15ecf(%rip), %rax     # 0x1d908
movq %rax, 0x38(%rbx)
movq %rbx, 0x15ec4(%rip)     # 0x1d908
callq 0xc634
movb $0x1, 0x15ec9(%rip)     # 0x1d919
movq %rbx, %rdi
callq 0xb804
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc85a(%rip), %rdi      # 0x142cc
leaq 0xd3d9(%rip), %rsi      # 0x14e52
leaq 0xd21f(%rip), %rcx      # 0x14c9f
movl $0x435, %edx            # imm = 0x435
jmp 0x7abd
leaq 0xc83e(%rip), %rdi      # 0x142cc
leaq 0xd3d6(%rip), %rsi      # 0x14e6b
leaq 0xd203(%rip), %rcx      # 0x14c9f
movl $0x436, %edx            # imm = 0x436
jmp 0x7abd
leaq 0xc822(%rip), %rdi      # 0x142cc
leaq 0xd3d6(%rip), %rsi      # 0x14e87
leaq 0xd1e7(%rip), %rcx      # 0x14c9f
movl $0x438, %edx            # imm = 0x438
xorl %eax, %eax
callq 0x20d0
xorl %edi, %edi
callq 0x21b0
callq 0x2050