#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
void vogl_delay(unsigned int ms) {
  int was_error;
  struct timespec elapsed, tv;
  elapsed.tv_sec = ms / 1000;
  elapsed.tv_nsec = (ms % 1000) * 1000000;
  do {
    errno = 0;
    tv.tv_sec = elapsed.tv_sec;
    tv.tv_nsec = elapsed.tv_nsec;
    was_error = nanosleep(&tv, &elapsed);
  } while (was_error && (errno == EINTR));
}


x86 assembly (clang, -O0):
<vogl_delay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0xf4240,%edx,%eax
mov    %eax,-0xc(%rbp)
mov    0x2e9a(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0x19(%rbp)
je     1192 <vogl_delay+0x82>
mov    0x2e61(%rip),%rax
mov    (%rax),%rax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    113f <vogl_delay+0x2f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vogl_delay>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,0x10(%rsp)
imul   $0x3e8,%eax,%ecx
sub    %ecx,%edi
imul   $0xf4240,%edi,%ecx
mov    %ecx,0x14(%rsp)
mov    0x2e9b(%rip),%rbx
movq   $0x0,(%rbx)
mov    %eax,0x8(%rsp)
mov    %ecx,0xc(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11b9 <vogl_delay+0xa9>
mov    0x2e6a(%rip),%r12
mov    (%r12),%rax
cmp    %rax,(%rbx)
jne    11b9 <vogl_delay+0xa9>
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rax
movq   $0x0,(%rbx)
mov    %rax,0x8(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11b9 <vogl_delay+0xa9>
mov    (%r12),%rax
cmp    %rax,(%rbx)
je     1190 <vogl_delay+0x80>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @vogl_delay(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = udiv i32 %6, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %7, i32* %8
%9 = load i32, i32* %2
%10 = urem i32 %9, 1000
%11 = mul i32 %10, 1000000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %11, i32* %12
br label %13
13: ; preds = %28, %1
store i64 0, i64* @errno
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = call i32 @nanosleep(%struct.timespec* %5, %struct.timespec* %4)
store i32 %20, i32* %3
br label %21
21: ; preds = %13
%22 = load i32, i32* %3
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br label %28
28: ; preds = %24, %21
%29 = phi i1 [ false, %21 ], [ %27, %24 ]
br i1 %29, label %13, label %30
30: ; preds = %28
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @vogl_delay(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = udiv i32 %0, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %6, i32* %7
%8 = urem i32 %0, 1000
%9 = mul nuw nsw i32 %8, 1000000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i64 0, i64* @errno
store i32 %6, i32* %11
store i32 %9, i32* %12
%13 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %2)
%14 = icmp ne i32 %13, 0
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %19, label %28
19: ; preds = %1, %19
%20 = load i32, i32* %7
%21 = load i32, i32* %10
store i64 0, i64* @errno
store i32 %20, i32* %11
store i32 %21, i32* %12
%22 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %2)
%23 = icmp ne i32 %22, 0
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
%27 = select i1 %23, i1 %26, i1 false
br i1 %27, label %19, label %28
28: ; preds = %19, %1
ret void
}