#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 /*<<< orphan*/ tv_nsec;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ atoi(char *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct timespec req = {0}, rem = {0};
  if (argc != 2) {
    fprintf(stderr, "%s <nanoseconds>\n", argv[0]);
    return 1;
  }
  req.tv_nsec = atoi(argv[1]);
  for (;;) {
    if (!nanosleep(&req, &rem))
      break;
    if (errno == EINTR) {
      req = rem;
      continue;
    }
    perror("failed to nanosleep");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
je     11b8 <main+0x68>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1229 <main+0xd9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0x0,%eax
jne    11e3 <main+0x93>
jmp    1222 <main+0xd2>
mov    0x2dee(%rip),%rax
mov    (%rax),%rax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <main+0xba>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11c8 <main+0x78>
lea    0xe01(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1229 <main+0xd9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
cmp    $0x2,%edi
jne    11c3 <main+0x83>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1030 <nanosleep@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     11e1 <main+0xa1>
mov    0x2e4f(%rip),%r15
mov    0x2e40(%rip),%r12
mov    %rsp,%rbx
lea    0x8(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    11f1 <main+0xb1>
mov    0x8(%rsp),%rax
mov    %rax,(%rsp)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
jne    11a0 <main+0x60>
jmp    11e1 <main+0xa1>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe2a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1a(%rip),%rdi
call   1050 <perror@plt>
jmp    11db <main+0x9b>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@stderr = global i32 0
@.str = [18 x i8] c"%s <nanoseconds>\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [20 x i8] c"failed to nanosleep\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = bitcast %struct.timespec* %6 to i8*
%9 = bitcast %struct.timespec* %7 to i8*
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 1, i32* %3
br label %38
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 %22, i32* %23
br label %24
24: ; preds = %32, %18
%25 = call i32 @nanosleep(%struct.timespec* %6, %struct.timespec* %7)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %24
br label %37
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = bitcast %struct.timespec* %6 to i8*
%34 = bitcast %struct.timespec* %7 to i8*
br label %24
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %38
37: ; preds = %27
store i32 0, i32* %3
br label %38
38: ; preds = %37, %35, %12
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@stderr = global i32 0
@.str = [18 x i8] c"%s <nanoseconds>\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [20 x i8] c"failed to nanosleep\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timespec*
%5 = alloca i64
%6 = bitcast i64* %5 to %struct.timespec*
%7 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%8 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%9 = icmp eq i32 %0, 2
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %31
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = bitcast i64* %3 to i32*
store i32 %17, i32* %18
%19 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %6)
%20 = icmp eq i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %14, %25
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i64, i64* %5
store i64 %26, i64* %3
%27 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %6)
%28 = icmp eq i32 %27, 0
br i1 %28, label %31, label %21
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %25, %14, %29, %10
%32 = phi i32 [ 1, %10 ], [ 1, %29 ], [ 0, %14 ], [ 0, %25 ]
ret i32 %32
}