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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int gpio_dir(int pin, int dir) {
  char path[40];
  FILE *fp = NULL;
  snprintf(path, 40, "/sys/class/gpio/gpio%d/direction", pin);
  fp = fopen(path, "r+");
  if (!fp) {
    printf("Failed to open gpio direction %d!\n", pin);
    return 1;
  }
  if (dir == 0) {
    if (fwrite("in", 1, 2, fp) != 2) {
      printf("Failed to set direction %d!\n", pin);
      fclose(fp);
      return 1;
    }
  }
  if (dir == 1) {
    if (fwrite("out", 1, 3, fp) != 3) {
      printf("Failed to set direction %d!\n", pin);
      fclose(fp);
      return 1;
    }
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x28,%esi
lea    0xe87(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe98(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11b8 <gpio_dir+0x68>
mov    -0x8(%rbp),%esi
lea    0xe7d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <gpio_dir+0x124>
cmpl   $0x0,-0xc(%rbp)
jne    120e <gpio_dir+0xbe>
mov    -0x48(%rbp),%rcx
lea    0xe7a(%rip),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
call   1070 <fwrite@plt>
cmp    $0x2,%eax
je     1209 <gpio_dir+0xb9>
mov    -0x8(%rbp),%esi
lea    0xe5b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <gpio_dir+0x124>
jmp    120e <gpio_dir+0xbe>
cmpl   $0x1,-0xc(%rbp)
jne    1264 <gpio_dir+0x114>
mov    -0x48(%rbp),%rcx
lea    0xe44(%rip),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
call   1070 <fwrite@plt>
cmp    $0x3,%eax
je     125f <gpio_dir+0x10f>
mov    -0x8(%rbp),%esi
lea    0xe05(%rip),%rdi
call   1040 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <gpio_dir+0x124>
jmp    1264 <gpio_dir+0x114>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_dir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xe9c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
lea    0xea3(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d3 <gpio_dir+0x83>
mov    %rax,%rbx
cmp    $0x1,%ebp
je     11e9 <gpio_dir+0x99>
test   %ebp,%ebp
jne    120c <gpio_dir+0xbc>
lea    0xea9(%rip),%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
cmp    $0x2,%eax
je     120c <gpio_dir+0xbc>
lea    0xe89(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1216 <gpio_dir+0xc6>
lea    0xe4a(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    $0x1,%ebp
jmp    1216 <gpio_dir+0xc6>
lea    0xe77(%rip),%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
cmp    $0x3,%eax
jne    11ba <gpio_dir+0x6a>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [35 x i8] c"Failed to open gpio direction %d!\0A\00"
@.str.3 = [3 x i8] c"in\00"
@.str.4 = [29 x i8] c"Failed to set direction %d!\0A\00"
@.str.5 = [4 x i8] c"out\00"
define i32 @gpio_dir(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [40 x i8]
%7 = alloca i32*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* null, i32** %7
%8 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %8, i32 40, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %4
%17 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 1, i32* %3
br label %47
18: ; preds = %2
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %18
%22 = load i32*, i32** %7
%23 = call i32 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 2, i32* %22)
%24 = icmp ne i32 %23, 2
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = load i32*, i32** %7
%29 = call i32 @fclose(i32* %28)
store i32 1, i32* %3
br label %47
30: ; preds = %21
br label %31
31: ; preds = %30, %18
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %44
34: ; preds = %31
%35 = load i32*, i32** %7
%36 = call i32 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 1, i32 3, i32* %35)
%37 = icmp ne i32 %36, 3
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = load i32, i32* %4
%40 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %39)
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
store i32 1, i32* %3
br label %47
43: ; preds = %34
br label %44
44: ; preds = %43, %31
%45 = load i32*, i32** %7
%46 = call i32 @fclose(i32* %45)
store i32 0, i32* %3
br label %47
47: ; preds = %44, %38, %25, %15
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [35 x i8] c"Failed to open gpio direction %d!\0A\00"
@.str.3 = [3 x i8] c"in\00"
@.str.4 = [29 x i8] c"Failed to set direction %d!\0A\00"
@.str.5 = [4 x i8] c"out\00"
define i32 @gpio_dir(i32 %0, i32 %1) {
%3 = alloca [40 x i8]
%4 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 40, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %25
10: ; preds = %2
switch i32 %1, label %23 [
i32 0, label %11
i32 1, label %17
]
11: ; preds = %10
%12 = call i32 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 2, i32* %6)
%13 = icmp eq i32 %12, 2
br i1 %13, label %23, label %14
14: ; preds = %11
%15 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%16 = call i32 @fclose(i32* %6)
br label %25
17: ; preds = %10
%18 = call i32 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 1, i32 3, i32* %6)
%19 = icmp eq i32 %18, 3
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%22 = call i32 @fclose(i32* %6)
br label %25
23: ; preds = %11, %10, %17
%24 = call i32 @fclose(i32* %6)
br label %25
25: ; preds = %23, %20, %14, %8
%26 = phi i32 [ 1, %14 ], [ 1, %20 ], [ 0, %23 ], [ 1, %8 ]
ret i32 %26
}