#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 */
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sched_yield();
void millisleep(int msecs) {
  if (msecs <= 0) {
    sched_yield();
  } else {
    struct timespec rqtp, rmtp;
    rqtp.tv_sec = msecs / 1000;
    rqtp.tv_nsec = (msecs % 1000) * 1000000;
    while (nanosleep(&rqtp, &rmtp)) {
      if (errno != EINTR) {
        printf("t\n");
        break;
      }
      rqtp = rmtp;
    }
  }
}


x86 assembly (clang, -O0):
<millisleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jg     1151 <millisleep+0x21>
mov    $0x0,%al
call   1050 <sched_yield@plt>
jmp    11c7 <millisleep+0x97>
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   1040 <nanosleep@plt>
cmp    $0x0,%rax
je     11c2 <millisleep+0x92>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
je     11b5 <millisleep+0x85>
lea    0xe55(%rip),%rdi
call   1030 <printf@plt>
jmp    11c2 <millisleep+0x92>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1173 <millisleep+0x43>
jmp    11c7 <millisleep+0x97>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<millisleep>:
test   %edi,%edi
jle    11c5 <millisleep+0x95>
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,0x8(%rsp)
imul   $0x3e8,%eax,%eax
sub    %eax,%edi
imul   $0xf4240,%edi,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <nanosleep@plt>
test   %rax,%rax
je     11d8 <millisleep+0xa8>
mov    0x2e5f(%rip),%r15
mov    0x2e50(%rip),%r12
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    11cc <millisleep+0x9c>
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <nanosleep@plt>
test   %rax,%rax
jne    11a0 <millisleep+0x70>
jmp    11d8 <millisleep+0xa8>
xor    %eax,%eax
jmp    1050 <sched_yield@plt>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
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
@.str = [3 x i8] c"t\0A\00"
define void @millisleep(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp sle i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 (...) @sched_yield()
br label %30
9: ; preds = %1
%10 = load i32, i32* %2
%11 = sdiv i32 %10, 1000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* %2
%14 = srem i32 %13, 1000
%15 = mul nsw i32 %14, 1000000
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %15, i32* %16
br label %17
17: ; preds = %26, %9
%18 = call i64 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %29
26: ; preds = %20
%27 = bitcast %struct.timespec* %3 to i8*
%28 = bitcast %struct.timespec* %4 to i8*
br label %17
29: ; preds = %24, %17
br label %30
30: ; preds = %29, %7
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@str = [2 x i8] c"t\00"
define void @millisleep(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.timespec*
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timespec*
%6 = icmp slt i32 %0, 1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 (...) @sched_yield()
br label %30
9: ; preds = %1
%10 = bitcast i64* %2 to i8*
%11 = bitcast i64* %4 to i8*
%12 = udiv i32 %0, 1000
%13 = bitcast i64* %2 to i32*
store i32 %12, i32* %13
%14 = urem i32 %0, 1000
%15 = mul nuw nsw i32 %14, 1000000
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 %15, i32* %16
%17 = call i64 @nanosleep(%struct.timespec* %3, %struct.timespec* %5)
%18 = icmp eq i64 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %9, %25
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %29
25: ; preds = %19
%26 = load i64, i64* %4
store i64 %26, i64* %2
%27 = call i64 @nanosleep(%struct.timespec* %3, %struct.timespec* %5)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %19
29: ; preds = %25, %9, %23
br label %30
30: ; preds = %29, %7
ret void
}