#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 errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char const *);
int write(int, char const *, int /*<<< orphan*/);
int GPIO_direction(int pin, int dir) {
  const char *s_directions_str[2] = {"in", "out"};
  char path[34];
  int fd;
  int ret_err;
  snprintf(path, 34, "/sys/class/gpio/gpio%d/direction", pin);
  fd = open(path, O_WRONLY);
  if (-1 != fd) {
    const char *curr_dir_str;
    curr_dir_str = s_directions_str[0 != dir];
    if (-1 != write(fd, curr_dir_str, strlen(curr_dir_str)))
      ret_err = 0;
    else
      ret_err = errno;
    close(fd);
  } else
    ret_err = errno;
  return (ret_err);
}


x86 assembly (clang, -O0):
<GPIO_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2c9b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c98(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x22,%esi
lea    0xe80(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x54(%rbp)
mov    $0xffffffff,%eax
cmp    -0x54(%rbp),%eax
je     1220 <GPIO_direction+0xd0>
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
cltq
mov    -0x20(%rbp,%rax,8),%rax
mov    %rax,-0x60(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     1207 <GPIO_direction+0xb7>
movl   $0x0,-0x58(%rbp)
jmp    1213 <GPIO_direction+0xc3>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x54(%rbp),%edi
call   1060 <close@plt>
jmp    122c <GPIO_direction+0xdc>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
add    $0x70,%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    $0x22,%esi
call   1050 <snprintf@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11c8 <GPIO_direction+0x78>
mov    %eax,%ebx
xor    %eax,%eax
test   %ebp,%ebp
setne  %al
lea    0x2c69(%rip),%rcx
mov    (%rcx,%rax,8),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
jne    11bf <GPIO_direction+0x6f>
mov    0x2e23(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11d1 <GPIO_direction+0x81>
mov    0x2e11(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"in\00"
@.str.1 = [4 x i8] c"out\00"
@__const.GPIO_direction.s_directions_str = [2 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0)]
@.str.2 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@errno = global i32 0
define i32 @GPIO_direction(i32 %0, i32 %1) {
%3 = alloca [34 x i8]
%4 = getelementptr inbounds [34 x i8], [34 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, 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 %20, label %9
9: ; preds = %2
%10 = icmp ne i32 %1, 0
%11 = zext i1 %10 to i64
%12 = getelementptr inbounds [2 x i8*], [2 x i8*]* @__const.GPIO_direction.s_directions_str, i64 0, i64 %11
%13 = load i8*, i8** %12
%14 = call i32 @strlen(i8* %13)
%15 = call i32 @write(i32 %7, i8* %13, i32 %14)
%16 = icmp eq i32 %15, -1
%17 = load i32, i32* @errno
%18 = select i1 %16, i32 %17, i32 0
%19 = call i32 @close(i32 %7)
br label %22
20: ; preds = %2
%21 = load i32, i32* @errno
br label %22
22: ; preds = %20, %9
%23 = phi i32 [ %18, %9 ], [ %21, %20 ]
ret i32 %23
}