#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);
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) {
  int fd;
  char filename[29];
  char buf;
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
  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)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x1d,%esi
lea    0xe92(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    119d <gpio_get_value+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <gpio_get_value+0x87>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     11c3 <gpio_get_value+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <gpio_get_value+0x87>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
lea    -0x31(%rbp),%rdi
call   1070 <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
mov    %edi,%ecx
lea    0xea1(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1d,%esi
call   1030 <snprintf@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11b6 <gpio_get_value+0x66>
mov    %eax,%ebp
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    11b6 <gpio_get_value+0x66>
mov    %ebp,%edi
call   1040 <close@plt>
lea    0xf(%rsp),%rdi
call   1070 <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 [29 x i8]
%6 = alloca i8
store i32 %0, i32* %3
%7 = getelementptr inbounds [29 x i8], [29 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [29 x i8], [29 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %24
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call i32 @read(i32 %16, i8* %6, i32 1)
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %2
br label %24
20: ; preds = %15
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = call i32 @atoi(i8* %6)
store i32 %23, i32* %2
br label %24
24: ; preds = %20, %19, %14
%25 = load i32, i32* %2
ret i32 %25
}

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 = getelementptr inbounds [29 x i8], [29 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %1
%10 = call i32 @read(i32 %7, i8* %3, i32 1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @close(i32 %7)
%14 = call i32 @atoi(i8* %3)
br label %15
15: ; preds = %9, %1, %12
%16 = phi i32 [ %14, %12 ], [ -1, %1 ], [ -1, %9 ]
ret i32 %16
}