// File: /workspace/github_c_makefile_star2_O3/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 0x3b38
leaq 0x1933c(%rip), %rax     # 0x1c8d8
movq (%rax), %rax
leaq 0x47a(%rip), %rcx       # 0x3a20
movq %rcx, 0xc0(%rax)
leaq 0x479(%rip), %rcx       # 0x3a2d
movq %rcx, 0xd0(%rax)
callq 0x84b0
testq %rax, %rax
je 0x395f
movq %rax, %rbx
leaq 0x10ce1(%rip), %rsi     # 0x142b4
movq %rax, %rdi
xorl %edx, %edx
callq 0x749a
testb %al, %al
jne 0x398d
movq %rbx, %rdi
movl $0xff, %esi
callq 0x77c5
leaq -0x1054(%rip), %rsi     # 0x25a5
movq %rax, %rdi
callq 0x85a1
callq 0xdc74
testq %rax, %rax
je 0x39be
movq %rax, %rbx
leaq 0x10c9b(%rip), %rsi     # 0x142b4
movq %rax, %rdi
xorl %edx, %edx
callq 0xd6e8
testb %al, %al
jne 0x39ef
leaq -0x83a(%rip), %rsi      # 0x2df8
movq %rbx, %rdi
xorl %edx, %edx
callq 0xdbc3
xorl %edi, %edi
callq 0x2230
movl %eax, 0x18b27(%rip)     # 0x1c170
movl $0x40, (%rsp)
leaq 0x109e2(%rip), %rdi     # 0x14039
leaq 0x109e8(%rip), %rsi     # 0x14046
leaq 0x109e6(%rip), %rcx     # 0x1404b
leaq 0x109e8(%rip), %r8      # 0x14054
leaq 0x109ef(%rip), %r9      # 0x14062
movl $0x5dc, %edx            # imm = 0x5DC
callq 0x332c
movl %eax, 0x18af1(%rip)     # 0x1c174
movl $0x38, %edi
callq 0x3b24
movl 0x18ae1(%rip), %ecx     # 0x1c174
movl %ecx, 0x4(%rax)
movl 0x18ad4(%rip), %esi     # 0x1c170
movq %rax, %rdi
movl $0x1, %edx
callq 0x3a3a
movl $0x5dc, %edi            # imm = 0x5DC
callq 0x3b24
movq %rax, %rbx
movq 0x18933(%rip), %rax     # 0x1bff0
movq (%rax), %rdi
leaq 0x109a3(%rip), %rsi     # 0x1406a
leaq 0x10964(%rip), %rdx     # 0x14032
leaq 0x109b6(%rip), %r8      # 0x1408b
movl $0x3b, %ecx
xorl %eax, %eax
callq 0x2170
leaq 0x109ed(%rip), %r13     # 0x140d5
movl 0x18a82(%rip), %edi     # 0x1c170
leaq 0x10(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
movl $0x14, %ecx
callq 0x21c0
testl %eax, %eax
jle 0x36e8
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 0x18a51(%rip), %ecx     # 0x1c174
jne 0x37fc
movl 0x18a45(%rip), %edi     # 0x1c174
movl $0x5dc, %edx            # imm = 0x5DC
movq %rbx, %rsi
callq 0x2140
movq %rax, %rbp
cmpl $-0x1, %ebp
je 0x381d
cmpl $0x5dc, %ebp            # imm = 0x5DC
je 0x38a1
testl %ebp, %ebp
jle 0x3799
movl %ebp, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %r15d, %r15d
movzbl (%rbx,%r15), %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0x20d0
incq %r15
testb $0xf, %r15b
je 0x378a
testb $0x7, %r15b
jne 0x3794
movl $0x20, %edi
jmp 0x378f
movl $0xa, %edi
callq 0x2040
cmpq %r15, %r14
jne 0x3765
testb $0xf, %bpl
je 0x37a9
movl $0xa, %edi
callq 0x2040
movl $0xa, %edi
callq 0x2040
movzwl %bp, %esi
xorl %edi, %edi
movl $0x182, %edx            # imm = 0x182
callq 0x4df7
testq %rax, %rax
je 0x38cf
movq %rax, %r14
movzwl %bp, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x58a6
leaq 0x190f5(%rip), %rax     # 0x1c8d8
movq (%rax), %rsi
movq %r14, %rdi
callq *0xb8(%rsi)
testb %al, %al
je 0x3729
jmp 0x38fd
leaq (%rsp,%rax,4), %rax
addq $0x10, %rax
movl (%rax), %edi
testb $0x5, %dil
je 0x383f
movl (%rsi), %eax
leal -0x4(%rax), %ecx
cmpl $0x2, %ecx
jae 0x3833
callq 0x30e5
jmp 0x3890
callq 0x2060
movl (%rax), %edi
cmpl $0xb, %edi
movq 0x8(%rsp), %r14
je 0x3890
jmp 0x3938
cmpl $0x3, %eax
jne 0x3865
callq 0x2acb
jmp 0x3890
movq 0x187aa(%rip), %rax     # 0x1bff0
movq (%rax), %rdi
leaq 0x1081a(%rip), %rsi     # 0x1406a
leaq 0x107db(%rip), %rdx     # 0x14032
movl $0x60, %ecx
leaq 0x1083f(%rip), %r8      # 0x140a2
jmp 0x3889
movq 0x18784(%rip), %rax     # 0x1bff0
movq (%rax), %rdi
leaq 0x107f4(%rip), %rsi     # 0x1406a
leaq 0x107b5(%rip), %rdx     # 0x14032
movl $0x5a, %ecx
leaq 0x1080d(%rip), %r8      # 0x14096
xorl %eax, %eax
callq 0x2170
incq %r12
cmpq %r14, %r12
jne 0x3711
jmp 0x36e8
movq 0x18748(%rip), %rax     # 0x1bff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x10746(%rip), %rsi     # 0x14004
leaq 0x1076d(%rip), %rdx     # 0x14032
movq %rbx, %rdi
movl $0x49, %ecx
jmp 0x3929
movq 0x1871a(%rip), %rax     # 0x1bff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x10718(%rip), %rsi     # 0x14004
leaq 0x1073f(%rip), %rdx     # 0x14032
movq %rbx, %rdi
movl $0x4d, %ecx
jmp 0x3929
movq 0x186ec(%rip), %rax     # 0x1bff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x106ea(%rip), %rsi     # 0x14004
leaq 0x10711(%rip), %rdx     # 0x14032
movq %rbx, %rdi
movl $0x50, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x2170
callq 0x2050
movq 0x186b1(%rip), %rax     # 0x1bff0
movq (%rax), %rbx
callq 0x2220
leaq 0x106b6(%rip), %rsi     # 0x14004
leaq 0x106dd(%rip), %rdx     # 0x14032
movq %rbx, %rdi
movl $0x46, %ecx
jmp 0x3929
movq 0x1868a(%rip), %rax     # 0x1bff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x10688(%rip), %rsi     # 0x14004
leaq 0x106af(%rip), %rdx     # 0x14032
movq %rbx, %rdi
movl $0x23, %ecx
jmp 0x3929
movq 0x1865c(%rip), %rax     # 0x1bff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x1065a(%rip), %rsi     # 0x14004
leaq 0x10681(%rip), %rdx     # 0x14032
movq %rbx, %rdi
movl $0x25, %ecx
jmp 0x3929
movq 0x1862b(%rip), %rax     # 0x1bff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x10629(%rip), %rsi     # 0x14004
leaq 0x10650(%rip), %rdx     # 0x14032
movq %rbx, %rdi
movl $0x28, %ecx
jmp 0x3929
movq 0x185fa(%rip), %rax     # 0x1bff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x105f8(%rip), %rsi     # 0x14004
leaq 0x1061f(%rip), %rdx     # 0x14032
movq %rbx, %rdi
movl $0x2a, %ecx
jmp 0x3929