#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 dummy;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
void thrd_sleep(const struct timespec *ts_in, struct timespec *rem_out) {
  int res;
  struct timespec rem, ts = *ts_in;
  do {
    res = nanosleep(&ts, &rem);
    ts = rem;
  } while (res == -1 && errno == EINTR);
  if (rem_out) {
    *rem_out = rem;
  }
}


x86 assembly (clang, -O0):
<thrd_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x14(%rbp)
mov    %al,-0x21(%rbp)
jne    1168 <thrd_sleep+0x58>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1129 <thrd_sleep+0x19>
cmpq   $0x0,-0x10(%rbp)
je     1187 <thrd_sleep+0x77>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thrd_sleep>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    (%rdi),%eax
mov    %eax,(%rsp)
mov    %rsp,%r15
lea    0x8(%rsp),%r13
mov    0x2eac(%rip),%r12
mov    0x2e9d(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r13,%rsi
call   1030 <nanosleep@plt>
mov    0x8(%rsp),%ecx
mov    %ecx,(%rsp)
cmp    $0xffffffff,%eax
jne    1160 <thrd_sleep+0x50>
mov    (%rbx),%rax
cmp    %rax,(%r12)
je     1140 <thrd_sleep+0x30>
test   %r14,%r14
je     1168 <thrd_sleep+0x58>
mov    %ecx,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @thrd_sleep(%struct.timespec* %0, %struct.timespec* %1) {
%3 = alloca %struct.timespec*
%4 = alloca %struct.timespec*
%5 = alloca i32
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
store %struct.timespec* %0, %struct.timespec** %3
store %struct.timespec* %1, %struct.timespec** %4
%8 = load %struct.timespec*, %struct.timespec** %3
%9 = bitcast %struct.timespec* %7 to i8*
%10 = bitcast %struct.timespec* %8 to i8*
br label %11
11: ; preds = %22, %2
%12 = call i32 @nanosleep(%struct.timespec* %7, %struct.timespec* %6)
store i32 %12, i32* %5
%13 = bitcast %struct.timespec* %7 to i8*
%14 = bitcast %struct.timespec* %6 to i8*
br label %15
15: ; preds = %11
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br label %22
22: ; preds = %18, %15
%23 = phi i1 [ false, %15 ], [ %21, %18 ]
br i1 %23, label %11, label %24
24: ; preds = %22
%25 = load %struct.timespec*, %struct.timespec** %4
%26 = icmp ne %struct.timespec* %25, null
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load %struct.timespec*, %struct.timespec** %4
%29 = bitcast %struct.timespec* %28 to i8*
%30 = bitcast %struct.timespec* %6 to i8*
br label %31
31: ; preds = %27, %24
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @thrd_sleep(%struct.timespec* nocapture %0, %struct.timespec* writeonly %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = bitcast %struct.timespec* %3 to i8*
%6 = bitcast %struct.timespec* %4 to i8*
%7 = getelementptr %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%9 = load i32, i32* %7
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
br label %11
11: ; preds = %11, %2
%12 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %3)
%13 = load i32, i32* %10
store i32 %13, i32* %8
%14 = icmp eq i32 %12, -1
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %11, label %19
19: ; preds = %11
%20 = icmp eq %struct.timespec* %1, null
br i1 %20, label %23, label %21
21: ; preds = %19
%22 = getelementptr %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 %13, i32* %22
br label %23
23: ; preds = %21, %19
ret void
}