#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;
  unsigned int tv_nsec;
};

/* Variables and functions */
int nanosleep(struct timespec *, int /*<<< orphan*/ *);
int os_sleep(unsigned int milliseconds) {
  struct timespec tv;
  if (milliseconds >= 1000) {
    int s = (int)(milliseconds / 1000);
    milliseconds = milliseconds - (s * 1000);
    tv.tv_sec = s;
  } else
    tv.tv_sec = 0;
  tv.tv_nsec = milliseconds * 1000000;
  return (nanosleep(&tv, NULL));
}


x86 assembly (clang, -O0):
<os_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x3e8,-0x4(%rbp)
jb     1151 <os_sleep+0x41>
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
imul   $0x3e8,-0x14(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1158 <os_sleep+0x48>
movl   $0x0,-0x10(%rbp)
imul   $0xf4240,-0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_sleep>:
xor    %eax,%eax
cmp    $0x3e8,%edi
jb     112f <os_sleep+0x1f>
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0xfffffc18,%eax,%ecx
add    %ecx,%edi
push   %rax
mov    %eax,(%rsp)
imul   $0xf4240,%edi,%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
pop    %rcx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define i32 @os_sleep(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp uge i32 %5, 1000
br i1 %6, label %7, label %16
7: ; preds = %1
%8 = load i32, i32* %2
%9 = udiv i32 %8, 1000
store i32 %9, i32* %4
%10 = load i32, i32* %2
%11 = load i32, i32* %4
%12 = mul nsw i32 %11, 1000
%13 = sub i32 %10, %12
store i32 %13, i32* %2
%14 = load i32, i32* %4
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %14, i32* %15
br label %18
16: ; preds = %1
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 0, i32* %17
br label %18
18: ; preds = %16, %7
%19 = load i32, i32* %2
%20 = mul i32 %19, 1000000
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %20, i32* %21
%22 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
ret i32 %22
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define i32 @os_sleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = icmp ugt i32 %0, 999
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = udiv i32 %0, 1000
%7 = mul i32 %6, -1000
%8 = add i32 %7, %0
br label %9
9: ; preds = %1, %5
%10 = phi i32 [ %6, %5 ], [ 0, %1 ]
%11 = phi i32 [ %8, %5 ], [ %0, %1 ]
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %10, i32* %12
%13 = mul i32 %11, 1000000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
ret i32 %15
}