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

/* Variables and functions */
int /*<<< orphan*/ clearLastError();
int pintoGPIO(int);
int readValueFromFile(char *, char *, int);
int /*<<< orphan*/ setLastError(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
scalar_t__ strncasecmp(char *, char *, int);
int getGPIODirection(int pin) {
  clearLastError();
  char buff[255] = {0};
  int direction;
  int ret;
  int gpio = pintoGPIO(pin);
  char fileName[255] = {0};
  sprintf(fileName, "/sys/class/gpio/gpio%d/%s", gpio, "direction");
  ret = readValueFromFile(fileName, buff, sizeof(buff) - 1);
  if (ret >= 0) {
    if (strncasecmp(buff, "out", 3) == 0) {
      direction = (2);
    } else if (strncasecmp(buff, "in", 2) == 0) {
      direction = (1);
    } else {
      setLastError("direction wrong, must be in or out,  but %s", buff);
      return -1;
    }
    return direction;
  }
  return ret;
}


x86 assembly (clang, -O0):
<getGPIODirection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <clearLastError@plt>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
call   1030 <pintoGPIO@plt>
mov    %eax,-0x11c(%rbp)
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1040 <memset@plt>
lea    -0x220(%rbp),%rdi
mov    -0x11c(%rbp),%edx
lea    0xe33(%rip),%rsi
lea    0xe46(%rip),%rcx
call   1090 <sprintf@plt>
lea    -0x220(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0xfe,%edx
call   1060 <readValueFromFile@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
jl     1298 <getGPIODirection+0x128>
lea    -0x110(%rbp),%rdi
lea    0xe12(%rip),%rsi
mov    $0x3,%edx
call   1080 <strncasecmp@plt>
cmp    $0x0,%rax
jne    1235 <getGPIODirection+0xc5>
movl   $0x2,-0x114(%rbp)
jmp    128a <getGPIODirection+0x11a>
lea    -0x110(%rbp),%rdi
lea    0xde5(%rip),%rsi
mov    $0x2,%edx
call   1080 <strncasecmp@plt>
cmp    $0x0,%rax
jne    1266 <getGPIODirection+0xf6>
movl   $0x1,-0x114(%rbp)
jmp    1285 <getGPIODirection+0x115>
lea    -0x110(%rbp),%rsi
lea    0xdb7(%rip),%rdi
call   1070 <setLastError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <getGPIODirection+0x131>
jmp    128a <getGPIODirection+0x11a>
mov    -0x114(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a1 <getGPIODirection+0x131>
mov    -0x118(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getGPIODirection>:
push   %rbx
sub    $0x200,%rsp
mov    %edi,%ebx
xor    %eax,%eax
call   1040 <clearLastError@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0xef(%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)
movaps %xmm0,(%rsp)
mov    %ebx,%edi
call   1030 <pintoGPIO@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x1ef(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
lea    0xd94(%rip),%rsi
lea    0xda7(%rip),%rcx
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1080 <sprintf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xfe,%edx
call   1050 <readValueFromFile@plt>
test   %eax,%eax
js     12ed <getGPIODirection+0x18d>
lea    0xd84(%rip),%rsi
mov    %rsp,%rdi
mov    $0x3,%edx
call   1070 <strncasecmp@plt>
test   %rax,%rax
je     12e1 <getGPIODirection+0x181>
lea    0xd6f(%rip),%rsi
mov    %rsp,%rdi
mov    $0x2,%edx
call   1070 <strncasecmp@plt>
test   %rax,%rax
je     12e8 <getGPIODirection+0x188>
lea    0xd59(%rip),%rdi
mov    %rsp,%rsi
call   1060 <setLastError@plt>
mov    $0xffffffff,%eax
jmp    12ed <getGPIODirection+0x18d>
mov    $0x2,%eax
jmp    12ed <getGPIODirection+0x18d>
mov    $0x1,%eax
add    $0x200,%rsp
pop    %rbx
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 = [4 x i8] c"out\00"
@.str.3 = [3 x i8] c"in\00"
@.str.4 = [44 x i8] c"direction wrong, must be in or out, but %s\00"
define i32 @getGPIODirection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [255 x i8]
store i32 %0, i32* %3
%9 = call i32 (...) @clearLastError()
%10 = bitcast [255 x i8]* %4 to i8*
%11 = load i32, i32* %3
%12 = call i32 @pintoGPIO(i32 %11)
store i32 %12, i32* %7
%13 = bitcast [255 x i8]* %8 to i8*
%14 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%15 = load i32, i32* %7
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%18 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%19 = call i32 @readValueFromFile(i8* %17, i8* %18, i32 254)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp sge i32 %20, 0
br i1 %21, label %22, label %38
22: ; preds = %1
%23 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%24 = call i64 @strncasecmp(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 2, i32* %5
br label %36
27: ; preds = %22
%28 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%29 = call i64 @strncasecmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 1, i32* %5
br label %35
32: ; preds = %27
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%34 = call i32 @setLastError(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %33)
store i32 -1, i32* %2
br label %40
35: ; preds = %31
br label %36
36: ; preds = %35, %26
%37 = load i32, i32* %5
store i32 %37, i32* %2
br label %40
38: ; preds = %1
%39 = load i32, i32* %6
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %36, %32
%41 = load i32, i32* %2
ret i32 %41
}

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 = [4 x i8] c"out\00"
@.str.3 = [3 x i8] c"in\00"
@.str.4 = [44 x i8] c"direction wrong, must be in or out, but %s\00"
define i32 @getGPIODirection(i32 %0) {
%2 = alloca [255 x i8]
%3 = alloca [255 x i8]
%4 = tail call i32 (...) @clearLastError()
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%6 = tail call i32 @pintoGPIO(i32 %0)
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%8 = call i32 @sprintf(i8* %7, 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))
%9 = call i32 @readValueFromFile(i8* %7, i8* %5, i32 254)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %19
11: ; preds = %1
%12 = call i64 @strncasecmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = call i64 @strncasecmp(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 @setLastError(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %5)
br label %19
19: ; preds = %1, %11, %14, %17
%20 = phi i32 [ -1, %17 ], [ 2, %11 ], [ 1, %14 ], [ %9, %1 ]
ret i32 %20
}