// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x18a0, %rsp           # imm = 0x18A0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xd, %edi
movl $0x1, %esi
callq 0x2160
callq 0x43f0
leaq 0x2d054(%rip), %rax     # 0x2f8d8
movq (%rax), %rax
leaq 0x622(%rip), %rcx       # 0x2eb0
movq %rcx, 0xc0(%rax)
leaq 0x2d03c(%rip), %rax     # 0x2f8d8
movq (%rax), %rax
leaq 0x63a(%rip), %rcx       # 0x2ee0
movq %rcx, 0xd0(%rax)
callq 0xdd60
movq %rax, -0x1828(%rbp)
cmpq $0x0, %rax
jne 0x2905
movq 0x2c732(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, -0x1858(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2220
movq -0x1858(%rbp), %rdi
movq %rax, %r8
leaq 0x24717(%rip), %rsi     # 0x27004
leaq 0x2473e(%rip), %rdx     # 0x27032
movl $0x23, %ecx
movb $0x0, %al
callq 0x2170
callq 0x2040
jmp 0x2907
movq -0x1828(%rbp), %rdi
leaq 0x2499f(%rip), %rsi     # 0x272b4
xorl %edx, %edx
callq 0xbcb0
movb %al, -0x1849(%rbp)
movsbl -0x1849(%rbp), %eax
cmpl $0x0, %eax
je 0x2974
movq 0x2c6c3(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, -0x1860(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2220
movq -0x1860(%rbp), %rdi
movq %rax, %r8
leaq 0x246a8(%rip), %rsi     # 0x27004
leaq 0x246cf(%rip), %rdx     # 0x27032
movl $0x25, %ecx
movb $0x0, %al
callq 0x2170
callq 0x2040
jmp 0x2976
movq -0x1828(%rbp), %rdi
movl $0xff, %esi
callq 0xc420
movq %rax, -0x1828(%rbp)
movq -0x1828(%rbp), %rdi
leaq 0x8a4(%rip), %rsi       # 0x3240
callq 0xdf40
callq 0x18da0
movq %rax, -0x1830(%rbp)
cmpq $0x0, %rax
jne 0x29f9
movq 0x2c63e(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, -0x1868(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2220
movq -0x1868(%rbp), %rdi
movq %rax, %r8
leaq 0x24623(%rip), %rsi     # 0x27004
leaq 0x2464a(%rip), %rdx     # 0x27032
movl $0x28, %ecx
movb $0x0, %al
callq 0x2170
callq 0x2040
jmp 0x29fb
movq -0x1830(%rbp), %rdi
leaq 0x248ab(%rip), %rsi     # 0x272b4
xorl %edx, %edx
callq 0x17fd0
movb %al, -0x1849(%rbp)
movsbl -0x1849(%rbp), %eax
cmpl $0x0, %eax
je 0x2a68
movq 0x2c5cf(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, -0x1870(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2220
movq -0x1870(%rbp), %rdi
movq %rax, %r8
leaq 0x245b4(%rip), %rsi     # 0x27004
leaq 0x245db(%rip), %rdx     # 0x27032
movl $0x2a, %ecx
movb $0x0, %al
callq 0x2170
callq 0x2040
jmp 0x2a6a
movq -0x1830(%rbp), %rdi
leaq 0x13c8(%rip), %rsi      # 0x3e40
xorl %eax, %eax
movl %eax, %edx
callq 0x18c60
xorl %edi, %edi
callq 0x2230
movl %eax, 0x2c6e2(%rip)     # 0x2f170
leaq 0x245a4(%rip), %rdi     # 0x27039
leaq 0x245aa(%rip), %rsi     # 0x27046
movl $0x5dc, %edx            # imm = 0x5DC
leaq 0x245a3(%rip), %rcx     # 0x2704b
leaq 0x245a5(%rip), %r8      # 0x27054
leaq 0x245ac(%rip), %r9      # 0x27062
movl $0x40, (%rsp)
callq 0x2370
movl %eax, 0x2c6ac(%rip)     # 0x2f174
movl $0x38, %edi
callq 0x31e0
movq %rax, -0x1848(%rbp)
movl 0x2c695(%rip), %ecx     # 0x2f174
movq -0x1848(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x1848(%rbp), %rdi
movl 0x2c67a(%rip), %esi     # 0x2f170
movl $0x1, %edx
callq 0x2f10
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x31e0
movq %rax, -0x1840(%rbp)
movq 0x2c4e0(%rip), %rax     # 0x2eff8
movq (%rax), %rdi
leaq 0x24548(%rip), %rsi     # 0x2706a
leaq 0x24509(%rip), %rdx     # 0x27032
movl $0x3b, %ecx
leaq 0x24556(%rip), %r8      # 0x2708b
movb $0x0, %al
callq 0x2170
jmp 0x2b3e
movl 0x2c62c(%rip), %edi     # 0x2f170
leaq -0x1820(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
movl $0x14, %ecx
callq 0x21c0
movl %eax, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2ea4
movslq -0x18(%rbp), %rcx
leaq -0x1820(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq 0x4(%rax), %rax
movq %rax, -0x1848(%rbp)
movq -0x1848(%rbp), %rax
movl 0x4(%rax), %eax
cmpl 0x2c5d7(%rip), %eax     # 0x2f174
jne 0x2d8c
jmp 0x2ba5
movl 0x2c5c9(%rip), %edi     # 0x2f174
movq -0x1840(%rbp), %rsi
movl $0x5dc, %edx            # imm = 0x5DC
callq 0x2140
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x2c1e
jmp 0x2bc7
callq 0x2050
cmpl $0xb, (%rax)
je 0x2c17
movq 0x2c420(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, -0x1878(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2220
movq -0x1878(%rbp), %rdi
movq %rax, %r8
leaq 0x24405(%rip), %rsi     # 0x27004
leaq 0x2442c(%rip), %rdx     # 0x27032
movl $0x46, %ecx
movb $0x0, %al
callq 0x2170
callq 0x2040
jmp 0x2c19
jmp 0x2d87
jmp 0x2c20
cmpl $0x5dc, -0x1c(%rbp)     # imm = 0x5DC
jne 0x2c6f
movq 0x2c3c8(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, -0x1880(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2220
movq -0x1880(%rbp), %rdi
movq %rax, %r8
leaq 0x243ad(%rip), %rsi     # 0x27004
leaq 0x243d4(%rip), %rdx     # 0x27032
movl $0x49, %ecx
movb $0x0, %al
callq 0x2170
callq 0x2040
jmp 0x2c71
movq -0x1840(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x2fd0
movl -0x1c(%rbp), %eax
xorl %edi, %edi
movl $0x182, %edx            # imm = 0x182
movzwl %ax, %esi
callq 0x6e20
movq %rax, -0x1838(%rbp)
cmpq $0x0, -0x1838(%rbp)
jne 0x2ce9
movq 0x2c34e(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, -0x1888(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2220
movq -0x1888(%rbp), %rdi
movq %rax, %r8
leaq 0x24333(%rip), %rsi     # 0x27004
leaq 0x2435a(%rip), %rdx     # 0x27032
movl $0x4d, %ecx
movb $0x0, %al
callq 0x2170
callq 0x2040
jmp 0x2ceb
movq -0x1838(%rbp), %rdi
movq -0x1840(%rbp), %rsi
movl -0x1c(%rbp), %eax
movzwl %ax, %edx
callq 0x8630
leaq 0x2cbcd(%rip), %rax     # 0x2f8d8
movq (%rax), %rax
movq 0xb8(%rax), %rax
movq -0x1838(%rbp), %rdi
leaq 0x2cbb5(%rip), %rcx     # 0x2f8d8
movq (%rcx), %rsi
callq *%rax
movb %al, -0x1849(%rbp)
movsbl -0x1849(%rbp), %eax
cmpl $0x0, %eax
je 0x2d80
movq 0x2c2b7(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, -0x1890(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2220
movq -0x1890(%rbp), %rdi
movq %rax, %r8
leaq 0x2429c(%rip), %rsi     # 0x27004
leaq 0x242c3(%rip), %rdx     # 0x27032
movl $0x50, %ecx
movb $0x0, %al
callq 0x2170
callq 0x2040
jmp 0x2d82
jmp 0x2ba5
jmp 0x2e94
movslq -0x18(%rbp), %rcx
leaq -0x1820(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x2dc8
movslq -0x18(%rbp), %rcx
leaq -0x1820(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x2e63
movq -0x1848(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x2df6
movslq -0x18(%rbp), %rcx
leaq -0x1820(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %edi
movq -0x1848(%rbp), %rsi
callq 0x39a0
jmp 0x2e61
movq -0x1848(%rbp), %rax
cmpl $0x4, (%rax)
je 0x2e0e
movq -0x1848(%rbp), %rax
cmpl $0x5, (%rax)
jne 0x2e30
movslq -0x18(%rbp), %rcx
leaq -0x1820(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %edi
movq -0x1848(%rbp), %rsi
callq 0x4210
jmp 0x2e5f
jmp 0x2e32
movq 0x2c1bf(%rip), %rax     # 0x2eff8
movq (%rax), %rdi
leaq 0x24227(%rip), %rsi     # 0x2706a
leaq 0x241e8(%rip), %rdx     # 0x27032
movl $0x5a, %ecx
leaq 0x24240(%rip), %r8      # 0x27096
movb $0x0, %al
callq 0x2170
jmp 0x2e96
jmp 0x2e61
jmp 0x2e92
jmp 0x2e65
movq 0x2c18c(%rip), %rax     # 0x2eff8
movq (%rax), %rdi
leaq 0x241f4(%rip), %rsi     # 0x2706a
leaq 0x241b5(%rip), %rdx     # 0x27032
movl $0x60, %ecx
leaq 0x24219(%rip), %r8      # 0x270a2
movb $0x0, %al
callq 0x2170
jmp 0x2e96
jmp 0x2e94
jmp 0x2e96
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x2b64
jmp 0x2b3e
nopl (%rax)