#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 */

/* Variables and functions */
int ENOENT;
int /*<<< orphan*/ O_RDWR;
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int tun_open_common0(char *dev, int istun) {
  char tunname[14];
  int i, fd, err;
  if (*dev) {
    sprintf(tunname, "/dev/%s", dev);
    return open(tunname, O_RDWR);
  }
  sprintf(tunname, "/dev/%s", istun ? "tun" : "tap");
  err = 0;
  for (i = 0; i < 255; i++) {
    sprintf(tunname + 8, "%d", i);
    if ((fd = open(tunname, O_RDWR)) > 0) {
      strcpy(dev, tunname + 5);
      return fd;
    } else if (errno != ENOENT)
      err = errno;
    else if (i)
      break;
  }
  if (err)
    errno = err;
  return -1;
}


x86 assembly (clang, -O0):
<tun_open_common0>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     117c <tun_open_common0+0x4c>
lea    -0x22(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xea5(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x22(%rbp),%rdi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x4(%rbp)
jmp    1286 <tun_open_common0+0x156>
lea    -0x22(%rbp),%rdi
mov    -0x14(%rbp),%ecx
lea    0xe82(%rip),%rdx
lea    0xe77(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
lea    0xe61(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0xff,-0x28(%rbp)
jge    1269 <tun_open_common0+0x139>
lea    -0x22(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%edx
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x22(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    1214 <tun_open_common0+0xe4>
mov    -0x10(%rbp),%rdi
lea    -0x22(%rbp),%rsi
add    $0x5,%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1286 <tun_open_common0+0x156>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%eax
je     123d <tun_open_common0+0x10d>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    1251 <tun_open_common0+0x121>
cmpl   $0x0,-0x28(%rbp)
je     124c <tun_open_common0+0x11c>
jmp    1269 <tun_open_common0+0x139>
jmp    1251 <tun_open_common0+0x121>
jmp    1256 <tun_open_common0+0x126>
jmp    125b <tun_open_common0+0x12b>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11b4 <tun_open_common0+0x84>
cmpl   $0x0,-0x30(%rbp)
je     127f <tun_open_common0+0x14f>
mov    -0x30(%rbp),%ecx
mov    0x2d5b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_open_common0>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rdx
cmpb   $0x0,(%rdi)
je     1180 <tun_open_common0+0x50>
lea    0xeb3(%rip),%rsi
lea    0x2(%rsp),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rdx,0x10(%rsp)
test   %esi,%esi
lea    0xe7e(%rip),%rax
lea    0xe73(%rip),%rdx
cmove  %rax,%rdx
lea    0xe60(%rip),%rsi
lea    0x2(%rsp),%rdi
xor    %eax,%eax
call   1050 <sprintf@plt>
lea    0xe5d(%rip),%r13
mov    0x2e2e(%rip),%r14
mov    0x2e07(%rip),%r15
xor    %r12d,%r12d
xor    %ebp,%ebp
jmp    11de <tun_open_common0+0xae>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %eax,%r12d
cmp    $0xff,%ebp
je     1215 <tun_open_common0+0xe5>
lea    0xa(%rsp),%rdi
mov    %r13,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    (%r14),%esi
lea    0x2(%rsp),%rdi
call   1040 <open@plt>
test   %eax,%eax
jg     1232 <tun_open_common0+0x102>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
cmp    (%r15),%eax
jne    11d0 <tun_open_common0+0xa0>
mov    %r12d,%eax
test   %ebp,%ebp
je     11d0 <tun_open_common0+0xa0>
mov    $0xffffffff,%ebx
test   %r12d,%r12d
je     116f <tun_open_common0+0x3f>
mov    0x2dae(%rip),%rax
mov    %r12d,(%rax)
jmp    116f <tun_open_common0+0x3f>
mov    %eax,%ebx
lea    0x7(%rsp),%rsi
mov    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
jmp    116f <tun_open_common0+0x3f>

LLVM IR (-O0):
@.str = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@.str.1 = [4 x i8] c"tun\00"
@.str.2 = [4 x i8] c"tap\00"
@.str.3 = [3 x i8] c"%d\00"
@errno = global i32 0
@ENOENT = global i32 0
define i32 @tun_open_common0(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [14 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%15 = load i8*, i8** %4
%16 = call i32 (i8*, i8*, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %3
br label %67
20: ; preds = %2
%21 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 0
%24 = zext i1 %23 to i64
%25 = select i1 %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0)
%26 = call i32 (i8*, i8*, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %25)
store i32 0, i32* %9
store i32 0, i32* %7
br label %27
27: ; preds = %58, %20
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 255
br i1 %29, label %30, label %61
30: ; preds = %27
%31 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%32 = getelementptr inbounds i8, i8* %31, i64 8
%33 = load i32, i32* %7
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%36 = load i32, i32* @O_RDWR
%37 = call i32 @open(i8* %35, i32 %36)
store i32 %37, i32* %8
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %30
%40 = load i8*, i8** %4
%41 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%42 = getelementptr inbounds i8, i8* %41, i64 5
%43 = call i32 @strcpy(i8* %40, i8* %42)
%44 = load i32, i32* %8
store i32 %44, i32* %3
br label %67
45: ; preds = %30
%46 = load i32, i32* @errno
%47 = load i32, i32* @ENOENT
%48 = icmp ne i32 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = load i32, i32* @errno
store i32 %50, i32* %9
br label %56
51: ; preds = %45
%52 = load i32, i32* %7
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %51
br label %61
55: ; preds = %51
br label %56
56: ; preds = %55, %49
br label %57
57: ; preds = %56
br label %58
58: ; preds = %57
%59 = load i32, i32* %7
%60 = add nsw i32 %59, 1
store i32 %60, i32* %7
br label %27
61: ; preds = %54, %27
%62 = load i32, i32* %9
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = load i32, i32* %9
store i32 %65, i32* @errno
br label %66
66: ; preds = %64, %61
store i32 -1, i32* %3
br label %67
67: ; preds = %66, %39, %13
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@.str.1 = [4 x i8] c"tun\00"
@.str.2 = [4 x i8] c"tap\00"
@.str.3 = [3 x i8] c"%d\00"
@errno = global i32 0
@ENOENT = global i32 0
define i32 @tun_open_common0(i8* %0, i32 %1) {
%3 = alloca [14 x i8]
%4 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %4, i32 %9)
br label %40
11: ; preds = %2
%12 = icmp eq i32 %1, 0
%13 = select i1 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%14 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 8
br label %16
16: ; preds = %11, %32
%17 = phi i32 [ 0, %11 ], [ %33, %32 ]
%18 = phi i32 [ 0, %11 ], [ %34, %32 ]
%19 = call i32 (i8*, i8*, ...) @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %4, i32 %20)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 5
%25 = call i32 @strcpy(i8* %0, i8* %24)
br label %40
26: ; preds = %16
%27 = load i32, i32* @errno
%28 = load i32, i32* @ENOENT
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = icmp eq i32 %18, 0
br i1 %31, label %32, label %36
32: ; preds = %26, %30
%33 = phi i32 [ %17, %30 ], [ %27, %26 ]
%34 = add nuw nsw i32 %18, 1
%35 = icmp eq i32 %34, 255
br i1 %35, label %36, label %16
36: ; preds = %30, %32
%37 = phi i32 [ %17, %30 ], [ %33, %32 ]
%38 = icmp eq i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %36
store i32 %37, i32* @errno
br label %40
40: ; preds = %36, %39, %23, %7
%41 = phi i32 [ %10, %7 ], [ %21, %23 ], [ -1, %39 ], [ -1, %36 ]
ret i32 %41
}