#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;
  unsigned long long tv_nsec;
};
typedef int /*<<< orphan*/ pthread_mutex_t;
typedef int /*<<< orphan*/ pthread_condattr_t;
typedef int /*<<< orphan*/ pthread_cond_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int ETIMEDOUT;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, unsigned long long, int);
int /*<<< orphan*/ pthread_cond_init(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int pthread_cond_timedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                           struct timespec *);
int /*<<< orphan*/ pthread_condattr_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_condattr_setclock(int /*<<< orphan*/ *,
                                             int /*<<< orphan*/);
int /*<<< orphan*/ pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main() {
  typedef unsigned long long u64;
  pthread_mutex_t m;
  pthread_cond_t c;
  pthread_condattr_t at;
  struct timespec ts0, ts1, ts2;
  int res;
  u64 sleep;
  pthread_mutex_init(&m, 0);
  pthread_condattr_init(&at);
  pthread_condattr_setclock(&at, CLOCK_MONOTONIC);
  pthread_cond_init(&c, &at);
  clock_gettime(CLOCK_MONOTONIC, &ts0);
  ts1 = ts0;
  ts1.tv_sec += 2;
  pthread_mutex_lock(&m);
  do {
    res = pthread_cond_timedwait(&c, &m, &ts1);
  } while (res == 0);
  pthread_mutex_unlock(&m);
  clock_gettime(CLOCK_MONOTONIC, &ts2);
  sleep = (u64)ts2.tv_sec * 1000000000 + ts2.tv_nsec -
          ((u64)ts0.tv_sec * 1000000000 + ts0.tv_nsec);
  if (res != ETIMEDOUT)
    exit(printf("bad return value %d, want %d\n", res, ETIMEDOUT));
  if (sleep < 1000000000)
    exit(printf("bad sleep duration %lluns, want %dns\n", sleep, 1000000000));
  fprintf(stderr, "OK\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
call   10c0 <pthread_mutex_init@plt>
lea    -0x10(%rbp),%rdi
call   1090 <pthread_condattr_init@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1050 <pthread_condattr_setclock@plt>
lea    -0xc(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <pthread_cond_init@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x30(%rbp)
lea    -0x8(%rbp),%rdi
call   10d0 <pthread_mutex_lock@plt>
lea    -0xc(%rbp),%rdi
lea    -0x8(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10a0 <pthread_cond_timedwait@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1226 <main+0x76>
lea    -0x8(%rbp),%rdi
call   1080 <pthread_mutex_unlock@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1030 <clock_gettime@plt>
movslq -0x40(%rbp),%rax
imul   $0x3b9aca00,%rax,%rax
add    -0x38(%rbp),%rax
movslq -0x20(%rbp),%rcx
imul   $0x3b9aca00,%rcx,%rcx
add    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x44(%rbp),%eax
mov    0x2d42(%rip),%rcx
cmp    (%rcx),%eax
je     12b6 <main+0x106>
movslq -0x44(%rbp),%rsi
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
lea    0xd56(%rip),%rdi
call   1040 <printf@plt>
mov    %eax,%edi
call   10b0 <exit@plt>
cmpq   $0x3b9aca00,-0x50(%rbp)
3b
jae    12e0 <main+0x130>
mov    -0x50(%rbp),%rsi
lea    0xd4f(%rip),%rdi
mov    $0x3b9aca00,%edx
call   1040 <printf@plt>
mov    %eax,%edi
call   10b0 <exit@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
lea    0x4(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   10c0 <pthread_mutex_init@plt>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <pthread_condattr_init@plt>
mov    0x2e09(%rip),%r12
mov    (%r12),%esi
mov    %rbx,%rdi
call   1050 <pthread_condattr_setclock@plt>
lea    0x1c(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <pthread_cond_init@plt>
mov    (%r12),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
movups 0x8(%rsp),%xmm0
movaps %xmm0,0x20(%rsp)
addl   $0x2,0x20(%rsp)
mov    %r14,%rdi
call   10d0 <pthread_mutex_lock@plt>
lea    0x20(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   10a0 <pthread_cond_timedwait@plt>
test   %eax,%eax
je     1230 <main+0x80>
mov    %eax,%ebp
lea    0x4(%rsp),%rdi
call   1080 <pthread_mutex_unlock@plt>
mov    (%r12),%edi
lea    0x30(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
cmp    %edx,%ebp
jne    12b7 <main+0x107>
movslq 0x30(%rsp),%rax
mov    0x38(%rsp),%rsi
movslq 0x8(%rsp),%rcx
sub    %rcx,%rax
imul   $0x3b9aca00,%rax,%rax
sub    0x10(%rsp),%rsi
add    %rax,%rsi
cmp    $0x3b9ac9ff,%rsi
jbe    12c3 <main+0x113>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
movslq %ebp,%rsi
lea    0xd3f(%rip),%rdi
jmp    12cf <main+0x11f>
lea    0xd54(%rip),%rdi
mov    $0x3b9aca00,%edx
call   1040 <printf@plt>
mov    %eax,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
@ETIMEDOUT = global i32 0
@.str = [30 x i8] c"bad return value %d, want %d\0A\00"
@.str.1 = [38 x i8] c"bad sleep duration %lluns, want %dns\0A\00"
@stderr = global i32 0
@.str.2 = [4 x i8] c"OK\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = alloca i32
%9 = alloca i64
store i32 0, i32* %1
%10 = call i32 @pthread_mutex_init(i32* %2, i32 0)
%11 = call i32 @pthread_condattr_init(i32* %4)
%12 = load i32, i32* @CLOCK_MONOTONIC
%13 = call i32 @pthread_condattr_setclock(i32* %4, i32 %12)
%14 = call i32 @pthread_cond_init(i32* %3, i32* %4)
%15 = load i32, i32* @CLOCK_MONOTONIC
%16 = call i32 @clock_gettime(i32 %15, %struct.timespec* %5)
%17 = bitcast %struct.timespec* %6 to i8*
%18 = bitcast %struct.timespec* %5 to i8*
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%20 = load i32, i32* %19
%21 = add nsw i32 %20, 2
store i32 %21, i32* %19
%22 = call i32 @pthread_mutex_lock(i32* %2)
br label %23
23: ; preds = %25, %0
%24 = call i32 @pthread_cond_timedwait(i32* %3, i32* %2, %struct.timespec* %6)
store i32 %24, i32* %8
br label %25
25: ; preds = %23
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, 0
br i1 %27, label %23, label %28
28: ; preds = %25
%29 = call i32 @pthread_mutex_unlock(i32* %2)
%30 = load i32, i32* @CLOCK_MONOTONIC
%31 = call i32 @clock_gettime(i32 %30, %struct.timespec* %7)
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%33 = load i32, i32* %32
%34 = sext i32 %33 to i64
%35 = mul i64 %34, 1000000000
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%37 = load i64, i64* %36
%38 = add i64 %35, %37
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%40 = load i32, i32* %39
%41 = sext i32 %40 to i64
%42 = mul i64 %41, 1000000000
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%44 = load i64, i64* %43
%45 = add i64 %42, %44
%46 = sub i64 %38, %45
store i64 %46, i64* %9
%47 = load i32, i32* %8
%48 = load i32, i32* @ETIMEDOUT
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %56
50: ; preds = %28
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = load i32, i32* @ETIMEDOUT
%54 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %52, i32 %53)
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %28
%57 = load i64, i64* %9
%58 = icmp ult i64 %57, 1000000000
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = load i64, i64* %9
%61 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i64 %60, i32 1000000000)
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %56
%64 = load i32, i32* @stderr
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
@ETIMEDOUT = global i32 0
@.str = [30 x i8] c"bad return value %d, want %d\0A\00"
@.str.1 = [38 x i8] c"bad sleep duration %lluns, want %dns\0A\00"
@stderr = global i32 0
@.str.2 = [4 x i8] c"OK\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = bitcast i32* %1 to i8*
%8 = bitcast i32* %2 to i8*
%9 = bitcast i32* %3 to i8*
%10 = bitcast %struct.timespec* %4 to i8*
%11 = bitcast %struct.timespec* %5 to i8*
%12 = bitcast %struct.timespec* %6 to i8*
%13 = call i32 @pthread_mutex_init(i32* %1, i32 0)
%14 = call i32 @pthread_condattr_init(i32* %3)
%15 = load i32, i32* @CLOCK_MONOTONIC
%16 = call i32 @pthread_condattr_setclock(i32* %3, i32 %15)
%17 = call i32 @pthread_cond_init(i32* %2, i32* %3)
%18 = load i32, i32* @CLOCK_MONOTONIC
%19 = call i32 @clock_gettime(i32 %18, %struct.timespec* %4)
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 2
store i32 %22, i32* %20
%23 = call i32 @pthread_mutex_lock(i32* %1)
br label %24
24: ; preds = %24, %0
%25 = call i32 @pthread_cond_timedwait(i32* %2, i32* %1, %struct.timespec* %5)
%26 = icmp eq i32 %25, 0
br i1 %26, label %24, label %27
27: ; preds = %24
%28 = call i32 @pthread_mutex_unlock(i32* %1)
%29 = load i32, i32* @CLOCK_MONOTONIC
%30 = call i32 @clock_gettime(i32 %29, %struct.timespec* %6)
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 0
%32 = load i32, i32* %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 1
%35 = load i64, i64* %34
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%37 = load i32, i32* %36
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%40 = load i64, i64* %39
%41 = sub nsw i64 %33, %38
%42 = mul nsw i64 %41, 1000000000
%43 = sub i64 %35, %40
%44 = add i64 %43, %42
%45 = load i32, i32* @ETIMEDOUT
%46 = icmp eq i32 %25, %45
br i1 %46, label %51, label %47
47: ; preds = %27
%48 = sext i32 %25 to i64
%49 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %48, i32 %45)
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %27
%52 = icmp ult i64 %44, 1000000000
br i1 %52, label %53, label %56
53: ; preds = %51
%54 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i64 %44, i32 1000000000)
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %51
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}