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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ read(int, char *, unsigned int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, unsigned int,
                            char const *);
int gpio_read(unsigned int gpio, const char *filename, char *value,
              unsigned int length) {
  int fd, len;
  char filepath[64];
  snprintf(filepath, sizeof(filepath), "%s/gpio%d/%s", "/sys/class/gpio", gpio,
           filename);
  if ((fd = open(filepath, O_RDONLY | O_NONBLOCK)) < 0)
    return -1;
  lseek(fd, 0, SEEK_SET);
  read(fd, value, length);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    -0x10(%rbp),%r9
mov    $0x40,%esi
lea    0xe82(%rip),%rdx
lea    0xe88(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11bd <gpio_read+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <gpio_read+0x9e>
mov    -0x20(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <read@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r9
mov    %edi,%r8d
lea    0xe93(%rip),%rdx
lea    0xe99(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e55(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11ce <gpio_read+0x7e>
mov    %eax,%ebx
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11d3 <gpio_read+0x83>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"%s/gpio%d/%s\00"
@.str.1 = [16 x i8] c"/sys/class/gpio\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@SEEK_SET = global i32 0
define i32 @gpio_read(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [64 x i8]
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%14 = load i32, i32* %6
%15 = load i8*, i8** %7
%16 = call i32 @snprintf(i8* %13, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14, i8* %15)
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%18 = load i32, i32* @O_RDONLY
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %17, i32 %20)
store i32 %21, i32* %10
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %4
store i32 -1, i32* %5
br label %34
24: ; preds = %4
%25 = load i32, i32* %10
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %25, i32 0, i32 %26)
%28 = load i32, i32* %10
%29 = load i8*, i8** %8
%30 = load i32, i32* %9
%31 = call i32 @read(i32 %28, i8* %29, i32 %30)
%32 = load i32, i32* %10
%33 = call i32 @close(i32 %32)
store i32 0, i32* %5
br label %34
34: ; preds = %24, %23
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
@.str = [13 x i8] c"%s/gpio%d/%s\00"
@.str.1 = [16 x i8] c"/sys/class/gpio\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@SEEK_SET = global i32 0
define i32 @gpio_read(i32 %0, i8* %1, i8* nocapture %2, i32 %3) {
%5 = alloca [64 x i8]
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %1)
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %9, %8
%11 = call i32 @open(i8* %6, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %4
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @lseek(i32 %11, i32 0, i32 %14)
%16 = call i32 @read(i32 %11, i8* %2, i32 %3)
%17 = call i32 @close(i32 %11)
br label %18
18: ; preds = %4, %13
%19 = phi i32 [ 0, %13 ], [ -1, %4 ]
ret i32 %19
}