#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*/ fputs(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int gpio_set_direction(int pin, int direction) {
  char *direction_str;
  int direction_length;
  char path[35];
  int fd;
  snprintf(path, 35, "/sys/class/gpio/gpio%d/direction", pin);
  fd = open(path, O_WRONLY);
  if (fd == -1) {
    fputs("Failed to open gpio direction for writing!\n", stderr);
    return -1;
  }
  if (direction == 0) {
    direction_str = "in";
    direction_length = 2;
  } else if (direction == 1) {
    direction_str = "out";
    direction_length = 3;
  } else {
    fputs("error: invalid direction\n", stderr);
    return -1;
  }
  if (write(fd, direction_str, direction_length) == -1) {
    fputs("Failed to set direction!\n", stderr);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x23,%esi
lea    0xe8f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    11b6 <gpio_set_direction+0x66>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1050 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <gpio_set_direction+0x116>
cmpl   $0x0,-0xc(%rbp)
jne    11d7 <gpio_set_direction+0x87>
lea    0xe86(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
jmp    121e <gpio_set_direction+0xce>
cmpl   $0x1,-0xc(%rbp)
jne    11f8 <gpio_set_direction+0xa8>
lea    0xe68(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x3,-0x1c(%rbp)
jmp    1219 <gpio_set_direction+0xc9>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1050 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <gpio_set_direction+0x116>
jmp    121e <gpio_set_direction+0xce>
mov    -0x44(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1257 <gpio_set_direction+0x107>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
lea    0xe28(%rip),%rdi
call   1050 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <gpio_set_direction+0x116>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_direction>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
lea    0xe9f(%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     11a0 <gpio_set_direction+0x50>
mov    %eax,%ebx
test   %ebp,%ebp
je     11b2 <gpio_set_direction+0x62>
cmp    $0x1,%ebp
jne    11d5 <gpio_set_direction+0x85>
mov    $0x3,%edx
lea    0xeb2(%rip),%rsi
jmp    11be <gpio_set_direction+0x6e>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
jmp    11f7 <gpio_set_direction+0xa7>
mov    $0x2,%edx
lea    0xe8f(%rip),%rsi
mov    %ebx,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11e7 <gpio_set_direction+0x97>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    1201 <gpio_set_direction+0xb1>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
jmp    11f7 <gpio_set_direction+0xa7>
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
call   1050 <fputs@plt>
mov    $0xffffffff,%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 = [44 x i8] c"Failed to open gpio direction for writing!\0A\00"
@stderr = global i32 0
@.str.2 = [3 x i8] c"in\00"
@.str.3 = [4 x i8] c"out\00"
@.str.4 = [26 x i8] c"error: invalid direction\0A\00"
@.str.5 = [26 x i8] c"Failed to set direction!\0A\00"
define i32 @gpio_set_direction(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [35 x i8]
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = getelementptr inbounds [35 x i8], [35 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %4
%12 = call i32 @snprintf(i8* %10, i32 35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [35 x i8], [35 x i8]* %8, i64 0, i64 0
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 @fputs(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 -1, i32* %3
br label %45
21: ; preds = %2
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %6
store i32 2, i32* %7
br label %33
25: ; preds = %21
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %29
28: ; preds = %25
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %6
store i32 3, i32* %7
br label %32
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = call i32 @fputs(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %30)
store i32 -1, i32* %3
br label %45
32: ; preds = %28
br label %33
33: ; preds = %32, %24
%34 = load i32, i32* %9
%35 = load i8*, i8** %6
%36 = load i32, i32* %7
%37 = call i32 @write(i32 %34, i8* %35, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = load i32, i32* @stderr
%41 = call i32 @fputs(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %40)
store i32 -1, i32* %3
br label %45
42: ; preds = %33
%43 = load i32, i32* %9
%44 = call i32 @close(i32 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %42, %39, %29, %18
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [44 x i8] c"Failed to open gpio direction for writing!\0A\00"
@stderr = global i32 0
@.str.2 = [3 x i8] c"in\00"
@.str.3 = [4 x i8] c"out\00"
@.str.4 = [26 x i8] c"error: invalid direction\0A\00"
@.str.5 = [26 x i8] c"Failed to set direction!\0A\00"
define i32 @gpio_set_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 @fputs(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %27
12: ; preds = %2
switch i32 %1, label %14 [
i32 0, label %17
i32 1, label %13
]
13: ; preds = %12
br label %17
14: ; preds = %12
%15 = load i32, i32* @stderr
%16 = call i32 @fputs(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %15)
br label %27
17: ; preds = %12, %13
%18 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %13 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %12 ]
%19 = phi i32 [ 3, %13 ], [ 2, %12 ]
%20 = call i32 @write(i32 %7, i8* %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fputs(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %23)
br label %27
25: ; preds = %17
%26 = call i32 @close(i32 %7)
br label %27
27: ; preds = %25, %22, %14, %9
%28 = phi i32 [ -1, %9 ], [ -1, %22 ], [ 0, %25 ], [ -1, %14 ]
ret i32 %28
}