#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 ms) {
  struct timespec delay;
  if (ms > 999) {
    delay.tv_sec = ms / 1000;
    delay.tv_nsec = (ms % 1000) * 1000000L;
  } else {
    delay.tv_sec = 0;
    delay.tv_nsec = (long)ms * 1000000;
  }
  if (nanosleep(&delay, NULL)) {
    perror("nanosleep");
  }
}


x86 assembly (clang, -O0):
<waitms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x3e7,-0x4(%rbp)
jle    1163 <waitms+0x43>
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
imul   $0xf4240,%rax,%rax
mov    %eax,-0xc(%rbp)
jmp    1178 <waitms+0x58>
movl   $0x0,-0x10(%rbp)
movslq -0x4(%rbp),%rax
imul   $0xf4240,%rax,%rax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
je     119b <waitms+0x7b>
lea    0xe6a(%rip),%rdi
call   1040 <perror@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitms>:
xor    %eax,%eax
cmp    $0x3e8,%edi
jl     113f <waitms+0x1f>
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%ecx
sub    %ecx,%edi
push   %rax
imul   $0xf4240,%edi,%ecx
mov    %eax,(%rsp)
mov    %ecx,0x4(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
test   %rax,%rax
jne    115e <waitms+0x3e>
pop    %rax
ret
lea    0xe9b(%rip),%rdi
call   1040 <perror@plt>
pop    %rax
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [10 x i8] c"nanosleep\00"
define void @waitms(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp sgt i32 %4, 999
br i1 %5, label %6, label %16
6: ; preds = %1
%7 = load i32, i32* %2
%8 = sdiv i32 %7, 1000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* %2
%11 = srem i32 %10, 1000
%12 = sext i32 %11 to i64
%13 = mul nsw i64 %12, 1000000
%14 = trunc i64 %13 to i32
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %14, i32* %15
br label %23
16: ; preds = %1
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 0, i32* %17
%18 = load i32, i32* %2
%19 = sext i32 %18 to i64
%20 = mul nsw i64 %19, 1000000
%21 = trunc i64 %20 to i32
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %21, i32* %22
br label %23
23: ; preds = %16, %6
%24 = call i64 @nanosleep(%struct.timespec* %3, i32* null)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str = [10 x i8] c"nanosleep\00"
define void @waitms(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = icmp sgt i32 %0, 999
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = udiv i32 %0, 1000
%7 = urem i32 %0, 1000
br label %8
8: ; preds = %1, %5
%9 = phi i32 [ %6, %5 ], [ 0, %1 ]
%10 = phi i32 [ %7, %5 ], [ %0, %1 ]
%11 = mul i32 %10, 1000000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %9, i32* %12
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %11, i32* %13
%14 = call i64 @nanosleep(%struct.timespec* %2, i32* null)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %8
%17 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %8
ret void
}