// File: /workspace/github_c_makefile_star2_O1/FH0[P]tun_direct/main.c
// Function: main
int main(int argc, char const *argv[]) {
  int nfds, n, nread;
  struct epoll_event events[EPOLL_SIZE];
  struct tcp_pcb *tpcb;
  struct udp_pcb *upcb;
  struct pbuf *p;
  char *buf;
  tun_data_t *data;
  err_t err;
  extern int errno;

  /* do not exit when socket broken */
  signal(SIGPIPE, SIG_IGN);

  /* lwip */
  lwip_init();
  netif_list->output = output4;
  netif_list->output_ip6 = output6;
  ASSERT(tpcb = tcp_new());
  err = tcp_bind(tpcb, IP_ANY_TYPE, 0);
  ASSERT(err == ERR_OK);
  tpcb = tcp_listen(tpcb);
  tcp_accept(tpcb, tun_tcp_accept);
  ASSERT(upcb = udp_new());
  err = udp_bind(upcb, IP_ANY_TYPE, 0);
  ASSERT(err == ERR_OK);
  udp_recv(upcb, tun_udp_recv, NULL);

  /* epoll */
  epollfd = epoll_create1(0);

  /* tun device */
  tunfd = create_tun(TUN_PATH, "tun3", MTU, "10.5.5.5", "255.255.255.0",
                     "fd55::5", 64);
  data = MALLOC(sizeof(tun_data_t));
  data->fd = tunfd;
  epoll_ET_add(data, epollfd, EPOLLIN);

  /* init buf to store data from tunfd */
  buf = MALLOC(MTU);

  /* loop */
  ELOG("entry loop");
  for (;;) {
    nfds = epoll_wait(epollfd, events, EPOLL_SIZE, 20);
    for (n = 0; n < nfds; n++) {
      data = (tun_data_t *)events[n].data.ptr;

      /* use lwip to handle ip data */
      if (data->fd == tunfd) {
        for (;;) {
          nread = read(tunfd, buf, MTU);
          if (nread == -1) {
            ASSERT(errno == EAGAIN);
            break;
          }
          ASSERT(nread != MTU);

          hex_dump(buf, nread);
          p = pbuf_alloc(PBUF_RAW, nread, PBUF_POOL);
          ASSERT(p);
          pbuf_take(p, buf, nread);
          err = netif_list->input(p, netif_list);
          ASSERT(err == ERR_OK);
        }
      }
      /* use lwip to generate ip data  */
      else if (events[n].events & EPOLLIN || events[n].events & EPOLLOUT) {
        if (data->type == TUN_TCP) {
          handle_epoll_tcp(events[n].events, data);
        } else if (data->type == TUN_UDP || data->type == TUN_UDP6) {
          handle_epoll_udp(events[n].events, data);
        } else {
          ELOG("unkown type");
          continue;
        }
      }
      /* something unexpected happened */
      else {
        ELOG("epoll events");
        continue;
      }
    }
  }

  FREE(buf);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1818, %rsp           # imm = 0x1818
movl $0x1, %esi
movl $0xd, %edi
callq 0x2160
callq 0x3b68
leaq 0x1a31c(%rip), %rax     # 0x1d8d8
movq (%rax), %rax
leaq 0x489(%rip), %rcx       # 0x3a4f
movq %rcx, 0xc0(%rax)
leaq 0x488(%rip), %rcx       # 0x3a5c
movq %rcx, 0xd0(%rax)
callq 0x85bc
testq %rax, %rax
je 0x398e
movq %rax, %rbx
leaq 0x11cc1(%rip), %rsi     # 0x152b4
movq %rax, %rdi
xorl %edx, %edx
callq 0x75a8
testb %al, %al
jne 0x39bc
movq %rbx, %rdi
movl $0xff, %esi
callq 0x78da
leaq -0x1074(%rip), %rsi     # 0x25a5
movq %rax, %rdi
callq 0x86ad
callq 0xdef5
testq %rax, %rax
je 0x39ed
movq %rax, %rbx
leaq 0x11c7b(%rip), %rsi     # 0x152b4
movq %rax, %rdi
xorl %edx, %edx
callq 0xd95d
testb %al, %al
jne 0x3a1e
leaq -0x84f(%rip), %rsi      # 0x2e03
movq %rbx, %rdi
xorl %edx, %edx
callq 0xde44
xorl %edi, %edi
callq 0x2230
movl %eax, 0x19b07(%rip)     # 0x1d170
movl $0x40, (%rsp)
leaq 0x119c2(%rip), %rdi     # 0x15039
leaq 0x119c8(%rip), %rsi     # 0x15046
leaq 0x119c6(%rip), %rcx     # 0x1504b
leaq 0x119c8(%rip), %r8      # 0x15054
leaq 0x119cf(%rip), %r9      # 0x15062
movl $0x5dc, %edx            # imm = 0x5DC
callq 0x333c
movl %eax, 0x19ad1(%rip)     # 0x1d174
movl $0x38, %edi
callq 0x3b54
movl 0x19ac1(%rip), %ecx     # 0x1d174
movl %ecx, 0x4(%rax)
movl 0x19ab4(%rip), %esi     # 0x1d170
movq %rax, %rdi
movl $0x1, %edx
callq 0x3a69
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x3b54
movq %rax, %rbx
movq 0x19913(%rip), %rax     # 0x1cff0
movq (%rax), %rdi
leaq 0x11983(%rip), %rsi     # 0x1506a
leaq 0x11944(%rip), %rdx     # 0x15032
leaq 0x11996(%rip), %r8      # 0x1508b
movl $0x3b, %ecx
xorl %eax, %eax
callq 0x2170
leaq 0x119cd(%rip), %r13     # 0x150d5
movl 0x19a62(%rip), %edi     # 0x1d170
leaq 0x10(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
movl $0x14, %ecx
callq 0x21c0
testl %eax, %eax
jle 0x3708
movl %eax, %r14d
xorl %r12d, %r12d
movq %r14, 0x8(%rsp)
leaq (%r12,%r12,2), %rax
movq 0x14(%rsp,%rax,4), %rsi
movl 0x4(%rsi), %ecx
cmpl 0x19a31(%rip), %ecx     # 0x1d174
jne 0x382b
movl 0x19a25(%rip), %edi     # 0x1d174
movl $0x5dc, %edx            # imm = 0x5DC
movq %rbx, %rsi
callq 0x2140
movq %rax, %rbp
cmpl $-0x1, %ebp
je 0x384c
cmpl $0x5dc, %ebp            # imm = 0x5DC
je 0x38d0
testl %ebp, %ebp
jle 0x37c8
movl %ebp, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
negq %r14
movl $0x1, %r15d
movzbl -0x1(%rbx,%r15), %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0x20d0
testb $0xf, %r15b
je 0x37ae
testb $0x7, %r15b
jne 0x37b8
movl $0x20, %edi
jmp 0x37b3
movl $0xa, %edi
callq 0x2040
leaq (%r14,%r15), %rax
incq %rax
incq %r15
cmpq $0x1, %rax
jne 0x378b
testb $0xf, %bpl
je 0x37d8
movl $0xa, %edi
callq 0x2040
movl $0xa, %edi
callq 0x2040
movzwl %bp, %esi
xorl %edi, %edi
movl $0x182, %edx            # imm = 0x182
callq 0x4e57
testq %rax, %rax
je 0x38fe
movq %rax, %r14
movzwl %bp, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5935
leaq 0x1a0c6(%rip), %rax     # 0x1d8d8
movq (%rax), %rsi
movq %r14, %rdi
callq *0xb8(%rsi)
testb %al, %al
je 0x3749
jmp 0x392c
leaq (%rsp,%rax,4), %rax
addq $0x10, %rax
movl (%rax), %edi
testb $0x5, %dil
je 0x386e
movl (%rsi), %eax
leal -0x4(%rax), %ecx
cmpl $0x2, %ecx
jae 0x3862
callq 0x30f2
jmp 0x38bf
callq 0x2060
movl (%rax), %edi
cmpl $0xb, %edi
movq 0x8(%rsp), %r14
je 0x38bf
jmp 0x3967
cmpl $0x3, %eax
jne 0x3894
callq 0x2ac9
jmp 0x38bf
movq 0x1977b(%rip), %rax     # 0x1cff0
movq (%rax), %rdi
leaq 0x117eb(%rip), %rsi     # 0x1506a
leaq 0x117ac(%rip), %rdx     # 0x15032
movl $0x60, %ecx
leaq 0x11810(%rip), %r8      # 0x150a2
jmp 0x38b8
movq 0x19755(%rip), %rax     # 0x1cff0
movq (%rax), %rdi
leaq 0x117c5(%rip), %rsi     # 0x1506a
leaq 0x11786(%rip), %rdx     # 0x15032
movl $0x5a, %ecx
leaq 0x117de(%rip), %r8      # 0x15096
xorl %eax, %eax
callq 0x2170
incq %r12
cmpq %r14, %r12
jne 0x3731
jmp 0x3708
movq 0x19719(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x11717(%rip), %rsi     # 0x15004
leaq 0x1173e(%rip), %rdx     # 0x15032
movq %rbx, %rdi
movl $0x49, %ecx
jmp 0x3958
movq 0x196eb(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x116e9(%rip), %rsi     # 0x15004
leaq 0x11710(%rip), %rdx     # 0x15032
movq %rbx, %rdi
movl $0x4d, %ecx
jmp 0x3958
movq 0x196bd(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x116bb(%rip), %rsi     # 0x15004
leaq 0x116e2(%rip), %rdx     # 0x15032
movq %rbx, %rdi
movl $0x50, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x2170
callq 0x2050
movq 0x19682(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2220
leaq 0x11687(%rip), %rsi     # 0x15004
leaq 0x116ae(%rip), %rdx     # 0x15032
movq %rbx, %rdi
movl $0x46, %ecx
jmp 0x3958
movq 0x1965b(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x11659(%rip), %rsi     # 0x15004
leaq 0x11680(%rip), %rdx     # 0x15032
movq %rbx, %rdi
movl $0x23, %ecx
jmp 0x3958
movq 0x1962d(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x1162b(%rip), %rsi     # 0x15004
leaq 0x11652(%rip), %rdx     # 0x15032
movq %rbx, %rdi
movl $0x25, %ecx
jmp 0x3958
movq 0x195fc(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x115fa(%rip), %rsi     # 0x15004
leaq 0x11621(%rip), %rdx     # 0x15032
movq %rbx, %rdi
movl $0x28, %ecx
jmp 0x3958
movq 0x195cb(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x115c9(%rip), %rsi     # 0x15004
leaq 0x115f0(%rip), %rdx     # 0x15032
movq %rbx, %rdi
movl $0x2a, %ecx
jmp 0x3958