#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 */
struct timespec {
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ TIMER_ABSTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ overflow(struct timespec *);
int /*<<< orphan*/ printf(char *);
int write(int, char *, int);
void *thread_blink(void *ptr) {
  int fd;
  int periode = 200000000;
  struct timespec t;
  clock_gettime(CLOCK_MONOTONIC, &t);
  t.tv_nsec += periode;
  overflow(&t);
  while (1) {
    clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
    fd = open("/sys/class/gpio/gpio18/direction", O_WRONLY);
    if (fd < 0) {
      printf("Can not open direction file.\n");
      exit(1);
    }
    if (write(fd, "out\n", 4) != 4) {
      printf("Can not write in direction file\n");
      exit(1);
    }
    close(fd);
    t.tv_nsec += periode;
    overflow(&t);
    clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
    fd = open("/sys/class/gpio/gpio18/direction", O_WRONLY);
    if (fd < 0) {
      printf("Can not open direction file.s\n");
      exit(1);
    }
    if (write(fd, "in\n", 3) != 3) {
      printf("Can not write in direction file.\n");
      exit(1);
    }
    close(fd);
    t.tv_nsec += periode;
    overflow(&t);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<thread_blink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xbebc200,-0x10(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x10(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <overflow@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10a0 <clock_nanosleep@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    0xe1a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    120e <thread_blink+0x8e>
lea    0xe22(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
lea    0xe27(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0x4,%eax
je     1241 <thread_blink+0xc1>
lea    0xe12(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <overflow@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10a0 <clock_nanosleep@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
lea    0xd76(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12b2 <thread_blink+0x132>
lea    0xdc2(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
lea    0xdc8(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
je     12e5 <thread_blink+0x165>
lea    0xdb2(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <overflow@plt>
jmp    11b7 <thread_blink+0x37>

x86 assembly (clang, -O2):
<thread_blink>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e4e(%rip),%r13
mov    0x0(%r13),%edi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1040 <clock_gettime@plt>
addl   $0xbebc200,(%rsp)
mov    %rbx,%rdi
call   1070 <overflow@plt>
mov    0x0(%r13),%edi
mov    0x2e2d(%rip),%r14
mov    (%r14),%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   10a0 <clock_nanosleep@plt>
mov    0x2df9(%rip),%r12
mov    (%r12),%esi
lea    0xe26(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
jns    11f9 <thread_blink+0x79>
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xdfb(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
lea    0xe08(%rip),%rsi
mov    $0x4,%edx
call   1050 <write@plt>
cmp    $0x4,%eax
jne    12bf <thread_blink+0x13f>
mov    %ebp,%edi
call   1060 <close@plt>
addl   $0xbebc200,(%rsp)
mov    %rbx,%rdi
call   1070 <overflow@plt>
mov    0x0(%r13),%edi
mov    (%r14),%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   10a0 <clock_nanosleep@plt>
mov    (%r12),%esi
mov    %r15,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12cb <thread_blink+0x14b>
mov    %eax,%ebp
mov    %eax,%edi
lea    0xdb8(%rip),%rsi
mov    $0x3,%edx
call   1050 <write@plt>
cmp    $0x3,%eax
jne    12d7 <thread_blink+0x157>
mov    %ebp,%edi
call   1060 <close@plt>
addl   $0xbebc200,(%rsp)
mov    %rbx,%rdi
call   1070 <overflow@plt>
mov    0x0(%r13),%edi
mov    (%r14),%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   10a0 <clock_nanosleep@plt>
mov    (%r12),%esi
mov    %r15,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1210 <thread_blink+0x90>
jmp    11e3 <thread_blink+0x63>
lea    0xda3(%rip),%rdi
jmp    11ea <thread_blink+0x6a>
lea    0xd79(%rip),%rdi
jmp    11ea <thread_blink+0x6a>
lea    0xd4c(%rip),%rdi
jmp    11ea <thread_blink+0x6a>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
@.str = [33 x i8] c"/sys/class/gpio/gpio18/direction\00"
@O_WRONLY = global i32 0
@.str.1 = [30 x i8] c"Can not open direction file.\0A\00"
@.str.2 = [5 x i8] c"out\0A\00"
@.str.3 = [33 x i8] c"Can not write in direction file\0A\00"
@.str.4 = [31 x i8] c"Can not open direction file.s\0A\00"
@.str.5 = [4 x i8] c"in\0A\00"
@.str.6 = [34 x i8] c"Can not write in direction file.\0A\00"
define i8* @thread_blink(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec
store i8* %0, i8** %2
store i32 200000000, i32* %4
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %5)
%8 = load i32, i32* %4
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%10 = load i32, i32* %9
%11 = add nsw i32 %10, %8
store i32 %11, i32* %9
%12 = call i32 @overflow(%struct.timespec* %5)
br label %13
13: ; preds = %1, %56
%14 = load i32, i32* @CLOCK_MONOTONIC
%15 = load i32, i32* @TIMER_ABSTIME
%16 = call i32 @clock_nanosleep(i32 %14, i32 %15, %struct.timespec* %5, i32* null)
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, 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 %24
21: ; preds = %13
%22 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %13
%25 = load i32, i32* %3
%26 = call i32 @write(i32 %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%27 = icmp ne i32 %26, 4
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = load i32, i32* %3
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %4
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%36 = load i32, i32* %35
%37 = add nsw i32 %36, %34
store i32 %37, i32* %35
%38 = call i32 @overflow(%struct.timespec* %5)
%39 = load i32, i32* @CLOCK_MONOTONIC
%40 = load i32, i32* @TIMER_ABSTIME
%41 = call i32 @clock_nanosleep(i32 %39, i32 %40, %struct.timespec* %5, i32* null)
%42 = load i32, i32* @O_WRONLY
%43 = call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %42)
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %31
%47 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %31
%50 = load i32, i32* %3
%51 = call i32 @write(i32 %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 3)
%52 = icmp ne i32 %51, 3
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %49
%57 = load i32, i32* %3
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %4
%60 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%61 = load i32, i32* %60
%62 = add nsw i32 %61, %59
store i32 %62, i32* %60
%63 = call i32 @overflow(%struct.timespec* %5)
br label %13
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
@.str = [33 x i8] c"/sys/class/gpio/gpio18/direction\00"
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"out\0A\00"
@.str.5 = [4 x i8] c"in\0A\00"
@str = [33 x i8] c"Can not write in direction file.\00"
@str.7 = [30 x i8] c"Can not open direction file.s\00"
@str.8 = [32 x i8] c"Can not write in direction file\00"
@str.9 = [29 x i8] c"Can not open direction file.\00"
define noalias i8* @thread_blink(i8* nocapture readnone %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%7 = load i32, i32* %6
%8 = add nsw i32 %7, 200000000
store i32 %8, i32* %6
%9 = call i32 @overflow(%struct.timespec* %2)
%10 = load i32, i32* @CLOCK_MONOTONIC
%11 = load i32, i32* @TIMER_ABSTIME
%12 = call i32 @clock_nanosleep(i32 %10, i32 %11, %struct.timespec* %2, i32* null)
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %46, %1
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.9, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1, %46
%20 = phi i32 [ %55, %46 ], [ %14, %1 ]
%21 = call i32 @write(i32 %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%22 = icmp eq i32 %21, 4
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.8, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = call i32 @close(i32 %20)
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 200000000
store i32 %29, i32* %6
%30 = call i32 @overflow(%struct.timespec* %2)
%31 = load i32, i32* @CLOCK_MONOTONIC
%32 = load i32, i32* @TIMER_ABSTIME
%33 = call i32 @clock_nanosleep(i32 %31, i32 %32, %struct.timespec* %2, i32* null)
%34 = load i32, i32* @O_WRONLY
%35 = call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %26
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.7, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %26
%41 = call i32 @write(i32 %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 3)
%42 = icmp eq i32 %41, 3
br i1 %42, label %46, label %43
43: ; preds = %40
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %40
%47 = call i32 @close(i32 %35)
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 200000000
store i32 %49, i32* %6
%50 = call i32 @overflow(%struct.timespec* %2)
%51 = load i32, i32* @CLOCK_MONOTONIC
%52 = load i32, i32* @TIMER_ABSTIME
%53 = call i32 @clock_nanosleep(i32 %51, i32 %52, %struct.timespec* %2, i32* null)
%54 = load i32, i32* @O_WRONLY
%55 = call i32 @open(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %54)
%56 = icmp slt i32 %55, 0
br i1 %56, label %16, label %19
}