#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int lngpio_export(int pin) {
  char buffer[3];
  ssize_t bytes_to_write;
  ssize_t ret;
  int fd;
  fd = open("/sys/class/gpio/export", O_WRONLY);
  if (-1 == fd) {
    fprintf(stderr, "Failed to open export for writing!\n");
    return (-1);
  }
  bytes_to_write = snprintf(buffer, 3, "%d", pin);
  ret = write(fd, buffer, bytes_to_write);
  if (-1 == ret) {
    fprintf(stderr, "Failed to export pin %d!\n", pin);
    return (-1);
  }
  assert(ret == bytes_to_write);
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<lngpio_export>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    11b4 <lngpio_export+0x54>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <lngpio_export+0xd7>
lea    -0xb(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x3,%esi
lea    0xe74(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%edi
lea    -0xb(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1215 <lngpio_export+0xb5>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <lngpio_export+0xd7>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lngpio_export>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11cd <lngpio_export+0x6d>
mov    %eax,%ebp
lea    0xeae(%rip),%rdx
lea    0x5(%rsp),%r15
mov    %r15,%rdi
mov    $0x3,%esi
mov    %r14d,%ecx
call   1040 <snprintf@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11e6 <lngpio_export+0x86>
xor    %edi,%edi
cmp    %ebx,%eax
sete   %dil
call   1060 <assert@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1205 <lngpio_export+0xa5>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1200 <lngpio_export+0xa0>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%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
@stderr = global i32 0
@.str.1 = [36 x i8] c"Failed to open export for writing!\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [26 x i8] c"Failed to export pin %d!\0A\00"
define i32 @lngpio_export(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [3 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp eq i32 -1, %10
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
15: ; preds = %1
%16 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%17 = load i32, i32* %3
%18 = call i32 @snprintf(i8* %16, i32 3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %7
%20 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%21 = load i32, i32* %5
%22 = call i32 @write(i32 %19, i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 -1, %23
br i1 %24, label %25, label %29
25: ; preds = %15
%26 = load i32, i32* @stderr
%27 = load i32, i32* %3
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %27)
store i32 -1, i32* %2
br label %37
29: ; preds = %15
%30 = load i32, i32* %6
%31 = load i32, i32* %5
%32 = icmp eq i32 %30, %31
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
store i32 0, i32* %2
br label %37
37: ; preds = %29, %25, %12
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [36 x i8] c"Failed to open export for writing!\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [26 x i8] c"Failed to export pin %d!\0A\00"
define i32 @lngpio_export(i32 %0) {
%2 = alloca [3 x i8]
%3 = getelementptr inbounds [3 x i8], [3 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 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %22
10: ; preds = %1
%11 = call i32 @snprintf(i8* %3, i32 3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%12 = call i32 @write(i32 %5, i8* %3, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %22
17: ; preds = %10
%18 = icmp eq i32 %12, %11
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = call i32 @close(i32 %5)
br label %22
22: ; preds = %17, %14, %7
%23 = phi i32 [ -1, %7 ], [ -1, %14 ], [ 0, %17 ]
ret i32 %23
}