#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);
  if (len < 0)
    return len;
  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)
cmpl   $0x0,-0x18(%rbp)
jge    118f <gpio_get_value+0x3f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120b <gpio_get_value+0xbb>
lea    -0x60(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c5 <gpio_get_value+0x75>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120b <gpio_get_value+0xbb>
mov    -0x14(%rbp),%edi
lea    -0x61(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl -0x61(%rbp),%eax
cmp    $0x30,%eax
je     11f2 <gpio_get_value+0xa2>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    11fc <gpio_get_value+0xac>
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),%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
test   %eax,%eax
js     11b6 <gpio_get_value+0x66>
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11c1 <gpio_get_value+0x71>
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
jmp    11b8 <gpio_get_value+0x68>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
jmp    11b8 <gpio_get_value+0x68>

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

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 = icmp slt i32 %6, 0
br i1 %7, label %20, label %8
8: ; preds = %2
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %5, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %20
14: ; preds = %8
%15 = call i32 @read(i32 %10, 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 %10)
br label %20
20: ; preds = %2, %14, %12
%21 = phi i32 [ %10, %12 ], [ 0, %14 ], [ %6, %2 ]
ret i32 %21
}