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

/* Variables and functions */
unsigned int nanosleep(struct timespec *, struct timespec *);
void delay(unsigned int timeout) {
  struct timespec timev;
  struct timespec timer;
  unsigned int rv;
  timer.tv_sec = timeout / 1000UL;
  timer.tv_nsec = (timeout * 1000000UL) % 1000000000UL;
  do {
    timev = timer;
    rv = nanosleep(&timev, &timer);
  } while (rv < 0);
}


x86 assembly (clang, -O0):
<delay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
imul   $0xf4240,%rax,%rax
mov    $0x3b9aca00,%ecx
xor    %edx,%edx
div    %rcx
mov    %edx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jb     1144 <delay+0x34>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delay>:
sub    $0x18,%rsp
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rcx
shr    $0x26,%rcx
mov    %ecx,0x8(%rsp)
imul   $0xf4240,%rax,%rcx
mov    %rcx,%rax
shr    $0x9,%rax
movabs $0x44b82fa09b5a53,%rdx
mul    %rdx
shr    $0xb,%rdx
imul   $0x3b9aca00,%edx,%eax
sub    %eax,%ecx
mov    %ecx,0xc(%rsp)
mov    0x8(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1030 <nanosleep@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define void @delay(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = zext i32 %6 to i64
%8 = udiv i64 %7, 1000
%9 = trunc i64 %8 to i32
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* %2
%12 = zext i32 %11 to i64
%13 = mul i64 %12, 1000000
%14 = urem i64 %13, 1000000000
%15 = trunc i64 %14 to i32
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %15, i32* %16
br label %17
17: ; preds = %21, %1
%18 = bitcast %struct.timespec* %3 to i8*
%19 = bitcast %struct.timespec* %4 to i8*
%20 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
store i32 %20, i32* %5
br label %21
21: ; preds = %17
%22 = load i32, i32* %5
%23 = icmp ult i32 %22, 0
br i1 %23, label %17, label %24
24: ; preds = %21
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define void @delay(i32 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timespec*
%5 = bitcast i64* %2 to i8*
%6 = bitcast i64* %3 to i8*
%7 = zext i32 %0 to i64
%8 = udiv i32 %0, 1000
%9 = bitcast i64* %3 to i32*
store i32 %8, i32* %9
%10 = mul nuw nsw i64 %7, 1000000
%11 = urem i64 %10, 1000000000
%12 = trunc i64 %11 to i32
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
store i32 %12, i32* %13
%14 = bitcast i64* %2 to %struct.timespec*
%15 = load i64, i64* %3
store i64 %15, i64* %2
%16 = call i32 @nanosleep(%struct.timespec* %14, %struct.timespec* %4)
ret void
}