#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;
};
typedef int /*<<< orphan*/ mtx_t;
typedef int /*<<< orphan*/ cnd_t;

/* Variables and functions */
scalar_t__ ETIMEDOUT;
scalar_t__ cond_wait_timed(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
scalar_t__ errno;
int thrd_error;
int thrd_success;
int thrd_timedout;
int cnd_timedwait(cnd_t *restrict cond, mtx_t *restrict mtx,
                  const struct timespec *restrict ts) {
  int ms = 0;
  ms = ts->tv_sec * 1000 + ts->tv_nsec / 1000000;
  if (ts->tv_nsec % 1000000)
    ++ms;
  if (cond_wait_timed(cond, mtx, ms)) {
    if (errno == ETIMEDOUT)
      return thrd_timedout;
    return thrd_error;
  }
  return thrd_success;
}


x86 assembly (clang, -O0):
<cnd_timedwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
imul   $0x3e8,(%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1172 <cnd_timedwait+0x62>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <cond_wait_timed@plt>
cmp    $0x0,%rax
je     11c8 <cnd_timedwait+0xb8>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11b7 <cnd_timedwait+0xa7>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <cnd_timedwait+0xc4>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <cnd_timedwait+0xc4>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cnd_timedwait>:
push   %rax
imul   $0x3e8,(%rdx),%r8d
movslq 0x4(%rdx),%rcx
imul   $0x431bde83,%rcx,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x32,%rdx
add    %eax,%edx
imul   $0x68c26139,%ecx,%eax
add    $0x218c0,%eax
ror    $0x6,%eax
add    %r8d,%edx
cmp    $0x10c7,%eax
sbb    $0xffffffff,%edx
call   1030 <cond_wait_timed@plt>
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e61(%rip),%rdx
cmp    (%rdx),%rcx
jne    1172 <cnd_timedwait+0x62>
mov    0x2e6d(%rip),%rcx
test   %rax,%rax
je     117e <cnd_timedwait+0x6e>
jmp    1185 <cnd_timedwait+0x75>
mov    0x2e4f(%rip),%rcx
test   %rax,%rax
jne    1185 <cnd_timedwait+0x75>
mov    0x2e63(%rip),%rcx
mov    (%rcx),%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@ETIMEDOUT = global i64 0
@thrd_timedout = global i32 0
@thrd_error = global i32 0
@thrd_success = global i32 0
define i32 @cnd_timedwait(i32* noalias %0, i32* noalias %1, %struct.timespec* noalias %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.timespec*
%8 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store %struct.timespec* %2, %struct.timespec** %7
store i32 0, i32* %8
%9 = load %struct.timespec*, %struct.timespec** %7
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = mul nsw i32 %11, 1000
%13 = load %struct.timespec*, %struct.timespec** %7
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = sdiv i32 %15, 1000000
%17 = add nsw i32 %12, %16
store i32 %17, i32* %8
%18 = load %struct.timespec*, %struct.timespec** %7
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = srem i32 %20, 1000000
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
br label %26
26: ; preds = %23, %3
%27 = load i32*, i32** %5
%28 = load i32*, i32** %6
%29 = load i32, i32* %8
%30 = call i64 @cond_wait_timed(i32* %27, i32* %28, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %26
%33 = load i64, i64* @errno
%34 = load i64, i64* @ETIMEDOUT
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i32, i32* @thrd_timedout
store i32 %37, i32* %4
br label %42
38: ; preds = %32
%39 = load i32, i32* @thrd_error
store i32 %39, i32* %4
br label %42
40: ; preds = %26
%41 = load i32, i32* @thrd_success
store i32 %41, i32* %4
br label %42
42: ; preds = %40, %38, %36
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@ETIMEDOUT = global i64 0
@thrd_timedout = global i32 0
@thrd_error = global i32 0
@thrd_success = global i32 0
define i32 @cnd_timedwait(i32* noalias %0, i32* noalias %1, %struct.timespec* noalias nocapture %2) {
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%5 = load i32, i32* %4
%6 = mul nsw i32 %5, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = sdiv i32 %8, 1000000
%10 = add nsw i32 %9, %6
%11 = srem i32 %8, 1000000
%12 = icmp ne i32 %11, 0
%13 = zext i1 %12 to i32
%14 = add nsw i32 %10, %13
%15 = tail call i64 @cond_wait_timed(i32* %0, i32* %1, i32 %14)
%16 = icmp eq i64 %15, 0
%17 = load i64, i64* @errno
%18 = load i64, i64* @ETIMEDOUT
%19 = icmp eq i64 %17, %18
%20 = load i32, i32* @thrd_success
%21 = load i32, i32* @thrd_timedout
%22 = load i32, i32* @thrd_error
%23 = select i1 %19, i32 %21, i32 %22
%24 = select i1 %16, i32 %20, i32 %23
ret i32 %24
}