#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_TUN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ logmsg(int, char *, ...);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tunio_open(char *dev) {
  struct ifreq ifr;
  int fd, r;
  logmsg(1, "Opening device ...");
  fd = open("/dev/net/tun", O_RDWR);
  if (fd == -1) {
    logmsg(6, "Could not open tun device: %s", strerror(errno));
    return -1;
  }
  logmsg(1, "Setting up ifreq ...");
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  if (dev)
    if (*dev)
      strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  logmsg(1, "Setting options ...");
  r = ioctl(fd, TUNSETIFF, (void *)&ifr);
  if (r < 0) {
    close(fd);
    return -1;
  }
  if (dev)
    strcpy(dev, ifr.ifr_name);
  return fd;
}


x86 assembly (clang, -O0):
<tunio_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x1,%edi
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1080 <logmsg@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f0 <tunio_open+0x70>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%edx
mov    $0x6,%edi
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1080 <logmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <tunio_open+0x140>
mov    $0x1,%edi
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1080 <logmsg@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    0x2d8d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     125a <tunio_open+0xda>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1255 <tunio_open+0xd5>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
jmp    125a <tunio_open+0xda>
mov    $0x1,%edi
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1080 <logmsg@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12a3 <tunio_open+0x123>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <tunio_open+0x140>
cmpq   $0x0,-0x10(%rbp)
je     12ba <tunio_open+0x13a>
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):
<tunio_open>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe73(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <logmsg@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     124c <tunio_open+0xcc>
mov    %eax,%ebp
lea    0xe7e(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <logmsg@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2dde(%rip),%rax
mov    0x2dc7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
test   %rbx,%rbx
je     120f <tunio_open+0x8f>
cmpb   $0x0,(%rbx)
je     120f <tunio_open+0x8f>
mov    0x4(%rsp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
lea    0xe3d(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <logmsg@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1271 <tunio_open+0xf1>
test   %rbx,%rbx
je     127d <tunio_open+0xfd>
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
jmp    127d <tunio_open+0xfd>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdbf(%rip),%rsi
mov    $0x6,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <logmsg@plt>
jmp    1278 <tunio_open+0xf8>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [19 x i8] c"Opening device ...\00"
@.str.1 = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.2 = [30 x i8] c"Could not open tun device: %s\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"Setting up ifreq ...\00"
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@.str.4 = [20 x i8] c"Setting options ...\00"
@TUNSETIFF = global i32 0
define i32 @tunio_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i32 (i32, i8*, ...) @logmsg(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 (i32, i8*, ...) @logmsg(i32 6, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %14)
store i32 -1, i32* %2
br label %57
16: ; preds = %1
%17 = call i32 (i32, i8*, ...) @logmsg(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%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* %4, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i8*, i8** %3
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %36
25: ; preds = %16
%26 = load i8*, i8** %3
%27 = load i8, i8* %26
%28 = icmp ne i8 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load i8*, i8** %3
%33 = load i32, i32* @IFNAMSIZ
%34 = call i32 @strncpy(i32 %31, i8* %32, i32 %33)
br label %35
35: ; preds = %29, %25
br label %36
36: ; preds = %35, %16
%37 = call i32 (i32, i8*, ...) @logmsg(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32, i32* %5
%39 = load i32, i32* @TUNSETIFF
%40 = bitcast %struct.ifreq* %4 to i8*
%41 = call i32 @ioctl(i32 %38, i32 %39, i8* %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %36
%45 = load i32, i32* %5
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %2
br label %57
47: ; preds = %36
%48 = load i8*, i8** %3
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i8*, i8** %3
%52 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%53 = load i32, i32* %52
%54 = call i32 @strcpy(i8* %51, i32 %53)
br label %55
55: ; preds = %50, %47
%56 = load i32, i32* %5
store i32 %56, i32* %2
br label %57
57: ; preds = %55, %44, %12
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [19 x i8] c"Opening device ...\00"
@.str.1 = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.2 = [30 x i8] c"Could not open tun device: %s\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"Setting up ifreq ...\00"
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@.str.4 = [20 x i8] c"Setting options ...\00"
@TUNSETIFF = global i32 0
define i32 @tunio_open(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = tail call i32 (i32, i8*, ...) @logmsg(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, ...) @logmsg(i32 6, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %40
12: ; preds = %1
%13 = tail call i32 (i32, i8*, ...) @logmsg(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%14 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%15 = load i32, i32* @IFF_TUN
%16 = load i32, i32* @IFF_NO_PI
%17 = or i32 %16, %15
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
store i32 %17, i32* %18
%19 = icmp eq i8* %0, null
br i1 %19, label %28, label %20
20: ; preds = %12
%21 = load i8, i8* %0
%22 = icmp eq i8 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %20
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%25 = load i32, i32* %24
%26 = load i32, i32* @IFNAMSIZ
%27 = call i32 @strncpy(i32 %25, i8* %0, i32 %26)
br label %28
28: ; preds = %20, %23, %12
%29 = call i32 (i32, i8*, ...) @logmsg(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%30 = load i32, i32* @TUNSETIFF
%31 = call i32 @ioctl(i32 %6, i32 %30, i8* %3)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @close(i32 %6)
br label %40
35: ; preds = %28
br i1 %19, label %40, label %36
36: ; preds = %35
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%38 = load i32, i32* %37
%39 = call i32 @strcpy(i8* %0, i32 %38)
br label %40
40: ; preds = %35, %36, %33, %8
%41 = phi i32 [ -1, %8 ], [ -1, %33 ], [ %6, %36 ], [ %6, %35 ]
ret i32 %41
}