#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*/ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int setPinDir(int pin, bool out) {
  int ret, fd;
  char buf[64];
  sprintf(buf, "/sys/class/gpio/gpio%d/direction", pin);
  fd = open(buf, O_WRONLY);
  if (fd < 0) {
    printf("ioctl error: %s\n", strerror(errno));
    return fd;
  }
  if (out)
    ret = write(fd, "out", 3);
  else
    ret = write(fd, "in", 2);
  close(fd);
  if (ret <= 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<setPinDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe84(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c8 <setPinDir+0x68>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122a <setPinDir+0xca>
cmpl   $0x0,-0xc(%rbp)
je     11ee <setPinDir+0x8e>
mov    -0x14(%rbp),%edi
lea    0xe56(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
jmp    1205 <setPinDir+0xa5>
mov    -0x14(%rbp),%edi
lea    0xe3e(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x10(%rbp)
jg     1223 <setPinDir+0xc3>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <setPinDir+0xca>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setPinDir>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %esi,%ebp
mov    %edi,%edx
lea    0xe8f(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11a7 <setPinDir+0x47>
test   %ebp,%ebp
je     11c6 <setPinDir+0x66>
lea    0xe94(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
jmp    11d4 <setPinDir+0x74>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe65(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
jmp    11eb <setPinDir+0x8b>
lea    0xe69(%rip),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
test   %ebp,%ebp
setle  %bl
neg    %ebx
mov    %ebx,%eax
add    $0x48,%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 = [17 x i8] c"ioctl error: %s\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"out\00"
@.str.3 = [3 x i8] c"in\00"
define i32 @setPinDir(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %7
store i32 %21, i32* %3
br label %38
22: ; preds = %2
%23 = load i32, i32* %5
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* %7
%27 = call i32 @write(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
store i32 %27, i32* %6
br label %31
28: ; preds = %22
%29 = load i32, i32* %7
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
store i32 %30, i32* %6
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %6
%35 = icmp sle i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %31
store i32 -1, i32* %3
br label %38
37: ; preds = %31
store i32 0, i32* %3
br label %38
38: ; preds = %37, %36, %17
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [17 x i8] c"ioctl error: %s\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"out\00"
@.str.3 = [3 x i8] c"in\00"
define i32 @setPinDir(i32 %0, i32 %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) 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 slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %24
13: ; preds = %2
%14 = icmp eq i32 %1, 0
br i1 %14, label %17, label %15
15: ; preds = %13
%16 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %19
17: ; preds = %13
%18 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ %16, %15 ], [ %18, %17 ]
%21 = tail call i32 @close(i32 %7)
%22 = icmp slt i32 %20, 1
%23 = sext i1 %22 to i32
br label %24
24: ; preds = %19, %9
%25 = phi i32 [ %7, %9 ], [ %23, %19 ]
ret i32 %25
}