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

/* Variables and functions */
scalar_t__ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
void waitms(int mstowait) {
  static struct timespec timestruct;
  timestruct.tv_sec = mstowait / 1000;
  timestruct.tv_nsec = (mstowait % 1000) * 1000000;
  if (nanosleep(&timestruct, NULL))
    perror("DANGER.. nanosleep problem! (maybe I just got a signal..)\n");
  return;
}


x86 assembly (clang, -O0):
<waitms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,0x2ef8(%rip)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%edx,%eax
mov    %eax,0x2ee5(%rip)
lea    0x2eda(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
je     1179 <waitms+0x59>
lea    0xe8c(%rip),%rdi
call   1040 <perror@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitms>:
push   %rax
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x2ef6(%rip)
lea    0x2eef(%rip),%rdi
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0x2edf(%rip)
xor    %esi,%esi
call   1030 <nanosleep@plt>
test   %rax,%rax
jne    1167 <waitms+0x47>
pop    %rax
ret
lea    0xe92(%rip),%rdi
pop    %rax
jmp    1040 <perror@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@waitms.timestruct = internal global %struct.timespec zeroinitializer
@.str = [59 x i8] c"DANGER.. nanosleep problem! (maybe I just got a signal..)\0A\00"
define void @waitms(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* %2
%4 = sdiv i32 %3, 1000
store i32 %4, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @waitms.timestruct, i32 0, i32 0)
%5 = load i32, i32* %2
%6 = srem i32 %5, 1000
%7 = mul nsw i32 %6, 1000000
store i32 %7, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @waitms.timestruct, i32 0, i32 1)
%8 = call i64 @nanosleep(%struct.timespec* @waitms.timestruct, i32* null)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@waitms.timestruct = internal global %struct.timespec zeroinitializer
@.str = [59 x i8] c"DANGER.. nanosleep problem! (maybe I just got a signal..)\0A\00"
define void @waitms(i32 %0) {
%2 = sdiv i32 %0, 1000
store i32 %2, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @waitms.timestruct, i64 0, i32 0)
%3 = srem i32 %0, 1000
%4 = mul nsw i32 %3, 1000000
store i32 %4, i32* getelementptr inbounds (%struct.timespec, %struct.timespec* @waitms.timestruct, i64 0, i32 1)
%5 = tail call i64 @nanosleep(%struct.timespec* @waitms.timestruct, i32* null)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
ret void
}