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


x86 assembly (clang, -O0):
<tun_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1199 <tun_alloc+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <tun_alloc+0xbd>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11d7 <tun_alloc+0x77>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    120b <tun_alloc+0xab>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <tun_alloc+0xbd>
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    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e6 <tun_alloc+0x86>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
cmpb   $0x0,(%r14)
je     11bf <tun_alloc+0x5f>
mov    0x8(%rsp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     11ed <tun_alloc+0x8d>
mov    0x8(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    11f8 <tun_alloc+0x98>
mov    $0xffffffff,%ebx
jmp    11f8 <tun_alloc+0x98>
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

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

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