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


x86 assembly (clang, -O0):
<GPIORead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x30,%esi
lea    0xe82(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    11c3 <GPIORead+0x63>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <GPIORead+0xb3>
mov    -0x48(%rbp),%edi
lea    -0x43(%rbp),%rsi
mov    $0x3,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    11ff <GPIORead+0x9f>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <GPIORead+0xb3>
mov    -0x48(%rbp),%edi
call   1040 <close@plt>
lea    -0x43(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GPIORead>:
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ecx
lea    0xe92(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x30,%esi
call   1030 <snprintf@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11c0 <GPIORead+0x60>
mov    %eax,%ebx
lea    0xd(%rsp),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1050 <read@plt>
test   %rax,%rax
js     11d2 <GPIORead+0x72>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xd(%rsp),%rdi
call   1080 <atoi@plt>
jmp    11ec <GPIORead+0x8c>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
jmp    11e2 <GPIORead+0x82>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x40,%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 = [40 x i8] c"failed to open gpio value for reading!\0A\00"
@.str.2 = [23 x i8] c"failed to read value!\0A\00"
define i32 @GPIORead(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [48 x i8]
%5 = alloca [3 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = getelementptr inbounds [48 x i8], [48 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 48, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [48 x i8], [48 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 slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %31
18: ; preds = %1
%19 = load i32, i32* %6
%20 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%21 = call i64 @read(i32 %19, i8* %20, i32 3)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %31
26: ; preds = %18
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %2
br label %31
31: ; preds = %26, %23, %15
%32 = load i32, i32* %2
ret i32 %32
}

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 = [40 x i8] c"failed to open gpio value for reading!\0A\00"
@.str.2 = [23 x i8] c"failed to read value!\0A\00"
define i32 @GPIORead(i32 %0) {
%2 = alloca [48 x i8]
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [48 x i8], [48 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 48, 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 slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %22
13: ; preds = %1
%14 = call i64 @read(i32 %8, i8* %5, i32 3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
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
}