#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 gpio_get_value(unsigned int gpio, unsigned int *value) {
  int fd;
  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') {
    *value = 1;
  } else {
    *value = 0;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_get_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe8e(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ad <gpio_get_value+0x5d>
lea    0xe80(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f3 <gpio_get_value+0xa3>
mov    -0x14(%rbp),%edi
lea    -0x61(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl -0x61(%rbp),%eax
cmp    $0x30,%eax
je     11da <gpio_get_value+0x8a>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    11e4 <gpio_get_value+0x94>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_get_value>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
mov    %rsi,%rbx
mov    %edi,%ecx
lea    0xe9e(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11ba <gpio_get_value+0x6a>
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <read@plt>
xor    %eax,%eax
cmpb   $0x30,0xf(%rsp)
setne  %al
mov    %eax,(%rbx)
mov    %ebp,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5c(%rip),%rdi
call   1070 <perror@plt>
jmp    11b1 <gpio_get_value+0x61>

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 @gpio_get_value(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [64 x i8]
%8 = alloca i8
store i32 %0, i32* %4
store i32* %1, i32** %5
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %6
store i32 %19, i32* %3
br label %33
20: ; preds = %2
%21 = load i32, i32* %6
%22 = call i32 @read(i32 %21, i8* %8, i32 1)
%23 = load i8, i8* %8
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 48
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = load i32*, i32** %5
store i32 1, i32* %27
br label %30
28: ; preds = %20
%29 = load i32*, i32** %5
store i32 0, i32* %29
br label %30
30: ; preds = %28, %26
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
store i32 0, i32* %3
br label %33
33: ; preds = %30, %17
%34 = load i32, i32* %3
ret i32 %34
}

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 @gpio_get_value(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca [64 x i8]
%4 = alloca i8
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 64, 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* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %18
12: ; preds = %2
%13 = call i32 @read(i32 %8, i8* %4, i32 1)
%14 = load i8, i8* %4
%15 = icmp ne i8 %14, 48
%16 = zext i1 %15 to i32
store i32 %16, i32* %1
%17 = call i32 @close(i32 %8)
br label %18
18: ; preds = %12, %10
%19 = phi i32 [ %8, %10 ], [ 0, %12 ]
ret i32 %19
}