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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ gpio_export(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
scalar_t__ sprintf(char *, char const *, int);
int gpio_get_value(int gpio_num) {
  const char *gpio_value_path = "/sys/class/gpio/gpio%d/value";
  int fd_v = 0, g_err = -1;
  char pinvalue_buf[256];
  char v_buf[8];
  if (sprintf(pinvalue_buf, gpio_value_path, gpio_num) < 0) {
    printf("Can't create pin direction file path\n");
    return g_err;
  }
  fd_v = open(pinvalue_buf, O_RDONLY);
  if (fd_v < 0) {
    if (gpio_export(gpio_num) < 0) {
      return g_err;
    }
    fd_v = open(pinvalue_buf, O_RDONLY);
    if (fd_v <= 0) {
      printf("Couldn't get value File for pin %d\n", gpio_num);
      return g_err;
    }
  }
  if (read(fd_v, v_buf, 1) == g_err) {
    printf("Couldn't get value for pin %d\n", gpio_num);
    return g_err;
  }
  close(fd_v);
  return atoi(v_buf);
}


x86 assembly (clang, -O0):
<gpio_get_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe7b(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1090 <sprintf@plt>
cmp    $0x0,%rax
jge    11cd <gpio_get_value+0x5d>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <gpio_get_value+0x12f>
lea    -0x120(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124f <gpio_get_value+0xdf>
mov    -0x8(%rbp),%edi
call   1040 <gpio_export@plt>
cmp    $0x0,%rax
jge    120c <gpio_get_value+0x9c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <gpio_get_value+0x12f>
lea    -0x120(%rbp),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     124a <gpio_get_value+0xda>
mov    -0x8(%rbp),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <gpio_get_value+0x12f>
jmp    124f <gpio_get_value+0xdf>
mov    -0x14(%rbp),%edi
lea    -0x128(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    -0x18(%rbp),%eax
jne    1288 <gpio_get_value+0x118>
mov    -0x8(%rbp),%esi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <gpio_get_value+0x12f>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
lea    -0x128(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_get_value>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %edi,%r14d
lea    0xe69(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r14d,%edx
call   10a0 <sprintf@plt>
test   %rax,%rax
js     11ee <gpio_get_value+0x6e>
mov    0x2e38(%rip),%r15
mov    (%r15),%esi
lea    0x10(%rsp),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11fc <gpio_get_value+0x7c>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1234 <gpio_get_value+0xb4>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0x8(%rsp),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
jmp    124a <gpio_get_value+0xca>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
jmp    1245 <gpio_get_value+0xc5>
mov    %r14d,%edi
call   1050 <gpio_export@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     124a <gpio_get_value+0xca>
mov    (%r15),%esi
lea    0x10(%rsp),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     11c3 <gpio_get_value+0x43>
lea    0xdf5(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    124a <gpio_get_value+0xca>
lea    0xe06(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.1 = [38 x i8] c"Can't create pin direction file path\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [36 x i8] c"Couldn't get value File for pin %d\0A\00"
@.str.3 = [31 x i8] c"Couldn't get value for pin %d\0A\00"
define i32 @gpio_get_value(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca [8 x i8]
store i32 %0, i32* %3
store i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8** %4
store i32 0, i32* %5
store i32 -1, i32* %6
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%10 = load i8*, i8** %4
%11 = load i32, i32* %3
%12 = call i64 @sprintf(i8* %9, i8* %10, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* %6
store i32 %16, i32* %2
br label %55
17: ; preds = %1
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %40
23: ; preds = %17
%24 = load i32, i32* %3
%25 = call i64 @gpio_export(i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i32, i32* %6
store i32 %28, i32* %2
br label %55
29: ; preds = %23
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %29
%36 = load i32, i32* %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %6
store i32 %38, i32* %2
br label %55
39: ; preds = %29
br label %40
40: ; preds = %39, %17
%41 = load i32, i32* %5
%42 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 1)
%44 = load i32, i32* %6
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = load i32, i32* %3
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %6
store i32 %49, i32* %2
br label %55
50: ; preds = %40
%51 = load i32, i32* %5
%52 = call i32 @close(i32 %51)
%53 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%54 = call i32 @atoi(i8* %53)
store i32 %54, i32* %2
br label %55
55: ; preds = %50, %46, %35, %27, %14
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@O_RDONLY = global i32 0
@.str.2 = [36 x i8] c"Couldn't get value File for pin %d\0A\00"
@.str.3 = [31 x i8] c"Couldn't get value for pin %d\0A\00"
@str = [37 x i8] c"Can't create pin direction file path\00"
define i32 @gpio_get_value(i32 %0) {
%2 = alloca [256 x i8]
%3 = alloca [8 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%6 = call i64 @sprintf(i8* %4, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %32
10: ; preds = %1
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %4, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %10
%15 = call i64 @gpio_export(i32 %0)
%16 = icmp slt i64 %15, 0
br i1 %16, label %32, label %17
17: ; preds = %14
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %4, i32 %18)
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %32
23: ; preds = %17, %10
%24 = phi i32 [ %19, %17 ], [ %12, %10 ]
%25 = call i32 @read(i32 %24, i8* %5, i32 1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %32
29: ; preds = %23
%30 = call i32 @close(i32 %24)
%31 = call i32 @atoi(i8* %5)
br label %32
32: ; preds = %14, %29, %27, %21, %8
%33 = phi i32 [ -1, %8 ], [ -1, %21 ], [ -1, %27 ], [ %31, %29 ], [ -1, %14 ]
ret i32 %33
}