#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*/);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int initPin(int pin, bool on) {
  char buf[8];
  int fd, ret = 0;
  if (on)
    fd = open("/sys/class/gpio/export", O_WRONLY);
  else
    fd = open("/sys/class/gpio/unexport", O_WRONLY);
  if (fd < 0) {
    printf("ioctl error: %s\n", strerror(errno));
    return ret;
  }
  sprintf(buf, "%d", pin);
  if (write(fd, buf, strlen(buf)) != strlen(buf))
    ret = -1;
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<initPin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11ac <initPin+0x3c>
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
jmp    11c4 <initPin+0x54>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f6 <initPin+0x86>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe4a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1261 <initPin+0xf1>
lea    -0x14(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe3d(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x14(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x14(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
lea    -0x14(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     1253 <initPin+0xe3>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initPin>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
test   %esi,%esi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rax
lea    0xe6c(%rip),%rdi
cmove  %rax,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11ee <initPin+0x7e>
mov    %eax,%ebx
lea    0xe97(%rip),%rsi
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14d,%edx
call   1080 <sprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,%r14
mov    %rbp,%rdi
call   1040 <strlen@plt>
xor    %ebp,%ebp
cmp    %rax,%r14
setne  %bpl
neg    %ebp
mov    %ebx,%edi
call   1060 <close@plt>
jmp    120d <initPin+0x9d>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe2d(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.1 = [25 x i8] c"/sys/class/gpio/unexport\00"
@.str.2 = [17 x i8] c"ioctl error: %s\0A\00"
@errno = global i32 0
@.str.3 = [3 x i8] c"%d\00"
define i32 @initPin(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [8 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %8
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %7
br label %17
14: ; preds = %2
%15 = load i32, i32* @O_WRONLY
%16 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i32 %16, i32* %7
br label %17
17: ; preds = %14, %11
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i32, i32* %8
store i32 %24, i32* %3
br label %42
25: ; preds = %17
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%27 = load i32, i32* %4
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %7
%30 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%31 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%32 = call i64 @strlen(i8* %31)
%33 = call i64 @write(i32 %29, i8* %30, i64 %32)
%34 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%35 = call i64 @strlen(i8* %34)
%36 = icmp ne i64 %33, %35
br i1 %36, label %37, label %38
37: ; preds = %25
store i32 -1, i32* %8
br label %38
38: ; preds = %37, %25
%39 = load i32, i32* %7
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %8
store i32 %41, i32* %3
br label %42
42: ; preds = %38, %20
%43 = load i32, i32* %3
ret i32 %43
}

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