#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 */
#define EFAULT 130
#define EINTR 129
#define EINVAL 128
int errno;
int nanosleep(struct timespec *, struct timespec *);
int do_complete_nanosleep(struct timespec sleeptime) {
  struct timespec treq, tret;
  int retval = 0;
  treq = sleeptime;
  do {
    retval = nanosleep(&treq, &tret);
    if (retval == -1) {
      switch (errno) {
      case EINTR:
        break;
      case EFAULT:
        goto DO_COMPLETE_NANOSLEEP_OUT;
        break;
      case EINVAL:
        goto DO_COMPLETE_NANOSLEEP_OUT;
        break;
      }
    }
    treq = tret;
  } while (retval == -1);
DO_COMPLETE_NANOSLEEP_OUT:
  return retval;
}


x86 assembly (clang, -O0):
<do_complete_nanosleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1198 <do_complete_nanosleep+0x88>
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
sub    $0x80,%eax
je     118e <do_complete_nanosleep+0x7e>
jmp    115e <do_complete_nanosleep+0x4e>
mov    -0x20(%rbp),%eax
sub    $0x81,%eax
je     1184 <do_complete_nanosleep+0x74>
jmp    1171 <do_complete_nanosleep+0x61>
mov    -0x20(%rbp),%eax
sub    $0x82,%eax
je     1189 <do_complete_nanosleep+0x79>
jmp    1193 <do_complete_nanosleep+0x83>
jmp    1193 <do_complete_nanosleep+0x83>
jmp    11ad <do_complete_nanosleep+0x9d>
jmp    11ad <do_complete_nanosleep+0x9d>
jmp    1198 <do_complete_nanosleep+0x88>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1128 <do_complete_nanosleep+0x18>
jmp    11ad <do_complete_nanosleep+0x9d>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_complete_nanosleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    116a <do_complete_nanosleep+0x5a>
mov    0x2eab(%rip),%rbx
mov    %rsp,%r14
lea    0x8(%rsp),%r15
nopl   (%rax)
mov    (%rbx),%eax
or     $0x2,%eax
cmp    $0x82,%eax
je     1165 <do_complete_nanosleep+0x55>
mov    0x8(%rsp),%eax
mov    %eax,(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     1140 <do_complete_nanosleep+0x30>
jmp    116a <do_complete_nanosleep+0x5a>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@errno = global i32 0
define i32 @do_complete_nanosleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
store i32 %0, i32* %6
store i32 0, i32* %5
%7 = bitcast %struct.timespec* %3 to i8*
%8 = bitcast %struct.timespec* %2 to i8*
br label %9
9: ; preds = %22, %1
%10 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = load i32, i32* @errno
switch i32 %14, label %18 [
i32 129, label %15
i32 130, label %16
i32 128, label %17
]
15: ; preds = %13
br label %18
16: ; preds = %13
br label %26
17: ; preds = %13
br label %26
18: ; preds = %13, %15
br label %19
19: ; preds = %18, %9
%20 = bitcast %struct.timespec* %3 to i8*
%21 = bitcast %struct.timespec* %4 to i8*
br label %22
22: ; preds = %19
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, -1
br i1 %24, label %9, label %25
25: ; preds = %22
br label %26
26: ; preds = %25, %17, %16
%27 = load i32, i32* %5
ret i32 %27
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@errno = global i32 0
define i32 @do_complete_nanosleep(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 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %0, i32* %6
%7 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
br label %11
11: ; preds = %9, %13
%12 = load i32, i32* @errno
switch i32 %12, label %13 [
i32 128, label %17
i32 130, label %17
]
13: ; preds = %11
%14 = load i32, i32* %10
store i32 %14, i32* %6
%15 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %11, label %17
17: ; preds = %11, %11, %13, %1
%18 = phi i32 [ %7, %1 ], [ %15, %13 ], [ -1, %11 ], [ -1, %11 ]
ret i32 %18
}