#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_WRONLY;
char *bbb_wildCardPath(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int snprintf(char *, int, char *, ...);
scalar_t__ write(int, char *, int);
int bbb_enableGpio(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", "/sys/class/gpio", gpio);
    if ((wpath = bbb_wildCardPath(path)) == NULL) {
      memset(path, 0, sizeof(path));
      snprintf(path, sizeof(path), "%s/export", "/sys/class/gpio");
      if ((wpath = bbb_wildCardPath(path)) != NULL) {
        if ((fd = open(wpath, O_WRONLY)) < 0) {
          return -1;
        }
        printf("Enabling GPIO(%d)\n", gpio);
        ;
        memset(ptr, 0, sizeof(ptr));
        ret = snprintf(ptr, sizeof(ptr), "%d", gpio);
        if (write(fd, ptr, ret) < 0) {
          ret = -1;
        } else {
          ret = 0;
        }
        close(fd);
      }
    } else {
      return 0;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<bbb_enableGpio>:
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    1308 <bbb_enableGpio+0x198>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    $0x80,%esi
lea    0xe3c(%rip),%rdx
lea    0xe3f(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x90(%rbp),%rdi
call   1090 <bbb_wildCardPath@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
jne    12f7 <bbb_enableGpio+0x187>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    -0x90(%rbp),%rdi
mov    $0x80,%esi
lea    0xe05(%rip),%rdx
lea    0xdee(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x90(%rbp),%rdi
call   1090 <bbb_wildCardPath@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
je     12f2 <bbb_enableGpio+0x182>
mov    -0x118(%rbp),%rdi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    126d <bbb_enableGpio+0xfd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1311 <bbb_enableGpio+0x1a1>
mov    -0x8(%rbp),%esi
lea    0xdad(%rip),%rdi
call   1040 <printf@plt>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x80,%esi
lea    0xd62(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,0x2da6(%rip)
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    0x2d96(%rip),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12e0 <bbb_enableGpio+0x170>
movl   $0xffffffff,0x2d7d(%rip)
ff ff ff
jmp    12ea <bbb_enableGpio+0x17a>
movl   $0x0,0x2d6e(%rip)
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
jmp    1303 <bbb_enableGpio+0x193>
movl   $0x0,-0x4(%rbp)
jmp    1311 <bbb_enableGpio+0x1a1>
jmp    1308 <bbb_enableGpio+0x198>
mov    0x2d4a(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_enableGpio>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
test   %edi,%edi
jle    1280 <bbb_enableGpio+0x110>
mov    %edi,%r14d
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    0xe5f(%rip),%rdx
lea    0xe62(%rip),%rcx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14d,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1090 <bbb_wildCardPath@plt>
test   %rax,%rax
jne    1286 <bbb_enableGpio+0x116>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    0xe36(%rip),%rdx
lea    0xe1f(%rip),%rcx
mov    %rbx,%rdi
mov    $0x80,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1090 <bbb_wildCardPath@plt>
test   %rax,%rax
je     1280 <bbb_enableGpio+0x110>
mov    0x2dca(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1294 <bbb_enableGpio+0x124>
mov    %eax,%ebp
lea    0xdff(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
lea    0x80(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    0xdbc(%rip),%rdx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %eax,0x2df5(%rip)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
sar    $0x3f,%rax
mov    %eax,0x2ddf(%rip)
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2dd2(%rip),%ebp
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    1286 <bbb_enableGpio+0x116>

LLVM IR (-O0):
@bbb_enableGpio.ret = internal global i32 -1
@.str = [10 x i8] c"%s/gpio%d\00"
@.str.1 = [16 x i8] c"/sys/class/gpio\00"
@.str.2 = [10 x i8] c"%s/export\00"
@O_WRONLY = global i32 0
@.str.3 = [19 x i8] c"Enabling GPIO(%d)\0A\00"
@.str.4 = [3 x i8] c"%d\00"
define i32 @bbb_enableGpio(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 %54
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 (i8*, i32, i8*, ...) @snprintf(i8* %13, i32 128, i8* getelementptr inbounds ([10 x i8], [10 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 eq i8* %17, null
br i1 %18, label %19, label %52
19: ; preds = %10
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 128)
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%23 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %22, i32 128, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%25 = call i8* @bbb_wildCardPath(i8* %24)
store i8* %25, i8** %7
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %51
27: ; preds = %19
%28 = load i8*, i8** %7
%29 = load i32, i32* @O_WRONLY
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 -1, i32* %2
br label %56
33: ; preds = %27
%34 = load i32, i32* %3
%35 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%37 = call i32 @memset(i8* %36, i32 0, i32 128)
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%39 = load i32, i32* %3
%40 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %38, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %39)
store i32 %40, i32* @bbb_enableGpio.ret
%41 = load i32, i32* %4
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%43 = load i32, i32* @bbb_enableGpio.ret
%44 = call i64 @write(i32 %41, i8* %42, i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %33
store i32 -1, i32* @bbb_enableGpio.ret
br label %48
47: ; preds = %33
store i32 0, i32* @bbb_enableGpio.ret
br label %48
48: ; preds = %47, %46
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %19
br label %53
52: ; preds = %10
store i32 0, i32* %2
br label %56
53: ; preds = %51
br label %54
54: ; preds = %53, %1
%55 = load i32, i32* @bbb_enableGpio.ret
store i32 %55, i32* %2
br label %56
56: ; preds = %54, %52, %32
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
@bbb_enableGpio.ret = internal unnamed_addr global i32 -1
@.str = [10 x i8] c"%s/gpio%d\00"
@.str.1 = [16 x i8] c"/sys/class/gpio\00"
@.str.2 = [10 x i8] c"%s/export\00"
@O_WRONLY = global i32 0
@.str.3 = [19 x i8] c"Enabling GPIO(%d)\0A\00"
@.str.4 = [3 x i8] c"%d\00"
define i32 @bbb_enableGpio(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 %29
7: ; preds = %1
%8 = call i32 @memset(i8* %4, i32 0, i32 128)
%9 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([10 x i8], [10 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 %12, label %31
12: ; preds = %7
%13 = call i32 @memset(i8* %4, i32 0, i32 128)
%14 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%15 = call i8* @bbb_wildCardPath(i8* %4)
%16 = icmp eq i8* %15, null
br i1 %16, label %29, label %17
17: ; preds = %12
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* %15, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%23 = call i32 @memset(i8* %5, i32 0, i32 128)
%24 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %0)
store i32 %24, i32* @bbb_enableGpio.ret
%25 = call i64 @write(i32 %19, i8* %5, i32 %24)
%26 = ashr i64 %25, 63
%27 = trunc i64 %26 to i32
store i32 %27, i32* @bbb_enableGpio.ret
%28 = call i32 @close(i32 %19)
br label %29
29: ; preds = %21, %12, %1
%30 = load i32, i32* @bbb_enableGpio.ret
br label %31
31: ; preds = %7, %17, %29
%32 = phi i32 [ %30, %29 ], [ -1, %17 ], [ 0, %7 ]
ret i32 %32
}