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

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


x86 assembly (clang, -O0):
<tun_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1188 <tun_open+0x38>
mov    $0x0,%al
call   1060 <tun_open_old@plt>
mov    %eax,-0x4(%rbp)
jmp    1232 <tun_open+0xe2>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    0x2e08(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%al
lea    -0x8(%rbp),%rdx
movsbl %al,%esi
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1218 <tun_open+0xc8>
mov    0x2df2(%rip),%rax
mov    (%rax),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    120e <tun_open+0xbe>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rdx
mov    $0xffffffca,%esi
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1209 <tun_open+0xb9>
jmp    1223 <tun_open+0xd3>
jmp    1213 <tun_open+0xc3>
jmp    1223 <tun_open+0xd3>
jmp    1218 <tun_open+0xc8>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1232 <tun_open+0xe2>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_open>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11f3 <tun_open+0xa3>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e34(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x2e2f(%rip),%rax
movsbl (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
jns    11e9 <tun_open+0x99>
mov    0x2e0e(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jne    11dd <tun_open+0x8d>
mov    %rsp,%rdx
mov    %ebx,%edi
mov    $0xffffffca,%esi
call   1040 <ioctl@plt>
test   %rax,%rax
jns    11e9 <tun_open+0x99>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <tun_open_old@plt>

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

LLVM IR (-O2):
%struct.ifreq = type { 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
@TUNSETIFF = global i8 0
@errno = global i64 0
@EBADFD = global i64 0
define i32 @tun_open() {
%1 = alloca %struct.ifreq
%2 = bitcast %struct.ifreq* %1 to i8*
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 (...) @tun_open_old()
br label %26
8: ; preds = %0
%9 = call i32 @memset(%struct.ifreq* %1, i32 0, i32 4)
%10 = load i32, i32* @IFF_TUN
%11 = load i32, i32* @IFF_NO_PI
%12 = or i32 %11, %10
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i8, i8* @TUNSETIFF
%15 = call i64 @ioctl(i32 %4, i8 signext %14, i8* %2)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %8
%18 = load i64, i64* @errno
%19 = load i64, i64* @EBADFD
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i64 @ioctl(i32 %4, i8 signext -54, i8* %2)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17, %21
%25 = call i32 @close(i32 %4)
br label %26
26: ; preds = %8, %21, %24, %6
%27 = phi i32 [ %7, %6 ], [ -1, %24 ], [ %4, %21 ], [ %4, %8 ]
ret i32 %27
}