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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int rc_gpio_get_value(unsigned int gpio) {
  int fd, ret;
  char buf[64];
  char ch;
  snprintf(buf, sizeof(buf),
           "/sys/class/gpio"
           "/gpio%d/value",
           gpio);
  fd = open(buf, O_RDONLY);
  if (fd < 0) {
    perror("gpio/get-value");
    return fd;
  }
  read(fd, &ch, 1);
  if (ch != '0')
    ret = 1;
  else
    ret = 0;
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<rc_gpio_get_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe92(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a9 <rc_gpio_get_value+0x59>
lea    0xe84(%rip),%rdi
call   1070 <perror@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e8 <rc_gpio_get_value+0x98>
mov    -0xc(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl -0x51(%rbp),%eax
cmp    $0x30,%eax
je     11d3 <rc_gpio_get_value+0x83>
movl   $0x1,-0x10(%rbp)
jmp    11da <rc_gpio_get_value+0x8a>
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rc_gpio_get_value>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
mov    %edi,%ecx
lea    0xea1(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b6 <rc_gpio_get_value+0x66>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
xor    %ebp,%ebp
cmpb   $0x30,0xf(%rsp)
setne  %bpl
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe60(%rip),%rdi
call   1070 <perror@plt>
jmp    11ad <rc_gpio_get_value+0x5d>

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"gpio/get-value\00"
define i32 @rc_gpio_get_value(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i8]
%7 = alloca i8
store i32 %0, i32* %3
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %4
store i32 %18, i32* %2
br label %31
19: ; preds = %1
%20 = load i32, i32* %4
%21 = call i32 @read(i32 %20, i8* %7, i32 1)
%22 = load i8, i8* %7
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 48
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 1, i32* %5
br label %27
26: ; preds = %19
store i32 0, i32* %5
br label %27
27: ; preds = %26, %25
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %5
store i32 %30, i32* %2
br label %31
31: ; preds = %27, %16
%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
@.str.1 = [15 x i8] c"gpio/get-value\00"
define i32 @rc_gpio_get_value(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca i8
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 64, 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 %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %17
11: ; preds = %1
%12 = call i32 @read(i32 %7, i8* %3, i32 1)
%13 = load i8, i8* %3
%14 = icmp ne i8 %13, 48
%15 = zext i1 %14 to i32
%16 = call i32 @close(i32 %7)
br label %17
17: ; preds = %11, %9
%18 = phi i32 [ %7, %9 ], [ %15, %11 ]
ret i32 %18
}