#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 /*<<< orphan*/ 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*/ 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) {
    perror("open(tun):");
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN;
  if (*dev)
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
    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    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11b5 <tun_alloc+0x45>
lea    0xe69(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <tun_alloc+0xc9>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f3 <tun_alloc+0x83>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1227 <tun_alloc+0xb7>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1239 <tun_alloc+0xc9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%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    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     120c <tun_alloc+0x9c>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
cmpb   $0x0,(%r14)
je     11cf <tun_alloc+0x5f>
mov    0x8(%rsp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     11f6 <tun_alloc+0x86>
mov    0x8(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    1201 <tun_alloc+0x91>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1201 <tun_alloc+0x91>

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

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