#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 {
  long long tv_sec;
  long long tv_nsec;
};

/* Variables and functions */
long long SYSTEM_MOD(long long, long long);
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
void Platform_Delay(long long ms) {
  long long s, ns;
  s = ((ms) >= 0 ? (ms) / (1000) : -(((1000) - 1 - (ms)) / (1000)));
  ns = ((ms) >= 0 ? (ms) % (1000)
                  : SYSTEM_MOD((long long)(ms), (long long)(1000))) *
       1000000;
  struct timespec req, rem;
  req.tv_sec = s;
  req.tv_nsec = ns;
  nanosleep(&req, &rem);
}


x86 assembly (clang, -O0):
<Platform_Delay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jl     114e <Platform_Delay+0x2e>
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x40(%rbp)
jmp    116d <Platform_Delay+0x4d>
mov    $0x3e7,%eax
sub    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
xor    %eax,%eax
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jl     119a <Platform_Delay+0x7a>
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rdx,%rax
mov    %rax,-0x48(%rbp)
jmp    11ac <Platform_Delay+0x8c>
mov    -0x8(%rbp),%rdi
mov    $0x3e8,%esi
call   1030 <SYSTEM_MOD@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
imul   $0xf4240,%rax,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1040 <nanosleep@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Platform_Delay>:
push   %rbx
sub    $0x20,%rsp
test   %rdi,%rdi
js     1151 <Platform_Delay+0x31>
mov    %rdi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
mov    %rdx,%rbx
shr    $0x4,%rbx
imul   $0x3e8,%rbx,%rax
sub    %rax,%rdi
jmp    1181 <Platform_Delay+0x61>
mov    $0x3e7,%eax
sub    %rdi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
mov    %rdx,%rbx
shr    $0x4,%rbx
neg    %rbx
mov    $0x3e8,%esi
call   1030 <SYSTEM_MOD@plt>
mov    %rax,%rdi
imul   $0xf4240,%rdi,%rax
mov    %rbx,(%rsp)
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
call   1040 <nanosleep@plt>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
define void @Platform_Delay(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
store i64 %0, i64* %2
%7 = load i64, i64* %2
%8 = icmp sge i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i64, i64* %2
%11 = sdiv i64 %10, 1000
br label %17
12: ; preds = %1
%13 = load i64, i64* %2
%14 = sub nsw i64 999, %13
%15 = sdiv i64 %14, 1000
%16 = sub nsw i64 0, %15
br label %17
17: ; preds = %12, %9
%18 = phi i64 [ %11, %9 ], [ %16, %12 ]
store i64 %18, i64* %3
%19 = load i64, i64* %2
%20 = icmp sge i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i64, i64* %2
%23 = srem i64 %22, 1000
br label %27
24: ; preds = %17
%25 = load i64, i64* %2
%26 = call i64 @SYSTEM_MOD(i64 %25, i64 1000)
br label %27
27: ; preds = %24, %21
%28 = phi i64 [ %23, %21 ], [ %26, %24 ]
%29 = mul nsw i64 %28, 1000000
store i64 %29, i64* %4
%30 = load i64, i64* %3
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i64 %30, i64* %31
%32 = load i64, i64* %4
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 %32, i64* %33
%34 = call i32 @nanosleep(%struct.timespec* %5, %struct.timespec* %6)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
define void @Platform_Delay(i64 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = icmp sgt i64 %0, -1
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = udiv i64 %0, 1000
%7 = urem i64 %0, 1000
br label %13
8: ; preds = %1
%9 = sub nsw i64 999, %0
%10 = udiv i64 %9, 1000
%11 = sub nsw i64 0, %10
%12 = tail call i64 @SYSTEM_MOD(i64 %0, i64 1000)
br label %13
13: ; preds = %8, %5
%14 = phi i64 [ %6, %5 ], [ %11, %8 ]
%15 = phi i64 [ %7, %5 ], [ %12, %8 ]
%16 = mul nsw i64 %15, 1000000
%17 = bitcast %struct.timespec* %2 to i8*
%18 = bitcast %struct.timespec* %3 to i8*
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %14, i64* %19
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %16, i64* %20
%21 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
ret void
}