#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int set_gpio_mode_input(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int get_gpio_value(int index, int *value) {
  char fn[64];
  FILE *f;
  int r;
  sprintf(fn, "/sys/class/gpio/gpio%d/value", index);
  f = fopen(fn, "r");
  if (!f) {
    r = set_gpio_mode_input(index);
    if (r)
      return r;
    f = fopen(fn, "r");
    if (!f)
      return -errno;
  }
  r = fscanf(f, "%d", value);
  if (r != 1)
    return (r < 0) ? r : -EINVAL;
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<get_gpio_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe93(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xea0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11e8 <get_gpio_value+0x98>
mov    -0x8(%rbp),%edi
call   1030 <set_gpio_mode_input@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     11b1 <get_gpio_value+0x61>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <get_gpio_value+0xf7>
lea    -0x50(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11e3 <get_gpio_value+0x93>
xor    %eax,%eax
mov    0x2e07(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <get_gpio_value+0xf7>
jmp    11e8 <get_gpio_value+0x98>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe28(%rip),%rsi
call   1050 <fscanf@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x1,-0x5c(%rbp)
je     1237 <get_gpio_value+0xe7>
cmpl   $0x0,-0x5c(%rbp)
jge    121e <get_gpio_value+0xce>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x60(%rbp)
jmp    122c <get_gpio_value+0xdc>
xor    %eax,%eax
mov    0x2db1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <get_gpio_value+0xf7>
mov    -0x58(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_gpio_value>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe9c(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
lea    0xea5(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ab <get_gpio_value+0x5b>
lea    0xe90(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jne    11da <get_gpio_value+0x8a>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    11ec <get_gpio_value+0x9c>
mov    %ebp,%edi
call   1030 <set_gpio_mode_input@plt>
test   %eax,%eax
jne    11ec <get_gpio_value+0x9c>
lea    0xe60(%rip),%rsi
mov    %rsp,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1188 <get_gpio_value+0x38>
mov    0x2e0c(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11ec <get_gpio_value+0x9c>
mov    %eax,%ecx
mov    0x2df5(%rip),%rdx
xor    %eax,%eax
sub    (%rdx),%eax
test   %ecx,%ecx
cmovs  %ecx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@EINVAL = global i32 0
define i32 @get_gpio_value(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [64 x i8]
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %32, label %16
16: ; preds = %2
%17 = load i32, i32* %4
%18 = call i32 @set_gpio_mode_input(i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = load i32, i32* %8
store i32 %22, i32* %3
br label %51
23: ; preds = %16
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %7
%26 = load i32*, i32** %7
%27 = icmp ne i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
store i32 %30, i32* %3
br label %51
31: ; preds = %23
br label %32
32: ; preds = %31, %2
%33 = load i32*, i32** %7
%34 = load i32*, i32** %5
%35 = call i32 @fscanf(i32* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp ne i32 %36, 1
br i1 %37, label %38, label %48
38: ; preds = %32
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = load i32, i32* %8
br label %46
43: ; preds = %38
%44 = load i32, i32* @EINVAL
%45 = sub nsw i32 0, %44
br label %46
46: ; preds = %43, %41
%47 = phi i32 [ %42, %41 ], [ %45, %43 ]
store i32 %47, i32* %3
br label %51
48: ; preds = %32
%49 = load i32*, i32** %7
%50 = call i32 @fclose(i32* %49)
store i32 0, i32* %3
br label %51
51: ; preds = %48, %46, %28, %21
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@EINVAL = global i32 0
define i32 @get_gpio_value(i32 %0, i32* %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %17
8: ; preds = %2
%9 = tail call i32 @set_gpio_mode_input(i32 %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %28
11: ; preds = %8
%12 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
br label %28
17: ; preds = %11, %2
%18 = phi i32* [ %6, %2 ], [ %12, %11 ]
%19 = tail call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %26, label %21
21: ; preds = %17
%22 = icmp slt i32 %19, 0
%23 = load i32, i32* @EINVAL
%24 = sub nsw i32 0, %23
%25 = select i1 %22, i32 %19, i32 %24
br label %28
26: ; preds = %17
%27 = tail call i32 @fclose(i32* %18)
br label %28
28: ; preds = %8, %26, %21, %14
%29 = phi i32 [ %25, %21 ], [ 0, %26 ], [ %16, %14 ], [ %9, %8 ]
ret i32 %29
}