#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 */
typedef int /*<<< orphan*/ device;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
int open_video_device(char *dev_name_prefix) {
  int video_fd, i;
  char device[16];
  for (i = 0; i < 100; i++) {
    snprintf(device, sizeof(device), "%s%d", dev_name_prefix, i);
    video_fd = open(device, O_RDONLY);
    if (video_fd != -1)
      return video_fd;
  }
  return -1;
}


x86 assembly (clang, -O0):
<open_video_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x64,-0x18(%rbp)
jge    1197 <open_video_device+0x77>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8d
mov    $0x10,%esi
lea    0xeab(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1184 <open_video_device+0x64>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119e <open_video_device+0x7e>
jmp    1189 <open_video_device+0x69>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1133 <open_video_device+0x13>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_video_device>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
xor    %ebx,%ebx
lea    0xec7(%rip),%r14
mov    %rsp,%r12
mov    0x2ea5(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %ebx,%r8d
call   1030 <snprintf@plt>
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
jne    1181 <open_video_device+0x61>
lea    0x1(%rbx),%ecx
cmp    $0x63,%ebx
mov    %ecx,%ebx
jne    1150 <open_video_device+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%d\00"
@O_RDONLY = global i32 0
define i32 @open_video_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [16 x i8]
store i8* %0, i8** %3
store i32 0, i32* %5
br label %7
7: ; preds = %23, %1
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 100
br i1 %9, label %10, label %26
10: ; preds = %7
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%12 = load i8*, i8** %3
%13 = load i32, i32* %5
%14 = call i32 @snprintf(i8* %11, i32 16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %13)
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %27
22: ; preds = %10
br label %23
23: ; preds = %22
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %7
26: ; preds = %7
store i32 -1, i32* %2
br label %27
27: ; preds = %26, %20
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%d\00"
@O_RDONLY = global i32 0
define i32 @open_video_device(i8* %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %4, %1
%5 = phi i32 [ 0, %1 ], [ %10, %4 ]
%6 = call i32 @snprintf(i8* %3, i32 16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %5)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %3, i32 %7)
%9 = icmp ne i32 %8, -1
%10 = add nuw nsw i32 %5, 1
%11 = icmp eq i32 %10, 100
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %13, label %4
13: ; preds = %4
ret i32 %8
}