#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_disableGpio(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/unexport", "/sys/class/gpio");
      if ((wpath = bbb_wildCardPath(path)) != NULL) {
        if ((fd = open(wpath, O_WRONLY)) < 0) {
          return -1;
        }
        printf("Disabling 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);
      }
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<bbb_disableGpio>:
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_disableGpio+0x18c>
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
je     12f7 <bbb_disableGpio+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_disableGpio+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_disableGpio+0xfd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <bbb_disableGpio+0x195>
mov    -0x8(%rbp),%esi
lea    0xdaf(%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_disableGpio+0x170>
movl   $0xffffffff,0x2d7d(%rip)
ff ff ff
jmp    12ea <bbb_disableGpio+0x17a>
movl   $0x0,0x2d6e(%rip)
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
jmp    12f7 <bbb_disableGpio+0x187>
jmp    12fc <bbb_disableGpio+0x18c>
mov    0x2d56(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_disableGpio>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
test   %edi,%edi
jle    127b <bbb_disableGpio+0x10b>
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    0xe62(%rip),%rdx
lea    0xe65(%rip),%rcx
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %ebx,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbp,%rdi
call   1090 <bbb_wildCardPath@plt>
test   %rax,%rax
je     127b <bbb_disableGpio+0x10b>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    0xe39(%rip),%rdx
lea    0xe22(%rip),%rcx
mov    %rbp,%rdi
mov    $0x80,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbp,%rdi
call   1090 <bbb_wildCardPath@plt>
test   %rax,%rax
je     127b <bbb_disableGpio+0x10b>
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     128d <bbb_disableGpio+0x11d>
mov    %eax,%ebp
lea    0xe04(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0x80(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    0xdc0(%rip),%rdx
mov    %r14,%rdi
mov    $0x80,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %eax,0x2dfa(%rip)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
sar    $0x3f,%rax
mov    %eax,0x2de4(%rip)
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2dd7(%rip),%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1281 <bbb_disableGpio+0x111>

LLVM IR (-O0):
@bbb_disableGpio.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 = [12 x i8] c"%s/unexport\00"
@O_WRONLY = global i32 0
@.str.3 = [20 x i8] c"Disabling GPIO(%d)\0A\00"
@.str.4 = [3 x i8] c"%d\00"
define i32 @bbb_disableGpio(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 %53
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 ne 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 ([12 x i8], [12 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 %55
33: ; preds = %27
%34 = load i32, i32* %3
%35 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 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_disableGpio.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_disableGpio.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_disableGpio.ret
br label %48
47: ; preds = %33
store i32 0, i32* @bbb_disableGpio.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 %52
52: ; preds = %51, %10
br label %53
53: ; preds = %52, %1
%54 = load i32, i32* @bbb_disableGpio.ret
store i32 %54, i32* %2
br label %55
55: ; preds = %53, %32
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
@bbb_disableGpio.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 = [12 x i8] c"%s/unexport\00"
@O_WRONLY = global i32 0
@.str.3 = [20 x i8] c"Disabling GPIO(%d)\0A\00"
@.str.4 = [3 x i8] c"%d\00"
define i32 @bbb_disableGpio(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 %29, label %12
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 ([12 x i8], [12 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 ([20 x i8], [20 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_disableGpio.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_disableGpio.ret
%28 = call i32 @close(i32 %19)
br label %29
29: ; preds = %7, %21, %12, %1
%30 = load i32, i32* @bbb_disableGpio.ret
br label %31
31: ; preds = %17, %29
%32 = phi i32 [ %30, %29 ], [ -1, %17 ]
ret i32 %32
}