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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ gpio_export(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
int gpio_direction(char *gp, char *direction) {
  int fd = 0;
  int er = 0;
  char gpio_path[64];
  snprintf(gpio_path, 64, "/sys/class/gpio/gpio%s/direction", gp);
  do {
    fd = open(gpio_path, O_WRONLY);
    if (errno == ENOENT) {
      gpio_export(gp);
    } else if (fd == -1) {
      fprintf(stderr, "%s\n", strerror(errno));
      return 1;
    };
  } while (fd == -1);
  usleep(100000);
  er = write(fd, direction, 3);
  close(fd);
  if (er == -1) {
    fprintf(stderr, "%s\n", strerror(errno));
    return 1;
  };
  return 0;
}


x86 assembly (clang, -O0):
<gpio_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x40,%esi
lea    0xe4e(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
jne    11f4 <gpio_direction+0x74>
mov    -0x10(%rbp),%rdi
call   1050 <gpio_export@plt>
jmp    123c <gpio_direction+0xbc>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1237 <gpio_direction+0xb7>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xdfb(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bb <gpio_direction+0x13b>
jmp    123c <gpio_direction+0xbc>
jmp    1241 <gpio_direction+0xc1>
cmpl   $0xffffffff,-0x1c(%rbp)
je     11b7 <gpio_direction+0x37>
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmpl   $0xffffffff,-0x20(%rbp)
jne    12b4 <gpio_direction+0x134>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xd7e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bb <gpio_direction+0x13b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_direction>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%rbx
lea    0xe63(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x40,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
mov    0x2e07(%rip),%r13
mov    0x2e18(%rip),%r12
mov    0x2e01(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
mov    (%r12),%rdi
cmp    (%r14),%rdi
jne    11f6 <gpio_direction+0x76>
mov    %rbx,%rdi
call   1050 <gpio_export@plt>
cmp    $0xffffffff,%ebp
je     11d0 <gpio_direction+0x50>
jmp    11fb <gpio_direction+0x7b>
cmp    $0xffffffff,%ebp
je     122a <gpio_direction+0xaa>
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
mov    %ebp,%edi
mov    0x8(%rsp),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
cmp    $0xffffffff,%ebx
jne    124e <gpio_direction+0xce>
mov    (%r12),%rdi
mov    0x2daf(%rip),%rax
mov    (%rax),%ebx
call   1090 <strerror@plt>
lea    0xde2(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%s/direction\00"
@O_WRONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @gpio_direction(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 0, i32* %7
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%10 = load i8*, i8** %4
%11 = call i32 @snprintf(i8* %9, i32 64, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %32, %2
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOENT
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i8*, i8** %4
%21 = call i32 @gpio_export(i8* %20)
br label %31
22: ; preds = %12
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = load i64, i64* @errno
%28 = call i8* @strerror(i64 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 1, i32* %3
br label %50
30: ; preds = %22
br label %31
31: ; preds = %30, %19
br label %32
32: ; preds = %31
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %12, label %35
35: ; preds = %32
%36 = call i32 @usleep(i32 100000)
%37 = load i32, i32* %6
%38 = load i8*, i8** %5
%39 = call i32 @write(i32 %37, i8* %38, i32 3)
store i32 %39, i32* %7
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %35
%45 = load i32, i32* @stderr
%46 = load i64, i64* @errno
%47 = call i8* @strerror(i64 %46)
%48 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %47)
store i32 1, i32* %3
br label %50
49: ; preds = %35
store i32 0, i32* %3
br label %50
50: ; preds = %49, %44, %25
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%s/direction\00"
@O_WRONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @gpio_direction(i8* %0, i8* nocapture %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %6
6: ; preds = %12, %2
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* %4, i32 %7)
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = call i32 @gpio_export(i8* %0)
%14 = icmp eq i32 %8, -1
br i1 %14, label %6, label %17
15: ; preds = %6
%16 = icmp eq i32 %8, -1
br i1 %16, label %24, label %17
17: ; preds = %12, %15
%18 = call i32 @usleep(i32 100000)
%19 = call i32 @write(i32 %8, i8* %1, i32 3)
%20 = call i32 @close(i32 %8)
%21 = icmp eq i32 %19, -1
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = load i64, i64* @errno
br label %24
24: ; preds = %15, %22
%25 = phi i64 [ %23, %22 ], [ %9, %15 ]
%26 = load i32, i32* @stderr
%27 = call i8* @strerror(i64 %25)
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %24, %17
%30 = phi i32 [ 0, %17 ], [ 1, %24 ]
ret i32 %30
}