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


x86 assembly (clang, -O0):
<timer_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
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>
cmp    $0xffffffff,%eax
jne    117d <timer_sleep+0x6d>
mov    0x2e87(%rip),%rax
mov    (%rax),%rax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%rax
je     1178 <timer_sleep+0x68>
movl   $0x0,-0x4(%rbp)
jmp    1184 <timer_sleep+0x74>
jmp    113d <timer_sleep+0x2d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timer_sleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%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),%rbx
mov    0x2e8e(%rip),%r14
mov    0x2e7f(%rip),%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    117c <timer_sleep+0x6c>
mov    (%r14),%rax
cmp    (%r15),%rax
je     1160 <timer_sleep+0x50>
xor    %eax,%eax
jmp    1181 <timer_sleep+0x71>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @timer_sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = sdiv i32 %5, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %6, i32* %7
%8 = load i32, i32* %3
%9 = srem i32 %8, 1000
%10 = mul nsw i32 %9, 1000000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %10, i32* %11
br label %12
12: ; preds = %20, %1
%13 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
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
store i32 0, i32* %2
br label %22
20: ; preds = %15
br label %12
21: ; preds = %12
store i32 1, i32* %2
br label %22
22: ; preds = %21, %19
%23 = load i32, i32* %2
ret i32 %23
}

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