#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*/ clearLastError();
int pintoGPIO(int);
int /*<<< orphan*/ setLastError(char *);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int writeValueToFile(char *, char *);
int setGPIODirection(int pin, int direction) {
  clearLastError();
  int gpio = pintoGPIO(pin);
  char directionStr[10];
  char fileName[255] = {0};
  sprintf(fileName, "/sys/class/gpio/gpio%d/%s", gpio, "direction");
  if (direction == (1)) {
    strcpy(directionStr, "in");
  } else if (direction == (2)) {
    strcpy(directionStr, "out");
  } else {
    setLastError("direction must be 1 or 2,  1->in, 2->out");
    return -1;
  }
  return writeValueToFile(fileName, directionStr);
}


x86 assembly (clang, -O0):
<setGPIODirection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1060 <clearLastError@plt>
mov    -0x8(%rbp),%edi
call   1040 <pintoGPIO@plt>
mov    %eax,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1050 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe49(%rip),%rsi
lea    0xe5c(%rip),%rcx
call   1080 <sprintf@plt>
cmpl   $0x1,-0xc(%rbp)
jne    11e2 <setGPIODirection+0x72>
lea    -0x1a(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1030 <strcpy@plt>
jmp    121e <setGPIODirection+0xae>
cmpl   $0x2,-0xc(%rbp)
jne    1201 <setGPIODirection+0x91>
lea    -0x1a(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1219 <setGPIODirection+0xa9>
lea    0xe1f(%rip),%rdi
call   1070 <setLastError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <setGPIODirection+0xc1>
jmp    121e <setGPIODirection+0xae>
lea    -0x120(%rbp),%rdi
lea    -0x1a(%rbp),%rsi
call   1090 <writeValueToFile@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setGPIODirection>:
push   %rbp
push   %rbx
sub    $0x118,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <clearLastError@plt>
mov    %ebp,%edi
call   1040 <pintoGPIO@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0xff(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0xe10(%rip),%rsi
lea    0xe23(%rip),%rcx
lea    0x10(%rsp),%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
cmp    $0x1,%ebx
je     1216 <setGPIODirection+0xb6>
cmp    $0x2,%ebx
jne    1239 <setGPIODirection+0xd9>
lea    0xe38(%rip),%rsi
jmp    121d <setGPIODirection+0xbd>
lea    0xe07(%rip),%rsi
lea    0x6(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1080 <writeValueToFile@plt>
jmp    124a <setGPIODirection+0xea>
lea    0xde7(%rip),%rdi
call   1060 <setLastError@plt>
mov    $0xffffffff,%eax
add    $0x118,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"/sys/class/gpio/gpio%d/%s\00"
@.str.1 = [10 x i8] c"direction\00"
@.str.2 = [3 x i8] c"in\00"
@.str.3 = [4 x i8] c"out\00"
@.str.4 = [41 x i8] c"direction must be 1 or 2, 1->in, 2->out\00"
define i32 @setGPIODirection(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [10 x i8]
%8 = alloca [255 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = call i32 (...) @clearLastError()
%10 = load i32, i32* %4
%11 = call i32 @pintoGPIO(i32 %10)
store i32 %11, i32* %6
%12 = bitcast [255 x i8]* %8 to i8*
%13 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%14 = load i32, i32* %6
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%20 = call i32 @strcpy(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %30
21: ; preds = %2
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 2
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%26 = call i32 @strcpy(i8* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %29
27: ; preds = %21
%28 = call i32 @setLastError(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %34
29: ; preds = %24
br label %30
30: ; preds = %29, %18
%31 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%33 = call i32 @writeValueToFile(i8* %31, i8* %32)
store i32 %33, i32* %3
br label %34
34: ; preds = %30, %27
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [26 x i8] c"/sys/class/gpio/gpio%d/%s\00"
@.str.1 = [10 x i8] c"direction\00"
@.str.2 = [3 x i8] c"in\00"
@.str.3 = [4 x i8] c"out\00"
@.str.4 = [41 x i8] c"direction must be 1 or 2, 1->in, 2->out\00"
define i32 @setGPIODirection(i32 %0, i32 %1) {
%3 = alloca [10 x i8]
%4 = alloca [255 x i8]
%5 = tail call i32 (...) @clearLastError()
%6 = tail call i32 @pintoGPIO(i32 %0)
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
switch i32 %1, label %11 [
i32 1, label %13
i32 2, label %10
]
10: ; preds = %2
br label %13
11: ; preds = %2
%12 = tail call i32 @setLastError(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %17
13: ; preds = %2, %10
%14 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %10 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %2 ]
%15 = call i32 @strcpy(i8* %7, i8* %14)
%16 = call i32 @writeValueToFile(i8* %8, i8* %7)
br label %17
17: ; preds = %13, %11
%18 = phi i32 [ %16, %13 ], [ -1, %11 ]
ret i32 %18
}