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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int readGpio(unsigned int index) {
  int fd;
  char buf[50];
  sprintf(buf, "/sys/class/gpio/gpio%d/value", index);
  if ((fd = open(buf, O_RDONLY)) == -1)
    return -1;
  if (read(fd, buf, 10) == -1)
    return -1;
  close(fd);
  return atoi(buf);
}


x86 assembly (clang, -O0):
<readGpio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe97(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1198 <readGpio+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <readGpio+0x82>
mov    -0xc(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xa,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    11be <readGpio+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <readGpio+0x82>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
lea    -0x40(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readGpio>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %edi,%edx
lea    0xea1(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11ac <readGpio+0x5c>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xa,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11ac <readGpio+0x5c>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %rsp,%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x38,%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
define i32 @readGpio(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [50 x i8]
store i32 %0, i32* %3
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* %3
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %25
14: ; preds = %1
%15 = load i32, i32* %4
%16 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 10)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %2
br label %25
20: ; preds = %14
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %2
br label %25
25: ; preds = %20, %19, %13
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
define i32 @readGpio(i32 %0) {
%2 = alloca [50 x i8]
%3 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %14, label %8
8: ; preds = %1
%9 = call i32 @read(i32 %6, i8* %3, i32 10)
%10 = icmp eq i32 %9, -1
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i32 @close(i32 %6)
%13 = call i32 @atoi(i8* %3)
br label %14
14: ; preds = %8, %1, %11
%15 = phi i32 [ %13, %11 ], [ -1, %1 ], [ -1, %8 ]
ret i32 %15
}