#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_TAP;
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_common0(char *, int);
int tun_open_common(char *dev, int istun) {
  struct ifreq ifr;
  int fd;
  if ((fd = open("/dev/tun", O_RDWR)) < 0)
    return tun_open_common0(dev, istun);
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = (istun ? IFF_TUN : IFF_TAP) | 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_common>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11b4 <tun_open_common+0x44>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <tun_open_common0@plt>
mov    %eax,-0x4(%rbp)
jmp    12ad <tun_open_common+0x13d>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
cmpl   $0x0,-0x14(%rbp)
je     11df <tun_open_common+0x6f>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11eb <tun_open_common+0x7b>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2dab(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     121c <tun_open_common+0xac>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%al
lea    -0x20(%rbp),%rdx
movsbl %al,%esi
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1287 <tun_open_common+0x117>
mov    0x2d73(%rip),%rax
mov    (%rax),%rax
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%rax
jne    127d <tun_open_common+0x10d>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rdx
mov    $0xffffffca,%esi
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1278 <tun_open_common+0x108>
jmp    129e <tun_open_common+0x12e>
jmp    1282 <tun_open_common+0x112>
jmp    129e <tun_open_common+0x12e>
jmp    1287 <tun_open_common+0x117>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <strcpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ad <tun_open_common+0x13d>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_open_common>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11b6 <tun_open_common+0x46>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
test   %ebp,%ebp
jne    11c8 <tun_open_common+0x58>
mov    0x2e1c(%rip),%rax
jmp    11cf <tun_open_common+0x5f>
mov    %r14,%rdi
mov    %ebp,%esi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1090 <tun_open_common0@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    0x2dc8(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
cmpb   $0x0,(%r14)
je     11f9 <tun_open_common+0x89>
mov    0xc(%rsp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2dc0(%rip),%rax
movsbl (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     1222 <tun_open_common+0xb2>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1040 <strcpy@plt>
jmp    125a <tun_open_common+0xea>
mov    0x2d8f(%rip),%rax
mov    (%rax),%rax
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%rax
jne    124e <tun_open_common+0xde>
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
mov    $0xffffffca,%esi
call   1060 <ioctl@plt>
test   %rax,%rax
jns    1214 <tun_open_common+0xa4>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [9 x i8] c"/dev/tun\00"
@O_RDWR = global i32 0
@IFF_TUN = global i32 0
@IFF_TAP = 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_common(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %7
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i32, i32* %5
%14 = call i32 @tun_open_common0(i8* %12, i32 %13)
store i32 %14, i32* %3
br label %65
15: ; preds = %2
%16 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32, i32* @IFF_TUN
br label %23
21: ; preds = %15
%22 = load i32, i32* @IFF_TAP
br label %23
23: ; preds = %21, %19
%24 = phi i32 [ %20, %19 ], [ %22, %21 ]
%25 = load i32, i32* @IFF_NO_PI
%26 = or i32 %24, %25
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i8*, i8** %4
%29 = load i8, i8* %28
%30 = icmp ne i8 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %23
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load i8*, i8** %4
%35 = load i32, i32* @IFNAMSIZ
%36 = call i32 @strncpy(i32 %33, i8* %34, i32 %35)
br label %37
37: ; preds = %31, %23
%38 = load i32, i32* %7
%39 = load i8, i8* @TUNSETIFF
%40 = bitcast %struct.ifreq* %6 to i8*
%41 = call i64 @ioctl(i32 %38, i8 signext %39, i8* %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %56
43: ; preds = %37
%44 = load i64, i64* @errno
%45 = load i64, i64* @EBADFD
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %54
47: ; preds = %43
%48 = load i32, i32* %7
%49 = bitcast %struct.ifreq* %6 to i8*
%50 = call i64 @ioctl(i32 %48, i8 signext -54, i8* %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
br label %62
53: ; preds = %47
br label %55
54: ; preds = %43
br label %62
55: ; preds = %53
br label %56
56: ; preds = %55, %37
%57 = load i8*, i8** %4
%58 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%59 = load i32, i32* %58
%60 = call i32 @strcpy(i8* %57, i32 %59)
%61 = load i32, i32* %7
store i32 %61, i32* %3
br label %65
62: ; preds = %54, %52
%63 = load i32, i32* %7
%64 = call i32 @close(i32 %63)
store i32 -1, i32* %3
br label %65
65: ; preds = %62, %56, %11
%66 = load i32, i32* %3
ret i32 %66
}

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