#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 open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int write(int, char *, int);
int gpio_direction(unsigned int gpio_number, int dir) {
  char buf[3 + 30];
  snprintf(buf, 3 + 30, "/sys/class/gpio/gpio%u/direction", gpio_number);
  int fd = open(buf, O_WRONLY);
  if (fd == -1) {
    return -1;
  }
  if (write(fd, dir == 0 ? "in" : "out", dir == 0 ? 2 : 3) != 1) {
    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    $0x21,%esi
lea    0xe9f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1191 <gpio_direction+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <gpio_direction+0xa8>
mov    -0x34(%rbp),%edi
mov    -0xc(%rbp),%ecx
lea    0xe86(%rip),%rsi
lea    0xe7c(%rip),%rax
cmp    $0x0,%ecx
cmove  %rax,%rsi
mov    -0xc(%rbp),%ecx
mov    $0x3,%edx
mov    $0x2,%eax
cmp    $0x0,%ecx
cmove  %eax,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     11d9 <gpio_direction+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <gpio_direction+0xa8>
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   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%r14d
mov    %edi,%ecx
lea    0xeac(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x21,%esi
call   1040 <snprintf@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11b3 <gpio_direction+0x73>
mov    %eax,%ebp
xor    %edx,%edx
test   %r14d,%r14d
sete   %dl
lea    0xe91(%rip),%rax
lea    0xe8d(%rip),%rsi
cmove  %rax,%rsi
xor    $0x3,%edx
mov    %ebp,%edi
call   1030 <write@plt>
cmp    $0x1,%eax
jne    11b3 <gpio_direction+0x73>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%u/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_direction(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [33 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [33 x i8], [33 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 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %33
17: ; preds = %2
%18 = load i32, i32* %7
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 0
%21 = zext i1 %20 to i64
%22 = select i1 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0)
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i32 2, i32 3
%27 = call i32 @write(i32 %18, i8* %22, i32 %26)
%28 = icmp ne i32 %27, 1
br i1 %28, label %29, label %30
29: ; preds = %17
store i32 -1, i32* %3
br label %33
30: ; preds = %17
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
store i32 0, i32* %3
br label %33
33: ; preds = %30, %29, %16
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%u/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_direction(i32 %0, i32 %1) {
%3 = alloca [33 x i8]
%4 = getelementptr inbounds [33 x i8], [33 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 33, 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 %17, label %9
9: ; preds = %2
%10 = icmp eq i32 %1, 0
%11 = select i1 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0)
%12 = select i1 %10, i32 2, i32 3
%13 = call i32 @write(i32 %7, i8* %11, i32 %12)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @close(i32 %7)
br label %17
17: ; preds = %9, %2, %15
%18 = phi i32 [ 0, %15 ], [ -1, %2 ], [ -1, %9 ]
ret i32 %18
}