#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 */
scalar_t__ EBADFD;
int IFF_NO_PI;
int IFF_TUN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
char TUNSETIFF;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ ioctl(int, char, 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_open_old(char *);
int tun_open(char *dev) {
  struct ifreq ifr;
  int fd;
  if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
    return tun_open_old(dev);
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  if (*dev)
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
    if (errno == EBADFD) {
      if (ioctl(fd, (('T' << 8) | 202), (void *)&ifr) < 0)
        goto failed;
    } else
      goto failed;
  }
  strcpy(dev, ifr.ifr_name);
  return fd;
failed:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<tun_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11ae <tun_open+0x3e>
mov    -0x10(%rbp),%rdi
call   1080 <tun_open_old@plt>
mov    %eax,-0x4(%rbp)
jmp    1286 <tun_open+0x116>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    0x2dda(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f5 <tun_open+0x85>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%al
lea    -0x18(%rbp),%rdx
movsbl %al,%esi
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1260 <tun_open+0xf0>
mov    0x2da2(%rip),%rax
mov    (%rax),%rax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
jne    1256 <tun_open+0xe6>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rdx
mov    $0xffffffca,%esi
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1251 <tun_open+0xe1>
jmp    1277 <tun_open+0x107>
jmp    125b <tun_open+0xeb>
jmp    1277 <tun_open+0x107>
jmp    1260 <tun_open+0xf0>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <strcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1286 <tun_open+0x116>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_open>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     11f8 <tun_open+0x88>
mov    %eax,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e10(%rip),%rax
mov    0x2df9(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
cmpb   $0x0,(%r14)
je     11d1 <tun_open+0x61>
mov    0x4(%rsp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2df0(%rip),%rax
movsbl (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     1207 <tun_open+0x97>
mov    0x4(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    123d <tun_open+0xcd>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1080 <tun_open_old@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%rax
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%rax
jne    1231 <tun_open+0xc1>
mov    %rsp,%rdx
mov    %ebx,%edi
mov    $0xffffffca,%esi
call   1060 <ioctl@plt>
test   %rax,%rax
jns    11ea <tun_open+0x7a>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
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
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i8 0
@errno = global i64 0
@EBADFD = global i64 0
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* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %5
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @tun_open_old(i8* %10)
store i32 %11, i32* %2
br label %55
12: ; preds = %1
%13 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%14 = load i32, i32* @IFF_TUN
%15 = load i32, i32* @IFF_NO_PI
%16 = or i32 %14, %15
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i8*, i8** %3
%19 = load i8, i8* %18
%20 = icmp ne i8 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %12
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%23 = load i32, i32* %22
%24 = load i8*, i8** %3
%25 = load i32, i32* @IFNAMSIZ
%26 = call i32 @strncpy(i32 %23, i8* %24, i32 %25)
br label %27
27: ; preds = %21, %12
%28 = load i32, i32* %5
%29 = load i8, i8* @TUNSETIFF
%30 = bitcast %struct.ifreq* %4 to i8*
%31 = call i64 @ioctl(i32 %28, i8 signext %29, i8* %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %46
33: ; preds = %27
%34 = load i64, i64* @errno
%35 = load i64, i64* @EBADFD
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = load i32, i32* %5
%39 = bitcast %struct.ifreq* %4 to i8*
%40 = call i64 @ioctl(i32 %38, i8 signext -54, i8* %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %37
br label %52
43: ; preds = %37
br label %45
44: ; preds = %33
br label %52
45: ; preds = %43
br label %46
46: ; preds = %45, %27
%47 = load i8*, i8** %3
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%49 = load i32, i32* %48
%50 = call i32 @strcpy(i8* %47, i32 %49)
%51 = load i32, i32* %5
store i32 %51, i32* %2
br label %55
52: ; preds = %44, %42
%53 = load i32, i32* %5
%54 = call i32 @close(i32 %53)
store i32 -1, i32* %2
br label %55
55: ; preds = %52, %46, %9
%56 = load i32, i32* %2
ret i32 %56
}

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
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i8 0
@errno = global i64 0
@EBADFD = global i64 0
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 slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @tun_open_old(i8* %0)
br label %39
9: ; preds = %1
%10 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%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 = load i8, i8* %0
%16 = icmp eq i8 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %9
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%19 = load i32, i32* %18
%20 = load i32, i32* @IFNAMSIZ
%21 = call i32 @strncpy(i32 %19, i8* %0, i32 %20)
br label %22
22: ; preds = %17, %9
%23 = load i8, i8* @TUNSETIFF
%24 = call i64 @ioctl(i32 %5, i8 signext %23, i8* %3)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i64, i64* @errno
%28 = load i64, i64* @EBADFD
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %37
30: ; preds = %26
%31 = call i64 @ioctl(i32 %5, i8 signext -54, i8* %3)
%32 = icmp slt i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %30, %22
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%35 = load i32, i32* %34
%36 = call i32 @strcpy(i8* %0, i32 %35)
br label %39
37: ; preds = %26, %30
%38 = call i32 @close(i32 %5)
br label %39
39: ; preds = %37, %33, %7
%40 = phi i32 [ %8, %7 ], [ -1, %37 ], [ %5, %33 ]
ret i32 %40
}