#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 /*<<< orphan*/ add_fd_list(unsigned int, int);
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int open_value_file(unsigned int gpio) {
  int fd;
  char filename[40];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
  if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0)
    return -1;
  add_fd_list(gpio, fd);
  return fd;
}


x86 assembly (clang, -O0):
<open_value_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x28,%esi
lea    0xeb2(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1186 <open_value_file+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    1197 <open_value_file+0x67>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <add_fd_list@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_value_file>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0xec1(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x28,%esi
mov    %ebx,%ecx
call   1030 <snprintf@plt>
mov    0x2e88(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     117c <open_value_file+0x4c>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <add_fd_list@plt>
jmp    1181 <open_value_file+0x51>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
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 [40 x i8]
store i32 %0, i32* %3
%6 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* %3
%8 = call i32 @snprintf(i8* %6, i32 40, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [40 x i8], [40 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 %21
16: ; preds = %1
%17 = load i32, i32* %3
%18 = load i32, i32* %4
%19 = call i32 @add_fd_list(i32 %17, i32 %18)
%20 = load i32, i32* %4
store i32 %20, i32* %2
br label %21
21: ; preds = %16, %15
%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 [40 x i8]
%3 = getelementptr inbounds [40 x i8], [40 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 40, 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 slt i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = call i32 @add_fd_list(i32 %0, i32 %8)
br label %12
12: ; preds = %1, %10
%13 = phi i32 [ %8, %10 ], [ -1, %1 ]
ret i32 %13
}