// File: /workspace/github_c_makefile_star2_O2/FH0[P]tun_direct/init_tun.c
// Function: create_tun
int create_tun(char *path, char *name, int mtu, char *addr, char *netmask,
               char *addr6, int prefixlen) {
  struct sockaddr_in sai;
  int fd, sockfd, sock6fd;
  struct ifreq ifr;
  struct in6_ifreq ifr6;

  /* get fd */
  fd = open(path, O_RDWR);
  ASSERT(fd != -1);

  /* set name */
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  strncpy(ifr.ifr_name, name, IFNAMSIZ);
  ASSERT(ioctl(fd, TUNSETIFF, &ifr) != -1);

  /* socket to connect kernel */
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  ASSERT(sockfd != -1);

  /* set addr */
  memset(&sai, 0, sizeof(sai));
  sai.sin_family = AF_INET;
  inet_pton(AF_INET, addr, &sai.sin_addr.s_addr);
  memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
  ASSERT(ioctl(sockfd, SIOCSIFADDR, &ifr) != -1);

  /* set netmask */
  inet_pton(AF_INET, netmask, &sai.sin_addr);
  memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
  ASSERT(ioctl(sockfd, SIOCSIFNETMASK, &ifr) != -1);

  /* ipv6 */
  if (addr6 && prefixlen) {
    /* socket to connect kernel */
    sock6fd = socket(AF_INET6, SOCK_DGRAM, 0);
    ASSERT(sock6fd != -1);

    /* address and prefixlen*/
    inet_pton(AF_INET6, addr6, &ifr6.ifr6_addr);
    ifr6.ifr6_prefixlen = prefixlen;
    ASSERT(-1 != (ioctl(sock6fd, SIOGIFINDEX, &ifr)));
    ifr6.ifr6_ifindex = ifr.ifr_ifindex;
    ASSERT(ioctl(sock6fd, SIOCSIFADDR, &ifr6) != -1);

    close(sock6fd);
  }

  /* set mtu */
  ifr.ifr_mtu = mtu;
  ASSERT(ioctl(sockfd, SIOCSIFMTU, &ifr) != -1);

  /* start device */
  ifr.ifr_flags |= IFF_UP;
  ASSERT(ioctl(sockfd, SIOCSIFFLAGS, &ifr) != -1);

  close(sockfd);
  return fd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x10(%rsp)
movq %r8, %r12
movq %rcx, %r13
movl %edx, %r15d
movq %rsi, %r14
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x21e0
cmpl $-0x1, %eax
je 0x256e
movl %eax, %ebx
xorps %xmm0, %xmm0
leaq 0x18(%rsp), %rbp
movups %xmm0, 0x10(%rbp)
andq $0x0, 0x20(%rbp)
movw $0x1001, 0x10(%rbp)     # imm = 0x1001
pushq $0x10
popq %rdx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2070
movl $0x400454ca, %esi       # imm = 0x400454CA
movl %ebx, 0xc(%rsp)
movl %ebx, %edi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2100
cmpl $-0x1, %eax
je 0x2599
movl %r15d, %ebx
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x2260
cmpl $-0x1, %eax
je 0x25c4
movl %eax, %r14d
leaq 0x28(%rsp), %r15
xorps %xmm0, %xmm0
leaq 0x44(%rsp), %rbp
movaps %xmm0, -0x4(%rbp)
movw $0x2, -0x4(%rbp)
pushq $0x2
popq %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x2190
movaps -0x4(%rbp), %xmm0
movups %xmm0, (%r15)
leaq 0x18(%rsp), %rdx
movl $0x8916, %esi           # imm = 0x8916
movl %r14d, %edi
xorl %eax, %eax
callq 0x2100
cmpl $-0x1, %eax
je 0x25ef
pushq $0x2
popq %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x2190
movaps 0x40(%rsp), %xmm0
movups %xmm0, (%r15)
leaq 0x18(%rsp), %rdx
movl $0x891c, %esi           # imm = 0x891C
movl %r14d, %edi
xorl %eax, %eax
callq 0x2100
cmpl $-0x1, %eax
je 0x261a
movl 0xa0(%rsp), %ebp
movq 0x10(%rsp), %r15
testq %r15, %r15
sete %al
testl %ebp, %ebp
sete %cl
orb %al, %cl
jne 0x2510
pushq $0xa
popq %rdi
pushq $0x2
popq %rsi
xorl %edx, %edx
callq 0x2260
cmpl $-0x1, %eax
je 0x2698
movl %eax, %r12d
pushq $0xa
popq %rdi
leaq 0x50(%rsp), %r13
movq %r15, %rsi
movq %r13, %rdx
callq 0x2190
movl %ebp, 0x10(%r13)
leaq 0x18(%rsp), %rdx
movl $0x8933, %esi           # imm = 0x8933
movl %r12d, %edi
xorl %eax, %eax
callq 0x2100
cmpl $-0x1, %eax
je 0x26c0
movl 0x28(%rsp), %eax
leaq 0x50(%rsp), %rdx
movl %eax, 0x14(%rdx)
movl $0x8916, %esi           # imm = 0x8916
movl %r12d, %edi
xorl %eax, %eax
callq 0x2100
cmpl $-0x1, %eax
je 0x26e8
movl %r12d, %edi
callq 0x2120
leaq 0x18(%rsp), %rdx
movl %ebx, 0x10(%rdx)
movl $0x8922, %esi           # imm = 0x8922
movl %r14d, %edi
xorl %eax, %eax
callq 0x2100
cmpl $-0x1, %eax
je 0x2645
leaq 0x18(%rsp), %rdx
orb $0x1, 0x10(%rdx)
movl $0x8914, %esi           # imm = 0x8914
movl %r14d, %edi
xorl %eax, %eax
callq 0x2100
cmpl $-0x1, %eax
movl 0xc(%rsp), %ebx
je 0x2670
movl %r14d, %edi
callq 0x2120
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1aa7b(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x11a79(%rip), %rsi     # 0x14004
leaq 0x11a95(%rip), %rdx     # 0x14027
pushq $0x1a
jmp 0x270e
movq 0x1aa50(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x11a4e(%rip), %rsi     # 0x14004
leaq 0x11a6a(%rip), %rdx     # 0x14027
pushq $0x20
jmp 0x270e
movq 0x1aa25(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x11a23(%rip), %rsi     # 0x14004
leaq 0x11a3f(%rip), %rdx     # 0x14027
pushq $0x24
jmp 0x270e
movq 0x1a9fa(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x119f8(%rip), %rsi     # 0x14004
leaq 0x11a14(%rip), %rdx     # 0x14027
pushq $0x2b
jmp 0x270e
movq 0x1a9cf(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x119cd(%rip), %rsi     # 0x14004
leaq 0x119e9(%rip), %rdx     # 0x14027
pushq $0x30
jmp 0x270e
movq 0x1a9a4(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x119a2(%rip), %rsi     # 0x14004
leaq 0x119be(%rip), %rdx     # 0x14027
pushq $0x44
jmp 0x270e
movq 0x1a979(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x11977(%rip), %rsi     # 0x14004
leaq 0x11993(%rip), %rdx     # 0x14027
pushq $0x48
jmp 0x270e
movq 0x1a951(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x1194f(%rip), %rsi     # 0x14004
leaq 0x1196b(%rip), %rdx     # 0x14027
pushq $0x36
jmp 0x270e
movq 0x1a929(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x11927(%rip), %rsi     # 0x14004
leaq 0x11943(%rip), %rdx     # 0x14027
pushq $0x3b
jmp 0x270e
movq 0x1a901(%rip), %rax     # 0x1cff0
movq (%rax), %rbx
callq 0x2060
movl (%rax), %edi
callq 0x2220
leaq 0x118ff(%rip), %rsi     # 0x14004
leaq 0x1191b(%rip), %rdx     # 0x14027
pushq $0x3d
popq %rcx
movq %rbx, %rdi
movq %rax, %r8
xorl %eax, %eax
callq 0x2170
callq 0x2050
nopl (%rax)