#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 */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char const *, int);
int gpio_direction(int pin, int dir) {
  static const char s_directions_str[] = "in\0out";
  char path[35];
  int fd;
  snprintf(path, 35, "/sys/class/gpio/gpio%d/direction", pin);
  fd = open(path, O_WRONLY);
  if (-1 == fd) {
    fprintf(stderr, "Failed to open gpio direction for writing!\n");
    return -1;
  }
  if (-1 == write(fd, &s_directions_str[0 == dir ? 0 : 3], 0 == dir ? 2 : 3)) {
    fprintf(stderr, "Failed to set direction!\n");
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x23,%esi
lea    0xe96(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    -0x34(%rbp),%eax
jne    11ba <gpio_direction+0x6a>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe7f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <gpio_direction+0xe2>
mov    -0x34(%rbp),%edi
mov    -0xc(%rbp),%edx
xor    %ecx,%ecx
mov    $0x3,%eax
cmp    %edx,%ecx
cmove  %ecx,%eax
cltq
lea    0xe2b(%rip),%rsi
add    %rax,%rsi
mov    -0xc(%rbp),%r8d
xor    %ecx,%ecx
mov    $0x3,%edx
mov    $0x2,%eax
cmp    %r8d,%ecx
cmove  %eax,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1223 <gpio_direction+0xd3>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <gpio_direction+0xe2>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_direction>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xea6(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x23,%esi
call   1040 <snprintf@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11bd <gpio_direction+0x6d>
mov    %eax,%ebx
xor    %edx,%edx
xor    %eax,%eax
test   %ebp,%ebp
sete   %dl
setne  %al
lea    (%rax,%rax,2),%rax
lea    0xe60(%rip),%rsi
add    %rax,%rsi
xor    $0x3,%edx
mov    %ebx,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11cf <gpio_direction+0x7f>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11e9 <gpio_direction+0x99>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
jmp    11df <gpio_direction+0x8f>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@gpio_direction.s_directions_str = internal constant [7 x i8] c"in\00out\00"
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [44 x i8] c"Failed to open gpio direction for writing!\0A\00"
@.str.2 = [26 x i8] c"Failed to set direction!\0A\00"
define i32 @gpio_direction(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [35 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [35 x i8], [35 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [35 x i8], [35 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %39
19: ; preds = %2
%20 = load i32, i32* %7
%21 = load i32, i32* %5
%22 = icmp eq i32 0, %21
%23 = zext i1 %22 to i64
%24 = select i1 %22, i32 0, i32 3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [7 x i8], [7 x i8]* @gpio_direction.s_directions_str, i64 0, i64 %25
%27 = load i32, i32* %5
%28 = icmp eq i32 0, %27
%29 = zext i1 %28 to i64
%30 = select i1 %28, i32 2, i32 3
%31 = call i32 @write(i32 %20, i8* %26, i32 %30)
%32 = icmp eq i32 -1, %31
br i1 %32, label %33, label %36
33: ; preds = %19
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %39
36: ; preds = %19
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
store i32 0, i32* %3
br label %39
39: ; preds = %36, %33, %16
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@gpio_direction.s_directions_str = internal constant [7 x i8] c"in\00out\00"
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [44 x i8] c"Failed to open gpio direction for writing!\0A\00"
@.str.2 = [26 x i8] c"Failed to set direction!\0A\00"
define i32 @gpio_direction(i32 %0, i32 %1) {
%3 = alloca [35 x i8]
%4 = getelementptr inbounds [35 x i8], [35 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* %4, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %24
12: ; preds = %2
%13 = icmp eq i32 %1, 0
%14 = select i1 %13, i64 0, i64 3
%15 = getelementptr inbounds [7 x i8], [7 x i8]* @gpio_direction.s_directions_str, i64 0, i64 %14
%16 = select i1 %13, i32 2, i32 3
%17 = call i32 @write(i32 %7, i8* %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %24
22: ; preds = %12
%23 = call i32 @close(i32 %7)
br label %24
24: ; preds = %22, %19, %9
%25 = phi i32 [ -1, %9 ], [ -1, %19 ], [ 0, %22 ]
ret i32 %25
}