#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*/ ch;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int fd_lookup(unsigned int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open_value_file(unsigned int);
int /*<<< orphan*/ read(int, char *, int);
int gpio_get_value(unsigned int gpio, unsigned int *value) {
  int fd = fd_lookup(gpio);
  char ch;
  if (!fd) {
    if ((fd = open_value_file(gpio)) == -1)
      return -1;
  }
  lseek(fd, 0, SEEK_SET);
  read(fd, &ch, sizeof(ch));
  if (ch != '0') {
    *value = 1;
  } else {
    *value = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gpio_get_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <fd_lookup@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1189 <gpio_get_value+0x49>
mov    -0x8(%rbp),%edi
call   1050 <open_value_file@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1184 <gpio_get_value+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <gpio_get_value+0x9a>
jmp    1189 <gpio_get_value+0x49>
mov    -0x14(%rbp),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
movsbl -0x15(%rbp),%eax
cmp    $0x30,%eax
je     11c9 <gpio_get_value+0x89>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    11d3 <gpio_get_value+0x93>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_get_value>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
call   1060 <fd_lookup@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1166 <gpio_get_value+0x26>
mov    %ebp,%edi
call   1050 <open_value_file@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11a3 <gpio_get_value+0x63>
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <read@plt>
xor    %eax,%eax
cmpb   $0x30,0xf(%rsp)
setne  %al
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    1198 <gpio_get_value+0x58>

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i32 @gpio_get_value(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8
store i32 %0, i32* %4
store i32* %1, i32** %5
%8 = load i32, i32* %4
%9 = call i32 @fd_lookup(i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = load i32, i32* %4
%14 = call i32 @open_value_file(i32 %13)
store i32 %14, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -1, i32* %3
br label %32
17: ; preds = %12
br label %18
18: ; preds = %17, %2
%19 = load i32, i32* %6
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @lseek(i32 %19, i32 0, i32 %20)
%22 = load i32, i32* %6
%23 = call i32 @read(i32 %22, i8* %7, i32 1)
%24 = load i8, i8* %7
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 48
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = load i32*, i32** %5
store i32 1, i32* %28
br label %31
29: ; preds = %18
%30 = load i32*, i32** %5
store i32 0, i32* %30
br label %31
31: ; preds = %29, %27
store i32 0, i32* %3
br label %32
32: ; preds = %31, %16
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i32 @gpio_get_value(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca i8
%4 = tail call i32 @fd_lookup(i32 %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @open_value_file(i32 %0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %17, label %9
9: ; preds = %6, %2
%10 = phi i32 [ %4, %2 ], [ %7, %6 ]
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @lseek(i32 %10, i32 0, i32 %11)
%13 = call i32 @read(i32 %10, i8* %3, i32 1)
%14 = load i8, i8* %3
%15 = icmp ne i8 %14, 48
%16 = zext i1 %15 to i32
store i32 %16, i32* %1
br label %17
17: ; preds = %6, %9
%18 = phi i32 [ 0, %9 ], [ -1, %6 ]
ret i32 %18
}