#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 long tv_nsec;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int nanosleep(struct timespec *, int /*<<< orphan*/ *);
int phys_sleep_msec(long unsigned int time) {
  struct timespec dur = {0};
  while (time * 1000000 > 999999999) {
    dur.tv_sec += 1;
    time -= 1000;
  }
  dur.tv_nsec = time * 1000000;
  return nanosleep(&dur, NULL);
}


x86 assembly (clang, -O0):
<phys_sleep_msec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
imul   $0xf4240,-0x8(%rbp),%rax
cmp    $0x3b9ac9ff,%rax
jbe    116c <phys_sleep_msec+0x4c>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
sub    $0x3e8,%rax
mov    %rax,-0x8(%rbp)
jmp    113c <phys_sleep_msec+0x1c>
imul   $0xf4240,-0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<phys_sleep_msec>:
sub    $0x18,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
imul   $0xf4240,%rdi,%rcx
cmp    $0x3b9aca00,%rcx
jb     1167 <phys_sleep_msec+0x57>
lea    -0x3b9aca00(%rcx),%rax
shr    $0x9,%rax
movabs $0x44b82fa09b5a53,%rdx
mul    %rdx
shr    $0xb,%rdx
imul   $0xffffffffc4653600,%rdx,%rax
add    $0x1,%edx
add    %rax,%rcx
add    $0xffffffffc4653600,%rcx
mov    %edx,(%rsp)
mov    %rcx,0x8(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
add    $0x18,%rsp
ret

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

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