#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);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
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 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);
  test_waitsig();
  clock_gettime(CLOCK_REALTIME, &end);
  err = check_timers((end.tv_sec - start.tv_sec) * 1000 +
                     (end.tv_nsec - start.tv_nsec) / 1000000);
  if (err)
    return err;
  test_msg("PASS\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11c1 <main+0x41>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <main+0xc9>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
mov    $0x0,%al
call   1060 <test_daemon@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    $0x0,%al
call   1090 <test_waitsig@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    -0x20(%rbp),%eax
sub    -0x18(%rbp),%eax
imul   $0x3e8,%eax,%edi
mov    -0x1c(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
add    %eax,%edi
call   1070 <check_timers@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1236 <main+0xb6>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <main+0xc9>
lea    0xdc3(%rip),%rdi
call   1080 <test_msg@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
call   1030 <test_init@plt>
xor    %eax,%eax
call   1040 <setup_timers@plt>
test   %eax,%eax
jne    120f <main+0x8f>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
xor    %eax,%eax
call   1060 <test_daemon@plt>
mov    0x2e33(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1050 <clock_gettime@plt>
xor    %eax,%eax
call   1090 <test_waitsig@plt>
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1050 <clock_gettime@plt>
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
call   1070 <check_timers@plt>
test   %eax,%eax
jne    120f <main+0x8f>
lea    0xdf8(%rip),%rdi
call   1080 <test_msg@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = load i8**, i8*** %5
%11 = call i32 @test_init(i32 %9, i8** %10)
%12 = call i32 (...) @setup_timers()
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i32, i32* %8
store i32 %16, i32* %3
br label %45
17: ; preds = %2
%18 = call i32 @usleep(i32 500000)
%19 = call i32 (...) @test_daemon()
%20 = load i32, i32* @CLOCK_REALTIME
%21 = call i32 @clock_gettime(i32 %20, %struct.timespec* %6)
%22 = call i32 (...) @test_waitsig()
%23 = load i32, i32* @CLOCK_REALTIME
%24 = call i32 @clock_gettime(i32 %23, %struct.timespec* %7)
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = sub nsw i32 %26, %28
%30 = mul nsw i32 %29, 1000
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%34 = load i32, i32* %33
%35 = sub nsw i32 %32, %34
%36 = sdiv i32 %35, 1000000
%37 = add nsw i32 %30, %36
%38 = call i32 @check_timers(i32 %37)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %17
%42 = load i32, i32* %8
store i32 %42, i32* %3
br label %45
43: ; preds = %17
%44 = call i32 @test_msg(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %45
45: ; preds = %43, %41, %15
%46 = load i32, i32* %3
ret i32 %46
}

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