#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__ EINTR;
int /*<<< orphan*/ O_WRONLY;
int close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
scalar_t__ snprintf(char *, int, char *, int);
int write(int, char *, int);
int direction(int gpio, char output) {
  int fd;
  char fn[256];
  int rv = 0;
  if (snprintf(fn, 256, "/sys/class/gpio/gpio%i/direction", gpio) < 0) {
    return -1;
  }
  fd = open(fn, O_WRONLY);
  if (fd < 0) {
    return -2;
  }
  if (output) {
    if (write(fd, "out\n", 4) < 4) {
      rv = -3;
    }
  } else {
    if (write(fd, "in\n", 3) < 3) {
      rv = -3;
    }
  }
  do {
    rv = close(fd);
  } while ((rv < 0) && (errno == EINTR));
  return rv;
}


x86 assembly (clang, -O0):
<direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movl   $0x0,-0x114(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x100,%esi
lea    0xe8c(%rip),%rdx
call   1040 <snprintf@plt>
cmp    $0x0,%rax
jge    118f <direction+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    127b <direction+0x13b>
lea    -0x110(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11bd <direction+0x7d>
movl   $0xfffffffe,-0x4(%rbp)
jmp    127b <direction+0x13b>
cmpb   $0x0,-0x9(%rbp)
je     11f3 <direction+0xb3>
mov    -0x10(%rbp),%edi
lea    0xe50(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jge    11ee <direction+0xae>
movl   $0xfffffffd,-0x114(%rbp)
ff ff ff
jmp    121f <direction+0xdf>
mov    -0x10(%rbp),%edi
lea    0xe29(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0x3,%eax
jge    121a <direction+0xda>
movl   $0xfffffffd,-0x114(%rbp)
ff ff ff
jmp    121f <direction+0xdf>
jmp    1224 <direction+0xe4>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x114(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x114(%rbp)
mov    %al,-0x115(%rbp)
jge    1264 <direction+0x124>
mov    0x2d92(%rip),%rax
mov    (%rax),%rax
mov    0x2d80(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x115(%rbp)
mov    -0x115(%rbp),%al
test   $0x1,%al
jne    1224 <direction+0xe4>
mov    -0x114(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<direction>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xeaa(%rip),%rdx
mov    %rsp,%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
test   %rax,%rax
js     1194 <direction+0x54>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     119b <direction+0x5b>
mov    %eax,%ebx
test   %bpl,%bpl
je     11a2 <direction+0x62>
lea    0xe96(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
jmp    11b0 <direction+0x70>
mov    $0xffffffff,%eax
jmp    11e4 <direction+0xa4>
mov    $0xfffffffe,%eax
jmp    11e4 <direction+0xa4>
lea    0xe7d(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
mov    0x2e24(%rip),%r14
mov    0x2e15(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
call   1050 <close@plt>
test   %eax,%eax
jns    11e4 <direction+0xa4>
mov    0x0(%rbp),%rcx
cmp    %rcx,(%r14)
je     11d0 <direction+0x90>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%i/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"out\0A\00"
@.str.2 = [4 x i8] c"in\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @direction(i32 %0, i8 signext %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i64 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %25, label %7
7: ; preds = %2
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %25, label %11
11: ; preds = %7
%12 = icmp eq i8 %1, 0
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = call i32 @write(i32 %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
br label %17
15: ; preds = %11
%16 = call i32 @write(i32 %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %17
17: ; preds = %15, %13
br label %18
18: ; preds = %17, %18
%19 = call i32 @close(i32 %9)
%20 = icmp slt i32 %19, 0
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
%24 = select i1 %20, i1 %23, i1 false
br i1 %24, label %18, label %25
25: ; preds = %18, %7, %2
%26 = phi i32 [ -1, %2 ], [ -2, %7 ], [ %19, %18 ]
ret i32 %26
}