#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 {
  char *ifr_name;
  int /*<<< orphan*/ ifr_flags;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ 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;
  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    1231 <tun_open+0xd1>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1215 <tun_open+0xb5>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <tun_open+0xd1>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2db4(%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     11fa <tun_open+0x9a>
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    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
mov    0x2e21(%rip),%r12
mov    (%r12),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     120d <tun_open+0xad>
mov    0x8(%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    0xe0c(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11ec <tun_open+0x8c>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    11ec <tun_open+0x8c>

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
@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
@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 %38
13: ; preds = %1
%14 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 16)
%15 = load i32, i32* @IFF_TUN
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = load i8*, i8** %3
%20 = load i32, i32* @IFNAMSIZ
%21 = call i32 @strncpy(i8* %18, i8* %19, i32 %20)
%22 = load i32, i32* %5
%23 = load i32, i32* @TUNSETIFF
%24 = bitcast %struct.ifreq* %4 to i8*
%25 = call i32 @ioctl(i32 %22, i32 %23, i8* %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %13
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %2
br label %38
31: ; preds = %13
%32 = load i8*, i8** %3
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = load i32, i32* @IFNAMSIZ
%36 = call i32 @strncpy(i8* %32, i8* %34, i32 %35)
%37 = load i32, i32* %5
store i32 %37, i32* %2
br label %38
38: ; preds = %31, %27, %11
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
@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
@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 %27
9: ; preds = %1
%10 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 16)
%11 = load i32, i32* @IFF_TUN
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = load i32, i32* @IFNAMSIZ
%16 = call i32 @strncpy(i8* %14, i8* %0, i32 %15)
%17 = load i32, i32* @TUNSETIFF
%18 = call i32 @ioctl(i32 %5, i32 %17, i8* %3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %9
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @close(i32 %5)
br label %27
23: ; preds = %9
%24 = load i8*, i8** %13
%25 = load i32, i32* @IFNAMSIZ
%26 = call i32 @strncpy(i8* %0, i8* %24, i32 %25)
br label %27
27: ; preds = %23, %20, %7
%28 = phi i32 [ -1, %7 ], [ -1, %20 ], [ %5, %23 ]
ret i32 %28
}