#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*/ printf(char *, char *);
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);
    printf("Opening /dev/%s\n", tunname);
    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 -2;
}


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     119c <tun_open_common0+0x5c>
lea    -0x22(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe95(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x22(%rbp),%rsi
lea    0xe8b(%rip),%rdi
call   1040 <printf@plt>
lea    -0x22(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x4(%rbp)
jmp    12a6 <tun_open_common0+0x166>
lea    -0x22(%rbp),%rdi
mov    -0x14(%rbp),%ecx
lea    0xe73(%rip),%rdx
lea    0xe68(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0xff,-0x28(%rbp)
jge    1289 <tun_open_common0+0x149>
lea    -0x22(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%edx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x22(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    1234 <tun_open_common0+0xf4>
mov    -0x10(%rbp),%rdi
lea    -0x22(%rbp),%rsi
add    $0x5,%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a6 <tun_open_common0+0x166>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%eax
je     125d <tun_open_common0+0x11d>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    1271 <tun_open_common0+0x131>
cmpl   $0x0,-0x28(%rbp)
je     126c <tun_open_common0+0x12c>
jmp    1289 <tun_open_common0+0x149>
jmp    1271 <tun_open_common0+0x131>
jmp    1276 <tun_open_common0+0x136>
jmp    127b <tun_open_common0+0x13b>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11d4 <tun_open_common0+0x94>
cmpl   $0x0,-0x30(%rbp)
je     129f <tun_open_common0+0x15f>
mov    -0x30(%rbp),%ecx
mov    0x2d3b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xfffffffe,-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     119f <tun_open_common0+0x5f>
lea    0xea3(%rip),%rsi
lea    0x2(%rsp),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0xe95(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <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    0xe70(%rip),%rax
lea    0xe65(%rip),%rdx
cmove  %rax,%rdx
lea    0xe41(%rip),%rsi
lea    0x2(%rsp),%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0xe4f(%rip),%r13
mov    0x2e0f(%rip),%r14
mov    0x2de8(%rip),%r15
xor    %r12d,%r12d
xor    %ebp,%ebp
jmp    11fe <tun_open_common0+0xbe>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %eax,%r12d
cmp    $0xff,%ebp
je     1235 <tun_open_common0+0xf5>
lea    0xa(%rsp),%rdi
mov    %r13,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    (%r14),%esi
lea    0x2(%rsp),%rdi
call   1050 <open@plt>
test   %eax,%eax
jg     1252 <tun_open_common0+0x112>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
cmp    (%r15),%eax
jne    11f0 <tun_open_common0+0xb0>
mov    %r12d,%eax
test   %ebp,%ebp
je     11f0 <tun_open_common0+0xb0>
mov    $0xfffffffe,%ebx
test   %r12d,%r12d
je     118e <tun_open_common0+0x4e>
mov    0x2d8e(%rip),%rax
mov    %r12d,(%rax)
jmp    118e <tun_open_common0+0x4e>
mov    %eax,%ebx
lea    0x7(%rsp),%rsi
mov    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
jmp    118e <tun_open_common0+0x4e>

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

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