#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 */
typedef int time_t;
struct timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
void Tetris_sleep_ms(unsigned int milliseconds) {
  time_t seconds = (int)(milliseconds / 1000);
  milliseconds = milliseconds - ((unsigned int)seconds * 1000);
  struct timespec req;
  req.tv_sec = seconds;
  req.tv_nsec = (int)milliseconds * 1000000L;
  while (nanosleep(&req, &req) == -1)
    ;
}


x86 assembly (clang, -O0):
<Tetris_sleep_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
imul   $0x3e8,-0x8(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
movslq -0x4(%rbp),%rax
imul   $0xf4240,%rax,%rax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    1167 <Tetris_sleep_ms+0x57>
jmp    114d <Tetris_sleep_ms+0x3d>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Tetris_sleep_ms>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0xfffffc18,%eax,%ecx
add    %edi,%ecx
mov    %eax,0x8(%rsp)
imul   $0xf4240,%ecx,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     1140 <Tetris_sleep_ms+0x30>
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define void @Tetris_sleep_ms(i32 %0) {
%2 = alloca %struct.timespec
%3 = udiv i32 %0, 1000
%4 = mul i32 %3, -1000
%5 = add i32 %4, %0
%6 = bitcast %struct.timespec* %2 to i8*
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %3, i32* %7
%8 = mul i32 %5, 1000000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %8, i32* %9
br label %10
10: ; preds = %10, %1
%11 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %10, label %13
13: ; preds = %10
ret void
}