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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
unsigned int gpioToSysPin(unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ write(int, char *, int);
int gpio_set_direction(unsigned int gpio, unsigned int in_flag) {
  unsigned int gpioSys = gpioToSysPin(gpio);
  int fd;
  char filename[33];
  snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/direction",
           gpioSys);
  if (0)
    printf("\n /sys/class/gpio/gpio%d/direction \n", gpioSys);
  if ((fd = open(filename, O_WRONLY)) < 0) {
    return -1;
  }
  if (in_flag)
    write(fd, "in", 3);
  else
    write(fd, "out", 4);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <gpioToSysPin@plt>
mov    %eax,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%ecx
mov    $0x21,%esi
lea    0xe84(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ab <gpio_set_direction+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <gpio_set_direction+0xa1>
cmpl   $0x0,-0xc(%rbp)
je     11ce <gpio_set_direction+0x7e>
mov    -0x14(%rbp),%edi
lea    0xe62(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
jmp    11e2 <gpio_set_direction+0x92>
mov    -0x14(%rbp),%edi
lea    0xe4c(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x14(%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_set_direction>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
call   1060 <gpioToSysPin@plt>
lea    0xe9c(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x21,%esi
mov    %eax,%ecx
call   1040 <snprintf@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11a1 <gpio_set_direction+0x51>
mov    %eax,%ebp
test   %ebx,%ebx
je     11a8 <gpio_set_direction+0x58>
lea    0xe89(%rip),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
jmp    11b6 <gpio_set_direction+0x66>
mov    $0xffffffff,%eax
jmp    11c4 <gpio_set_direction+0x74>
lea    0xe75(%rip),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

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