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

/* Variables and functions */
int EINVAL;
int ENODEV;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int strlen(char *);
int gpio_get(int gpio) {
  FILE *fd;
  if (gpio < 0)
    return -EINVAL;
  char path[32] = "/sys/class/gpio/gpio";
  sprintf(path + strlen(path), "%d/value", gpio);
  fd = fopen(path, "r");
  if (fd < 0)
    return -ENODEV;
  path[0] = fgetc(fd);
  fclose(fd);
  return path[0] == '0' ? 0 : 1;
}


x86 assembly (clang, -O0):
<gpio_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1178 <gpio_get+0x28>
xor    %eax,%eax
mov    0x2e72(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <gpio_get+0xd8>
mov    0xe81(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe7e(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe7b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe78(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%edx
lea    0xe58(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jae    11ff <gpio_get+0xaf>
xor    %eax,%eax
mov    0x2de3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <gpio_get+0xd8>
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movsbl -0x30(%rbp),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x30,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_get>:
push   %r14
push   %rbx
sub    $0x28,%rsp
test   %edi,%edi
js     11c3 <gpio_get+0x73>
mov    %edi,%ebx
movaps 0xeac(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe90(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rdi
lea    0xe94(%rip),%rsi
mov    %ebx,%edx
call   1070 <sprintf@plt>
lea    0xe8f(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fgetc@plt>
mov    %al,(%rsp)
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmpb   $0x30,(%rsp)
setne  %al
jmp    11ce <gpio_get+0x7e>
mov    0x2e16(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@__const.gpio_get.path = [32 x i8] c"/sys/class/gpio/gpio\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [9 x i8] c"%d/value\00"
@.str.1 = [2 x i8] c"r\00"
@ENODEV = global i32 0
define i32 @gpio_get(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [32 x i8]
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @EINVAL
%10 = sub nsw i32 0, %9
store i32 %10, i32* %2
br label %39
11: ; preds = %1
%12 = bitcast [32 x i8]* %5 to i8*
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
%18 = load i32, i32* %3
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %4
%22 = load i32*, i32** %4
%23 = icmp ult i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %11
%25 = load i32, i32* @ENODEV
%26 = sub nsw i32 0, %25
store i32 %26, i32* %2
br label %39
27: ; preds = %11
%28 = load i32*, i32** %4
%29 = call signext i8 @fgetc(i32* %28)
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
store i8 %29, i8* %30
%31 = load i32*, i32** %4
%32 = call i32 @fclose(i32* %31)
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 48
%37 = zext i1 %36 to i64
%38 = select i1 %36, i32 0, i32 1
store i32 %38, i32* %2
br label %39
39: ; preds = %27, %24, %8
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@EINVAL = global i32 0
@__const.gpio_get.path = [32 x i8] c"/sys/class/gpio/gpio\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [9 x i8] c"%d/value\00"
@.str.1 = [2 x i8] c"r\00"
@ENODEV = global i32 0
define i32 @gpio_get(i32 %0) {
%2 = alloca [32 x i8]
%3 = icmp slt i32 %0, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @EINVAL
%6 = sub nsw i32 0, %5
br label %19
7: ; preds = %1
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%9 = call i32 @strlen(i8* %8)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 %10
%12 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%13 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = call signext i8 @fgetc(i32* %13)
store i8 %14, i8* %8
%15 = call i32 @fclose(i32* %13)
%16 = load i8, i8* %8
%17 = icmp ne i8 %16, 48
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %7, %4
%20 = phi i32 [ %6, %4 ], [ %18, %7 ]
ret i32 %20
}