#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, char *);
int gpio_get_value(unsigned int gpio, char *ident, unsigned int *value) {
  int fd, len;
  char buf[64];
  char ch;
  len = snprintf(buf, sizeof(buf),
                 "/sys/class/gpio"
                 "/gpio%d_%s/value",
                 gpio, ident);
  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)
mov    %rdx,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    $0x40,%esi
lea    0xe86(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x20(%rbp)
lea    -0x60(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b8 <gpio_get_value+0x68>
lea    0xe78(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fe <gpio_get_value+0xae>
mov    -0x1c(%rbp),%edi
lea    -0x61(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl -0x61(%rbp),%eax
cmp    $0x30,%eax
je     11e5 <gpio_get_value+0x95>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    11ef <gpio_get_value+0x9f>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x1c(%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    %rdx,%rbx
mov    %rsi,%r8
mov    %edi,%ecx
lea    0xe9b(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11bd <gpio_get_value+0x6d>
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    11b4 <gpio_get_value+0x64>

LLVM IR (-O0):
@.str = [32 x i8] c"/sys/class/gpio/gpio%d_%s/value\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"gpio/get-value\00"
define i32 @gpio_get_value(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [64 x i8]
%11 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = call i32 @snprintf(i8* %12, i32 64, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %14)
store i32 %15, i32* %9
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %3
%22 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %8
store i32 %23, i32* %4
br label %37
24: ; preds = %3
%25 = load i32, i32* %8
%26 = call i32 @read(i32 %25, i8* %11, i32 1)
%27 = load i8, i8* %11
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 48
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = load i32*, i32** %7
store i32 1, i32* %31
br label %34
32: ; preds = %24
%33 = load i32*, i32** %7
store i32 0, i32* %33
br label %34
34: ; preds = %32, %30
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
store i32 0, i32* %4
br label %37
37: ; preds = %34, %21
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@.str = [32 x i8] c"/sys/class/gpio/gpio%d_%s/value\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"gpio/get-value\00"
define i32 @gpio_get_value(i32 %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca [64 x i8]
%5 = alloca i8
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 64, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %19
13: ; preds = %3
%14 = call i32 @read(i32 %9, i8* %5, i32 1)
%15 = load i8, i8* %5
%16 = icmp ne i8 %15, 48
%17 = zext i1 %16 to i32
store i32 %17, i32* %2
%18 = call i32 @close(i32 %9)
br label %19
19: ; preds = %13, %11
%20 = phi i32 [ %9, %11 ], [ 0, %13 ]
ret i32 %20
}