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

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


x86 assembly (clang, -O0):
<gpio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x20,%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    11c7 <gpio_read+0x67>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <gpio_read+0xbc>
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    1208 <gpio_read+0xa8>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <gpio_read+0xbc>
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):
<gpio_read>:
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ecx
lea    0xe92(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11c1 <gpio_read+0x61>
mov    %eax,%ebx
lea    0xd(%rsp),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11c1 <gpio_read+0x61>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xd(%rsp),%rdi
call   1080 <atoi@plt>
jmp    11db <gpio_read+0x7b>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
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 read gpio pin value!\0A\00"
define i32 @gpio_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [32 x i8]
%5 = alloca [3 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 32, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [32 x i8], [32 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 %18
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 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 i32 @read(i32 %19, i8* %20, i32 3)
%22 = icmp eq i32 -1, %21
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, 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 = [32 x i8] c"Failed to read gpio pin value!\0A\00"
define i32 @gpio_read(i32 %0) {
%2 = alloca [32 x i8]
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [32 x i8], [32 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 32, 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))
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 ([32 x i8], [32 x i8]* @.str.1, 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
}