#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_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int check_timers(int, struct timespec *, struct timespec *);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int get_total_sleep_time(struct timespec *, char *);
int setup_timers();
int /*<<< orphan*/ test_daemon();
int /*<<< orphan*/ test_init(int, char **);
int /*<<< orphan*/ test_msg(char *);
int /*<<< orphan*/ test_waitsig();
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  struct timespec sleep_start, sleep_end;
  struct timespec start, end;
  int err;
  test_init(argc, argv);
  err = setup_timers();
  if (err)
    return err;
  usleep(500 * 1000);
  test_daemon();
  clock_gettime(CLOCK_REALTIME, &start);
  err = get_total_sleep_time(&sleep_start, "start");
  if (err)
    return err;
  test_waitsig();
  clock_gettime(CLOCK_REALTIME, &end);
  err = get_total_sleep_time(&sleep_end, "end");
  if (err)
    return err;
  err = check_timers((end.tv_sec - start.tv_sec) * 1000 +
                         (end.tv_nsec - start.tv_nsec) / 1000000,
                     &sleep_start, &sleep_end);
  if (err)
    return err;
  test_msg("PASS\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <test_init@plt>
mov    $0x0,%al
call   1040 <setup_timers@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11d1 <main+0x41>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <main+0x121>
mov    $0x7a120,%edi
call   10b0 <usleep@plt>
mov    $0x0,%al
call   1060 <test_daemon@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1050 <clock_gettime@plt>
lea    -0x18(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   1070 <get_total_sleep_time@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     121c <main+0x8c>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <main+0x121>
mov    $0x0,%al
call   10a0 <test_waitsig@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1050 <clock_gettime@plt>
lea    -0x20(%rbp),%rdi
lea    0xdc6(%rip),%rsi
call   1070 <get_total_sleep_time@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     125d <main+0xcd>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <main+0x121>
mov    -0x30(%rbp),%eax
sub    -0x28(%rbp),%eax
imul   $0x3e8,%eax,%edi
mov    -0x2c(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
add    %eax,%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <check_timers@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     129e <main+0x10e>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <main+0x121>
lea    0xd65(%rip),%rdi
call   1090 <test_msg@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
call   1030 <test_init@plt>
xor    %eax,%eax
call   1040 <setup_timers@plt>
test   %eax,%eax
jne    1257 <main+0xc7>
mov    $0x7a120,%edi
call   10b0 <usleep@plt>
xor    %eax,%eax
call   1060 <test_daemon@plt>
mov    0x2e1f(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1050 <clock_gettime@plt>
lea    0xe2c(%rip),%rsi
lea    0x18(%rsp),%rdi
call   1070 <get_total_sleep_time@plt>
test   %eax,%eax
jne    1257 <main+0xc7>
xor    %eax,%eax
call   10a0 <test_waitsig@plt>
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1050 <clock_gettime@plt>
lea    0xe0c(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1070 <get_total_sleep_time@plt>
test   %eax,%eax
jne    1257 <main+0xc7>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
sub    0x8(%rsp),%eax
imul   $0x3e8,%eax,%eax
sub    0xc(%rsp),%ecx
movslq %ecx,%rcx
imul   $0x431bde83,%rcx,%rdi
mov    %rdi,%rcx
shr    $0x3f,%rcx
sar    $0x32,%rdi
add    %ecx,%edi
add    %eax,%edi
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   1080 <check_timers@plt>
test   %eax,%eax
jne    1257 <main+0xc7>
lea    0xdba(%rip),%rdi
call   1090 <test_msg@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [6 x i8] c"start\00"
@.str.1 = [4 x i8] c"end\00"
@.str.2 = [6 x i8] c"PASS\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = alloca %struct.timespec
%9 = alloca %struct.timespec
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = load i8**, i8*** %5
%13 = call i32 @test_init(i32 %11, i8** %12)
%14 = call i32 (...) @setup_timers()
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = load i32, i32* %10
store i32 %18, i32* %3
br label %59
19: ; preds = %2
%20 = call i32 @usleep(i32 500000)
%21 = call i32 (...) @test_daemon()
%22 = load i32, i32* @CLOCK_REALTIME
%23 = call i32 @clock_gettime(i32 %22, %struct.timespec* %8)
%24 = call i32 @get_total_sleep_time(%struct.timespec* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = load i32, i32* %10
store i32 %28, i32* %3
br label %59
29: ; preds = %19
%30 = call i32 (...) @test_waitsig()
%31 = load i32, i32* @CLOCK_REALTIME
%32 = call i32 @clock_gettime(i32 %31, %struct.timespec* %9)
%33 = call i32 @get_total_sleep_time(%struct.timespec* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = load i32, i32* %10
store i32 %37, i32* %3
br label %59
38: ; preds = %29
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%42 = load i32, i32* %41
%43 = sub nsw i32 %40, %42
%44 = mul nsw i32 %43, 1000
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
%48 = load i32, i32* %47
%49 = sub nsw i32 %46, %48
%50 = sdiv i32 %49, 1000000
%51 = add nsw i32 %44, %50
%52 = call i32 @check_timers(i32 %51, %struct.timespec* %6, %struct.timespec* %7)
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %38
%56 = load i32, i32* %10
store i32 %56, i32* %3
br label %59
57: ; preds = %38
%58 = call i32 @test_msg(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %59
59: ; preds = %57, %55, %36, %27, %17
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [6 x i8] c"start\00"
@.str.1 = [4 x i8] c"end\00"
@.str.2 = [6 x i8] c"PASS\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = bitcast %struct.timespec* %3 to i8*
%8 = bitcast %struct.timespec* %4 to i8*
%9 = bitcast %struct.timespec* %5 to i8*
%10 = bitcast %struct.timespec* %6 to i8*
%11 = tail call i32 @test_init(i32 %0, i8** %1)
%12 = tail call i32 (...) @setup_timers()
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %45
14: ; preds = %2
%15 = tail call i32 @usleep(i32 500000)
%16 = tail call i32 (...) @test_daemon()
%17 = load i32, i32* @CLOCK_REALTIME
%18 = call i32 @clock_gettime(i32 %17, %struct.timespec* %5)
%19 = call i32 @get_total_sleep_time(%struct.timespec* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %45
21: ; preds = %14
%22 = call i32 (...) @test_waitsig()
%23 = load i32, i32* @CLOCK_REALTIME
%24 = call i32 @clock_gettime(i32 %23, %struct.timespec* %6)
%25 = call i32 @get_total_sleep_time(%struct.timespec* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %45
27: ; preds = %21
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%31 = load i32, i32* %30
%32 = sub nsw i32 %29, %31
%33 = mul nsw i32 %32, 1000
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 1
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
%37 = load i32, i32* %36
%38 = sub nsw i32 %35, %37
%39 = sdiv i32 %38, 1000000
%40 = add nsw i32 %39, %33
%41 = call i32 @check_timers(i32 %40, %struct.timespec* %3, %struct.timespec* %4)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %27
%44 = call i32 @test_msg(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %27, %21, %14, %2, %43
%46 = phi i32 [ 0, %43 ], [ %12, %2 ], [ %19, %14 ], [ %25, %21 ], [ %41, %27 ]
ret i32 %46
}