// File: /workspace/github_c_makefile_star2_O2/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 $0x1828, %rsp           # imm = 0x1828
pushq $0xd
popq %rdi
pushq $0x1
popq %rsi
callq 0x2160
callq 0x3a14
leaq 0x1b18c(%rip), %rax     # 0x1d8d8
movq (%rax), %rax
leaq 0x44a(%rip), %rcx       # 0x2ba0
movq %rcx, 0xc0(%rax)
leaq 0x449(%rip), %rcx       # 0x2bad
movq %rcx, 0xd0(%rax)
callq 0x8141
testq %rax, %rax
je 0x2af7
movq %rax, %rbx
leaq 0x11b31(%rip), %rsi     # 0x142b4
movq %rax, %rdi
xorl %edx, %edx
callq 0x71f4
testb %al, %al
jne 0x2b1f
movq %rbx, %rdi
movl $0xff, %esi
callq 0x7511
leaq 0x5cb(%rip), %rsi       # 0x2d74
movq %rax, %rdi
callq 0x822e
callq 0xd6ae
testq %rax, %rax
je 0x2b4a
movq %rax, %rbx
leaq 0x11aeb(%rip), %rsi     # 0x142b4
movq %rax, %rdi
xorl %edx, %edx
callq 0xd12a
testb %al, %al
jne 0x2b75
leaq 0xdea(%rip), %rsi       # 0x35cc
movq %rbx, %rdi
xorl %edx, %edx
callq 0xd5ff
xorl %edi, %edi
callq 0x2230
movl %eax, 0x1a977(%rip)     # 0x1d170
movl $0x40, (%rsp)
leaq 0x11832(%rip), %rdi     # 0x14039
leaq 0x11838(%rip), %rsi     # 0x14046
leaq 0x11836(%rip), %rcx     # 0x1404b
leaq 0x11838(%rip), %r8      # 0x14054
leaq 0x1183f(%rip), %r9      # 0x14062
movl $0x5dc, %edx            # imm = 0x5DC
callq 0x236c
movl %eax, 0x1a941(%rip)     # 0x1d174
pushq $0x38
popq %rdi
callq 0x2d3c
movl 0x1a933(%rip), %ecx     # 0x1d174
movl %ecx, 0x4(%rax)
movl 0x1a926(%rip), %esi     # 0x1d170
pushq $0x1
popq %rdx
movq %rax, %rdi
callq 0x2bba
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x2d3c
movq %rax, %rbx
movq 0x1a787(%rip), %rax     # 0x1cff0
movq (%rax), %rdi
leaq 0x117f7(%rip), %rsi     # 0x1406a
leaq 0x117b8(%rip), %rdx     # 0x14032
leaq 0x1180a(%rip), %r8      # 0x1408b
pushq $0x3b
popq %rcx
xorl %eax, %eax
callq 0x2170
pushq $0xa
popq %r13
leaq 0x1183f(%rip), %r12     # 0x140d5
movl 0x1a8d4(%rip), %edi     # 0x1d170
leaq 0x20(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
pushq $0x14
popq %rcx
callq 0x21c0
movl %eax, %r14d
testl %eax, %eax
movl $0x0, %eax
cmovlel %eax, %r14d
xorl %edx, %edx
movq %r14, 0x10(%rsp)
cmpq %r14, %rdx
je 0x2896
imulq $0xc, %rdx, %rax
movq 0x24(%rsp,%rax), %rsi
movl 0x4(%rsi), %ecx
cmpl 0x1a89a(%rip), %ecx     # 0x1d174
movq %rdx, 0x18(%rsp)
jne 0x29b1
movl 0x1a889(%rip), %edi     # 0x1d174
movl $0x5dc, %edx            # imm = 0x5DC
movq %rbx, %rsi
callq 0x2140
movq %rax, %r14
cmpl $-0x1, %r14d
je 0x29d1
cmpl $0x5dc, %r14d           # imm = 0x5DC
je 0x2a4d
testl %r14d, %r14d
movl $0x0, %r15d
cmovgl %r14d, %r15d
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0x2950
movzbl (%rbx,%rbp), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x20d0
incq %rbp
movl %r13d, %edi
testb $0xf, %bpl
je 0x2949
pushq $0x20
popq %rdi
testb $0x7, %bpl
jne 0x2921
callq 0x2040
jmp 0x2921
testb $0xf, %r14b
je 0x295e
movl %r13d, %edi
callq 0x2040
movl %r13d, %edi
callq 0x2040
movzwl %r14w, %esi
xorl %edi, %edi
movl $0x182, %edx            # imm = 0x182
callq 0x4c47
testq %rax, %rax
je 0x2a9d
movq %rax, %r15
movzwl %r14w, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x56b4
leaq 0x1af40(%rip), %rax     # 0x1d8d8
movq (%rax), %rsi
movq %r15, %rdi
callq *0xb8(%rsi)
testb %al, %al
je 0x28e5
jmp 0x2a75
addq %rsp, %rax
addq $0x20, %rax
movl (%rax), %edi
testb $0x5, %dil
je 0x29f3
movl (%rsi), %eax
leal -0x4(%rax), %ecx
cmpl $0x2, %ecx
jae 0x29e7
callq 0x38b9
jmp 0x2a40
callq 0x2060
movl (%rax), %edi
cmpl $0xb, %edi
movq 0x10(%rsp), %r14
je 0x2a40
jmp 0x2ad6
cmpl $0x3, %eax
jne 0x2a17
callq 0x3299
jmp 0x2a40
movq 0x1a5f6(%rip), %rax     # 0x1cff0
movq (%rax), %rdi
leaq 0x11666(%rip), %rsi     # 0x1406a
leaq 0x11627(%rip), %rdx     # 0x14032
pushq $0x60
popq %rcx
leaq 0x1168d(%rip), %r8      # 0x140a2
jmp 0x2a39
movq 0x1a5d2(%rip), %rax     # 0x1cff0
movq (%rax), %rdi
leaq 0x11642(%rip), %rsi     # 0x1406a
leaq 0x11603(%rip), %rdx     # 0x14032
pushq $0x5a
popq %rcx
leaq 0x1165d(%rip), %r8      # 0x14096
xorl %eax, %eax
callq 0x2170
movq 0x18(%rsp), %rdx
incq %rdx
jmp 0x28c3
movq 0x1a59c(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x1159a(%rip), %rsi     # 0x14004
leaq 0x115c1(%rip), %rdx     # 0x14032
pushq $0x49
jmp 0x2ac3
movq 0x1a574(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x11572(%rip), %rsi     # 0x14004
leaq 0x11599(%rip), %rdx     # 0x14032
pushq $0x50
jmp 0x2ac3
movq 0x1a54c(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x1154a(%rip), %rsi     # 0x14004
leaq 0x11571(%rip), %rdx     # 0x14032
pushq $0x4d
popq %rcx
movq %rbx, %rdi
movq %rax, %r8
xorl %eax, %eax
callq 0x2170
callq 0x2050
movq 0x1a513(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2220
leaq 0x11518(%rip), %rsi     # 0x14004
leaq 0x1153f(%rip), %rdx     # 0x14032
pushq $0x46
jmp 0x2ac3
movq 0x1a4f2(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x114f0(%rip), %rsi     # 0x14004
leaq 0x11517(%rip), %rdx     # 0x14032
pushq $0x23
jmp 0x2ac3
movq 0x1a4ca(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x114c8(%rip), %rsi     # 0x14004
leaq 0x114ef(%rip), %rdx     # 0x14032
pushq $0x25
jmp 0x2ac3
movq 0x1a49f(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x1149d(%rip), %rsi     # 0x14004
leaq 0x114c4(%rip), %rdx     # 0x14032
pushq $0x28
jmp 0x2ac3
movq 0x1a474(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x11472(%rip), %rsi     # 0x14004
leaq 0x11499(%rip), %rdx     # 0x14032
pushq $0x2a
jmp 0x2ac3