#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*/ printf(char *, unsigned int);
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");
    printf("\n GPIO: %u \n", *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    0xe7e(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11cf <gpio_get_value+0x6f>
lea    0xe70(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1215 <gpio_get_value+0xb5>
mov    -0x14(%rbp),%edi
lea    -0x61(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movsbl -0x61(%rbp),%eax
cmp    $0x30,%eax
je     11fc <gpio_get_value+0x9c>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    1206 <gpio_get_value+0xa6>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x14(%rbp),%edi
call   1050 <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    0xe8e(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11ca <gpio_get_value+0x6a>
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   1050 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe4c(%rip),%rdi
call   1080 <perror@plt>
mov    (%rbx),%esi
lea    0xe4d(%rip),%rdi
call   1030 <printf@plt>
jmp    11c1 <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"
@.str.2 = [13 x i8] c"\0A GPIO: %u \0A\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 %23
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** %5
%20 = load i32, i32* %19
%21 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %6
store i32 %22, i32* %3
br label %36
23: ; preds = %2
%24 = load i32, i32* %6
%25 = call i32 @read(i32 %24, i8* %8, i32 1)
%26 = load i8, i8* %8
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 48
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = load i32*, i32** %5
store i32 1, i32* %30
br label %33
31: ; preds = %23
%32 = load i32*, i32** %5
store i32 0, i32* %32
br label %33
33: ; preds = %31, %29
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i32 0, i32* %3
br label %36
36: ; preds = %33, %17
%37 = load i32, i32* %3
ret i32 %37
}

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"
@.str.2 = [13 x i8] c"\0A GPIO: %u \0A\00"
define i32 @gpio_get_value(i32 %0, i32* nocapture %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 %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* %1
%13 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %20
14: ; preds = %2
%15 = call i32 @read(i32 %8, i8* %4, i32 1)
%16 = load i8, i8* %4
%17 = icmp ne i8 %16, 48
%18 = zext i1 %17 to i32
store i32 %18, i32* %1
%19 = call i32 @close(i32 %8)
br label %20
20: ; preds = %14, %10
%21 = phi i32 [ %8, %10 ], [ 0, %14 ]
ret i32 %21
}