#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int GPIORead(int pin) {
  char path[30];
  char value_str[3];
  int fd;
  snprintf(path, 30, "/sys/class/gpio/gpio%d/value", pin);
  fd = open(path, O_RDONLY);
  if (-1 == fd) {
    fprintf(stderr, "Failed to open %s for reading!\n", path);
    return (-1);
  }
  if (-1 == read(fd, value_str, 3)) {
    fprintf(stderr, "Failed to read %s!\n", path);
    return (-1);
  }
  close(fd);
  return (atoi(value_str));
}


x86 assembly (clang, -O0):
<GPIORead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x1e,%esi
lea    0xe82(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
mov    $0xffffffff,%eax
cmp    -0x38(%rbp),%eax
jne    11cb <GPIORead+0x6b>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    0xe63(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <GPIORead+0xc4>
mov    -0x38(%rbp),%edi
lea    -0x33(%rbp),%rsi
mov    $0x3,%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1210 <GPIORead+0xb0>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    0xe3e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <GPIORead+0xc4>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
lea    -0x33(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GPIORead>:
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ecx
lea    0xe92(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1e,%esi
call   1030 <snprintf@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11c1 <GPIORead+0x61>
mov    %eax,%ebx
lea    0xd(%rsp),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11d3 <GPIORead+0x73>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xd(%rsp),%rdi
call   1080 <atoi@plt>
jmp    11f2 <GPIORead+0x92>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
jmp    11e3 <GPIORead+0x83>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [32 x i8] c"Failed to open %s for reading!\0A\00"
@.str.2 = [20 x i8] c"Failed to read %s!\0A\00"
define i32 @GPIORead(i32 %0) {
%2 = alloca [30 x i8]
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %4, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %22
13: ; preds = %1
%14 = call i32 @read(i32 %8, i8* %5, i32 3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %22
19: ; preds = %13
%20 = call i32 @close(i32 %8)
%21 = call i32 @atoi(i8* %5)
br label %22
22: ; preds = %19, %16, %10
%23 = phi i32 [ -1, %10 ], [ -1, %16 ], [ %21, %19 ]
ret i32 %23
}