#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 snprintf(char *, int, char *, unsigned int);
int gpio_get_value(unsigned int gpio, unsigned int *value) {
  int fd, len;
  char buf[64];
  char ch;
  len = 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>
mov    %eax,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b0 <gpio_get_value+0x60>
lea    0xe7d(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <gpio_get_value+0xa6>
mov    -0x14(%rbp),%edi
lea    -0x61(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl -0x61(%rbp),%eax
cmp    $0x30,%eax
je     11dd <gpio_get_value+0x8d>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    11e7 <gpio_get_value+0x97>
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 i32
%8 = alloca [64 x i8]
%9 = alloca i8
store i32 %0, i32* %4
store i32* %1, i32** %5
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %4
%12 = call i32 @snprintf(i8* %10, i32 64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %7
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %34
21: ; preds = %2
%22 = load i32, i32* %6
%23 = call i32 @read(i32 %22, i8* %9, i32 1)
%24 = load i8, i8* %9
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 48
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i32*, i32** %5
store i32 1, i32* %28
br label %31
29: ; preds = %21
%30 = load i32*, i32** %5
store i32 0, i32* %30
br label %31
31: ; preds = %29, %27
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 0, i32* %3
br label %34
34: ; preds = %31, %18
%35 = load i32, i32* %3
ret i32 %35
}

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
}