#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 open(char const *, int);
int parse_device(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int open_videodevice(const char *devicename, int mode) {
  int fd = open(devicename, mode);
  if (fd < 0) {
    int devnr = parse_device(devicename);
    if (devnr >= 0) {
      char devname[100];
      snprintf(devname, 99, "/dev/video%d", devnr);
      devname[99] = 0;
      fd = open(devname, mode);
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_videodevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11a1 <open_videodevice+0x71>
mov    -0x8(%rbp),%rdi
call   1040 <parse_device@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     119c <open_videodevice+0x6c>
lea    -0x80(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x63,%esi
lea    0xe7c(%rip),%rdx
call   1030 <snprintf@plt>
movb   $0x0,-0x1d(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
jmp    11a1 <open_videodevice+0x71>
mov    -0x10(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_videodevice>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1180 <open_videodevice+0x50>
mov    %rbx,%rdi
call   1040 <parse_device@plt>
test   %eax,%eax
js     1180 <open_videodevice+0x50>
lea    0xea4(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x63,%esi
mov    %eax,%ecx
call   1030 <snprintf@plt>
movb   $0x0,0x63(%rsp)
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <open@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/video%d\00"
define i32 @open_videodevice(i8* %0, i32 %1) {
%3 = alloca [100 x i8]
%4 = tail call i32 @open(i8* %0, i32 %1)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %2
%7 = tail call i32 @parse_device(i8* %0)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%11 = call i32 @snprintf(i8* %10, i32 99, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 99
store i8 0, i8* %12
%13 = call i32 @open(i8* %10, i32 %1)
br label %14
14: ; preds = %6, %9, %2
%15 = phi i32 [ %4, %2 ], [ %13, %9 ], [ %4, %6 ]
ret i32 %15
}