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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ millisleep(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int GPIO_export(int pin) {
  char name_buffer[4];
  ssize_t bytes_written;
  int fd;
  int ret_err;
  fd = open("/sys/class/gpio/export", O_WRONLY);
  if (-1 != fd) {
    char path[34];
    int fs_struct_created;
    int n_wait_cycle;
    bytes_written = snprintf(name_buffer, 4, "%d", pin);
    write(fd, name_buffer, bytes_written);
    close(fd);
    snprintf(path, 34, "/sys/class/gpio/gpio%d/direction", pin);
    fs_struct_created = 0;
    n_wait_cycle = 0;
    do {
      millisleep(50);
      fd = open(path, O_WRONLY);
      if (-1 != fd) {
        fs_struct_created = 1;
        close(fd);
      } else
        fs_struct_created = 0;
    } while (!fs_struct_created && n_wait_cycle++ < 20);
    if (fs_struct_created)
      ret_err = 0;
    else
      ret_err = errno;
  } else
    ret_err = errno;
  return (ret_err);
}


x86 assembly (clang, -O0):
<GPIO_export>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
mov    $0xffffffff,%eax
cmp    -0x10(%rbp),%eax
je     127b <GPIO_export+0x12b>
lea    -0x8(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x4,%esi
lea    0xe83(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x22,%esi
lea    0xe54(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    $0x32,%edi
call   1060 <millisleep@plt>
lea    -0x40(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
mov    $0xffffffff,%eax
cmp    -0x10(%rbp),%eax
je     121a <GPIO_export+0xca>
movl   $0x1,-0x44(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
jmp    1221 <GPIO_export+0xd1>
movl   $0x0,-0x44(%rbp)
jmp    1226 <GPIO_export+0xd6>
xor    %eax,%eax
cmpl   $0x0,-0x44(%rbp)
mov    %al,-0x49(%rbp)
jne    1249 <GPIO_export+0xf9>
mov    -0x48(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x48(%rbp)
cmp    $0x14,%eax
setl   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11d9 <GPIO_export+0x89>
cmpl   $0x0,-0x44(%rbp)
je     126a <GPIO_export+0x11a>
movl   $0x0,-0x14(%rbp)
jmp    1276 <GPIO_export+0x126>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1287 <GPIO_export+0x137>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GPIO_export>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebp
mov    0x2e6d(%rip),%r15
mov    (%r15),%esi
lea    0xe93(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1201 <GPIO_export+0xb1>
mov    %eax,%ebx
lea    0xe93(%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>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe68(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x22,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
mov    $0x15,%ebx
nopl   0x0(%rax,%rax,1)
mov    $0x32,%edi
call   1060 <millisleep@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11f3 <GPIO_export+0xa3>
add    $0xffffffff,%ebx
jne    11d0 <GPIO_export+0x80>
jmp    11fa <GPIO_export+0xaa>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
cmp    $0xffffffff,%ebp
jne    120a <GPIO_export+0xba>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
add    $0x38,%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"
@.str.2 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@errno = global i32 0
define i32 @GPIO_export(i32 %0) {
%2 = alloca i32
%3 = alloca [4 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [34 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %2
%10 = load i32, i32* @O_WRONLY
%11 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp ne i32 -1, %12
br i1 %13, label %14, label %55
14: ; preds = %1
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%16 = load i32, i32* %2
%17 = call i32 @snprintf(i8* %15, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %5
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%20 = load i32, i32* %4
%21 = call i32 @write(i32 %18, i8* %19, i32 %20)
%22 = load i32, i32* %5
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [34 x i8], [34 x i8]* %7, i64 0, i64 0
%25 = load i32, i32* %2
%26 = call i32 @snprintf(i8* %24, i32 34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %25)
store i32 0, i32* %8
store i32 0, i32* %9
br label %27
27: ; preds = %46, %14
%28 = call i32 @millisleep(i32 50)
%29 = getelementptr inbounds [34 x i8], [34 x i8]* %7, i64 0, i64 0
%30 = load i32, i32* @O_WRONLY
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp ne i32 -1, %32
br i1 %33, label %34, label %37
34: ; preds = %27
store i32 1, i32* %8
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
br label %38
37: ; preds = %27
store i32 0, i32* %8
br label %38
38: ; preds = %37, %34
br label %39
39: ; preds = %38
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %39
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
store i32 %44, i32* %9
%45 = icmp slt i32 %43, 20
br label %46
46: ; preds = %42, %39
%47 = phi i1 [ false, %39 ], [ %45, %42 ]
br i1 %47, label %27, label %48
48: ; preds = %46
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48
store i32 0, i32* %6
br label %54
52: ; preds = %48
%53 = load i32, i32* @errno
store i32 %53, i32* %6
br label %54
54: ; preds = %52, %51
br label %57
55: ; preds = %1
%56 = load i32, i32* @errno
store i32 %56, i32* %6
br label %57
57: ; preds = %55, %54
%58 = load i32, i32* %6
ret i32 %58
}

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"
@.str.2 = [33 x i8] c"/sys/class/gpio/gpio%d/direction\00"
@errno = global i32 0
define i32 @GPIO_export(i32 %0) {
%2 = alloca [4 x i8]
%3 = alloca [34 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %28, label %8
8: ; preds = %1
%9 = getelementptr inbounds [34 x i8], [34 x i8]* %3, i64 0, i64 0
%10 = call i32 @snprintf(i8* %4, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%11 = call i32 @write(i32 %6, i8* %4, i32 %10)
%12 = call i32 @close(i32 %6)
%13 = call i32 @snprintf(i8* %9, i32 34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %14
14: ; preds = %22, %8
%15 = phi i32 [ 0, %8 ], [ %23, %22 ]
%16 = call i32 @millisleep(i32 50)
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* %9, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = call i32 @close(i32 %18)
br label %25
22: ; preds = %14
%23 = add nuw nsw i32 %15, 1
%24 = icmp eq i32 %23, 21
br i1 %24, label %25, label %14
25: ; preds = %22, %20
%26 = load i32, i32* @errno
%27 = select i1 %19, i32 %26, i32 0
br label %30
28: ; preds = %1
%29 = load i32, i32* @errno
br label %30
30: ; preds = %28, %25
%31 = phi i32 [ %27, %25 ], [ %29, %28 ]
ret i32 %31
}