#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;
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);
scalar_t__ strncmp(char *, char *, int);
int bbb_getGpioDirection(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/direction", "/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;
      }
      if (ret >= 2 && strncmp(ptr, "in", 2) == 0) {
        ret = 0;
      } else {
        if (ret >= 3 && strncmp(ptr, "out", 3) == 0) {
          ret = 1;
        } else {
          ret = -1;
        }
      }
      close(fd);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<bbb_getGpioDirection>:
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    12fc <bbb_getGpioDirection+0x18c>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    $0x80,%esi
lea    0xe3c(%rip),%rdx
lea    0xe49(%rip),%rcx
call   1040 <snprintf@plt>
lea    -0x90(%rbp),%rdi
call   1090 <bbb_wildCardPath@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
je     12f7 <bbb_getGpioDirection+0x187>
mov    -0x118(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    121a <bbb_getGpioDirection+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <bbb_getGpioDirection+0x195>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,0x2e11(%rip)
cmp    $0x0,%eax
jg     1264 <bbb_getGpioDirection+0xf4>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <bbb_getGpioDirection+0x195>
cmpl   $0x2,0x2ded(%rip)
jl     12a2 <bbb_getGpioDirection+0x132>
lea    -0x110(%rbp),%rdi
lea    0xda5(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12a2 <bbb_getGpioDirection+0x132>
movl   $0x0,0x2dbb(%rip)
jmp    12ef <bbb_getGpioDirection+0x17f>
cmpl   $0x3,0x2daf(%rip)
jl     12e0 <bbb_getGpioDirection+0x170>
lea    -0x110(%rbp),%rdi
lea    0xd6a(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12e0 <bbb_getGpioDirection+0x170>
movl   $0x1,0x2d7d(%rip)
jmp    12ea <bbb_getGpioDirection+0x17a>
movl   $0xffffffff,0x2d6e(%rip)
ff ff ff
jmp    12ef <bbb_getGpioDirection+0x17f>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
jmp    12fc <bbb_getGpioDirection+0x18c>
mov    0x2d56(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_getGpioDirection>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
test   %edi,%edi
jle    1268 <bbb_getGpioDirection+0xf8>
mov    %edi,%ebx
lea    0x80(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe5d(%rip),%rdx
lea    0xe6a(%rip),%rcx
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %ebx,%r8d
call   1040 <snprintf@plt>
mov    %rbp,%rdi
call   1090 <bbb_wildCardPath@plt>
test   %rax,%rax
je     1268 <bbb_getGpioDirection+0xf8>
mov    0x2e16(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1080 <open@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     126e <bbb_getGpioDirection+0xfe>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,0x2e46(%rip)
test   %eax,%eax
jle    127c <bbb_getGpioDirection+0x10c>
cmp    $0x1,%eax
je     1256 <bbb_getGpioDirection+0xe6>
lea    0xe02(%rip),%rsi
mov    %rsp,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1285 <bbb_getGpioDirection+0x115>
cmpl   $0x3,0x2e1d(%rip)
jl     1256 <bbb_getGpioDirection+0xe6>
lea    0xde3(%rip),%rsi
mov    %rsp,%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1289 <bbb_getGpioDirection+0x119>
mov    $0xffffffff,%eax
mov    %eax,0x2df7(%rip)
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2dea(%rip),%ebp
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebx,%edi
call   1060 <close@plt>
jmp    126e <bbb_getGpioDirection+0xfe>
xor    %eax,%eax
jmp    125b <bbb_getGpioDirection+0xeb>
mov    $0x1,%eax
jmp    125b <bbb_getGpioDirection+0xeb>

LLVM IR (-O0):
@bbb_getGpioDirection.ret = internal global i32 -1
@.str = [20 x i8] c"%s/gpio%d/direction\00"
@.str.1 = [16 x i8] c"/sys/class/gpio\00"
@O_RDONLY = global i32 0
@.str.2 = [3 x i8] c"in\00"
@.str.3 = [4 x i8] c"out\00"
define i32 @bbb_getGpioDirection(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 %57
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 ([20 x i8], [20 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 %56
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 %59
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_getGpioDirection.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 %59
35: ; preds = %25
%36 = load i32, i32* @bbb_getGpioDirection.ret
%37 = icmp sge i32 %36, 2
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%40 = call i64 @strncmp(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
store i32 0, i32* @bbb_getGpioDirection.ret
br label %53
43: ; preds = %38, %35
%44 = load i32, i32* @bbb_getGpioDirection.ret
%45 = icmp sge i32 %44, 3
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%48 = call i64 @strncmp(i8* %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 1, i32* @bbb_getGpioDirection.ret
br label %52
51: ; preds = %46, %43
store i32 -1, i32* @bbb_getGpioDirection.ret
br label %52
52: ; preds = %51, %50
br label %53
53: ; preds = %52, %42
%54 = load i32, i32* %4
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %53, %10
br label %57
57: ; preds = %56, %1
%58 = load i32, i32* @bbb_getGpioDirection.ret
store i32 %58, i32* %2
br label %59
59: ; preds = %57, %32, %24
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
@bbb_getGpioDirection.ret = internal unnamed_addr global i32 -1
@.str = [20 x i8] c"%s/gpio%d/direction\00"
@.str.1 = [16 x i8] c"/sys/class/gpio\00"
@O_RDONLY = global i32 0
@.str.2 = [3 x i8] c"in\00"
@.str.3 = [4 x i8] c"out\00"
define i32 @bbb_getGpioDirection(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 %37
7: ; preds = %1
%8 = call i32 @memset(i8* %4, i32 0, i32 128)
%9 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([20 x i8], [20 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 %37, 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 %39, 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_getGpioDirection.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 %39
22: ; preds = %16
%23 = icmp eq i32 %18, 1
br i1 %23, label %33, label %24
24: ; preds = %22
%25 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%26 = icmp eq i64 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %24
%28 = load i32, i32* @bbb_getGpioDirection.ret
%29 = icmp sgt i32 %28, 2
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %22, %30, %27
br label %34
34: ; preds = %30, %24, %33
%35 = phi i32 [ -1, %33 ], [ 0, %24 ], [ 1, %30 ]
store i32 %35, i32* @bbb_getGpioDirection.ret
%36 = call i32 @close(i32 %14)
br label %37
37: ; preds = %7, %34, %1
%38 = load i32, i32* @bbb_getGpioDirection.ret
br label %39
39: ; preds = %12, %37, %20
%40 = phi i32 [ -1, %20 ], [ %38, %37 ], [ -1, %12 ]
ret i32 %40
}