#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ O_WRONLY;
int close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int write(int, char *, int);
int export(int gpio) {
  int fd;
  char bf[32];
  int rv = 0;
  int blen = 0;
  blen = snprintf(bf, 32, "%i", gpio);
  if (blen < 0) {
    return -1;
  }
  fd = open("/sys/class/gpio/export", O_WRONLY);
  if (fd < 0) {
    return -2;
  }
  if (write(fd, bf, blen) < blen) {
    rv = -3;
  }
  do {
    rv = close(fd);
  } while ((rv < 0) && (errno == EINTR));
  return rv;
}


x86 assembly (clang, -O0):
<export>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x20,%esi
lea    0xe94(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    118a <export+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <export+0xe1>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b8 <export+0x78>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1221 <export+0xe1>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1030 <write@plt>
cmp    -0x38(%rbp),%eax
jge    11d7 <export+0x97>
movl   $0xfffffffd,-0x34(%rbp)
jmp    11dc <export+0x9c>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x34(%rbp)
mov    %al,-0x39(%rbp)
jge    1210 <export+0xd0>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11dc <export+0x9c>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<export>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ecx
lea    0xeaf(%rip),%rdx
mov    %rsp,%rdi
mov    $0x20,%esi
call   1040 <snprintf@plt>
test   %eax,%eax
js     11b6 <export+0x76>
mov    %eax,%ebp
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11bd <export+0x7d>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    %ebp,%edx
call   1030 <write@plt>
mov    0x2e4e(%rip),%r14
mov    0x2e3f(%rip),%rbp
nopl   0x0(%rax)
mov    %ebx,%edi
call   1050 <close@plt>
test   %eax,%eax
jns    11c2 <export+0x82>
mov    0x0(%rbp),%rcx
cmp    %rcx,(%r14)
je     11a0 <export+0x60>
jmp    11c2 <export+0x82>
mov    $0xffffffff,%eax
jmp    11c2 <export+0x82>
mov    $0xfffffffe,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%i\00"
@.str.1 = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @export(i32 %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %19, label %6
6: ; preds = %1
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %19, label %10
10: ; preds = %6
%11 = call i32 @write(i32 %8, i8* %3, i32 %4)
br label %12
12: ; preds = %12, %10
%13 = call i32 @close(i32 %8)
%14 = icmp slt i32 %13, 0
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %12, label %19
19: ; preds = %12, %6, %1
%20 = phi i32 [ -1, %1 ], [ -2, %6 ], [ %13, %12 ]
ret i32 %20
}