#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 open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int write(int, char *, int);
int gpio_export(int pin) {
  char buffer[4];
  int len;
  int fd;
  fd = open("/sys/class/gpio/export", O_WRONLY);
  if (fd < 0) {
    ;
    return -1;
  }
  len = snprintf(buffer, 4, "%d", pin);
  if (write(fd, buffer, len) != len) {
    close(fd);
    return -1;
  };
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gpio_export>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xea5(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1179 <gpio_export+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <gpio_export+0x8f>
lea    -0xc(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x4,%esi
lea    0xe8b(%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    -0x10(%rbp),%eax
je     11c0 <gpio_export+0x80>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <gpio_export+0x8f>
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   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a2 <gpio_export+0x62>
mov    %eax,%ebx
lea    0xeac(%rip),%rdx
lea    0x4(%rsp),%r14
mov    %r14,%rdi
mov    $0x4,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
mov    %eax,%r15d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %ebp,%ebp
cmp    %r15d,%eax
setne  %bpl
mov    %ebx,%edi
call   1050 <close@plt>
neg    %ebp
jmp    11a7 <gpio_export+0x67>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.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 @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 slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %28
12: ; preds = %1
%13 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* %3
%15 = call i32 @snprintf(i8* %13, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %6
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%18 = load i32, i32* %5
%19 = call i32 @write(i32 %16, i8* %17, i32 %18)
%20 = load i32, i32* %5
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = load i32, i32* %6
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %2
br label %28
25: ; preds = %12
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
store i32 0, i32* %2
br label %28
28: ; preds = %25, %22, %11
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.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 @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 slt i32 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %1
%8 = call i32 @snprintf(i8* %3, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%9 = call i32 @write(i32 %5, i8* %3, i32 %8)
%10 = icmp ne i32 %9, %8
%11 = call i32 @close(i32 %5)
%12 = sext i1 %10 to i32
br label %13
13: ; preds = %7, %1
%14 = phi i32 [ -1, %1 ], [ %12, %7 ]
ret i32 %14
}