#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*/ value;
typedef int /*<<< orphan*/ filename;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int snprintf(char *, int, char *, int);
int gpio_read(int gpio) {
  int ret;
  char filename[128];
  ret = snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value",
                 gpio);
  if (ret < 0)
    goto out;
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    ret = -errno;
    goto out;
  }
  char value;
  if (read(fd, &value, sizeof(value)) != sizeof(value)) {
    ret = -errno;
    goto out_close;
  }
  ret = (value == '1' ? 1 : 0);
out_close:
  if (close(fd) < 0)
    ret = -errno;
out:
  return ret;
}


x86 assembly (clang, -O0):
<gpio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x80,%esi
lea    0xe9c(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    117b <gpio_read+0x3b>
jmp    122b <gpio_read+0xeb>
lea    -0x90(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jge    11b6 <gpio_read+0x76>
xor    %eax,%eax
mov    0x2e2c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x8(%rbp)
jmp    122b <gpio_read+0xeb>
mov    -0x94(%rbp),%edi
lea    -0x95(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cltq
cmp    $0x1,%rax
je     11ec <gpio_read+0xac>
xor    %eax,%eax
mov    0x2df6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x8(%rbp)
jmp    1203 <gpio_read+0xc3>
movsbl -0x95(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x31,%edx
cmove  %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x94(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1226 <gpio_read+0xe6>
xor    %eax,%eax
mov    0x2db7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x8(%rbp)
jmp    122b <gpio_read+0xeb>
mov    -0x8(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_read>:
push   %rbp
push   %rbx
sub    $0x98,%rsp
mov    %edi,%ecx
lea    0xeae(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
call   1030 <snprintf@plt>
test   %eax,%eax
js     11a0 <gpio_read+0x60>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11bb <gpio_read+0x7b>
mov    %eax,%ebp
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    11a4 <gpio_read+0x64>
xor    %ebx,%ebx
cmpb   $0x31,0xf(%rsp)
sete   %bl
jmp    11af <gpio_read+0x6f>
mov    %eax,%ebx
jmp    11c6 <gpio_read+0x86>
mov    0x2e2d(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
test   %rax,%rax
jns    11c6 <gpio_read+0x86>
mov    0x2e16(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x98,%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
@errno = global i32 0
define i32 @gpio_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = alloca i32
%6 = alloca i8
store i32 %0, i32* %2
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %2
%9 = call i32 @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
br label %44
13: ; preds = %1
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
store i32 %21, i32* %3
br label %44
22: ; preds = %13
%23 = load i32, i32* %5
%24 = call i32 @read(i32 %23, i8* %6, i32 1)
%25 = sext i32 %24 to i64
%26 = icmp ne i64 %25, 1
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @errno
%29 = sub nsw i32 0, %28
store i32 %29, i32* %3
br label %36
30: ; preds = %22
%31 = load i8, i8* %6
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 49
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 1, i32 0
store i32 %35, i32* %3
br label %36
36: ; preds = %30, %27
%37 = load i32, i32* %5
%38 = call i64 @close(i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* @errno
%42 = sub nsw i32 0, %41
store i32 %42, i32* %3
br label %43
43: ; preds = %40, %36
br label %44
44: ; preds = %43, %19, %12
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @gpio_read(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca i8
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %31, label %7
7: ; preds = %1
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %31
14: ; preds = %7
%15 = call i32 @read(i32 %9, i8* %3, i32 1)
%16 = icmp eq i32 %15, 1
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
br label %24
20: ; preds = %14
%21 = load i8, i8* %3
%22 = icmp eq i8 %21, 49
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %20, %17
%25 = phi i32 [ %19, %17 ], [ %23, %20 ]
%26 = call i64 @close(i32 %9)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
br label %31
31: ; preds = %24, %28, %1, %11
%32 = phi i32 [ %5, %1 ], [ %13, %11 ], [ %30, %28 ], [ %25, %24 ]
ret i32 %32
}