#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 /*<<< orphan*/ O_RDWR;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int tun_open(char *dev) {
  char tunname[14];
  int i, fd;
  if (*dev) {
    sprintf(tunname, "/dev/%s", dev);
    return open(tunname, O_RDWR);
  }
  for (i = 0; i < 255; i++) {
    sprintf(tunname, "/dev/tun%d", i);
    if ((fd = open(tunname, O_RDWR)) > 0) {
      sprintf(dev, "tun%d", i);
      return fd;
    }
  }
  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     1169 <tun_open+0x49>
lea    -0x1e(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xeb8(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
lea    -0x1e(%rbp),%rdi
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
call   1030 <open@plt>
mov    %eax,-0x4(%rbp)
jmp    11ea <tun_open+0xca>
movl   $0x0,-0x24(%rbp)
cmpl   $0xff,-0x24(%rbp)
jge    11e3 <tun_open+0xc3>
lea    -0x1e(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe7d(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
lea    -0x1e(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1030 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jle    11d0 <tun_open+0xb0>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ea <tun_open+0xca>
jmp    11d5 <tun_open+0xb5>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1170 <tun_open+0x50>
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,%r14
cmpb   $0x0,(%rdi)
je     1164 <tun_open+0x44>
lea    0xec3(%rip),%rsi
lea    0xa(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <open@plt>
mov    %eax,%ebx
jmp    11c6 <tun_open+0xa6>
lea    0xe9d(%rip),%r15
lea    0xa(%rsp),%r12
mov    0x2e71(%rip),%r13
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   1030 <open@plt>
test   %eax,%eax
jg     11b1 <tun_open+0x91>
add    $0x1,%ebp
cmp    $0xff,%ebp
jne    1180 <tun_open+0x60>
mov    $0xffffffff,%ebx
jmp    11c6 <tun_open+0xa6>
mov    %eax,%ebx
lea    0xe53(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <sprintf@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 = [11 x i8] c"/dev/tun%d\00"
@.str.2 = [6 x i8] c"tun%d\00"
define i32 @tun_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [14 x i8]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = icmp ne i8 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %1
%11 = getelementptr inbounds [14 x i8], [14 x i8]* %4, i64 0, i64 0
%12 = load i8*, i8** %3
%13 = call i32 (i8*, i8*, ...) @sprintf(i8* %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [14 x i8], [14 x i8]* %4, i64 0, i64 0
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %2
br label %39
17: ; preds = %1
store i32 0, i32* %5
br label %18
18: ; preds = %35, %17
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 255
br i1 %20, label %21, label %38
21: ; preds = %18
%22 = getelementptr inbounds [14 x i8], [14 x i8]* %4, i64 0, i64 0
%23 = load i32, i32* %5
%24 = call i32 (i8*, i8*, ...) @sprintf(i8* %22, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [14 x i8], [14 x i8]* %4, i64 0, i64 0
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %6
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %21
%30 = load i8*, i8** %3
%31 = load i32, i32* %5
%32 = call i32 (i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %6
store i32 %33, i32* %2
br label %39
34: ; preds = %21
br label %35
35: ; preds = %34
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %18
38: ; preds = %18
store i32 -1, i32* %2
br label %39
39: ; preds = %38, %29, %10
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@.str = [8 x i8] c"/dev/%s\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"/dev/tun%d\00"
@.str.2 = [6 x i8] c"tun%d\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 %21
10: ; preds = %1, %18
%11 = phi i32 [ %19, %18 ], [ 0, %1 ]
%12 = call i32 (i8*, i8*, ...) @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* %3, i32 %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %21
18: ; preds = %10
%19 = add nuw nsw i32 %11, 1
%20 = icmp eq i32 %19, 255
br i1 %20, label %21, label %10
21: ; preds = %18, %16, %6
%22 = phi i32 [ %9, %6 ], [ %14, %16 ], [ -1, %18 ]
ret i32 %22
}