#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*/ F_OK;
int /*<<< orphan*/ O_WRONLY;
int __get_pin_directory(char *, int);
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int write(int, char *, int /*<<< orphan*/);
int pin_open(int pin) {
  char buf[128];
  char pin_s[4];
  int fd;
  if (__get_pin_directory(buf, pin) >= 128)
    return -1;
  if (!access(buf, F_OK))
    return 0;
  if ((fd = open("/sys/class/gpio/export", O_WRONLY)) == -1)
    return -1;
  if (write(fd, pin_s, sprintf(pin_s, "%d", pin & 0xFF)) == -1) {
    close(fd);
    return -1;
  }
  return close(fd);
}


x86 assembly (clang, -O0):
<pin_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <__get_pin_directory@plt>
cmp    $0x80,%eax
jl     1194 <pin_open+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <pin_open+0x106>
lea    -0x90(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%eax
jne    11be <pin_open+0x5e>
movl   $0x0,-0x4(%rbp)
jmp    1266 <pin_open+0x106>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x98(%rbp)
cmp    $0xffffffff,%eax
jne    11ee <pin_open+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <pin_open+0x106>
mov    -0x98(%rbp),%eax
mov    %eax,-0xa4(%rbp)
lea    -0x94(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x94(%rbp),%rdi
mov    -0x8(%rbp),%edx
and    $0xff,%edx
lea    0xdf8(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0xa4(%rbp),%edi
mov    -0xa0(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1258 <pin_open+0xf8>
mov    -0x98(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <pin_open+0x106>
mov    -0x98(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pin_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %edi,%ebp
lea    0x10(%rsp),%rdi
mov    %ebp,%esi
call   1030 <__get_pin_directory@plt>
mov    $0xffffffff,%ebx
cmp    $0x7f,%eax
jg     11f2 <pin_open+0x92>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1070 <access@plt>
test   %eax,%eax
je     11f0 <pin_open+0x90>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11f2 <pin_open+0x92>
mov    %eax,%r14d
movzbl %bpl,%edx
lea    0xe55(%rip),%rsi
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%ebp
mov    %r14d,%edi
call   1050 <close@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%ebp
cmove  %ebp,%ebx
jmp    11f2 <pin_open+0x92>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_OK = global i32 0
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @pin_open(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = alloca [4 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @__get_pin_directory(i8* %7, i32 %8)
%10 = icmp sge i32 %9, 128
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %38
12: ; preds = %1
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* @F_OK
%15 = call i32 @access(i8* %13, i32 %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %12
store i32 0, i32* %2
br label %38
18: ; preds = %12
%19 = load i32, i32* @O_WRONLY
%20 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 %20, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %2
br label %38
23: ; preds = %18
%24 = load i32, i32* %6
%25 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%27 = load i32, i32* %3
%28 = and i32 %27, 255
%29 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = call i32 @write(i32 %24, i8* %25, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %2
br label %38
35: ; preds = %23
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
store i32 %37, i32* %2
br label %38
38: ; preds = %35, %32, %22, %17, %11
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@F_OK = global i32 0
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @pin_open(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%6 = call i32 @__get_pin_directory(i8* %4, i32 %0)
%7 = icmp sgt i32 %6, 127
br i1 %7, label %23, label %8
8: ; preds = %1
%9 = load i32, i32* @F_OK
%10 = call i32 @access(i8* %4, i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %8
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %23, label %16
16: ; preds = %12
%17 = and i32 %0, 255
%18 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 @write(i32 %14, i8* %5, i32 %18)
%20 = icmp eq i32 %19, -1
%21 = call i32 @close(i32 %14)
%22 = select i1 %20, i32 -1, i32 %21
br label %23
23: ; preds = %16, %12, %8, %1
%24 = phi i32 [ -1, %1 ], [ 0, %8 ], [ -1, %12 ], [ %22, %16 ]
ret i32 %24
}