#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 */
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
void OS_Delay(int ms) {
  struct timespec ts;
  ts.tv_sec = ms / 1000;
  ts.tv_nsec = (ms % 1000) * 1000;
  while (ts.tv_sec || ts.tv_nsec)
    nanosleep(&ts, &ts);
}


x86 assembly (clang, -O0):
<OS_Delay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0xc(%rbp)
mov    $0x1,%al
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x11(%rbp)
jne    1156 <OS_Delay+0x46>
cmpl   $0x0,-0xc(%rbp)
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1166 <OS_Delay+0x56>
jmp    1177 <OS_Delay+0x67>
lea    -0x10(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
jmp    113d <OS_Delay+0x2d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OS_Delay>:
push   %rbx
sub    $0x10,%rsp
movslq %edi,%rcx
imul   $0x10624dd3,%rcx,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
mov    %eax,0x8(%rsp)
imul   $0x3e8,%eax,%edx
mov    %ecx,%eax
sub    %edx,%eax
imul   $0x3e8,%eax,%edx
mov    %edx,0xc(%rsp)
add    $0xfffffc18,%ecx
cmp    $0xfffff831,%ecx
jb     1156 <OS_Delay+0x46>
test   %eax,%eax
je     1175 <OS_Delay+0x65>
lea    0x8(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
mov    0x8(%rsp),%eax
or     0xc(%rsp),%eax
jne    1160 <OS_Delay+0x50>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define void @OS_Delay(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = sdiv i32 %4, 1000
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %5, i32* %6
%7 = load i32, i32* %2
%8 = srem i32 %7, 1000
%9 = mul nsw i32 %8, 1000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %9, i32* %10
br label %11
11: ; preds = %21, %1
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%13 = load i32, i32* %12
%14 = icmp ne i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%17 = load i32, i32* %16
%18 = icmp ne i32 %17, 0
br label %19
19: ; preds = %15, %11
%20 = phi i1 [ true, %11 ], [ %18, %15 ]
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
br label %11
23: ; preds = %19
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define void @OS_Delay(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = sdiv i32 %0, 1000
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %4, i32* %5
%6 = srem i32 %0, 1000
%7 = mul nsw i32 %6, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %7, i32* %8
%9 = add i32 %0, -1000
%10 = icmp ult i32 %9, -1999
%11 = icmp ne i32 %6, 0
%12 = or i1 %10, %11
br i1 %12, label %13, label %20
13: ; preds = %1, %13
%14 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
%17 = load i32, i32* %8
%18 = icmp ne i32 %17, 0
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %13, label %20
20: ; preds = %13, %1
ret void
}