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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
char *bbb_wildCardPath(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
scalar_t__ write(int, char *, int);
int bbb_setGpioDirection(int gpio, int direction) {
  static int ret = -1;
  int fd = -1;
  char path[128];
  char *ptr = "IN";
  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_WRONLY)) < 0) {
        return -1;
      }
      if (direction > 0) {
        ret = 3;
        ptr = "out";
      } else {
        ret = 2;
        ptr = "in";
      }
      if (write(fd, ptr, ret) < 0) {
        ret = -1;
      } else {
        ret = 0;
      }
      close(fd);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<bbb_setGpioDirection>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
lea    0xe81(%rip),%rax
mov    %rax,-0x98(%rbp)
movq   $0x0,-0xa0(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    129f <bbb_setGpioDirection+0x13f>
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    0xe3e(%rip),%rdx
lea    0xe4b(%rip),%rcx
call   1040 <snprintf@plt>
lea    -0x90(%rbp),%rdi
call   1080 <bbb_wildCardPath@plt>
mov    %rax,-0xa0(%rbp)
cmp    $0x0,%rax
je     129a <bbb_setGpioDirection+0x13a>
mov    -0xa0(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    121b <bbb_setGpioDirection+0xbb>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <bbb_setGpioDirection+0x148>
cmpl   $0x0,-0xc(%rbp)
jle    1242 <bbb_setGpioDirection+0xe2>
movl   $0x3,0x2e21(%rip)
lea    0xdf1(%rip),%rax
mov    %rax,-0x98(%rbp)
jmp    125a <bbb_setGpioDirection+0xfa>
movl   $0x2,0x2e04(%rip)
lea    0xdd8(%rip),%rax
mov    %rax,-0x98(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x98(%rbp),%rsi
mov    0x2de6(%rip),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1288 <bbb_setGpioDirection+0x128>
movl   $0xffffffff,0x2dcd(%rip)
ff ff ff
jmp    1292 <bbb_setGpioDirection+0x132>
movl   $0x0,0x2dbe(%rip)
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
jmp    129f <bbb_setGpioDirection+0x13f>
mov    0x2dab(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_setGpioDirection>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
test   %edi,%edi
jle    1207 <bbb_setGpioDirection+0xa7>
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe6f(%rip),%rdx
lea    0xe7c(%rip),%rcx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %ebp,%r8d
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1080 <bbb_wildCardPath@plt>
test   %rax,%rax
je     1207 <bbb_setGpioDirection+0xa7>
mov    0x2e1c(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1219 <bbb_setGpioDirection+0xb9>
mov    %eax,%ebp
xor    %edx,%edx
test   %r14d,%r14d
setg   %dl
lea    0xe49(%rip),%rax
lea    0xe46(%rip),%rsi
cmovg  %rax,%rsi
or     $0x2,%edx
mov    %edx,0x2e61(%rip)
mov    %ebp,%edi
call   1030 <write@plt>
sar    $0x3f,%rax
mov    %eax,0x2e50(%rip)
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2e43(%rip),%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    120d <bbb_setGpioDirection+0xad>

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

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