#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;
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int open_value_file(unsigned int gpio) {
  int fd;
  char filename[29];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
  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)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x1d,%esi
lea    0xec2(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    0x2e91(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1176 <open_value_file+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    117c <open_value_file+0x5c>
mov    -0xc(%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
mov    %edi,%ecx
lea    0xed2(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1d,%esi
call   1030 <snprintf@plt>
mov    0x2e9b(%rip),%rax
mov    0x2e9c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <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 [29 x i8]
store i32 %0, i32* %3
%6 = getelementptr inbounds [29 x i8], [29 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* %3
%8 = call i32 @snprintf(i8* %6, i32 29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [29 x i8], [29 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12)
store i32 %13, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %18
16: ; preds = %1
%17 = load i32, i32* %4
store i32 %17, i32* %2
br label %18
18: ; preds = %16, %15
%19 = load i32, i32* %2
ret i32 %19
}

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 = getelementptr inbounds [29 x i8], [29 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = call i32 @open(i8* %3, i32 %7)
%9 = icmp sgt i32 %8, -1
%10 = select i1 %9, i32 %8, i32 -1
ret i32 %10
}