#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int atoi(char *);
scalar_t__ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
int main() {
  char buf[2];
  int fd = 0;
  int size = sizeof(buf);
  int countSwitch = 0;
  int number = 1;
  while (1) {
    fd = open("/sys/class/gpio/export", O_WRONLY);
    if (fd < 0) {
      printf("Export failed\n");
      return 1;
    }
    if (write(fd, "17\n", 3) != 3) {
      printf("Write gpio 17 failed export.\n");
      return 1;
    }
    if (close(fd) != 0) {
      printf("File descriptor for export gpio 17 failed.\n");
      return 1;
    }
    fd = open("/sys/class/gpio/gpio17/value", O_RDONLY);
    if (fd < 0) {
      printf("Open value file failed.\n");
      return 1;
    }
    read(fd, buf, size);
    number = atoi(buf);
    if (number == 0) {
      countSwitch++;
      printf("Inhalt von countSwitch: %d\n", countSwitch);
      usleep(500000);
    }
    close(fd);
    fd = open("/sys/class/gpio/unexport", O_WRONLY);
    if (fd < 0) {
      printf("Unexoprt failed\n");
      return 1;
    }
    if (write(fd, "17\n", 3) != 3) {
      printf("Write gpio 17 failed unexport.\n");
      return 1;
    }
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x2,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d7 <main+0x67>
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1347 <main+0x1d7>
mov    -0xc(%rbp),%edi
lea    0xe45(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0x3,%eax
je     120e <main+0x9e>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1347 <main+0x1d7>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     123a <main+0xca>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1347 <main+0x1d7>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    0xe2a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1276 <main+0x106>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1347 <main+0x1d7>
mov    -0xc(%rbp),%edi
lea    -0x6(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1060 <read@plt>
lea    -0x6(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    12bf <main+0x14f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x7a120,%edi
call   1090 <usleep@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
lea    0xdef(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1303 <main+0x193>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1347 <main+0x1d7>
mov    -0xc(%rbp),%edi
lea    0xd19(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0x3,%eax
je     133a <main+0x1ca>
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1347 <main+0x1d7>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    119b <main+0x2b>
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    0x2e3e(%rip),%r14
mov    (%r14),%esi
lea    0xe64(%rip),%rdi
call   1080 <open@plt>
lea    0xf54(%rip),%rdi
test   %eax,%eax
js     12d8 <main+0x158>
mov    %eax,%r15d
lea    0xe5d(%rip),%r12
lea    0x6(%rsp),%rbx
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
jne    12ad <main+0x12d>
mov    %r15d,%edi
call   1060 <close@plt>
test   %rax,%rax
jne    12b6 <main+0x136>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0xe11(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12bf <main+0x13f>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1070 <read@plt>
mov    %rbx,%rdi
call   1090 <atoi@plt>
test   %eax,%eax
jne    1253 <main+0xd3>
add    $0x1,%r13d
lea    0xdf9(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%r14),%esi
lea    0xdf0(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12c8 <main+0x148>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
jne    12d1 <main+0x151>
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%r14),%esi
lea    0xd6c(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11d0 <main+0x50>
lea    0xe51(%rip),%rdi
jmp    12d8 <main+0x158>
lea    0xe2b(%rip),%rdi
jmp    12d8 <main+0x158>
lea    0xdf7(%rip),%rdi
jmp    12d8 <main+0x158>
lea    0xdd6(%rip),%rdi
jmp    12d8 <main+0x158>
lea    0xdbd(%rip),%rdi
jmp    12d8 <main+0x158>
lea    0xd95(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%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 = [15 x i8] c"Export failed\0A\00"
@.str.2 = [4 x i8] c"17\0A\00"
@.str.3 = [30 x i8] c"Write gpio 17 failed export.\0A\00"
@.str.4 = [44 x i8] c"File descriptor for export gpio 17 failed.\0A\00"
@.str.5 = [29 x i8] c"/sys/class/gpio/gpio17/value\00"
@O_RDONLY = global i32 0
@.str.6 = [25 x i8] c"Open value file failed.\0A\00"
@.str.7 = [28 x i8] c"Inhalt von countSwitch: %d\0A\00"
@.str.8 = [25 x i8] c"/sys/class/gpio/unexport\00"
@.str.9 = [17 x i8] c"Unexoprt failed\0A\00"
@.str.10 = [32 x i8] c"Write gpio 17 failed unexport.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
store i32 2, i32* %4
store i32 0, i32* %5
store i32 1, i32* %6
br label %7
7: ; preds = %0, %63
%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* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %66
14: ; preds = %7
%15 = load i32, i32* %3
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %66
20: ; preds = %14
%21 = load i32, i32* %3
%22 = call i64 @close(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %66
26: ; preds = %20
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %27)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %66
33: ; preds = %26
%34 = load i32, i32* %3
%35 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%36 = load i32, i32* %4
%37 = call i32 @read(i32 %34, i8* %35, i32 %36)
%38 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%39 = call i32 @atoi(i8* %38)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %33
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
%45 = load i32, i32* %5
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %45)
%47 = call i32 @usleep(i32 500000)
br label %48
48: ; preds = %42, %33
%49 = load i32, i32* %3
%50 = call i64 @close(i32 %49)
%51 = load i32, i32* @O_WRONLY
%52 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i32 %51)
store i32 %52, i32* %3
%53 = load i32, i32* %3
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %48
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %1
br label %66
57: ; preds = %48
%58 = load i32, i32* %3
%59 = call i32 @write(i32 %58, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%60 = icmp ne i32 %59, 3
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %1
br label %66
63: ; preds = %57
%64 = load i32, i32* %3
%65 = call i64 @close(i32 %64)
br label %7
66: ; preds = %61, %55, %31, %24, %18, %12
%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 = [4 x i8] c"17\0A\00"
@.str.5 = [29 x i8] c"/sys/class/gpio/gpio17/value\00"
@O_RDONLY = global i32 0
@.str.7 = [28 x i8] c"Inhalt von countSwitch: %d\0A\00"
@.str.8 = [25 x i8] c"/sys/class/gpio/unexport\00"
@str = [31 x i8] c"Write gpio 17 failed unexport.\00"
@str.11 = [16 x i8] c"Unexoprt failed\00"
@str.12 = [24 x i8] c"Open value file failed.\00"
@str.13 = [43 x i8] c"File descriptor for export gpio 17 failed.\00"
@str.14 = [29 x i8] c"Write gpio 17 failed export.\00"
@str.15 = [14 x i8] c"Export failed\00"
define i32 @main() {
%1 = alloca [2 x i8]
%2 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %40, label %6
6: ; preds = %0, %35
%7 = phi i32 [ %38, %35 ], [ %4, %0 ]
%8 = phi i32 [ %27, %35 ], [ 0, %0 ]
%9 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%10 = icmp eq i32 %9, 3
br i1 %10, label %11, label %40
11: ; preds = %6
%12 = tail call i64 @close(i32 %7)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %40
14: ; preds = %11
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %40, label %18
18: ; preds = %14
%19 = call i32 @read(i32 %16, i8* %2, i32 2)
%20 = call i32 @atoi(i8* %2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = add nsw i32 %8, 1
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %23)
%25 = tail call i32 @usleep(i32 500000)
br label %26
26: ; preds = %22, %18
%27 = phi i32 [ %23, %22 ], [ %8, %18 ]
%28 = tail call i64 @close(i32 %16)
%29 = load i32, i32* @O_WRONLY
%30 = tail call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i32 %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %40, label %32
32: ; preds = %26
%33 = tail call i32 @write(i32 %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%34 = icmp eq i32 %33, 3
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = tail call i64 @close(i32 %30)
%37 = load i32, i32* @O_WRONLY
%38 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %6
40: ; preds = %32, %26, %14, %11, %6, %35, %0
%41 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.15, i64 0, i64 0), %0 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.15, i64 0, i64 0), %35 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @str.14, i64 0, i64 0), %6 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @str.13, i64 0, i64 0), %11 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @str.12, i64 0, i64 0), %14 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.11, i64 0, i64 0), %26 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0), %32 ]
%42 = tail call i32 @puts(i8* dereferenceable(1) %41)
ret i32 1
}