#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 /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int exporter = open("/sys/class/gpio/export", O_WRONLY);
  if (exporter < 0) {
    printf("fail to open exporter\n");
    return -1;
  }
  write(exporter, "91", 2);
  close(exporter);
  int direction = open("/sys/class/gpio/gpio91/direction", O_WRONLY);
  if (direction < 0) {
    printf("fail to open direction\n");
    return -1;
  }
  write(direction, "out", 3);
  close(direction);
  int value = open("/sys/class/gpio/gpio91/value", O_WRONLY);
  if (value < 0) {
    printf("fail to open value\n");
    return -1;
  }
  for (int i = 0; i < 1000000; i++) {
    write(value, "0", 1);
    write(value, "1", 1);
  }
  close(value);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1189 <main+0x49>
lea    0xe9f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <main+0x14e>
mov    -0x8(%rbp),%edi
lea    0xe9b(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11df <main+0x9f>
lea    0xe84(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <main+0x14e>
mov    -0xc(%rbp),%edi
lea    0xe81(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1235 <main+0xf5>
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <main+0x14e>
movl   $0x0,-0x14(%rbp)
cmpl   $0xf4240,-0x14(%rbp)
jge    127f <main+0x13f>
mov    -0x10(%rbp),%edi
lea    0xe4c(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
lea    0xdc8(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    123c <main+0xfc>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8a(%rip),%r14
mov    (%r14),%esi
lea    0xea8(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     120e <main+0xce>
mov    %eax,%ebx
lea    0xea9(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    (%r14),%esi
lea    0xe8f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1217 <main+0xd7>
mov    %eax,%ebx
lea    0xe9a(%rip),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    (%r14),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1220 <main+0xe0>
mov    %eax,%ebx
mov    $0xf4240,%ebp
lea    0xe87(%rip),%r14
lea    0xe3c(%rip),%r15
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
add    $0xffffffff,%ebp
jne    11e0 <main+0xa0>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1231 <main+0xf1>
lea    0xe73(%rip),%rdi
jmp    1227 <main+0xe7>
lea    0xe53(%rip),%rdi
jmp    1227 <main+0xe7>
lea    0xe37(%rip),%rdi
call   1030 <puts@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
@.str.1 = [23 x i8] c"fail to open exporter\0A\00"
@.str.2 = [3 x i8] c"91\00"
@.str.3 = [33 x i8] c"/sys/class/gpio/gpio91/direction\00"
@.str.4 = [24 x i8] c"fail to open direction\0A\00"
@.str.5 = [4 x i8] c"out\00"
@.str.6 = [29 x i8] c"/sys/class/gpio/gpio91/value\00"
@.str.7 = [20 x i8] c"fail to open value\0A\00"
@.str.8 = [2 x i8] c"0\00"
@.str.9 = [2 x i8] c"1\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %49
12: ; preds = %0
%13 = load i32, i32* %2
%14 = call i32 @write(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%15 = load i32, i32* %2
%16 = call i32 @close(i32 %15)
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %17)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %49
23: ; preds = %12
%24 = load i32, i32* %3
%25 = call i32 @write(i32 %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 3)
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* @O_WRONLY
%29 = call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %28)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %1
br label %49
34: ; preds = %23
store i32 0, i32* %5
br label %35
35: ; preds = %43, %34
%36 = load i32, i32* %5
%37 = icmp slt i32 %36, 1000000
br i1 %37, label %38, label %46
38: ; preds = %35
%39 = load i32, i32* %4
%40 = call i32 @write(i32 %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
%41 = load i32, i32* %4
%42 = call i32 @write(i32 %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32 1)
br label %43
43: ; preds = %38
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
br label %35
46: ; preds = %35
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
store i32 0, i32* %1
br label %49
49: ; preds = %46, %32, %21, %10
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@.str = [23 x i8] c"/sys/class/gpio/export\00"
@O_WRONLY = global i32 0
@.str.2 = [3 x i8] c"91\00"
@.str.3 = [33 x i8] c"/sys/class/gpio/gpio91/direction\00"
@.str.5 = [4 x i8] c"out\00"
@.str.6 = [29 x i8] c"/sys/class/gpio/gpio91/value\00"
@.str.8 = [2 x i8] c"0\00"
@.str.9 = [2 x i8] c"1\00"
@str = [19 x i8] c"fail to open value\00"
@str.10 = [23 x i8] c"fail to open direction\00"
@str.11 = [22 x i8] c"fail to open exporter\00"
define i32 @main() {
%1 = load i32, i32* @O_WRONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.11, i64 0, i64 0))
br label %30
6: ; preds = %0
%7 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%8 = tail call i32 @close(i32 %2)
%9 = load i32, i32* @O_WRONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.10, i64 0, i64 0))
br label %30
14: ; preds = %6
%15 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 3)
%16 = tail call i32 @close(i32 %10)
%17 = load i32, i32* @O_WRONLY
%18 = tail call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %30
22: ; preds = %24
%23 = tail call i32 @close(i32 %18)
br label %30
24: ; preds = %14, %24
%25 = phi i32 [ %28, %24 ], [ 0, %14 ]
%26 = tail call i32 @write(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
%27 = tail call i32 @write(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32 1)
%28 = add nuw nsw i32 %25, 1
%29 = icmp eq i32 %28, 1000000
br i1 %29, label %22, label %24
30: ; preds = %12, %22, %20, %4
%31 = phi i32 [ -1, %4 ], [ -1, %12 ], [ -1, %20 ], [ 0, %22 ]
ret i32 %31
}