// File: /workspace/github_c_makefile_star2_O0/FH0[P]tun_direct/tcp.c
// Function: tun_tcp_accept
err_t tun_tcp_accept(void *arg, struct tcp_pcb *newpcb, err_t err) {
  ELOG(__FUNCTION__);

  if (err != ERR_OK || newpcb == NULL) {
    ELOG("err != ERR_OK || newpcb == NULL");
    return ERR_VAL;
  }

  if (newpcb->local_ip.type != IPADDR_TYPE_V4 &&
      newpcb->local_ip.type != IPADDR_TYPE_V6) {
    ELOG("unsupport ip type");
    tcp_close(newpcb);
  }

  tun_data_t *data;
  extern int errno;

  /* filled struct */
  data = MALLOC(sizeof(tun_data_t));
  data->type = TUN_TCP;
  data->tpcb = newpcb;
  data->client_buf = MALLOC(TUN_TCP_SIZE);
  data->server_buf = MALLOC(TUN_TCP_SIZE);
  data->client_buf_len = 0;
  data->server_buf_len = 0;
  data->type = TUN_TCP;
  data->client_status = TUN_CONNECTED;
  data->server_status = TUN_CONNECTING;

  /* add event to epoll */
  if (newpcb->local_ip.type == IPADDR_TYPE_V4) {
    struct sockaddr_in sai;

    sai.sin_family = AF_INET;
    memcpy(&sai.sin_addr, &newpcb->local_ip.u_addr, 4);
    sai.sin_port = htons(newpcb->local_port);

    data->fd = socket(AF_INET, SOCK_STREAM, 0);
    ASSERT(data->fd != -1);
    epoll_ET_add(data, epollfd, EPOLLIN | EPOLLOUT);
    if (connect(data->fd, (struct sockaddr *)&sai, sizeof(sai)) == -1 &&
        errno != EINPROGRESS) {
      ELOG("connect()");
      tun_tcp_close(data);
    }
  } else if (newpcb->local_ip.type == IPADDR_TYPE_V6) {
    struct sockaddr_in6 sai;

    sai.sin6_family = AF_INET6;
    memcpy(&sai.sin6_addr, &newpcb->local_ip.u_addr, 16);
    sai.sin6_port = htons(newpcb->local_port);

    data->fd = socket(AF_INET6, SOCK_STREAM, 0);
    ASSERT(data->fd != -1);
    epoll_ET_add(data, epollfd, EPOLLIN | EPOLLOUT);
    if (connect(data->fd, (struct sockaddr *)&sai, sizeof(sai)) == -1 &&
        errno != EINPROGRESS) {
      ELOG("connect()");
      tun_tcp_close(data);
    }
  }

  /* lwip callback */
  tcp_arg(newpcb, data);
  tcp_err(newpcb, tun_tcp_error);
  tcp_recv(newpcb, tun_tcp_recv);
  tcp_sent(newpcb, tun_tcp_send);

  return ERR_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb %al, -0x19(%rbp)
movq 0x2bd9c(%rip), %rax     # 0x2eff8
movq (%rax), %rdi
leaq 0x23e04(%rip), %rsi     # 0x2706a
leaq 0x24b12(%rip), %rdx     # 0x27d7f
movl $0xd, %ecx
leaq 0x23e84(%rip), %r8      # 0x270fd
movb $0x0, %al
callq 0x2170
movsbl -0x19(%rbp), %eax
cmpl $0x0, %eax
jne 0x3290
cmpq $0x0, -0x18(%rbp)
jne 0x32c6
jmp 0x3292
movq 0x2bd5f(%rip), %rax     # 0x2eff8
movq (%rax), %rdi
leaq 0x23dc7(%rip), %rsi     # 0x2706a
leaq 0x24ad5(%rip), %rdx     # 0x27d7f
movl $0x10, %ecx
leaq 0x23e56(%rip), %r8      # 0x2710c
movb $0x0, %al
callq 0x2170
movb $-0x6, -0x1(%rbp)
jmp 0x35fe
movq -0x18(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x0, %eax
je 0x3316
movq -0x18(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x6, %eax
je 0x3316
jmp 0x32e2
movq 0x2bd0f(%rip), %rax     # 0x2eff8
movq (%rax), %rdi
leaq 0x23d77(%rip), %rsi     # 0x2706a
leaq 0x24a85(%rip), %rdx     # 0x27d7f
movl $0x16, %ecx
leaq 0x23e26(%rip), %r8      # 0x2712c
movb $0x0, %al
callq 0x2170
movq -0x18(%rbp), %rdi
callq 0xb2a0
movl $0x38, %edi
callq 0x31e0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl $0x3, (%rax)
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0xffff, %edi           # imm = 0xFFFF
callq 0x31e0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0xffff, %edi           # imm = 0xFFFF
callq 0x31e0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x2c(%rax)
movq -0x28(%rbp), %rax
movl $0x3, (%rax)
movq -0x28(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x18(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x0, %eax
jne 0x34a7
movw $0x2, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x4e(%rax), %edi
callq 0x20c0
movw %ax, -0x36(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2260
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
cmpl $-0x1, 0x4(%rax)
jne 0x342f
movq 0x2bc02(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, -0x60(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2220
movq -0x60(%rbp), %rdi
movq %rax, %r8
leaq 0x23bed(%rip), %rsi     # 0x27004
leaq 0x24961(%rip), %rdx     # 0x27d7f
movl $0x32, %ecx
movb $0x0, %al
callq 0x2170
callq 0x2040
jmp 0x3431
movq -0x28(%rbp), %rdi
leaq 0x2bd34(%rip), %rax     # 0x2f170
movl (%rax), %esi
movl $0x5, %edx
callq 0x2f10
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edi
leaq -0x38(%rbp), %rsi
movl $0x10, %edx
callq 0x2210
cmpl $-0x1, %eax
jne 0x34a2
callq 0x2050
cmpl $0x73, (%rax)
je 0x34a2
jmp 0x346e
movq 0x2bb83(%rip), %rax     # 0x2eff8
movq (%rax), %rdi
leaq 0x23beb(%rip), %rsi     # 0x2706a
leaq 0x248f9(%rip), %rdx     # 0x27d7f
movl $0x36, %ecx
leaq 0x23cac(%rip), %r8      # 0x2713e
movb $0x0, %al
callq 0x2170
movq -0x28(%rbp), %rdi
callq 0x3610
jmp 0x35bd
movq -0x18(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x6, %eax
jne 0x35bb
movw $0xa, -0x54(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x4c(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x4e(%rax), %edi
callq 0x20c0
movw %ax, -0x52(%rbp)
movl $0xa, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2260
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
cmpl $-0x1, 0x4(%rax)
jne 0x3546
movq 0x2baeb(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, -0x68(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2220
movq -0x68(%rbp), %rdi
movq %rax, %r8
leaq 0x23ad6(%rip), %rsi     # 0x27004
leaq 0x2484a(%rip), %rdx     # 0x27d7f
movl $0x41, %ecx
movb $0x0, %al
callq 0x2170
callq 0x2040
jmp 0x3548
movq -0x28(%rbp), %rdi
leaq 0x2bc1d(%rip), %rax     # 0x2f170
movl (%rax), %esi
movl $0x5, %edx
callq 0x2f10
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edi
leaq -0x54(%rbp), %rsi
movl $0x1c, %edx
callq 0x2210
cmpl $-0x1, %eax
jne 0x35b9
callq 0x2050
cmpl $0x73, (%rax)
je 0x35b9
jmp 0x3585
movq 0x2ba6c(%rip), %rax     # 0x2eff8
movq (%rax), %rdi
leaq 0x23ad4(%rip), %rsi     # 0x2706a
leaq 0x247e2(%rip), %rdx     # 0x27d7f
movl $0x45, %ecx
leaq 0x23b95(%rip), %r8      # 0x2713e
movb $0x0, %al
callq 0x2170
movq -0x28(%rbp), %rdi
callq 0x3610
jmp 0x35bb
jmp 0x35bd
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0xddc0
movq -0x18(%rbp), %rdi
leaq 0xeb(%rip), %rsi        # 0x36c0
callq 0xded0
movq -0x18(%rbp), %rdi
leaq 0x14b(%rip), %rsi       # 0x3730
callq 0xddf0
movq -0x18(%rbp), %rdi
leaq 0x34b(%rip), %rsi       # 0x3940
callq 0xde60
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)