#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;
  int tv_sec;
};
typedef int /*<<< orphan*/ pthread_mutex_t;
typedef int /*<<< orphan*/ pthread_cond_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ PTHREAD_COND_INITIALIZER;
int /*<<< orphan*/ PTHREAD_MUTEX_INITIALIZER;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int pthread_cond_timedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                           struct timespec *);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int pthread_msleep(int msecond) {
  pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t cnd = PTHREAD_COND_INITIALIZER;
  struct timespec ts;
  pthread_mutex_lock(&mtx);
  clock_gettime(CLOCK_REALTIME, &ts);
  ts.tv_nsec += 1000000 * msecond;
  if (ts.tv_nsec > 1000000000) {
    int sec = ts.tv_nsec / 1000000000;
    ts.tv_sec += sec;
    ts.tv_nsec -= (sec * 1000000000);
  }
  int rc = 0;
  while (rc == 0)
    rc = pthread_cond_timedwait(&cnd, &mtx, &ts);
  pthread_mutex_unlock(&mtx);
}


x86 assembly (clang, -O0):
<pthread_msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
imul   $0xf4240,-0x8(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x3b9aca00,-0x18(%rbp)
jle    11be <pthread_msleep+0x7e>
mov    -0x18(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
imul   $0x3b9aca00,-0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11e8 <pthread_msleep+0xa8>
lea    -0x10(%rbp),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1050 <pthread_cond_timedwait@plt>
mov    %eax,-0x20(%rbp)
jmp    11c5 <pthread_msleep+0x85>
lea    -0xc(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_msleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0xc(%rsp),%rdi
call   1060 <pthread_mutex_lock@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
imul   $0xf4240,%ebx,%eax
add    0x10(%rsp),%eax
mov    %eax,0x10(%rsp)
cmp    $0x3b9aca01,%eax
jl     11b7 <pthread_msleep+0x77>
mov    %eax,%ecx
shr    $0x9,%ecx
imul   $0x44b83,%rcx,%rcx
shr    $0x27,%rcx
add    %ecx,0x14(%rsp)
imul   $0xc4653600,%ecx,%ecx
add    %eax,%ecx
mov    %ecx,0x10(%rsp)
lea    0x1c(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <pthread_cond_timedwait@plt>
test   %eax,%eax
je     11d0 <pthread_msleep+0x90>
lea    0xc(%rsp),%rdi
call   1040 <pthread_mutex_unlock@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@PTHREAD_COND_INITIALIZER = global i32 0
@CLOCK_REALTIME = global i32 0
define i32 @pthread_msleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timespec
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
%9 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %9, i32* %4
%10 = load i32, i32* @PTHREAD_COND_INITIALIZER
store i32 %10, i32* %5
%11 = call i32 @pthread_mutex_lock(i32* %4)
%12 = load i32, i32* @CLOCK_REALTIME
%13 = call i32 @clock_gettime(i32 %12, %struct.timespec* %6)
%14 = load i32, i32* %3
%15 = mul nsw i32 1000000, %14
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%17 = load i32, i32* %16
%18 = add nsw i32 %17, %15
store i32 %18, i32* %16
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%20 = load i32, i32* %19
%21 = icmp sgt i32 %20, 1000000000
br i1 %21, label %22, label %35
22: ; preds = %1
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = sdiv i32 %24, 1000000000
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%28 = load i32, i32* %27
%29 = add nsw i32 %28, %26
store i32 %29, i32* %27
%30 = load i32, i32* %7
%31 = mul nsw i32 %30, 1000000000
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%33 = load i32, i32* %32
%34 = sub nsw i32 %33, %31
store i32 %34, i32* %32
br label %35
35: ; preds = %22, %1
store i32 0, i32* %8
br label %36
36: ; preds = %39, %35
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @pthread_cond_timedwait(i32* %5, i32* %4, %struct.timespec* %6)
store i32 %40, i32* %8
br label %36
41: ; preds = %36
%42 = call i32 @pthread_mutex_unlock(i32* %4)
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@PTHREAD_COND_INITIALIZER = global i32 0
@CLOCK_REALTIME = global i32 0
define i32 @pthread_msleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = bitcast i32* %2 to i8*
%6 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %6, i32* %2
%7 = bitcast i32* %3 to i8*
%8 = load i32, i32* @PTHREAD_COND_INITIALIZER
store i32 %8, i32* %3
%9 = bitcast %struct.timespec* %4 to i8*
%10 = call i32 @pthread_mutex_lock(i32* %2)
%11 = load i32, i32* @CLOCK_REALTIME
%12 = call i32 @clock_gettime(i32 %11, %struct.timespec* %4)
%13 = mul nsw i32 %0, 1000000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%15 = load i32, i32* %14
%16 = add nsw i32 %15, %13
store i32 %16, i32* %14
%17 = icmp sgt i32 %16, 1000000000
br i1 %17, label %18, label %25
18: ; preds = %1
%19 = udiv i32 %16, 1000000000
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%21 = load i32, i32* %20
%22 = add nsw i32 %21, %19
store i32 %22, i32* %20
%23 = mul nsw i32 %19, -1000000000
%24 = add nsw i32 %23, %16
store i32 %24, i32* %14
br label %25
25: ; preds = %18, %1
br label %26
26: ; preds = %25, %26
%27 = call i32 @pthread_cond_timedwait(i32* %3, i32* %2, %struct.timespec* %4)
%28 = icmp eq i32 %27, 0
br i1 %28, label %26, label %29
29: ; preds = %26
%30 = call i32 @pthread_mutex_unlock(i32* %2)
ret i32 undef
}