#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 snprintf(char *, int, char *, ...);
scalar_t__ write(int, char *, int);
int bbb_setGpioValue(int gpio, int value) {
  static int ret = -1;
  int fd = -1;
  char path[128];
  char ptr[128];
  char *wpath = NULL;
  if (gpio > 0 && value >= 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_WRONLY)) < 0) {
        return -1;
      }
      memset(ptr, 0, sizeof(ptr));
      ret = snprintf(ptr, sizeof(ptr), "%d", (value > 0) ? 1 : 0);
      if (write(fd, ptr, ret) < 0) {
        ret = -1;
      } else {
        ret = 0;
      }
      close(fd);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<bbb_setGpioValue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movq   $0x0,-0x118(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    12a1 <bbb_setGpioValue+0x141>
cmpl   $0x0,-0xc(%rbp)
jl     12a1 <bbb_setGpioValue+0x141>
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    0xe3f(%rip),%rdx
lea    0xe48(%rip),%rcx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x90(%rbp),%rdi
call   1080 <bbb_wildCardPath@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
je     129c <bbb_setGpioValue+0x13c>
mov    -0x118(%rbp),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1219 <bbb_setGpioValue+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    12aa <bbb_setGpioValue+0x14a>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %ecx,%ecx
mov    $0x1,%eax
cmp    $0x0,%edx
cmovg  %eax,%ecx
mov    $0x80,%esi
lea    0xdd1(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,0x2df4(%rip)
mov    -0x10(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    0x2de4(%rip),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    128a <bbb_setGpioValue+0x12a>
movl   $0xffffffff,0x2dcb(%rip)
ff ff ff
jmp    1294 <bbb_setGpioValue+0x134>
movl   $0x0,0x2dbc(%rip)
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
jmp    12a1 <bbb_setGpioValue+0x141>
mov    0x2da9(%rip),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbb_setGpioValue>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
test   %edi,%edi
jle    122c <bbb_setGpioValue+0xcc>
mov    %esi,%ebx
test   %esi,%esi
js     122c <bbb_setGpioValue+0xcc>
mov    %edi,%ebp
lea    0x80(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
lea    0xe63(%rip),%rdx
lea    0xe6c(%rip),%rcx
mov    %r14,%rdi
mov    $0x80,%esi
mov    %ebp,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %r14,%rdi
call   1080 <bbb_wildCardPath@plt>
test   %rax,%rax
je     122c <bbb_setGpioValue+0xcc>
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     123e <bbb_setGpioValue+0xde>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1050 <memset@plt>
xor    %ecx,%ecx
test   %ebx,%ebx
setne  %cl
lea    0xe26(%rip),%rdx
mov    %r14,%rdi
mov    $0x80,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %eax,0x2e41(%rip)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
sar    $0x3f,%rax
mov    %eax,0x2e2b(%rip)
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2e1e(%rip),%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1232 <bbb_setGpioValue+0xd2>

LLVM IR (-O0):
@bbb_setGpioValue.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_WRONLY = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @bbb_setGpioValue(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [128 x i8]
%8 = alloca [128 x i8]
%9 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 -1, i32* %6
store i8* null, i8** %9
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %50
12: ; preds = %2
%13 = load i32, i32* %5
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %50
15: ; preds = %12
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%17 = call i32 @memset(i8* %16, i32 0, i32 128)
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%19 = load i32, i32* %4
%20 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %18, 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 %19)
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%22 = call i8* @bbb_wildCardPath(i8* %21)
store i8* %22, i8** %9
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %49
24: ; preds = %15
%25 = load i8*, i8** %9
%26 = load i32, i32* @O_WRONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 -1, i32* %3
br label %52
30: ; preds = %24
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%32 = call i32 @memset(i8* %31, i32 0, i32 128)
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%34 = load i32, i32* %5
%35 = icmp sgt i32 %34, 0
%36 = zext i1 %35 to i64
%37 = select i1 %35, i32 1, i32 0
%38 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %33, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %37)
store i32 %38, i32* @bbb_setGpioValue.ret
%39 = load i32, i32* %6
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%41 = load i32, i32* @bbb_setGpioValue.ret
%42 = call i64 @write(i32 %39, i8* %40, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %30
store i32 -1, i32* @bbb_setGpioValue.ret
br label %46
45: ; preds = %30
store i32 0, i32* @bbb_setGpioValue.ret
br label %46
46: ; preds = %45, %44
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %46, %15
br label %50
50: ; preds = %49, %12, %2
%51 = load i32, i32* @bbb_setGpioValue.ret
store i32 %51, i32* %3
br label %52
52: ; preds = %50, %29
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@bbb_setGpioValue.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_WRONLY = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @bbb_setGpioValue(i32 %0, i32 %1) {
%3 = alloca [128 x i8]
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %0, 0
%8 = icmp sgt i32 %1, -1
%9 = and i1 %7, %8
br i1 %9, label %10, label %28
10: ; preds = %2
%11 = call i32 @memset(i8* %5, i32 0, i32 128)
%12 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, 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)
%13 = call i8* @bbb_wildCardPath(i8* %5)
%14 = icmp eq i8* %13, null
br i1 %14, label %28, label %15
15: ; preds = %10
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* %13, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %30, label %19
19: ; preds = %15
%20 = call i32 @memset(i8* %6, i32 0, i32 128)
%21 = icmp ne i32 %1, 0
%22 = zext i1 %21 to i32
%23 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i32 %23, i32* @bbb_setGpioValue.ret
%24 = call i64 @write(i32 %17, i8* %6, i32 %23)
%25 = ashr i64 %24, 63
%26 = trunc i64 %25 to i32
store i32 %26, i32* @bbb_setGpioValue.ret
%27 = call i32 @close(i32 %17)
br label %28
28: ; preds = %10, %19, %2
%29 = load i32, i32* @bbb_setGpioValue.ret
br label %30
30: ; preds = %15, %28
%31 = phi i32 [ %29, %28 ], [ -1, %15 ]
ret i32 %31
}