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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
void wait_ms(int ms) {
  struct timespec ts = {ms / 1000, (ms % 1000) * 1000000};
  struct timespec rem;
  while (nanosleep(&ts, &rem) == -1) {
    if (errno != EINTR)
      break;
    ts = rem;
  }
}


x86 assembly (clang, -O0):
<wait_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
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
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    117f <wait_ms+0x6f>
mov    0x2e86(%rip),%rax
mov    (%rax),%rax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%rax
je     1172 <wait_ms+0x62>
jmp    117f <wait_ms+0x6f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    113d <wait_ms+0x2d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_ms>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11a3 <wait_ms+0x93>
mov    0x2e7d(%rip),%r15
mov    0x2e6e(%rip),%r12
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15),%rax
cmp    (%r12),%rax
jne    11a3 <wait_ms+0x93>
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     1180 <wait_ms+0x70>
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 @wait_ms(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store i32 %0, i32* %2
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%6 = load i32, i32* %2
%7 = sdiv i32 %6, 1000
store i32 %7, i32* %5
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%9 = load i32, i32* %2
%10 = srem i32 %9, 1000
%11 = mul nsw i32 %10, 1000000
store i32 %11, i32* %8
br label %12
12: ; preds = %20, %1
%13 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
br label %23
20: ; preds = %15
%21 = bitcast %struct.timespec* %3 to i8*
%22 = bitcast %struct.timespec* %4 to i8*
br label %12
23: ; preds = %19, %12
ret void
}

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