#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);
int tun_open_old() {
  char tunname[14];
  int i, fd;
  for (i = 0; i < 255; i++) {
    sprintf(tunname, "/dev/tun%d", i);
    if ((fd = open(tunname, O_RDWR)) > 0) {
      return fd;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<tun_open_old>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x18(%rbp)
cmpl   $0xff,-0x18(%rbp)
jge    118b <tun_open_old+0x6b>
lea    -0x12(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xeb6(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x12(%rbp),%rdi
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
call   1030 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1178 <tun_open_old+0x58>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1192 <tun_open_old+0x72>
jmp    117d <tun_open_old+0x5d>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112f <tun_open_old+0xf>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tun_open_old>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %ebx,%ebx
lea    0xecc(%rip),%r14
lea    0xa(%rsp),%r15
mov    0x2ea8(%rip),%r12
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <sprintf@plt>
mov    (%r12),%esi
mov    %r15,%rdi
call   1030 <open@plt>
test   %eax,%eax
jg     116d <tun_open_old+0x4d>
add    $0x1,%ebx
cmp    $0xff,%ebx
jne    1140 <tun_open_old+0x20>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/tun%d\00"
@O_RDWR = global i32 0
define i32 @tun_open_old() {
%1 = alloca i32
%2 = alloca [14 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %3
br label %5
5: ; preds = %19, %0
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 255
br i1 %7, label %8, label %22
8: ; preds = %5
%9 = getelementptr inbounds [14 x i8], [14 x i8]* %2, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [14 x i8], [14 x i8]* %2, i64 0, i64 0
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %4
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = load i32, i32* %4
store i32 %17, i32* %1
br label %23
18: ; preds = %8
br label %19
19: ; preds = %18
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %5
22: ; preds = %5
store i32 -1, i32* %1
br label %23
23: ; preds = %22, %16
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/tun%d\00"
@O_RDWR = global i32 0
define i32 @tun_open_old() {
%1 = alloca [14 x i8]
%2 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 0
br label %6
3: ; preds = %6
%4 = add nuw nsw i32 %7, 1
%5 = icmp eq i32 %4, 255
br i1 %5, label %12, label %6
6: ; preds = %0, %3
%7 = phi i32 [ 0, %0 ], [ %4, %3 ]
%8 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %2, i32 %9)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %3
12: ; preds = %3, %6
%13 = phi i32 [ %10, %6 ], [ -1, %3 ]
ret i32 %13
}