#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*/ ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, int /*<<< orphan*/);
int gpio_export(int pin) {
  char buffer[4];
  ssize_t bytes_written;
  int fd;
  fd = open("/sys/class/gpio/export", O_WRONLY);
  if (-1 == fd) {
    fprintf(stderr, "Failed to allocate gpio pin resources!\n");
    return (-1);
  }
  bytes_written = snprintf(buffer, sizeof(buffer), "%d", pin);
  if (write(fd, buffer, bytes_written) < 0) {
    fprintf(stderr, "Failed to allocate gpio pin resources!\n");
  }
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<gpio_export>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11a2 <gpio_export+0x52>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <gpio_export+0xaa>
lea    -0xc(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x4,%esi
lea    0xe8a(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11eb <gpio_export+0x9b>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpio_export>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
lea    0xe96(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11c2 <gpio_export+0x72>
mov    %eax,%ebx
lea    0xec2(%rip),%rdx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
mov    $0x4,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11b7 <gpio_export+0x67>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11dc <gpio_export+0x8c>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%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
@stderr = global i32 0
@.str.1 = [40 x i8] c"Failed to allocate gpio pin resources!\0A\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @gpio_export(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %29
14: ; preds = %1
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* %3
%17 = call i32 @snprintf(i8* %15, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %6
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%20 = load i32, i32* %5
%21 = call i64 @write(i32 %18, i8* %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %23, %14
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
store i32 0, i32* %2
br label %29
29: ; preds = %26, %11
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [40 x i8] c"Failed to allocate gpio pin resources!\0A\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @gpio_export(i32 %0) {
%2 = alloca [4 x i8]
%3 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %19
10: ; preds = %1
%11 = call i32 @snprintf(i8* %3, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%12 = call i64 @write(i32 %5, i8* %3, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %14, %10
%18 = call i32 @close(i32 %5)
br label %19
19: ; preds = %17, %7
%20 = phi i32 [ -1, %7 ], [ 0, %17 ]
ret i32 %20
}