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

/* Variables and functions */
int /*<<< orphan*/ fatal(char *, int);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
void delay(int n) {
  struct timespec tdelay;
  if (n <= 0 || n > 10000)
    fatal("unreasonable delay period (%d)", n);
  tdelay.tv_sec = n / 1000;
  tdelay.tv_nsec = (n % 1000) * 1000000;
  nanosleep(&tdelay, NULL);
}


x86 assembly (clang, -O0):
<delay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    1142 <delay+0x22>
cmpl   $0x2710,-0x4(%rbp)
jle    1151 <delay+0x31>
mov    -0x4(%rbp),%esi
lea    0xeb4(%rip),%rdi
call   1040 <fatal@plt>
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%edx,%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delay>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    -0x2711(%rbx),%eax
cmp    $0xffffd8ef,%eax
ja     1142 <delay+0x22>
lea    0xec5(%rip),%rdi
mov    %ebx,%esi
call   1040 <fatal@plt>
movslq %ebx,%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
mov    %eax,0x8(%rsp)
imul   $0x3e8,%eax,%eax
sub    %eax,%ebx
imul   $0xf4240,%ebx,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [31 x i8] c"unreasonable delay period (%d)\00"
define void @delay(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp sle i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* %2
%8 = icmp sgt i32 %7, 10000
br i1 %8, label %9, label %12
9: ; preds = %6, %1
%10 = load i32, i32* %2
%11 = call i32 @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %9, %6
%13 = load i32, i32* %2
%14 = sdiv i32 %13, 1000
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %2
%17 = srem i32 %16, 1000
%18 = mul nsw i32 %17, 1000000
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str = [31 x i8] c"unreasonable delay period (%d)\00"
define void @delay(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = add i32 %0, -10001
%5 = icmp ult i32 %4, -10000
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %8
8: ; preds = %1, %6
%9 = sdiv i32 %0, 1000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %9, i32* %10
%11 = srem i32 %0, 1000
%12 = mul nsw i32 %11, 1000000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
ret void
}