#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 /*<<< orphan*/ error_exit(char *);
int open(char *, int /*<<< orphan*/);
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)
    error_exit("gpio/get-value");
  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,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x4(%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   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a2 <gpio_get_value+0x52>
lea    0xe80(%rip),%rdi
call   1050 <error_exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x61(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movsbl -0x61(%rbp),%eax
cmp    $0x30,%eax
je     11cf <gpio_get_value+0x7f>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    11d9 <gpio_get_value+0x89>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%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   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1197 <gpio_get_value+0x47>
lea    0xe8b(%rip),%rdi
call   1050 <error_exit@plt>
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1060 <read@plt>
xor    %eax,%eax
cmpb   $0x30,0xf(%rsp)
setne  %al
mov    %eax,(%rbx)
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x58,%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
@.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 [64 x i8]
%7 = alloca i8
store i32 %0, i32* %3
store i32* %1, i32** %4
%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* %5
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @error_exit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i32, i32* %5
%20 = call i32 @read(i32 %19, i8* %7, i32 1)
%21 = load i8, i8* %7
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 48
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i32*, i32** %4
store i32 1, i32* %25
br label %28
26: ; preds = %18
%27 = load i32*, i32** %4
store i32 0, i32* %27
br label %28
28: ; preds = %26, %24
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
ret i32 0
}

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 @error_exit(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %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)
ret i32 0
}