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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int eim_get_value(int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int gpio_get_value(int gpio) {
  char gpio_path[256];
  int fd;
  if (gpio & (0x80000000))
    return eim_get_value(gpio & (~(0x80000000)));
  snprintf(gpio_path, sizeof(gpio_path) - 1,
           "/sys/class/gpio"
           "/gpio%d/value",
           gpio);
  fd = open(gpio_path, O_RDONLY);
  if (fd == -1) {
    perror("Couldn't open value file for gpio");
    return -errno;
  }
  if (read(fd, gpio_path, sizeof(gpio_path)) <= 0) {
    perror("Couldn't get input value");
    close(fd);
    return -errno;
  }
  close(fd);
  return gpio_path[0] != '0';
}


x86 assembly (clang, -O0):
<gpio_get_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x80000000,%eax
cmp    $0x0,%eax
je     1195 <gpio_get_value+0x35>
mov    -0x8(%rbp),%edi
and    $0x7fffffff,%edi
call   1080 <eim_get_value@plt>
mov    %eax,-0x4(%rbp)
jmp    1262 <gpio_get_value+0x102>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe55(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    11f7 <gpio_get_value+0x97>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
mov    0x2deb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <gpio_get_value+0x102>
mov    -0x114(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jg     1242 <gpio_get_value+0xe2>
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    -0x114(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
mov    0x2da0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <gpio_get_value+0x102>
mov    -0x114(%rbp),%edi
call   1040 <close@plt>
movsbl -0x110(%rbp),%eax
cmp    $0x30,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_get_value>:
mov    %edi,%ecx
test   %edi,%edi
js     11ca <gpio_get_value+0x6a>
push   %rbx
sub    $0x100,%rsp
lea    0xe8b(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1030 <snprintf@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d7 <gpio_get_value+0x77>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x100,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11e5 <gpio_get_value+0x85>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
cmpb   $0x30,(%rsp)
setne  %al
add    $0x100,%rsp
pop    %rbx
ret
and    $0x7fffffff,%ecx
mov    %ecx,%edi
jmp    1080 <eim_get_value@plt>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
jmp    11f8 <gpio_get_value+0x98>
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2dd9(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
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 = [34 x i8] c"Couldn't open value file for gpio\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Couldn't get input value\00"
define i32 @gpio_get_value(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = and i32 %6, -2147483648
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* %3
%11 = and i32 %10, 2147483647
%12 = call i32 @eim_get_value(i32 %11)
store i32 %12, i32* %2
br label %45
13: ; preds = %1
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%15 = load i32, i32* %3
%16 = call i32 @snprintf(i8* %14, i32 255, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
store i32 %25, i32* %2
br label %45
26: ; preds = %13
%27 = load i32, i32* %5
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%29 = call i64 @read(i32 %27, i8* %28, i32 256)
%30 = icmp sle i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* %5
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* @errno
%36 = sub nsw i32 0, %35
store i32 %36, i32* %2
br label %45
37: ; preds = %26
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 48
%44 = zext i1 %43 to i32
store i32 %44, i32* %2
br label %45
45: ; preds = %37, %31, %22, %9
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.1 = [34 x i8] c"Couldn't open value file for gpio\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Couldn't get input value\00"
define i32 @gpio_get_value(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = icmp sgt i32 %0, -1
br i1 %4, label %8, label %5
5: ; preds = %1
%6 = and i32 %0, 2147483647
%7 = tail call i32 @eim_get_value(i32 %6)
br label %30
8: ; preds = %1
%9 = call i32 @snprintf(i8* %3, i32 255, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %3, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
br label %30
17: ; preds = %8
%18 = call i64 @read(i32 %11, i8* %3, i32 256)
%19 = icmp slt i64 %18, 1
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @close(i32 %11)
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
br label %30
25: ; preds = %17
%26 = call i32 @close(i32 %11)
%27 = load i8, i8* %3
%28 = icmp ne i8 %27, 48
%29 = zext i1 %28 to i32
br label %30
30: ; preds = %25, %20, %13, %5
%31 = phi i32 [ %7, %5 ], [ %16, %13 ], [ %24, %20 ], [ %29, %25 ]
ret i32 %31
}