#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_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ nanosleep(struct timespec *, struct timespec *);
void sleep_demo(void) {
  struct timespec req = {.tv_sec = 0, .tv_nsec = 200};
  struct timespec rem, *a = &req, *b = &rem;
  while (nanosleep(a, b) && errno == EINTR) {
    struct timespec *tmp = a;
    a = b;
    b = tmp;
  }
}


x86 assembly (clang, -O0):
<sleep_demo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0xee1(%rip),%rax
mov    %rax,-0x8(%rbp)
lea    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
je     116c <sleep_demo+0x5c>
mov    0x2e87(%rip),%rax
mov    (%rax),%rax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    117c <sleep_demo+0x6c>
jmp    1199 <sleep_demo+0x89>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1133 <sleep_demo+0x23>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_demo>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x8(%rsp),%r14
movq   $0xc8,0x8(%rsp)
lea    0x10(%rsp),%rax
mov    0x2eab(%rip),%r15
mov    0x2e9c(%rip),%r12
nopl   0x0(%rax)
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
je     1162 <sleep_demo+0x52>
mov    (%r12),%rcx
mov    %r14,%rax
mov    %rbx,%r14
cmp    %rcx,(%r15)
je     1140 <sleep_demo+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @sleep_demo() {
%1 = alloca i64
%2 = bitcast i64* %1 to %struct.timespec*
%3 = alloca %struct.timespec
%4 = bitcast i64* %1 to i8*
store i64 200, i64* %1
%5 = bitcast %struct.timespec* %3 to i8*
br label %6
6: ; preds = %6, %0
%7 = phi %struct.timespec* [ %3, %0 ], [ %8, %6 ]
%8 = phi %struct.timespec* [ %2, %0 ], [ %7, %6 ]
%9 = call i64 @nanosleep(%struct.timespec* %8, %struct.timespec* %7)
%10 = icmp ne i64 %9, 0
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
%14 = select i1 %10, i1 %13, i1 false
br i1 %14, label %6, label %15
15: ; preds = %6
ret void
}