#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_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ CLOCK_MONOTONIC_RAW;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/, struct timespec *,
                    struct timespec *);
int /*<<< orphan*/ memset(struct timespec *, int /*<<< orphan*/, int);
int nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int ret;
  struct timespec timeres[2];
  struct timespec req, rem;
  req.tv_sec = 1;
  req.tv_nsec = 5000000;
  memset(&rem, 0, sizeof(struct timespec));
  clock_gettime(CLOCK_MONOTONIC_RAW, &timeres[0]);
  ret = nanosleep(&req, &rem);
  if (ret != 0) {
    perror("fail1");
  }
  clock_gettime(CLOCK_MONOTONIC_RAW, &timeres[1]);
  printf("%ld %ld\n", timeres[1].tv_sec - timeres[0].tv_sec,
         timeres[1].tv_nsec - timeres[0].tv_nsec);
  printf("%ld %ld\n", rem.tv_sec, rem.tv_nsec);
  printf("------------------\n");
  clock_gettime(CLOCK_MONOTONIC_RAW, &timeres[0]);
  ret = clock_nanosleep(CLOCK_MONOTONIC, 0, &req, &rem);
  if (ret != 0) {
    printf("fail\nno errno\n");
  }
  clock_gettime(CLOCK_MONOTONIC_RAW, &timeres[1]);
  printf("%ld %ld\n", timeres[1].tv_sec - timeres[0].tv_sec,
         timeres[1].tv_nsec - timeres[0].tv_nsec);
  printf("%ld %ld\n", rem.tv_sec, rem.tv_nsec);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0x4c4b40,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <nanosleep@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11c5 <main+0x65>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
add    $0x8,%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%esi
sub    -0x20(%rbp),%esi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1080 <clock_nanosleep@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     125c <main+0xfc>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
add    $0x8,%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%esi
sub    -0x20(%rbp),%esi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
movabs $0x4c4b4000000001,%rax
mov    %rax,0x28(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2e3d(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
lea    0x28(%rsp),%rdi
mov    %rbx,%rsi
call   1060 <nanosleep@plt>
test   %eax,%eax
jne    1295 <main+0x125>
mov    (%r15),%edi
lea    0x18(%rsp),%r14
mov    %r14,%rsi
call   1040 <clock_gettime@plt>
mov    0x18(%rsp),%esi
mov    0x1c(%rsp),%edx
sub    0x10(%rsp),%esi
sub    0x14(%rsp),%edx
lea    0xe1a(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe00(%rip),%rdi
call   1030 <puts@plt>
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0x28(%rsp),%rdx
lea    0x8(%rsp),%rcx
xor    %esi,%esi
call   1090 <clock_nanosleep@plt>
test   %eax,%eax
je     124b <main+0xdb>
lea    0xddc(%rip),%rdi
call   1030 <puts@plt>
mov    (%r15),%edi
mov    %r14,%rsi
call   1040 <clock_gettime@plt>
mov    0x18(%rsp),%esi
mov    0x1c(%rsp),%edx
sub    0x10(%rsp),%esi
sub    0x14(%rsp),%edx
lea    0xd99(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd64(%rip),%rdi
call   1080 <perror@plt>
jmp    11c5 <main+0x55>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
@.str = [6 x i8] c"fail1\00"
@.str.1 = [9 x i8] c"%ld %ld\0A\00"
@.str.2 = [20 x i8] c"------------------\0A\00"
@CLOCK_MONOTONIC = global i32 0
@.str.3 = [15 x i8] c"fail\0Ano errno\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x %struct.timespec]
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i32 0, i32* %1
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 1, i32* %6
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 5000000, i32* %7
%8 = call i32 @memset(%struct.timespec* %5, i32 0, i32 8)
%9 = load i32, i32* @CLOCK_MONOTONIC_RAW
%10 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 0
%11 = call i32 @clock_gettime(i32 %9, %struct.timespec* %10)
%12 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = load i32, i32* @CLOCK_MONOTONIC_RAW
%19 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 1
%20 = call i32 @clock_gettime(i32 %18, %struct.timespec* %19)
%21 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 1
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 0
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sub nsw i32 %23, %26
%28 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 1
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 0
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = sub nsw i32 %30, %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 %34)
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%39 = load i32, i32* %38
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @CLOCK_MONOTONIC_RAW
%43 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 0
%44 = call i32 @clock_gettime(i32 %42, %struct.timespec* %43)
%45 = load i32, i32* @CLOCK_MONOTONIC
%46 = call i32 @clock_nanosleep(i32 %45, i32 0, %struct.timespec* %4, %struct.timespec* %5)
store i32 %46, i32* %2
%47 = load i32, i32* %2
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %17
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %49, %17
%52 = load i32, i32* @CLOCK_MONOTONIC_RAW
%53 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 1
%54 = call i32 @clock_gettime(i32 %52, %struct.timespec* %53)
%55 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 1
%56 = getelementptr inbounds %struct.timespec, %struct.timespec* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 0
%59 = getelementptr inbounds %struct.timespec, %struct.timespec* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = sub nsw i32 %57, %60
%62 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 1
%63 = getelementptr inbounds %struct.timespec, %struct.timespec* %62, i32 0, i32 1
%64 = load i32, i32* %63
%65 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 0
%66 = getelementptr inbounds %struct.timespec, %struct.timespec* %65, i32 0, i32 1
%67 = load i32, i32* %66
%68 = sub nsw i32 %64, %67
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %61, i32 %68)
%70 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%71 = load i32, i32* %70
%72 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%73 = load i32, i32* %72
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %71, i32 %73)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
@.str = [6 x i8] c"fail1\00"
@.str.1 = [9 x i8] c"%ld %ld\0A\00"
@CLOCK_MONOTONIC = global i32 0
@str = [19 x i8] c"------------------\00"
@str.4 = [14 x i8] c"fail\0Ano errno\00"
define i32 @main() {
%1 = alloca [2 x %struct.timespec]
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast [2 x %struct.timespec]* %1 to i8*
%5 = bitcast %struct.timespec* %2 to i8*
%6 = bitcast %struct.timespec* %3 to i8*
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 1, i32* %7
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 5000000, i32* %8
%9 = call i32 @memset(%struct.timespec* %3, i32 0, i32 8)
%10 = load i32, i32* @CLOCK_MONOTONIC_RAW
%11 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %1, i64 0, i64 0
%12 = call i32 @clock_gettime(i32 %10, %struct.timespec* %11)
%13 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = load i32, i32* @CLOCK_MONOTONIC_RAW
%19 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %1, i64 0, i64 1
%20 = call i32 @clock_gettime(i32 %18, %struct.timespec* %19)
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %19, i64 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %1, i64 0, i64 0, i32 0
%24 = load i32, i32* %23
%25 = sub nsw i32 %22, %24
%26 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %1, i64 0, i64 1, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %1, i64 0, i64 0, i32 1
%29 = load i32, i32* %28
%30 = sub nsw i32 %27, %29
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %25, i32 %30)
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%35 = load i32, i32* %34
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 %35)
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%38 = load i32, i32* @CLOCK_MONOTONIC_RAW
%39 = call i32 @clock_gettime(i32 %38, %struct.timespec* %11)
%40 = load i32, i32* @CLOCK_MONOTONIC
%41 = call i32 @clock_nanosleep(i32 %40, i32 0, %struct.timespec* %2, %struct.timespec* %3)
%42 = icmp eq i32 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %17
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %17
%46 = load i32, i32* @CLOCK_MONOTONIC_RAW
%47 = call i32 @clock_gettime(i32 %46, %struct.timespec* %19)
%48 = load i32, i32* %21
%49 = load i32, i32* %23
%50 = sub nsw i32 %48, %49
%51 = load i32, i32* %26
%52 = load i32, i32* %28
%53 = sub nsw i32 %51, %52
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %50, i32 %53)
%55 = load i32, i32* %32
%56 = load i32, i32* %34
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %55, i32 %56)
ret i32 0
}