#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 uint64_t;
struct timespec {
  long tv_sec;
  long tv_nsec;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ printf(char *, long, long);
void goSleep(uint64_t nsec) {
  struct timespec timeout0;
  struct timespec timeout1;
  struct timespec *tmp;
  struct timespec *t0 = &timeout0;
  struct timespec *t1 = &timeout1;
  t0->tv_sec = (long)(nsec / 1000000000);
  t0->tv_nsec = (long)(nsec % 1000000000);
  printf("Sleeping for %lu sec, %lu nsec\n", t0->tv_sec, t0->tv_nsec);
  while ((nanosleep(t0, t1) == (-1)) && (errno == EINTR)) {
    tmp = t0;
    t0 = t1;
    t1 = tmp;
  }
}


x86 assembly (clang, -O0):
<goSleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
movslq %eax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x4(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
movslq %edx,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1040 <nanosleep@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x41(%rbp)
jne    11b8 <goSleep+0x98>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11c8 <goSleep+0xa8>
jmp    11e5 <goSleep+0xc5>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1181 <goSleep+0x61>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<goSleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
movslq %edi,%rax
imul   $0x44b82fa1,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x3c,%rcx
add    %edx,%ecx
movslq %ecx,%rsi
mov    %rsi,0x8(%rsp)
imul   $0x3b9aca00,%esi,%ecx
sub    %ecx,%eax
movslq %eax,%rdx
mov    %rdx,0x10(%rsp)
lea    0xe9f(%rip),%rdi
call   1030 <printf@plt>
lea    0x18(%rsp),%r14
lea    0x8(%rsp),%rax
mov    0x2e69(%rip),%r15
mov    0x2e5a(%rip),%r12
xchg   %ax,%ax
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11a2 <goSleep+0x82>
mov    (%r12),%rcx
mov    %r14,%rax
mov    %rbx,%r14
cmp    %rcx,(%r15)
je     1180 <goSleep+0x60>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@.str = [32 x i8] c"Sleeping for %lu sec, %lu nsec\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define void @goSleep(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec*
%6 = alloca %struct.timespec*
%7 = alloca %struct.timespec*
store i32 %0, i32* %2
store %struct.timespec* %3, %struct.timespec** %6
store %struct.timespec* %4, %struct.timespec** %7
%8 = load i32, i32* %2
%9 = sdiv i32 %8, 1000000000
%10 = sext i32 %9 to i64
%11 = load %struct.timespec*, %struct.timespec** %6
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
store i64 %10, i64* %12
%13 = load i32, i32* %2
%14 = srem i32 %13, 1000000000
%15 = sext i32 %14 to i64
%16 = load %struct.timespec*, %struct.timespec** %6
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %16, i32 0, i32 1
store i64 %15, i64* %17
%18 = load %struct.timespec*, %struct.timespec** %6
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load %struct.timespec*, %struct.timespec** %6
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %21, i32 0, i32 1
%23 = load i64, i64* %22
%24 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i64 %20, i64 %23)
br label %25
25: ; preds = %36, %1
%26 = load %struct.timespec*, %struct.timespec** %6
%27 = load %struct.timespec*, %struct.timespec** %7
%28 = call i32 @nanosleep(%struct.timespec* %26, %struct.timespec* %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
br label %34
34: ; preds = %30, %25
%35 = phi i1 [ false, %25 ], [ %33, %30 ]
br i1 %35, label %36, label %40
36: ; preds = %34
%37 = load %struct.timespec*, %struct.timespec** %6
store %struct.timespec* %37, %struct.timespec** %5
%38 = load %struct.timespec*, %struct.timespec** %7
store %struct.timespec* %38, %struct.timespec** %6
%39 = load %struct.timespec*, %struct.timespec** %5
store %struct.timespec* %39, %struct.timespec** %7
br label %25
40: ; preds = %34
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@.str = [32 x i8] c"Sleeping for %lu sec, %lu nsec\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define void @goSleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = sdiv i32 %0, 1000000000
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %7, i64* %8
%9 = srem i32 %0, 1000000000
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %10, i64* %11
%12 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i64 %7, i64 %10)
br label %13
13: ; preds = %13, %1
%14 = phi %struct.timespec* [ %2, %1 ], [ %15, %13 ]
%15 = phi %struct.timespec* [ %3, %1 ], [ %14, %13 ]
%16 = call i32 @nanosleep(%struct.timespec* %14, %struct.timespec* %15)
%17 = icmp eq i32 %16, -1
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
%21 = select i1 %17, i1 %20, i1 false
br i1 %21, label %13, label %22
22: ; preds = %13
ret void
}