#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;
  char *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 ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
int tun_open(char name[IFNAMSIZ]) {
  struct ifreq ifr;
  int fd;
  if ((fd = open("/dev/net/tun", O_RDWR)) == -1) {
    perror("open");
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  strncpy(ifr.ifr_name, name, IFNAMSIZ);
  if (ioctl(fd, TUNSETIFF, (void *)&ifr) == -1) {
    perror("ioctl");
    close(fd);
    return -1;
  }
  strncpy(name, ifr.ifr_name, IFNAMSIZ);
  return fd;
}


x86 assembly (clang, -O0):
<tun_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11a5 <tun_open+0x45>
lea    0xe79(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <tun_open+0xda>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    0x2df3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    121e <tun_open+0xbe>
lea    0xe0d(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <tun_open+0xda>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_open>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1203 <tun_open+0xa3>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e23(%rip),%rax
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x10(%rsp),%rdi
mov    0x2e18(%rip),%r12
mov    (%r12),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1216 <tun_open+0xb6>
mov    0x10(%rsp),%rsi
mov    (%r12),%edx
mov    %r14,%rdi
call   1030 <strncpy@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe03(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11f5 <tun_open+0x95>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    11f5 <tun_open+0x95>

LLVM IR (-O0):
%struct.ifreq = type { i32, i8* }
@IFNAMSIZ = global i32 0
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
define i32 @tun_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @IFNAMSIZ
%7 = zext i32 %6 to i64
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %40
13: ; preds = %1
%14 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 16)
%15 = load i32, i32* @IFF_TUN
%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 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%20 = load i8*, i8** %19
%21 = load i8*, i8** %3
%22 = load i32, i32* @IFNAMSIZ
%23 = call i32 @strncpy(i8* %20, i8* %21, i32 %22)
%24 = load i32, i32* %5
%25 = load i32, i32* @TUNSETIFF
%26 = bitcast %struct.ifreq* %4 to i8*
%27 = call i32 @ioctl(i32 %24, i32 %25, i8* %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %13
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %2
br label %40
33: ; preds = %13
%34 = load i8*, i8** %3
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%36 = load i8*, i8** %35
%37 = load i32, i32* @IFNAMSIZ
%38 = call i32 @strncpy(i8* %34, i8* %36, i32 %37)
%39 = load i32, i32* %5
store i32 %39, i32* %2
br label %40
40: ; preds = %33, %29, %11
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i8* }
@IFNAMSIZ = global i32 0
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
define i32 @tun_open(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 eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %29
9: ; preds = %1
%10 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 16)
%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* %2, i64 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @IFNAMSIZ
%18 = call i32 @strncpy(i8* %16, i8* %0, i32 %17)
%19 = load i32, i32* @TUNSETIFF
%20 = call i32 @ioctl(i32 %5, i32 %19, i8* %3)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %9
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @close(i32 %5)
br label %29
25: ; preds = %9
%26 = load i8*, i8** %15
%27 = load i32, i32* @IFNAMSIZ
%28 = call i32 @strncpy(i8* %0, i8* %26, i32 %27)
br label %29
29: ; preds = %25, %22, %7
%30 = phi i32 [ -1, %7 ], [ -1, %22 ], [ %5, %25 ]
ret i32 %30
}