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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
unsigned int gpioToSysPin(unsigned int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int gpio_get_value(unsigned int gpio) {
  unsigned int gpioSys = gpioToSysPin(gpio);
  int fd;
  char filename[29];
  char buf;
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpioSys);
  if ((fd = open(filename, O_RDONLY)) < 0)
    return -1;
  if (read(fd, &buf, 1) != 1)
    return -1;
  close(fd);
  return atoi(&buf);
}


x86 assembly (clang, -O0):
<gpio_get_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <gpioToSysPin@plt>
mov    %eax,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x1d,%esi
lea    0xe77(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11b8 <gpio_get_value+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <gpio_get_value+0x92>
mov    -0x10(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     11de <gpio_get_value+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <gpio_get_value+0x92>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
lea    -0x31(%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_get_value>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
call   1060 <gpioToSysPin@plt>
lea    0xe8e(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1d,%esi
mov    %eax,%ecx
call   1030 <snprintf@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11cb <gpio_get_value+0x6b>
mov    %eax,%ebp
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    11cb <gpio_get_value+0x6b>
mov    %ebp,%edi
call   1040 <close@plt>
lea    0xf(%rsp),%rdi
call   1080 <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 @gpio_get_value(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [29 x i8]
%7 = alloca i8
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @gpioToSysPin(i32 %8)
store i32 %9, i32* %4
%10 = getelementptr inbounds [29 x i8], [29 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* %4
%12 = call i32 @snprintf(i8* %10, i32 29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [29 x i8], [29 x i8]* %6, i64 0, i64 0
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %27
18: ; preds = %1
%19 = load i32, i32* %5
%20 = call i32 @read(i32 %19, i8* %7, i32 1)
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %2
br label %27
23: ; preds = %18
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
%26 = call i32 @atoi(i8* %7)
store i32 %26, i32* %2
br label %27
27: ; preds = %23, %22, %17
%28 = load i32, i32* %2
ret i32 %28
}

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