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

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
int nsleep(long miliseconds) {
  struct timespec req, rem;
  if (miliseconds > 999) {
    req.tv_sec = (int)(miliseconds / 1000);
    req.tv_nsec = (miliseconds - ((long)req.tv_sec * 1000)) * 1000000;
  } else {
    req.tv_sec = 0;
    req.tv_nsec = miliseconds * 1000000;
  }
  return nanosleep(&req, &rem);
}


x86 assembly (clang, -O0):
<nsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x3e7,-0x8(%rbp)
jle    115d <nsleep+0x4d>
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
imul   $0x3e8,%rcx,%rcx
sub    %rcx,%rax
imul   $0xf4240,%rax,%rax
mov    %rax,-0x10(%rbp)
jmp    1170 <nsleep+0x60>
movl   $0x0,-0x18(%rbp)
imul   $0xf4240,-0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nsleep>:
xor    %edx,%edx
cmp    $0x3e8,%rdi
jl     1140 <nsleep+0x30>
mov    %rdi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
movslq %edx,%rax
imul   $0xfffffffffffffc18,%rax,%rax
add    %rax,%rdi
sub    $0x28,%rsp
imul   $0xf4240,%rdi,%rax
mov    %edx,0x8(%rsp)
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   1030 <nanosleep@plt>
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
define i32 @nsleep(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = icmp sgt i64 %5, 999
br i1 %6, label %7, label %20
7: ; preds = %1
%8 = load i64, i64* %2
%9 = sdiv i64 %8, 1000
%10 = trunc i64 %9 to i32
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i64, i64* %2
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%14 = load i32, i32* %13
%15 = sext i32 %14 to i64
%16 = mul nsw i64 %15, 1000
%17 = sub nsw i64 %12, %16
%18 = mul nsw i64 %17, 1000000
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %18, i64* %19
br label %25
20: ; preds = %1
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 0, i32* %21
%22 = load i64, i64* %2
%23 = mul nsw i64 %22, 1000000
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %23, i64* %24
br label %25
25: ; preds = %20, %7
%26 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
ret i32 %26
}

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