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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
unsigned int gpioToSysPin(unsigned int);
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int open_value_file(unsigned int gpio) {
  unsigned int gpioSys = gpioToSysPin(gpio);
  int fd;
  char filename[29];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpioSys);
  if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<open_value_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <gpioToSysPin@plt>
mov    %eax,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x1d,%esi
lea    0xea7(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1191 <open_value_file+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    1197 <open_value_file+0x67>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_value_file>:
push   %rbx
sub    $0x20,%rsp
call   1040 <gpioToSysPin@plt>
lea    0xebf(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1d,%esi
mov    %eax,%ecx
call   1030 <snprintf@plt>
mov    0x2e86(%rip),%rax
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
mov    $0xffffffff,%ecx
cmovs  %ecx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
define i32 @open_value_file(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [29 x i8]
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @gpioToSysPin(i32 %7)
store i32 %8, i32* %4
%9 = getelementptr inbounds [29 x i8], [29 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [29 x i8], [29 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15)
store i32 %16, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %1
store i32 -1, i32* %2
br label %21
19: ; preds = %1
%20 = load i32, i32* %5
store i32 %20, i32* %2
br label %21
21: ; preds = %19, %18
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
define i32 @open_value_file(i32 %0) {
%2 = alloca [29 x i8]
%3 = tail call i32 @gpioToSysPin(i32 %0)
%4 = getelementptr inbounds [29 x i8], [29 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %3)
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %6
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp sgt i32 %9, -1
%11 = select i1 %10, i32 %9, i32 -1
ret i32 %11
}