#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 IFF_NO_PI;
int IFF_TAP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tun_alloc(char *dev) {
  struct ifreq ifr;
  int fd, err;
  if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
    fprintf(stderr, "Cannot open TUN/TAP dev\n"
                    "Make sure one exists with "
                    "'$ mknod /dev/net/tap c 10 200'\n");
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  if (*dev)
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if ((err = ioctl(fd, TUNSETIFF, &ifr)) < 0) {
    perror("ERR: Could not ioctl tun");
    close(fd);
    return err;
  }
  strcpy(dev, ifr.ifr_name);
  return fd;
}


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11ce <tun_alloc+0x4e>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <tun_alloc+0xe7>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    0x2dc2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1215 <tun_alloc+0x95>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dba(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1255 <tun_alloc+0xd5>
lea    0xe23(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1267 <tun_alloc+0xe7>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <strcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_alloc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     120f <tun_alloc+0x8f>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e1a(%rip),%rax
mov    0x2deb(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
cmpb   $0x0,(%r14)
je     11e8 <tun_alloc+0x68>
mov    0xc(%rsp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1234 <tun_alloc+0xb4>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    1229 <tun_alloc+0xa9>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%ebp
lea    0xe23(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%ebx
jmp    1229 <tun_alloc+0xa9>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [83 x i8] c"Cannot open TUN/TAP dev\0AMake sure one exists with '$ mknod /dev/net/tap c 10 200'\0A\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [25 x i8] c"ERR: Could not ioctl tun\00"
define i32 @tun_alloc(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %44
13: ; preds = %1
%14 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%15 = load i32, i32* @IFF_TAP
%16 = load i32, i32* @IFF_NO_PI
%17 = or i32 %15, %16
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %13
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i8*, i8** %3
%26 = load i32, i32* @IFNAMSIZ
%27 = call i32 @strncpy(i32 %24, i8* %25, i32 %26)
br label %28
28: ; preds = %22, %13
%29 = load i32, i32* %5
%30 = load i32, i32* @TUNSETIFF
%31 = call i32 @ioctl(i32 %29, i32 %30, %struct.ifreq* %4)
store i32 %31, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %6
store i32 %37, i32* %2
br label %44
38: ; preds = %28
%39 = load i8*, i8** %3
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%41 = load i32, i32* %40
%42 = call i32 @strcpy(i8* %39, i32 %41)
%43 = load i32, i32* %5
store i32 %43, i32* %2
br label %44
44: ; preds = %38, %33, %10
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [83 x i8] c"Cannot open TUN/TAP dev\0AMake sure one exists with '$ mknod /dev/net/tap c 10 200'\0A\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [25 x i8] c"ERR: Could not ioctl tun\00"
define i32 @tun_alloc(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.1, i64 0, i64 0))
br label %34
10: ; preds = %1
%11 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%12 = load i32, i32* @IFF_TAP
%13 = load i32, i32* @IFF_NO_PI
%14 = or i32 %13, %12
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
store i32 %14, i32* %15
%16 = load i8, i8* %0
%17 = icmp eq i8 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %10
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%20 = load i32, i32* %19
%21 = load i32, i32* @IFNAMSIZ
%22 = call i32 @strncpy(i32 %20, i8* %0, i32 %21)
br label %23
23: ; preds = %18, %10
%24 = load i32, i32* @TUNSETIFF
%25 = call i32 @ioctl(i32 %5, i32 %24, %struct.ifreq* %2)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @close(i32 %5)
br label %34
30: ; preds = %23
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @strcpy(i8* %0, i32 %32)
br label %34
34: ; preds = %30, %27, %7
%35 = phi i32 [ -1, %7 ], [ %25, %27 ], [ %5, %30 ]
ret i32 %35
}