#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 */

/* Type definitions */
struct ifreq {
  int ifr_flags;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFF_RUNNING;
int IFF_UP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
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, int flags) {
  struct ifreq ifr;
  int fd, err;
  char *clonedev = "/dev/net/tun";
  if ((fd = open(clonedev, O_RDWR)) < 0) {
    perror("Opening /dev/net/tun");
    return fd;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = flags;
  if (*dev) {
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  }
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
    perror("ioctl(TUNSETIFF)");
    close(fd);
    return err;
  }
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
  if ((err = ioctl(sock, SIOCSIFFLAGS, (void *)&ifr)) < 0) {
    perror("ioctl(FLAGS)");
  }
  strcpy(dev, ifr.ifr_name);
  return fd;
}


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe72(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11cf <tun_alloc+0x4f>
lea    0xe41(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <tun_alloc+0x13a>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1207 <tun_alloc+0x87>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1247 <tun_alloc+0xc7>
lea    0xde6(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <tun_alloc+0x13a>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    0x2d5d(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    12a8 <tun_alloc+0x128>
lea    0xd83(%rip),%rdi
call   1090 <perror@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <strcpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_alloc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1254 <tun_alloc+0xd4>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %ebp,0x8(%rsp)
cmpb   $0x0,(%r14)
je     11dc <tun_alloc+0x5c>
mov    0xc(%rsp),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1262 <tun_alloc+0xe2>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    0x2db3(%rip),%rcx
mov    0x2db4(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
or     %edx,0x8(%rsp)
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     127b <tun_alloc+0xfb>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xda5(%rip),%rdi
call   1090 <perror@plt>
jmp    1249 <tun_alloc+0xc9>
mov    %eax,%ebp
lea    0xdaa(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%ebx
jmp    1249 <tun_alloc+0xc9>
lea    0xda4(%rip),%rdi
call   1090 <perror@plt>
jmp    123d <tun_alloc+0xbd>

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

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