#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 */

/* Variables and functions */
int /*<<< orphan*/ pthread_testintr();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ usleep(int);
void pegasus_sleep(int msec) {
  int seconds;
  int microsecs = msec * 1000;
  if (microsecs < 1000000) {
    usleep(microsecs / 2);
    pthread_testintr();
    usleep(microsecs / 2);
  } else {
    seconds = microsecs / 1000000;
    sleep(seconds);
    if ((seconds * 1000000) < microsecs)
      usleep(microsecs - (seconds * 1000000));
  }
}


x86 assembly (clang, -O0):
<pegasus_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
imul   $0x3e8,-0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0xf4240,-0xc(%rbp)
jge    1182 <pegasus_sleep+0x52>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1050 <usleep@plt>
mov    $0x0,%al
call   1030 <pthread_testintr@plt>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1050 <usleep@plt>
jmp    11be <pegasus_sleep+0x8e>
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <sleep@plt>
imul   $0xf4240,-0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b9 <pegasus_sleep+0x89>
mov    -0xc(%rbp),%edi
imul   $0xf4240,-0x8(%rbp),%eax
sub    %eax,%edi
call   1050 <usleep@plt>
jmp    11be <pegasus_sleep+0x8e>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pegasus_sleep>:
push   %rbp
push   %rbx
push   %rax
imul   $0x3e8,%edi,%ebp
cmp    $0xf423f,%ebp
jg     1157 <pegasus_sleep+0x27>
imul   $0x1f4,%edi,%ebp
mov    %ebp,%edi
call   1050 <usleep@plt>
xor    %eax,%eax
call   1030 <pthread_testintr@plt>
jmp    117f <pegasus_sleep+0x4f>
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x26,%rbx
add    %eax,%ebx
mov    %ebx,%edi
call   1040 <sleep@plt>
imul   $0xf4240,%ebx,%eax
sub    %eax,%ebp
jle    118c <pegasus_sleep+0x5c>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <usleep@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @pegasus_sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = mul nsw i32 %5, 1000
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 1000000
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = load i32, i32* %4
%11 = sdiv i32 %10, 2
%12 = call i32 @usleep(i32 %11)
%13 = call i32 (...) @pthread_testintr()
%14 = load i32, i32* %4
%15 = sdiv i32 %14, 2
%16 = call i32 @usleep(i32 %15)
br label %33
17: ; preds = %1
%18 = load i32, i32* %4
%19 = sdiv i32 %18, 1000000
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = call i32 @sleep(i32 %20)
%22 = load i32, i32* %3
%23 = mul nsw i32 %22, 1000000
%24 = load i32, i32* %4
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %32
26: ; preds = %17
%27 = load i32, i32* %4
%28 = load i32, i32* %3
%29 = mul nsw i32 %28, 1000000
%30 = sub nsw i32 %27, %29
%31 = call i32 @usleep(i32 %30)
br label %32
32: ; preds = %26, %17
br label %33
33: ; preds = %32, %9
ret void
}

LLVM IR (-O2):
define void @pegasus_sleep(i32 %0) {
%2 = mul nsw i32 %0, 1000
%3 = icmp slt i32 %2, 1000000
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = mul nsw i32 %0, 500
%6 = tail call i32 @usleep(i32 %5)
%7 = tail call i32 (...) @pthread_testintr()
br label %15
8: ; preds = %1
%9 = sdiv i32 %0, 1000
%10 = tail call i32 @sleep(i32 %9)
%11 = mul nsw i32 %9, 1000000
%12 = icmp sgt i32 %2, %11
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = sub nsw i32 %2, %11
br label %15
15: ; preds = %4, %13
%16 = phi i32 [ %14, %13 ], [ %5, %4 ]
%17 = tail call i32 @usleep(i32 %16)
br label %18
18: ; preds = %15, %8
ret void
}