#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;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char const *, int);
int gpioEnable(int gpio, const char *direction) {
  int fd = open("/sys/class/gpio/export", O_WRONLY, 0);
  if (fd > -1) {
    char buffer[16];
    snprintf(buffer, sizeof(buffer), "%d\n", gpio);
    write(fd, buffer, strlen(buffer));
    close(fd);
  }
  char path[PATH_MAX];
  snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", gpio);
  int i = 20;
  while (1) {
    fd = open(path, O_WRONLY, 0);
    if (fd < 0) {
      usleep(20000);
      if (i-- < 0) {
        close(fd);
        return -1;
      }
    } else {
      write(fd, direction, strlen(direction));
      write(fd, "\n", 1);
      close(fd);
      break;
    }
  }
  snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", gpio);
  fd = -1;
  if (strncmp("out", direction, sizeof("out")) == 0) {
    fd = open(path, O_WRONLY, 0);
  }
  if (strncmp("in", direction, sizeof("in")) == 0) {
    fd = open(path, O_RDONLY, O_NONBLOCK);
  }
  if (fd < 0) {
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<gpioEnable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jle    11eb <gpioEnable+0x7b>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x10,%esi
lea    0xe5e(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%ecx
lea    0xdf4(%rip),%rdx
call   1060 <snprintf@plt>
movl   $0x14,-0x44(%rbp)
jmp    1238 <gpioEnable+0xc8>
mov    -0x60(%rbp),%rdi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1297 <gpioEnable+0x127>
mov    $0x4e20,%edi
call   1090 <usleep@plt>
mov    -0x44(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x44(%rbp)
cmp    $0x0,%eax
jge    1292 <gpioEnable+0x122>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    139f <gpioEnable+0x22f>
jmp    12dd <gpioEnable+0x16d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
lea    0xd53(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
jmp    12e2 <gpioEnable+0x172>
jmp    1233 <gpioEnable+0xc3>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%ecx
lea    0xd46(%rip),%rdx
call   1060 <snprintf@plt>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xd4c(%rip),%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1338 <gpioEnable+0x1c8>
mov    -0x60(%rbp),%rdi
mov    0x2c94(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xd1a(%rip),%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1375 <gpioEnable+0x205>
mov    -0x60(%rbp),%rdi
mov    0x2c76(%rip),%rax
mov    (%rax),%esi
mov    0x2c75(%rip),%rax
mov    (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1392 <gpioEnable+0x222>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    139f <gpioEnable+0x22f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpioEnable>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,-0x38(%rbp)
mov    %edi,%r15d
mov    0x2e31(%rip),%r14
mov    (%r14),%esi
lea    0xe67(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
js     11dc <gpioEnable+0x6c>
mov    %eax,%ebx
lea    0xe6a(%rip),%rdx
lea    -0x50(%rbp),%r12
mov    %r12,%rdi
mov    $0x10,%esi
mov    %r15d,%ecx
call   1060 <snprintf@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r13
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    0xe1a(%rip),%rdx
mov    %r13,%rdi
mov    %r12d,%esi
mov    %r15d,-0x2c(%rbp)
mov    %r15d,%ecx
call   1060 <snprintf@plt>
mov    $0x15,%r15d
nopl   0x0(%rax)
mov    (%r14),%esi
mov    %r13,%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1254 <gpioEnable+0xe4>
mov    $0x4e20,%edi
call   1090 <usleep@plt>
add    $0xffffffff,%r15d
jns    1220 <gpioEnable+0xb0>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
jmp    1307 <gpioEnable+0x197>
mov    -0x38(%rbp),%r15
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xda6(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xdaf(%rip),%rdx
mov    %r13,%rdi
mov    %r12d,%esi
mov    -0x2c(%rbp),%ecx
call   1060 <snprintf@plt>
lea    0xdb7(%rip),%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    12c8 <gpioEnable+0x158>
mov    (%r14),%esi
mov    %r13,%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%ebx
lea    0xd8e(%rip),%rdi
mov    %r15,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    12fd <gpioEnable+0x18d>
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
mov    0x2cef(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %ebx,%ebx
mov    $0xffffffff,%eax
cmovns %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@PATH_MAX = global i32 0
@.str.2 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.5 = [4 x i8] c"out\00"
@.str.6 = [3 x i8] c"in\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
define i32 @gpioEnable(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [16 x i8]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %12, i32 0)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, -1
br i1 %15, label %16, label %27
16: ; preds = %2
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%18 = load i32, i32* %4
%19 = call i32 @snprintf(i8* %17, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %6
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @write(i32 %20, i8* %21, i32 %23)
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
br label %27
27: ; preds = %16, %2
%28 = load i32, i32* @PATH_MAX
%29 = zext i32 %28 to i64
%30 = call i8* @llvm.stacksave()
store i8* %30, i8** %8
%31 = alloca i8, i64 %29
store i64 %29, i64* %9
%32 = trunc i64 %29 to i32
%33 = load i32, i32* %4
%34 = call i32 @snprintf(i8* %31, i32 %32, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 20, i32* %10
br label %35
35: ; preds = %60, %27
br label %36
36: ; preds = %35
%37 = load i32, i32* @O_WRONLY
%38 = call i32 @open(i8* %31, i32 %37, i32 0)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %36
%42 = call i32 @usleep(i32 20000)
%43 = load i32, i32* %10
%44 = add nsw i32 %43, -1
store i32 %44, i32* %10
%45 = icmp slt i32 %43, 0
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
store i32 -1, i32* %3
store i32 1, i32* %11
br label %85
49: ; preds = %41
br label %60
50: ; preds = %36
%51 = load i32, i32* %6
%52 = load i8*, i8** %5
%53 = load i8*, i8** %5
%54 = call i32 @strlen(i8* %53)
%55 = call i32 @write(i32 %51, i8* %52, i32 %54)
%56 = load i32, i32* %6
%57 = call i32 @write(i32 %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
br label %61
60: ; preds = %49
br label %35
61: ; preds = %50
%62 = trunc i64 %29 to i32
%63 = load i32, i32* %4
%64 = call i32 @snprintf(i8* %31, i32 %62, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %63)
store i32 -1, i32* %6
%65 = load i8*, i8** %5
%66 = call i64 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %65, i32 4)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %61
%69 = load i32, i32* @O_WRONLY
%70 = call i32 @open(i8* %31, i32 %69, i32 0)
store i32 %70, i32* %6
br label %71
71: ; preds = %68, %61
%72 = load i8*, i8** %5
%73 = call i64 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %72, i32 3)
%74 = icmp eq i64 %73, 0
br i1 %74, label %75, label %79
75: ; preds = %71
%76 = load i32, i32* @O_RDONLY
%77 = load i32, i32* @O_NONBLOCK
%78 = call i32 @open(i8* %31, i32 %76, i32 %77)
store i32 %78, i32* %6
br label %79
79: ; preds = %75, %71
%80 = load i32, i32* %6
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %83
82: ; preds = %79
store i32 -1, i32* %3
store i32 1, i32* %11
br label %85
83: ; preds = %79
%84 = load i32, i32* %6
store i32 %84, i32* %3
store i32 1, i32* %11
br label %85
85: ; preds = %83, %82, %46
%86 = load i8*, i8** %8
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@PATH_MAX = global i32 0
@.str.2 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [29 x i8] c"/sys/class/gpio/gpio%d/value\00"
@.str.5 = [4 x i8] c"out\00"
@.str.6 = [3 x i8] c"in\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
define i32 @gpioEnable(i32 %0, i8* %1) {
%3 = alloca [16 x i8]
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %4, i32 0)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%9 = call i32 @snprintf(i8* %8, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%10 = call i32 @strlen(i8* %8)
%11 = call i32 @write(i32 %5, i8* %8, i32 %10)
%12 = call i32 @close(i32 %5)
br label %13
13: ; preds = %7, %2
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = alloca i8, i64 %15
%17 = call i32 @snprintf(i8* %16, i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %18
18: ; preds = %23, %13
%19 = phi i32 [ 20, %13 ], [ %25, %23 ]
%20 = load i32, i32* @O_WRONLY
%21 = call i32 @open(i8* %16, i32 %20, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = call i32 @usleep(i32 20000)
%25 = add nsw i32 %19, -1
%26 = icmp slt i32 %19, 0
br i1 %26, label %27, label %18
27: ; preds = %23
%28 = call i32 @close(i32 %21)
br label %52
29: ; preds = %18
%30 = call i32 @strlen(i8* %1)
%31 = call i32 @write(i32 %21, i8* %1, i32 %30)
%32 = call i32 @write(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%33 = call i32 @close(i32 %21)
%34 = call i32 @snprintf(i8* %16, i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%35 = call i64 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %1, i32 4)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %29
%38 = load i32, i32* @O_WRONLY
%39 = call i32 @open(i8* %16, i32 %38, i32 0)
br label %40
40: ; preds = %37, %29
%41 = phi i32 [ %39, %37 ], [ -1, %29 ]
%42 = call i64 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %1, i32 3)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = load i32, i32* @O_RDONLY
%46 = load i32, i32* @O_NONBLOCK
%47 = call i32 @open(i8* %16, i32 %45, i32 %46)
br label %48
48: ; preds = %44, %40
%49 = phi i32 [ %47, %44 ], [ %41, %40 ]
%50 = icmp sgt i32 %49, -1
%51 = select i1 %50, i32 %49, i32 -1
br label %52
52: ; preds = %48, %27
%53 = phi i32 [ -1, %27 ], [ %51, %48 ]
ret i32 %53
}