// File: /workspace/github_c_makefile_star2_O3/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
movl $0x2, %esi
xorl %eax, %eax
callq 0x21e0
cmpl $-0x1, %eax
je 0x353c
movl %eax, %ebx
xorps %xmm0, %xmm0
leaq 0x18(%rsp), %rbp
movups %xmm0, 0x10(%rbp)
movq $0x0, 0x20(%rbp)
movw $0x1001, 0x10(%rbp)     # imm = 0x1001
movl $0x10, %edx
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 0x3541
movl %r15d, %ebx
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x2260
cmpl $-0x1, %eax
je 0x3546
movl %eax, %r14d
leaq 0x28(%rsp), %r15
xorps %xmm0, %xmm0
leaq 0x44(%rsp), %rbp
movaps %xmm0, -0x4(%rbp)
movw $0x2, -0x4(%rbp)
movl $0x2, %edi
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 0x354b
movl $0x2, %edi
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 0x3550
movl 0xa0(%rsp), %ebp
movq 0x10(%rsp), %r15
testq %r15, %r15
sete %al
testl %ebp, %ebp
sete %cl
orb %al, %cl
jne 0x34e6
movl $0xa, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x2260
cmpl $-0x1, %eax
je 0x355f
movl %eax, %r12d
leaq 0x50(%rsp), %r13
movl $0xa, %edi
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 0x3564
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 0x3569
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 0x3555
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 0x355a
movl %r14d, %edi
callq 0x2120
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x248a
callq 0x2450
callq 0x2416
callq 0x23dc
callq 0x23a2
callq 0x22ba
callq 0x2280
callq 0x2368
callq 0x232e
callq 0x22f4
nop