#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_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
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, "in", 2);
  close(direction);
  int value = open("/sys/class/gpio/gpio91/value", O_RDONLY);
  if (value < 0) {
    printf("fail to open value\n");
    return -1;
  }
  int i = 0;
  int count = 0;
  char oldc = '0';
  char c = '0';
  for (i = 0; i < 10000000 / 2; ++i) {
    read(value, &c, 1);
    lseek(value, 0, SEEK_SET);
    if (c == oldc) {
      continue;
    } else {
      oldc = c;
      ++count;
    }
  }
  printf("count is %d\n", count);
  close(value);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ab <main+0x4b>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <main+0x1a0>
mov    -0x8(%rbp),%edi
lea    0xe79(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1203 <main+0xa3>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <main+0x1a0>
mov    -0xc(%rbp),%edi
lea    0xe5d(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    125b <main+0xfb>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <main+0x1a0>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movb   $0x30,-0x19(%rbp)
movb   $0x30,-0x1a(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x4c4b40,-0x14(%rbp)
jge    12e0 <main+0x180>
mov    -0x10(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x10(%rbp),%edi
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
movsbl -0x1a(%rbp),%eax
movsbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
jne    12be <main+0x15e>
jmp    12d2 <main+0x172>
mov    -0x1a(%rbp),%al
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12d2 <main+0x172>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1278 <main+0x118>
mov    -0x18(%rbp),%esi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1060 <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   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e46(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe74(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1282 <main+0x112>
mov    %eax,%ebx
lea    0xe75(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x0(%rbp),%esi
lea    0xe5b(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     128b <main+0x11b>
mov    %eax,%ebx
lea    0xe66(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1294 <main+0x124>
mov    %eax,%r15d
movb   $0x30,0x7(%rsp)
xor    %ebp,%ebp
mov    $0x30,%r13b
mov    $0x4c4b40,%ebx
lea    0x7(%rsp),%r14
mov    0x2db5(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    (%r12),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
movzbl 0x7(%rsp),%eax
xor    %ecx,%ecx
cmp    %r13b,%al
setne  %cl
add    %ecx,%ebp
mov    %eax,%r13d
add    $0xffffffff,%ebx
jne    1230 <main+0xc0>
lea    0xdef(%rip),%rdi
xor    %r14d,%r14d
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%edi
call   1070 <close@plt>
jmp    12a6 <main+0x136>
lea    0xe09(%rip),%rdi
jmp    129b <main+0x12b>
lea    0xde9(%rip),%rdi
jmp    129b <main+0x12b>
lea    0xdcd(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 = [3 x i8] c"in\00"
@.str.6 = [29 x i8] c"/sys/class/gpio/gpio91/value\00"
@O_RDONLY = global i32 0
@.str.7 = [20 x i8] c"fail to open value\0A\00"
@SEEK_SET = global i32 0
@.str.8 = [13 x i8] c"count is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
store i32 0, i32* %1
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %66
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i32 @write(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* @O_WRONLY
%21 = call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %20)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %66
26: ; preds = %15
%27 = load i32, i32* %3
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
%29 = load i32, i32* %3
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %31)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %26
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %1
br label %66
37: ; preds = %26
store i32 0, i32* %5
store i32 0, i32* %6
store i8 48, i8* %7
store i8 48, i8* %8
store i32 0, i32* %5
br label %38
38: ; preds = %58, %37
%39 = load i32, i32* %5
%40 = icmp slt i32 %39, 5000000
br i1 %40, label %41, label %61
41: ; preds = %38
%42 = load i32, i32* %4
%43 = call i32 @read(i32 %42, i8* %8, i32 1)
%44 = load i32, i32* %4
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @lseek(i32 %44, i32 0, i32 %45)
%47 = load i8, i8* %8
%48 = sext i8 %47 to i32
%49 = load i8, i8* %7
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %48, %50
br i1 %51, label %52, label %53
52: ; preds = %41
br label %58
53: ; preds = %41
%54 = load i8, i8* %8
store i8 %54, i8* %7
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
br label %57
57: ; preds = %53
br label %58
58: ; preds = %57, %52
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 1
store i32 %60, i32* %5
br label %38
61: ; preds = %38
%62 = load i32, i32* %6
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %62)
%64 = load i32, i32* %4
%65 = call i32 @close(i32 %64)
store i32 0, i32* %1
br label %66
66: ; preds = %61, %35, %24, %13
%67 = load i32, i32* %1
ret i32 %67
}

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 = [3 x i8] c"in\00"
@.str.6 = [29 x i8] c"/sys/class/gpio/gpio91/value\00"
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str.8 = [13 x i8] c"count is %d\0A\00"
@str = [19 x i8] c"fail to open value\00"
@str.9 = [23 x i8] c"fail to open direction\00"
@str.10 = [22 x i8] c"fail to open exporter\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_WRONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.10, i64 0, i64 0))
br label %40
7: ; preds = %0
%8 = tail call i32 @write(i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%9 = tail call i32 @close(i32 %3)
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
br label %40
15: ; preds = %7
%16 = tail call i32 @write(i32 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
%17 = tail call i32 @close(i32 %11)
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %40
23: ; preds = %15
store i8 48, i8* %1
br label %24
24: ; preds = %23, %24
%25 = phi i8 [ 48, %23 ], [ %31, %24 ]
%26 = phi i32 [ 0, %23 ], [ %34, %24 ]
%27 = phi i32 [ 0, %23 ], [ %35, %24 ]
%28 = call i32 @read(i32 %19, i8* %1, i32 1)
%29 = load i32, i32* @SEEK_SET
%30 = tail call i32 @lseek(i32 %19, i32 0, i32 %29)
%31 = load i8, i8* %1
%32 = icmp ne i8 %31, %25
%33 = zext i1 %32 to i32
%34 = add nuw nsw i32 %26, %33
%35 = add nuw nsw i32 %27, 1
%36 = icmp eq i32 %35, 5000000
br i1 %36, label %37, label %24
37: ; preds = %24
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %34)
%39 = tail call i32 @close(i32 %19)
br label %40
40: ; preds = %13, %37, %21, %5
%41 = phi i32 [ -1, %5 ], [ -1, %13 ], [ -1, %21 ], [ 0, %37 ]
ret i32 %41
}