#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;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned long long, ...);
int nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
unsigned int sleep(int);
int /*<<< orphan*/ stderr;
unsigned long long timestamp_ns_now();
int usleep(int);
int main() {
  printf("Going to sleep()...\n");
  unsigned long long ns_start = timestamp_ns_now();
  unsigned int left =
      sleep((1 * 1000 * 1000 * 1000UL) / (1000 * 1000 * 1000UL));
  unsigned long long ns_stop = timestamp_ns_now();
  if (left != 0) {
    fprintf(stderr, "Error, sleep() interrupted: %ds left\n", left);
    return left;
  }
  if ((ns_stop - ns_start) < ((1 * 1000 * 1000 * 1000UL) - (500 * 1000UL)) ||
      ((ns_stop - ns_start) > ((1 * 1000 * 1000 * 1000UL) + (500 * 1000UL)))) {
    fprintf(stderr, "Error: sleep() %llu ns instead of %llu\n",
            ns_stop - ns_start, (unsigned long long)(1 * 1000 * 1000 * 1000UL));
    return 1;
  }
  printf("Going to usleep()...\n");
  ns_start = timestamp_ns_now();
  int res = usleep((500 * 1000 * 1000UL) / (1000UL));
  ns_stop = timestamp_ns_now();
  if (res != 0) {
    fprintf(stderr, "Error, usleep() returned %d\n", res);
    return res;
  }
  if ((ns_stop - ns_start) < ((500 * 1000 * 1000UL) - (500 * 1000UL)) ||
      ((ns_stop - ns_start) > ((500 * 1000 * 1000UL) + (500 * 1000UL)))) {
    fprintf(stderr, "Error: usleep() %llu ns instead of %llu\n",
            ns_stop - ns_start, (unsigned long long)(500 * 1000 * 1000UL));
    return 1;
  }
  printf("Going to nanosleep()...\n");
  ns_start = timestamp_ns_now();
  struct timespec ts;
  ts.tv_sec = 0;
  ts.tv_nsec = 1000 * 1000UL;
  res = nanosleep(&ts, NULL);
  ns_stop = timestamp_ns_now();
  if (res != 0) {
    fprintf(stderr, "Error, nanosleep() returned %d\n", res);
    return res;
  }
  if ((ns_stop - ns_start) < (1000 * 1000UL - (500 * 1000UL)) ||
      ((ns_stop - ns_start) > (1000 * 1000UL + (500 * 1000UL)))) {
    fprintf(stderr, "Error: nanosleep() %llu ns instead of %llu\n",
            ns_stop - ns_start, (unsigned long long)1000 * 1000UL);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <timestamp_ns_now@plt>
mov    %rax,-0x10(%rbp)
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1060 <timestamp_ns_now@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11cf <main+0x6f>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%eax
mov    %eax,%edx
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13ae <main+0x24e>
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
cmp    $0x3b9328e0,%rax
jb     11f7 <main+0x97>
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
cmp    $0x3ba26b20,%rax
jbe    1227 <main+0xc7>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
sub    -0x10(%rbp),%rdx
lea    0xe2c(%rip),%rsi
mov    $0x3b9aca00,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ae <main+0x24e>
lea    0xe35(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <timestamp_ns_now@plt>
mov    %rax,-0x10(%rbp)
mov    $0x7a120,%edi
call   1080 <usleep@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <timestamp_ns_now@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1286 <main+0x126>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
movslq -0x24(%rbp),%rdx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13ae <main+0x24e>
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
cmp    $0x1dc5c3e0,%rax
jb     12ae <main+0x14e>
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
cmp    $0x1dd50620,%rax
jbe    12de <main+0x17e>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
sub    -0x10(%rbp),%rdx
lea    0xdd0(%rip),%rsi
mov    $0x1dcd6500,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ae <main+0x24e>
lea    0xdda(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <timestamp_ns_now@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0xf4240,-0x38(%rbp)
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <nanosleep@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <timestamp_ns_now@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     134f <main+0x1ef>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
movslq -0x24(%rbp),%rdx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13ae <main+0x24e>
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
cmp    $0x7a120,%rax
jb     1377 <main+0x217>
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
cmp    $0x16e360,%rax
jbe    13a7 <main+0x247>
mov    0x2c62(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
sub    -0x10(%rbp),%rdx
lea    0xd69(%rip),%rsi
mov    $0xf4240,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ae <main+0x24e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xf71(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <timestamp_ns_now@plt>
mov    %rax,%rbp
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <timestamp_ns_now@plt>
test   %ebx,%ebx
je     11b0 <main+0x50>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%edx
lea    0xe59(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11e7 <main+0x87>
sub    %rbp,%rax
lea    -0x3ba26b21(%rax),%rcx
cmp    $0xfffffffffff0bdbe,%rcx
ja     11f2 <main+0x92>
mov    0x2e16(%rip),%rcx
mov    (%rcx),%edi
lea    0xe53(%rip),%rsi
mov    $0x3b9aca00,%ecx
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xefb(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <timestamp_ns_now@plt>
mov    %rax,%rbp
mov    $0x7a120,%edi
call   1080 <usleep@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <timestamp_ns_now@plt>
test   %ebx,%ebx
je     1237 <main+0xd7>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
movslq %ebx,%rdx
lea    0xe1c(%rip),%rsi
jmp    11a7 <main+0x47>
sub    %rbp,%rax
lea    -0x1dd50621(%rax),%rcx
cmp    $0xfffffffffff0bdbe,%rcx
ja     1264 <main+0x104>
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%edi
lea    0xe11(%rip),%rsi
mov    $0x1dcd6500,%ecx
jmp    11d8 <main+0x78>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <timestamp_ns_now@plt>
mov    %rax,%r14
movq   $0x0,0x8(%rsp)
movl   $0xf4240,(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <timestamp_ns_now@plt>
test   %ebp,%ebp
je     12c4 <main+0x164>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
movslq %ebp,%rdx
lea    0xdde(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%ebx
jmp    11e7 <main+0x87>
sub    %r14,%rax
lea    -0x16e361(%rax),%rcx
cmp    $0xfffffffffff0bdbe,%rcx
ja     11e7 <main+0x87>
mov    0x2cfe(%rip),%rcx
mov    (%rcx),%edi
lea    0xdc9(%rip),%rsi
mov    $0xf4240,%ecx
jmp    11d8 <main+0x78>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@.str = [21 x i8] c"Going to sleep()...\0A\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"Error, sleep() interrupted: %ds left\0A\00"
@.str.2 = [40 x i8] c"Error: sleep() %llu ns instead of %llu\0A\00"
@.str.3 = [22 x i8] c"Going to usleep()...\0A\00"
@.str.4 = [29 x i8] c"Error, usleep() returned %d\0A\00"
@.str.5 = [41 x i8] c"Error: usleep() %llu ns instead of %llu\0A\00"
@.str.6 = [25 x i8] c"Going to nanosleep()...\0A\00"
@.str.7 = [32 x i8] c"Error, nanosleep() returned %d\0A\00"
@.str.8 = [44 x i8] c"Error: nanosleep() %llu ns instead of %llu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca %struct.timespec
store i32 0, i32* %1
%7 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%8 = call i64 (...) @timestamp_ns_now()
store i64 %8, i64* %2
%9 = call i32 @sleep(i32 1)
store i32 %9, i32* %3
%10 = call i64 (...) @timestamp_ns_now()
store i64 %10, i64* %4
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = load i32, i32* %3
%16 = zext i32 %15 to i64
%17 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i64 %16)
%18 = load i32, i32* %3
store i32 %18, i32* %1
br label %96
19: ; preds = %0
%20 = load i64, i64* %4
%21 = load i64, i64* %2
%22 = sub i64 %20, %21
%23 = icmp ult i64 %22, 999500000
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load i64, i64* %4
%26 = load i64, i64* %2
%27 = sub i64 %25, %26
%28 = icmp ugt i64 %27, 1000500000
br i1 %28, label %29, label %35
29: ; preds = %24, %19
%30 = load i32, i32* @stderr
%31 = load i64, i64* %4
%32 = load i64, i64* %2
%33 = sub i64 %31, %32
%34 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i64 %33, i64 1000000000)
store i32 1, i32* %1
br label %96
35: ; preds = %24
%36 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%37 = call i64 (...) @timestamp_ns_now()
store i64 %37, i64* %2
%38 = call i32 @usleep(i32 500000)
store i32 %38, i32* %5
%39 = call i64 (...) @timestamp_ns_now()
store i64 %39, i64* %4
%40 = load i32, i32* %5
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %35
%43 = load i32, i32* @stderr
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i64 %45)
%47 = load i32, i32* %5
store i32 %47, i32* %1
br label %96
48: ; preds = %35
%49 = load i64, i64* %4
%50 = load i64, i64* %2
%51 = sub i64 %49, %50
%52 = icmp ult i64 %51, 499500000
br i1 %52, label %58, label %53
53: ; preds = %48
%54 = load i64, i64* %4
%55 = load i64, i64* %2
%56 = sub i64 %54, %55
%57 = icmp ugt i64 %56, 500500000
br i1 %57, label %58, label %64
58: ; preds = %53, %48
%59 = load i32, i32* @stderr
%60 = load i64, i64* %4
%61 = load i64, i64* %2
%62 = sub i64 %60, %61
%63 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i64 %62, i64 500000000)
store i32 1, i32* %1
br label %96
64: ; preds = %53
%65 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%66 = call i64 (...) @timestamp_ns_now()
store i64 %66, i64* %2
%67 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 0, i64* %67
%68 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 1000000, i32* %68
%69 = call i32 @nanosleep(%struct.timespec* %6, i32* null)
store i32 %69, i32* %5
%70 = call i64 (...) @timestamp_ns_now()
store i64 %70, i64* %4
%71 = load i32, i32* %5
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %79
73: ; preds = %64
%74 = load i32, i32* @stderr
%75 = load i32, i32* %5
%76 = sext i32 %75 to i64
%77 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i64 %76)
%78 = load i32, i32* %5
store i32 %78, i32* %1
br label %96
79: ; preds = %64
%80 = load i64, i64* %4
%81 = load i64, i64* %2
%82 = sub i64 %80, %81
%83 = icmp ult i64 %82, 500000
br i1 %83, label %89, label %84
84: ; preds = %79
%85 = load i64, i64* %4
%86 = load i64, i64* %2
%87 = sub i64 %85, %86
%88 = icmp ugt i64 %87, 1500000
br i1 %88, label %89, label %95
89: ; preds = %84, %79
%90 = load i32, i32* @stderr
%91 = load i64, i64* %4
%92 = load i64, i64* %2
%93 = sub i64 %91, %92
%94 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %90, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0), i64 %93, i64 1000000)
store i32 1, i32* %1
br label %96
95: ; preds = %84
store i32 0, i32* %1
br label %96
96: ; preds = %95, %89, %73, %58, %42, %29, %13
%97 = load i32, i32* %1
ret i32 %97
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@stderr = global i32 0
@.str.1 = [38 x i8] c"Error, sleep() interrupted: %ds left\0A\00"
@.str.2 = [40 x i8] c"Error: sleep() %llu ns instead of %llu\0A\00"
@.str.4 = [29 x i8] c"Error, usleep() returned %d\0A\00"
@.str.5 = [41 x i8] c"Error: usleep() %llu ns instead of %llu\0A\00"
@.str.7 = [32 x i8] c"Error, nanosleep() returned %d\0A\00"
@.str.8 = [44 x i8] c"Error: nanosleep() %llu ns instead of %llu\0A\00"
@str = [20 x i8] c"Going to sleep()...\00"
@str.9 = [21 x i8] c"Going to usleep()...\00"
@str.10 = [24 x i8] c"Going to nanosleep()...\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%3 = tail call i64 (...) @timestamp_ns_now()
%4 = tail call i32 @sleep(i32 1)
%5 = tail call i64 (...) @timestamp_ns_now()
%6 = icmp eq i32 %4, 0
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = zext i32 %4 to i64
%10 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i64 %9)
br label %57
11: ; preds = %0
%12 = sub i64 %5, %3
%13 = add i64 %12, -1000500001
%14 = icmp ult i64 %13, -1000001
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i64 %12, i64 1000000000)
br label %57
18: ; preds = %11
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
%20 = tail call i64 (...) @timestamp_ns_now()
%21 = tail call i32 @usleep(i32 500000)
%22 = tail call i64 (...) @timestamp_ns_now()
%23 = icmp eq i32 %21, 0
br i1 %23, label %28, label %24
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = sext i32 %21 to i64
%27 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i64 %26)
br label %57
28: ; preds = %18
%29 = sub i64 %22, %20
%30 = add i64 %29, -500500001
%31 = icmp ult i64 %30, -1000001
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i64 %29, i64 500000000)
br label %57
35: ; preds = %28
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.10, i64 0, i64 0))
%37 = tail call i64 (...) @timestamp_ns_now()
%38 = bitcast %struct.timespec* %1 to i8*
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i64 0, i64* %39
%40 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 1000000, i32* %40
%41 = call i32 @nanosleep(%struct.timespec* %1, i32* null)
%42 = call i64 (...) @timestamp_ns_now()
%43 = icmp eq i32 %41, 0
br i1 %43, label %48, label %44
44: ; preds = %35
%45 = load i32, i32* @stderr
%46 = sext i32 %41 to i64
%47 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i64 %46)
br label %55
48: ; preds = %35
%49 = sub i64 %42, %37
%50 = add i64 %49, -1500001
%51 = icmp ult i64 %50, -1000001
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0), i64 %49, i64 1000000)
br label %55
55: ; preds = %48, %52, %44
%56 = phi i32 [ %41, %44 ], [ 1, %52 ], [ 0, %48 ]
br label %57
57: ; preds = %24, %32, %55, %15, %7
%58 = phi i32 [ %4, %7 ], [ 1, %15 ], [ %21, %24 ], [ 1, %32 ], [ %56, %55 ]
ret i32 %58
}