#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 timeval {
  unsigned long tv_usec;
  scalar_t__ tv_sec;
};
struct timespec {
  unsigned int tv_sec;
  unsigned int tv_nsec;
};
typedef int /*<<< orphan*/ pthread_mutex_t;
typedef int /*<<< orphan*/ pthread_cond_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int pthread_cond_timedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                           struct timespec *);
int /*<<< orphan*/ stderr;
int msleep(pthread_cond_t *cond, pthread_mutex_t *mutex, unsigned int ms) {
  struct timeval now;
  struct timespec then;
  int err;
  then.tv_sec = ms / 1000UL;
  then.tv_nsec = (ms % 1000UL) * 1000000UL;
  err = gettimeofday(&now, NULL);
  if (err) {
    fprintf(stderr, "Error getting time of day\n");
    exit(-err);
  }
  then.tv_sec += now.tv_sec;
  then.tv_nsec += now.tv_usec * 1000UL;
  then.tv_sec += then.tv_nsec / 1000000000UL;
  then.tv_nsec %= 1000000000UL;
  return pthread_cond_timedwait(cond, mutex, &then);
}


x86 assembly (clang, -O0):
<msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
imul   $0xf4240,%rdx,%rax
mov    %eax,-0x2c(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11b3 <msleep+0x73>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1040 <fprintf@plt>
xor    %edi,%edi
sub    -0x34(%rbp),%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rcx
mov    -0x30(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x30(%rbp)
imul   $0x3e8,-0x28(%rbp),%rcx
mov    -0x2c(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x3b9aca00,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rcx
mov    -0x30(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x3b9aca00,%ecx
xor    %edx,%edx
div    %rcx
mov    %edx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1050 <pthread_cond_timedwait@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msleep>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %eax,%eax
jne    11c5 <msleep+0x85>
mov    %ebp,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%ecx
sub    %ecx,%ebp
add    0x10(%rsp),%eax
imul   $0xf4240,%ebp,%ecx
imul   $0x3e8,0x8(%rsp),%edx
add    %ecx,%edx
mov    %edx,%ecx
shr    $0x9,%ecx
imul   $0x44b83,%rcx,%rcx
shr    $0x27,%rcx
add    %ecx,%eax
mov    %eax,(%rsp)
imul   $0x3b9aca00,%ecx,%eax
sub    %eax,%edx
mov    %edx,0x4(%rsp)
mov    %rsp,%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <pthread_cond_timedwait@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1040 <fprintf@plt>
neg    %ebx
mov    %ebx,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
%struct.timespec = type { i32, i32 }
@stderr = global i32 0
@.str = [27 x i8] c"Error getting time of day\0A\00"
define i32 @msleep(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca %struct.timespec
%9 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%10 = load i32, i32* %6
%11 = zext i32 %10 to i64
%12 = udiv i64 %11, 1000
%13 = trunc i64 %12 to i32
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* %6
%16 = zext i32 %15 to i64
%17 = urem i64 %16, 1000
%18 = mul i64 %17, 1000000
%19 = trunc i64 %18 to i32
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i32 %19, i32* %20
%21 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %3
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* %9
%28 = sub nsw i32 0, %27
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %3
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%32 = load i64, i64* %31
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%34 = load i32, i32* %33
%35 = zext i32 %34 to i64
%36 = add nsw i64 %35, %32
%37 = trunc i64 %36 to i32
store i32 %37, i32* %33
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%39 = load i64, i64* %38
%40 = mul i64 %39, 1000
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
%42 = load i32, i32* %41
%43 = zext i32 %42 to i64
%44 = add i64 %43, %40
%45 = trunc i64 %44 to i32
store i32 %45, i32* %41
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
%47 = load i32, i32* %46
%48 = zext i32 %47 to i64
%49 = udiv i64 %48, 1000000000
%50 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%51 = load i32, i32* %50
%52 = zext i32 %51 to i64
%53 = add i64 %52, %49
%54 = trunc i64 %53 to i32
store i32 %54, i32* %50
%55 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
%56 = load i32, i32* %55
%57 = zext i32 %56 to i64
%58 = urem i64 %57, 1000000000
%59 = trunc i64 %58 to i32
store i32 %59, i32* %55
%60 = load i32*, i32** %4
%61 = load i32*, i32** %5
%62 = call i32 @pthread_cond_timedwait(i32* %60, i32* %61, %struct.timespec* %8)
ret i32 %62
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
%struct.timespec = type { i32, i32 }
@stderr = global i32 0
@.str = [27 x i8] c"Error getting time of day\0A\00"
define i32 @msleep(i32* %0, i32* %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.timespec
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast %struct.timespec* %5 to i8*
%8 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%9 = icmp eq i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%13 = sub nsw i32 0, %8
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %3
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
%17 = urem i32 %2, 1000
%18 = mul nuw nsw i32 %17, 1000000
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%20 = udiv i32 %2, 1000
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%22 = load i64, i64* %21
%23 = trunc i64 %22 to i32
%24 = add i32 %20, %23
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%26 = load i64, i64* %25
%27 = trunc i64 %26 to i32
%28 = mul i32 %27, 1000
%29 = add i32 %28, %18
%30 = udiv i32 %29, 1000000000
%31 = add i32 %30, %24
store i32 %31, i32* %19
%32 = urem i32 %29, 1000000000
store i32 %32, i32* %16
%33 = call i32 @pthread_cond_timedwait(i32* %0, i32* %1, %struct.timespec* %5)
ret i32 %33
}