#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct TYPE_2__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  int ifr_flags;
  int /*<<< orphan*/ ifr_name;
  TYPE_1__ ifr_addr;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFF_NO_PI;
int IFF_RUNNING;
int IFF_TUN;
int IFF_UP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFADDR;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SIOCSIFNETMASK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tun_alloc(char *dev, const char *ip, const char *netmask) {
  struct ifreq ifr;
  int fd, err, s;
  if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
    return -1;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  if (*dev)
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if ((err = ioctl(fd, TUNSETIFF, &ifr)) < 0) {
    close(fd);
    return err;
  }
  strcpy(dev, ifr.ifr_name);
  s = socket(AF_INET, SOCK_DGRAM, 0);
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  ifr.ifr_addr.sa_family = AF_INET;
  struct sockaddr_in *addr = (struct sockaddr_in *)&ifr.ifr_addr;
  inet_pton(AF_INET, ip, &addr->sin_addr);
  ioctl(s, SIOCSIFADDR, &ifr);
  inet_pton(AF_INET, netmask, &addr->sin_addr);
  ioctl(s, SIOCSIFNETMASK, &ifr);
  ioctl(s, SIOCGIFFLAGS, &ifr);
  strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
  ioctl(s, SIOCSIFFLAGS, &ifr);
  close(s);
  return fd;
}


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11c1 <tun_alloc+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <tun_alloc+0x1dc>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    0x2d9f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1208 <tun_alloc+0x88>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x34(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    123c <tun_alloc+0xbc>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    135c <tun_alloc+0x1dc>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <strcpy@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d1e(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x48(%rbp)
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1080 <inet_pton@plt>
mov    -0x3c(%rbp),%edi
mov    0x2cb7(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1080 <inet_pton@plt>
mov    -0x3c(%rbp),%edi
mov    0x2ce4(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x3c(%rbp),%edi
mov    0x2caf(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2c86(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2c98(%rip),%rax
mov    (%rax),%eax
mov    0x2c97(%rip),%rcx
or     (%rcx),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2cb5(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_alloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     130f <tun_alloc+0x18f>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dc2(%rip),%rax
mov    0x2dab(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
cmpb   $0x0,(%r14)
je     11f8 <tun_alloc+0x78>
mov    0xc(%rsp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1316 <tun_alloc+0x196>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
lea    0x10(%rsp),%r12
mov    %edi,0x10(%rsp)
mov    %r12,%rdx
call   1080 <inet_pton@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r13,%rdx
call   1060 <ioctl@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1080 <inet_pton@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r13,%rdx
call   1060 <ioctl@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r13,%rdx
call   1060 <ioctl@plt>
mov    0xc(%rsp),%edi
mov    0x2cc6(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2cd5(%rip),%rax
mov    0x2cd6(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,0x8(%rsp)
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r13,%rdx
call   1060 <ioctl@plt>
jmp    131a <tun_alloc+0x19a>
mov    $0xffffffff,%ebx
jmp    1321 <tun_alloc+0x1a1>
mov    %ebx,%ebp
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCSIFADDR = global i32 0
@SIOCSIFNETMASK = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i32 0
@IFF_RUNNING = global i32 0
@SIOCSIFFLAGS = global i32 0
define i32 @tun_alloc(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.ifreq
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_in*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %96
17: ; preds = %3
%18 = call i32 @memset(%struct.ifreq* %8, i32 0, i32 12)
%19 = load i32, i32* @IFF_TUN
%20 = load i32, i32* @IFF_NO_PI
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = icmp ne i8 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %17
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i8*, i8** %5
%30 = load i32, i32* @IFNAMSIZ
%31 = call i32 @strncpy(i32 %28, i8* %29, i32 %30)
br label %32
32: ; preds = %26, %17
%33 = load i32, i32* %9
%34 = load i32, i32* @TUNSETIFF
%35 = call i32 @ioctl(i32 %33, i32 %34, %struct.ifreq* %8)
store i32 %35, i32* %10
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i32, i32* %9
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %10
store i32 %40, i32* %4
br label %96
41: ; preds = %32
%42 = load i8*, i8** %5
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%44 = load i32, i32* %43
%45 = call i32 @strcpy(i8* %42, i32 %44)
%46 = load i32, i32* @AF_INET
%47 = load i32, i32* @SOCK_DGRAM
%48 = call i32 @socket(i32 %46, i32 %47, i32 0)
store i32 %48, i32* %11
%49 = call i32 @memset(%struct.ifreq* %8, i32 0, i32 12)
%50 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load i8*, i8** %5
%53 = load i32, i32* @IFNAMSIZ
%54 = call i32 @strncpy(i32 %51, i8* %52, i32 %53)
%55 = load i32, i32* @AF_INET
%56 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 2
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
store i32 %55, i32* %57
%58 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 2
%59 = bitcast %struct.TYPE_2__* %58 to %struct.sockaddr_in*
store %struct.sockaddr_in* %59, %struct.sockaddr_in** %12
%60 = load i32, i32* @AF_INET
%61 = load i8*, i8** %6
%62 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %62, i32 0, i32 0
%64 = call i32 @inet_pton(i32 %60, i8* %61, i32* %63)
%65 = load i32, i32* %11
%66 = load i32, i32* @SIOCSIFADDR
%67 = call i32 @ioctl(i32 %65, i32 %66, %struct.ifreq* %8)
%68 = load i32, i32* @AF_INET
%69 = load i8*, i8** %7
%70 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %70, i32 0, i32 0
%72 = call i32 @inet_pton(i32 %68, i8* %69, i32* %71)
%73 = load i32, i32* %11
%74 = load i32, i32* @SIOCSIFNETMASK
%75 = call i32 @ioctl(i32 %73, i32 %74, %struct.ifreq* %8)
%76 = load i32, i32* %11
%77 = load i32, i32* @SIOCGIFFLAGS
%78 = call i32 @ioctl(i32 %76, i32 %77, %struct.ifreq* %8)
%79 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%80 = load i32, i32* %79
%81 = load i8*, i8** %5
%82 = load i32, i32* @IFNAMSIZ
%83 = call i32 @strncpy(i32 %80, i8* %81, i32 %82)
%84 = load i32, i32* @IFF_UP
%85 = load i32, i32* @IFF_RUNNING
%86 = or i32 %84, %85
%87 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%88 = load i32, i32* %87
%89 = or i32 %88, %86
store i32 %89, i32* %87
%90 = load i32, i32* %11
%91 = load i32, i32* @SIOCSIFFLAGS
%92 = call i32 @ioctl(i32 %90, i32 %91, %struct.ifreq* %8)
%93 = load i32, i32* %11
%94 = call i32 @close(i32 %93)
%95 = load i32, i32* %9
store i32 %95, i32* %4
br label %96
96: ; preds = %41, %37, %16
%97 = load i32, i32* %4
ret i32 %97
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCSIFADDR = global i32 0
@SIOCSIFNETMASK = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i32 0
@IFF_RUNNING = global i32 0
@SIOCSIFFLAGS = global i32 0
define i32 @tun_alloc(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %62, label %9
9: ; preds = %3
%10 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 12)
%11 = load i32, i32* @IFF_TUN
%12 = load i32, i32* @IFF_NO_PI
%13 = or i32 %12, %11
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
store i32 %13, i32* %14
%15 = load i8, i8* %0
%16 = icmp eq i8 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %9
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%19 = load i32, i32* %18
%20 = load i32, i32* @IFNAMSIZ
%21 = call i32 @strncpy(i32 %19, i8* %0, i32 %20)
br label %22
22: ; preds = %17, %9
%23 = load i32, i32* @TUNSETIFF
%24 = call i32 @ioctl(i32 %7, i32 %23, %struct.ifreq* %4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %58, label %26
26: ; preds = %22
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @strcpy(i8* %0, i32 %28)
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_DGRAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
%33 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 12)
%34 = load i32, i32* %27
%35 = load i32, i32* @IFNAMSIZ
%36 = call i32 @strncpy(i32 %34, i8* %0, i32 %35)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 2, i32 0
store i32 %37, i32* %38
%39 = call i32 @inet_pton(i32 %37, i8* %1, i32* %38)
%40 = load i32, i32* @SIOCSIFADDR
%41 = call i32 @ioctl(i32 %32, i32 %40, %struct.ifreq* %4)
%42 = load i32, i32* @AF_INET
%43 = call i32 @inet_pton(i32 %42, i8* %2, i32* %38)
%44 = load i32, i32* @SIOCSIFNETMASK
%45 = call i32 @ioctl(i32 %32, i32 %44, %struct.ifreq* %4)
%46 = load i32, i32* @SIOCGIFFLAGS
%47 = call i32 @ioctl(i32 %32, i32 %46, %struct.ifreq* %4)
%48 = load i32, i32* %27
%49 = load i32, i32* @IFNAMSIZ
%50 = call i32 @strncpy(i32 %48, i8* %0, i32 %49)
%51 = load i32, i32* @IFF_UP
%52 = load i32, i32* @IFF_RUNNING
%53 = or i32 %52, %51
%54 = load i32, i32* %14
%55 = or i32 %53, %54
store i32 %55, i32* %14
%56 = load i32, i32* @SIOCSIFFLAGS
%57 = call i32 @ioctl(i32 %32, i32 %56, %struct.ifreq* %4)
br label %58
58: ; preds = %22, %26
%59 = phi i32 [ %32, %26 ], [ %7, %22 ]
%60 = phi i32 [ %7, %26 ], [ %24, %22 ]
%61 = call i32 @close(i32 %59)
br label %62
62: ; preds = %58, %3
%63 = phi i32 [ -1, %3 ], [ %60, %58 ]
ret i32 %63
}