#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 */
int EINTR;
int nanosleep(struct timespec *, struct timespec *);
int m_sleep(int x) {
  if (!x)
    return 0;
  struct timespec ts;
  ts.tv_sec = x / 1000;
  ts.tv_nsec = 1000000 * (x % 1000);
  int ec;
  do
    ec = nanosleep(&ts, &ts);
  while (ec == EINTR);
  return ec;
}


x86 assembly (clang, -O0):
<m_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1131 <m_sleep+0x21>
movl   $0x0,-0x4(%rbp)
jmp    117a <m_sleep+0x6a>
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%edx,%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%eax
je     1153 <m_sleep+0x43>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<m_sleep>:
test   %edi,%edi
je     1168 <m_sleep+0x58>
push   %r14
push   %rbx
push   %rax
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
mov    %eax,(%rsp)
imul   $0x3e8,%eax,%eax
sub    %eax,%edi
imul   $0xf4240,%edi,%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rbx
mov    0x2e92(%rip),%r14
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    (%r14),%eax
je     1150 <m_sleep+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
ret

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

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