#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*/ ptr;
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
char *bbb_wildCardPath(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
int bbb_getGpioValue(int gpio) {
  static int ret = -1;
  int fd = -1;
  char path[128];
  char ptr[128];
  char *wpath = NULL;
  if (gpio > 0) {
    memset(path, 0, sizeof(path));
    snprintf(path, sizeof(path), "%s/gpio%d/value", "/sys/class/gpio", gpio);
    if ((wpath = bbb_wildCardPath(path)) != NULL) {
      if ((fd = open(wpath, O_RDONLY)) < 0) {
        return -1;
      }
      memset(ptr, 0, sizeof(ptr));
      if ((ret = read(fd, ptr, sizeof(ptr))) <= 0) {
        close(fd);
        return -1;
      }
      ret = atoi(ptr);
      close(fd);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<bbb_getGpioValue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movq   $0x0,-0x118(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1283 <bbb_getGpioValue+0x113>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    $0x80,%esi
lea    0xe3c(%rip),%rdx
lea    0xe45(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0x90(%rbp),%rdi
call   1090 <bbb_wildCardPath@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
je     127e <bbb_getGpioValue+0x10e>
mov    -0x118(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    121a <bbb_getGpioValue+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <bbb_getGpioValue+0x11c>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,0x2e11(%rip)
cmp    $0x0,%eax
jg     1264 <bbb_getGpioValue+0xf4>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <bbb_getGpioValue+0x11c>
lea    -0x110(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,0x2de2(%rip)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    1283 <bbb_getGpioValue+0x113>
mov    0x2dcf(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_getGpioValue>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
test   %edi,%edi
jle    1223 <bbb_getGpioValue+0xb3>
mov    %edi,%ebx
lea    0x80(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    0xe5d(%rip),%rdx
lea    0xe66(%rip),%rcx
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %ebx,%r8d
call   1030 <snprintf@plt>
mov    %rbp,%rdi
call   1090 <bbb_wildCardPath@plt>
test   %rax,%rax
je     1223 <bbb_getGpioValue+0xb3>
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1070 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1229 <bbb_getGpioValue+0xb9>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,0x2e4e(%rip)
test   %eax,%eax
jle    1237 <bbb_getGpioValue+0xc7>
mov    %rsp,%rdi
call   1080 <atoi@plt>
mov    %eax,0x2e3c(%rip)
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2e2f(%rip),%ebx
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1229 <bbb_getGpioValue+0xb9>

LLVM IR (-O0):
@bbb_getGpioValue.ret = internal global i32 -1
@.str = [16 x i8] c"%s/gpio%d/value\00"
@.str.1 = [16 x i8] c"/sys/class/gpio\00"
@O_RDONLY = global i32 0
define i32 @bbb_getGpioValue(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca [128 x i8]
%7 = alloca i8*
store i32 %0, i32* %3
store i32 -1, i32* %4
store i8* null, i8** %7
%8 = load i32, i32* %3
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %41
10: ; preds = %1
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 128)
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%14 = load i32, i32* %3
%15 = call i32 @snprintf(i8* %13, i32 128, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%17 = call i8* @bbb_wildCardPath(i8* %16)
store i8* %17, i8** %7
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %40
19: ; preds = %10
%20 = load i8*, i8** %7
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %2
br label %43
25: ; preds = %19
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 128)
%28 = load i32, i32* %4
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 128)
store i32 %30, i32* @bbb_getGpioValue.ret
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %2
br label %43
35: ; preds = %25
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* @bbb_getGpioValue.ret
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %35, %10
br label %41
41: ; preds = %40, %1
%42 = load i32, i32* @bbb_getGpioValue.ret
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %32, %24
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@bbb_getGpioValue.ret = internal unnamed_addr global i32 -1
@.str = [16 x i8] c"%s/gpio%d/value\00"
@.str.1 = [16 x i8] c"/sys/class/gpio\00"
@O_RDONLY = global i32 0
define i32 @bbb_getGpioValue(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %25
7: ; preds = %1
%8 = call i32 @memset(i8* %4, i32 0, i32 128)
%9 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%10 = call i8* @bbb_wildCardPath(i8* %4)
%11 = icmp eq i8* %10, null
br i1 %11, label %25, label %12
12: ; preds = %7
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %10, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %12
%17 = call i32 @memset(i8* %5, i32 0, i32 128)
%18 = call i32 @read(i32 %14, i8* %5, i32 128)
store i32 %18, i32* @bbb_getGpioValue.ret
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @close(i32 %14)
br label %27
22: ; preds = %16
%23 = call i32 @atoi(i8* %5)
store i32 %23, i32* @bbb_getGpioValue.ret
%24 = call i32 @close(i32 %14)
br label %25
25: ; preds = %7, %22, %1
%26 = load i32, i32* @bbb_getGpioValue.ret
br label %27
27: ; preds = %12, %25, %20
%28 = phi i32 [ -1, %20 ], [ %26, %25 ], [ -1, %12 ]
ret i32 %28
}