#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*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int tun_open(char *dev) {
  char tunname[14];
  int i, fd, err;
  if (*dev) {
    sprintf(tunname, "/dev/%s", dev);
    return open(tunname, O_RDWR);
  }
  sprintf(tunname, "/dev/tun");
  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;
  perror("no /dev/tun* available\n");
  return -1;
}


x86 assembly (clang, -O0):
<tun_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1189 <tun_open+0x49>
lea    -0x1e(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe98(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x1e(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x4(%rbp)
jmp    1287 <tun_open+0x147>
lea    -0x1e(%rbp),%rdi
lea    0xe74(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0xff,-0x24(%rbp)
jge    125e <tun_open+0x11e>
lea    -0x1e(%rbp),%rdi
add    $0x8,%rdi
mov    -0x24(%rbp),%edx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x1e(%rbp),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jle    1209 <tun_open+0xc9>
mov    -0x10(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
add    $0x5,%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1287 <tun_open+0x147>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%eax
je     1232 <tun_open+0xf2>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1246 <tun_open+0x106>
cmpl   $0x0,-0x24(%rbp)
je     1241 <tun_open+0x101>
jmp    125e <tun_open+0x11e>
jmp    1246 <tun_open+0x106>
jmp    124b <tun_open+0x10b>
jmp    1250 <tun_open+0x110>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a9 <tun_open+0x69>
cmpl   $0x0,-0x2c(%rbp)
je     1274 <tun_open+0x134>
mov    -0x2c(%rbp),%ecx
mov    0x2d66(%rip),%rax
mov    %ecx,(%rax)
lea    0xd99(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rdx
cmpb   $0x0,(%rdi)
je     1182 <tun_open+0x42>
lea    0xea3(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%ebx
jmp    123a <tun_open+0xfa>
mov    %rdx,0x10(%rsp)
movabs $0x6e75742f7665642f,%rax
74 75 6e
mov    %rax,(%rsp)
movb   $0x0,0x8(%rsp)
mov    0x2e47(%rip),%r14
mov    %rsp,%r13
mov    0x2e1d(%rip),%r15
xor    %r12d,%r12d
xor    %ebp,%ebp
jmp    11ce <tun_open+0x8e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%ebp
mov    %eax,%r12d
cmp    $0xff,%ebp
je     1207 <tun_open+0xc7>
lea    0x8(%rsp),%rdi
lea    0xe37(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    (%r14),%esi
mov    %r13,%rdi
call   1040 <open@plt>
test   %eax,%eax
jg     1229 <tun_open+0xe9>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
cmp    (%r15),%eax
jne    11c0 <tun_open+0x80>
mov    %r12d,%eax
test   %ebp,%ebp
je     11c0 <tun_open+0x80>
test   %r12d,%r12d
je     1216 <tun_open+0xd6>
mov    0x2dc5(%rip),%rax
mov    %r12d,(%rax)
lea    0xdf7(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    123a <tun_open+0xfa>
mov    %eax,%ebx
lea    0x5(%rsp),%rsi
mov    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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