#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*/ sysdev;

/* Variables and functions */
int /*<<< orphan*/ dprintf(int, char *, char const *);
int open(char *, int);
int parse_device(char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, char const *);
int open_sysfs_file(const char *devicename, const char *filename, int flags) {
  int fd = -1;
  char sysdev[100];
  int dev = parse_device(devicename);
  if (dev < 0) {
    dprintf(2, "ignoring illegal devicename '%s'\n", devicename);
    return -1;
  }
  snprintf(sysdev, sizeof(sysdev) - 1,
           "/sys/devices/virtual/video4linux/video%d/%s", dev, filename);
  sysdev[sizeof(sysdev) - 1] = 0;
  fd = open(sysdev, flags);
  if (fd < 0) {
    perror("unable to open /sys-device");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_sysfs_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <parse_device@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jge    11aa <open_sysfs_file+0x5a>
mov    -0x10(%rbp),%rdx
mov    $0x2,%edi
lea    0xe67(%rip),%rsi
call   1040 <dprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <open_sysfs_file+0xba>
lea    -0x90(%rbp),%rdi
mov    -0x94(%rbp),%ecx
mov    -0x18(%rbp),%r8
mov    $0x63,%esi
lea    0xe5b(%rip),%rdx
call   1030 <snprintf@plt>
movb   $0x0,-0x2d(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1204 <open_sysfs_file+0xb4>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <open_sysfs_file+0xba>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_sysfs_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1050 <parse_device@plt>
test   %eax,%eax
js     11a8 <open_sysfs_file+0x58>
lea    0xeb1(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x63,%esi
mov    %eax,%ecx
mov    %rbx,%r8
call   1030 <snprintf@plt>
movb   $0x0,0x63(%rsp)
mov    %rbp,%rdi
mov    %r14d,%esi
call   1060 <open@plt>
test   %eax,%eax
jns    11c1 <open_sysfs_file+0x71>
lea    0xead(%rip),%rdi
call   1070 <perror@plt>
jmp    11bc <open_sysfs_file+0x6c>
lea    0xe51(%rip),%rsi
mov    $0x2,%edi
mov    %rbp,%rdx
call   1040 <dprintf@plt>
mov    $0xffffffff,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"ignoring illegal devicename '%s'\0A\00"
@.str.1 = [44 x i8] c"/sys/devices/virtual/video4linux/video%d/%s\00"
@.str.2 = [27 x i8] c"unable to open /sys-device\00"
define i32 @open_sysfs_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [100 x i8]
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 -1, i32* %8
%11 = load i8*, i8** %5
%12 = call i32 @parse_device(i8* %11)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call i32 @dprintf(i32 2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %4
br label %33
18: ; preds = %3
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%20 = load i32, i32* %10
%21 = load i8*, i8** %6
%22 = call i32 @snprintf(i8* %19, i32 99, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* %21)
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 99
store i8 0, i8* %23
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%25 = load i32, i32* %7
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %33
31: ; preds = %18
%32 = load i32, i32* %8
store i32 %32, i32* %4
br label %33
33: ; preds = %31, %29, %15
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@.str = [34 x i8] c"ignoring illegal devicename '%s'\0A\00"
@.str.1 = [44 x i8] c"/sys/devices/virtual/video4linux/video%d/%s\00"
@.str.2 = [27 x i8] c"unable to open /sys-device\00"
define i32 @open_sysfs_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @parse_device(i8* %0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @dprintf(i32 2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %17
10: ; preds = %3
%11 = call i32 @snprintf(i8* %5, i32 99, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %6, i8* %1)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 99
store i8 0, i8* %12
%13 = call i32 @open(i8* %5, i32 %2)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %10, %15, %8
%18 = phi i32 [ -1, %8 ], [ -1, %15 ], [ %13, %10 ]
ret i32 %18
}