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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int eim_set_direction(int, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int gpio_set_direction(int gpio, int is_output) {
  char gpio_path[256];
  int fd;
  int ret;
  if (gpio & (0x80000000))
    return eim_set_direction(gpio & (~(0x80000000)), is_output);
  snprintf(gpio_path, sizeof(gpio_path) - 1,
           "/sys/class/gpio"
           "/gpio%d/direction",
           gpio);
  fd = open(gpio_path, O_WRONLY);
  if (fd == -1) {
    fprintf(stderr, "Direction file: [%s]\n", gpio_path);
    perror("Couldn't open direction file for gpio");
    return -errno;
  }
  if (is_output)
    ret = write(fd, "out", 4);
  else
    ret = write(fd, "in", 3);
  if (ret == -1) {
    perror("Couldn't set output direction");
    close(fd);
    return -errno;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_set_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0x80000000,%eax
cmp    $0x0,%eax
je     11ab <gpio_set_direction+0x3b>
mov    -0x8(%rbp),%edi
and    $0x7fffffff,%edi
mov    -0xc(%rbp),%esi
call   1070 <eim_set_direction@plt>
mov    %eax,-0x4(%rbp)
jmp    12bb <gpio_set_direction+0x14b>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe3f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
jne    1229 <gpio_set_direction+0xb9>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rdx
lea    0xe1c(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe26(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
mov    0x2db9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12bb <gpio_set_direction+0x14b>
cmpl   $0x0,-0xc(%rbp)
je     1255 <gpio_set_direction+0xe5>
mov    -0x114(%rbp),%edi
lea    0xe1d(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x118(%rbp)
jmp    1272 <gpio_set_direction+0x102>
mov    -0x114(%rbp),%edi
lea    0xdff(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0xffffffff,-0x118(%rbp)
jne    12a9 <gpio_set_direction+0x139>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
mov    -0x114(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    0x2d39(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12bb <gpio_set_direction+0x14b>
mov    -0x114(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_set_direction>:
push   %rbp
push   %rbx
sub    $0x108,%rsp
mov    %esi,%ebp
mov    %edi,%ecx
test   %edi,%edi
js     11c4 <gpio_set_direction+0x54>
lea    0xe78(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1040 <snprintf@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1207 <gpio_set_direction+0x97>
mov    %eax,%ebx
test   %ebp,%ebp
je     11dc <gpio_set_direction+0x6c>
lea    0xea2(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
jmp    11ea <gpio_set_direction+0x7a>
and    $0x7fffffff,%ecx
mov    %ecx,%edi
mov    %ebp,%esi
add    $0x108,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <eim_set_direction@plt>
lea    0xe7e(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     122d <gpio_set_direction+0xbd>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    %rsp,%rdx
call   1060 <fprintf@plt>
lea    0xe11(%rip),%rdi
call   1090 <perror@plt>
jmp    1240 <gpio_set_direction+0xd0>
lea    0xe30(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2d91(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    11fd <gpio_set_direction+0x8d>

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Direction file: [%s]\0A\00"
@.str.2 = [38 x i8] c"Couldn't open direction file for gpio\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"out\00"
@.str.4 = [3 x i8] c"in\00"
@.str.5 = [30 x i8] c"Couldn't set output direction\00"
define i32 @gpio_set_direction(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = and i32 %9, -2147483648
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* %4
%14 = and i32 %13, 2147483647
%15 = load i32, i32* %5
%16 = call i32 @eim_set_direction(i32 %14, i32 %15)
store i32 %16, i32* %3
br label %54
17: ; preds = %2
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%19 = load i32, i32* %4
%20 = call i32 @snprintf(i8* %18, i32 255, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%22 = load i32, i32* @O_WRONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %33
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %3
br label %54
33: ; preds = %17
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* %7
%38 = call i32 @write(i32 %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 4)
store i32 %38, i32* %8
br label %42
39: ; preds = %33
%40 = load i32, i32* %7
%41 = call i32 @write(i32 %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 3)
store i32 %41, i32* %8
br label %42
42: ; preds = %39, %36
%43 = load i32, i32* %8
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %51
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* @errno
%50 = sub nsw i32 0, %49
store i32 %50, i32* %3
br label %54
51: ; preds = %42
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
store i32 0, i32* %3
br label %54
54: ; preds = %51, %45, %26, %12
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Direction file: [%s]\0A\00"
@.str.2 = [38 x i8] c"Couldn't open direction file for gpio\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"out\00"
@.str.4 = [3 x i8] c"in\00"
@.str.5 = [30 x i8] c"Couldn't set output direction\00"
define i32 @gpio_set_direction(i32 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, -1
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = and i32 %0, 2147483647
%8 = tail call i32 @eim_set_direction(i32 %7, i32 %1)
br label %36
9: ; preds = %2
%10 = call i32 @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %4, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%17 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
br label %36
20: ; preds = %9
%21 = icmp eq i32 %1, 0
br i1 %21, label %24, label %22
22: ; preds = %20
%23 = call i32 @write(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 4)
br label %26
24: ; preds = %20
%25 = call i32 @write(i32 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 3)
br label %26
26: ; preds = %24, %22
%27 = phi i32 [ %23, %22 ], [ %25, %24 ]
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%31 = call i32 @close(i32 %12)
%32 = load i32, i32* @errno
%33 = sub nsw i32 0, %32
br label %36
34: ; preds = %26
%35 = call i32 @close(i32 %12)
br label %36
36: ; preds = %34, %29, %14, %6
%37 = phi i32 [ %8, %6 ], [ %19, %14 ], [ %33, %29 ], [ 0, %34 ]
ret i32 %37
}