#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_REALTIME;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                           struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(void) {
  struct timespec tssleep, tsbefore, tsafter;
  int slepts = 0, sleptns = 0;
  if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
    perror("clock_gettime() did not return success\n");
    return 2;
  }
  tssleep.tv_sec = 0;
  tssleep.tv_nsec = 3000000;
  if (clock_nanosleep(CLOCK_REALTIME, 0, &tssleep, NULL) != 0) {
    printf("clock_nanosleep() did not return success\n");
    return 2;
  }
  if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
    perror("Error in clock_gettime()\n");
    return 2;
  }
  slepts = tsafter.tv_sec - tsbefore.tv_sec;
  sleptns = tsafter.tv_nsec - tsbefore.tv_nsec;
  if (sleptns < 0) {
    sleptns = sleptns + 1000000000;
    slepts = slepts - 1;
  }
  if ((slepts > 0) || (sleptns > 3000000)) {
    printf("Test PASSED\n");
    return 0;
  } else {
    printf("clock_nanosleep() did not sleep long enough\n");
    return 1;
  }
  printf("This code should not be executed.\n");
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%eax
je     1190 <main+0x50>
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    127d <main+0x13d>
movl   $0x0,-0x10(%rbp)
movl   $0x2dc6c0,-0xc(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <clock_nanosleep@plt>
cmp    $0x0,%rax
je     11d8 <main+0x98>
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    127d <main+0x13d>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    120b <main+0xcb>
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    127d <main+0x13d>
mov    -0x20(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    123b <main+0xfb>
mov    -0x28(%rbp),%eax
add    $0x3b9aca00,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1252 <main+0x112>
cmpl   $0x2dc6c0,-0x28(%rbp)
jle    126a <main+0x12a>
lea    0xe13(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    127d <main+0x13d>
lea    0xe08(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e94(%rip),%rbx
mov    (%rbx),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %eax,%eax
jne    11fa <main+0xba>
movabs $0x2dc6c000000000,%rax
mov    %rax,0x18(%rsp)
mov    (%rbx),%edi
lea    0x18(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <clock_nanosleep@plt>
test   %rax,%rax
je     1197 <main+0x57>
lea    0xeef(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
jmp    11f4 <main+0xb4>
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     1203 <main+0xc3>
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%edx
sub    0x10(%rsp),%ecx
sub    0x14(%rsp),%edx
lea    0x3b9aca00(%rdx),%eax
mov    %edx,%esi
sar    $0x1f,%esi
test   %edx,%edx
cmovns %edx,%eax
add    %ecx,%esi
jg     11e6 <main+0xa6>
cmp    $0x2dc6c1,%eax
jge    11e6 <main+0xa6>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    11f4 <main+0xb4>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdff(%rip),%rdi
jmp    120a <main+0xca>
lea    0xe1e(%rip),%rdi
call   1050 <perror@plt>
jmp    1190 <main+0x50>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [40 x i8] c"clock_gettime() did not return success\0A\00"
@.str.1 = [42 x i8] c"clock_nanosleep() did not return success\0A\00"
@.str.2 = [26 x i8] c"Error in clock_gettime()\0A\00"
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
@.str.4 = [45 x i8] c"clock_nanosleep() did not sleep long enough\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
store i32 0, i32* %6
%7 = load i32, i32* @CLOCK_REALTIME
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %3)
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %54
12: ; preds = %0
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
store i32 3000000, i32* %14
%15 = load i32, i32* @CLOCK_REALTIME
%16 = call i64 @clock_nanosleep(i32 %15, i32 0, %struct.timespec* %2, i32* null)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %54
20: ; preds = %12
%21 = load i32, i32* @CLOCK_REALTIME
%22 = call i32 @clock_gettime(i32 %21, %struct.timespec* %4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %54
26: ; preds = %20
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%30 = load i32, i32* %29
%31 = sub nsw i32 %28, %30
store i32 %31, i32* %5
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%35 = load i32, i32* %34
%36 = sub nsw i32 %33, %35
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %26
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1000000000
store i32 %41, i32* %6
%42 = load i32, i32* %5
%43 = sub nsw i32 %42, 1
store i32 %43, i32* %5
br label %44
44: ; preds = %39, %26
%45 = load i32, i32* %5
%46 = icmp sgt i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %44
%48 = load i32, i32* %6
%49 = icmp sgt i32 %48, 3000000
br i1 %49, label %50, label %52
50: ; preds = %47, %44
%51 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %54
52: ; preds = %47
%53 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %54
54: ; preds = %52, %50, %24, %18, %10
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [40 x i8] c"clock_gettime() did not return success\0A\00"
@.str.2 = [26 x i8] c"Error in clock_gettime()\0A\00"
@str = [44 x i8] c"clock_nanosleep() did not sleep long enough\00"
@str.5 = [12 x i8] c"Test PASSED\00"
@str.6 = [41 x i8] c"clock_nanosleep() did not return success\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %1 to i8*
%5 = bitcast %struct.timespec* %2 to i8*
%6 = bitcast %struct.timespec* %3 to i8*
%7 = load i32, i32* @CLOCK_REALTIME
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %49
12: ; preds = %0
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i32 3000000, i32* %14
%15 = load i32, i32* @CLOCK_REALTIME
%16 = call i64 @clock_nanosleep(i32 %15, i32 0, %struct.timespec* %1, i32* null)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %12
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.6, i64 0, i64 0))
br label %49
20: ; preds = %12
%21 = load i32, i32* @CLOCK_REALTIME
%22 = call i32 @clock_gettime(i32 %21, %struct.timespec* %3)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %49
26: ; preds = %20
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%30 = load i32, i32* %29
%31 = sub i32 %28, %30
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%35 = load i32, i32* %34
%36 = sub nsw i32 %33, %35
%37 = icmp slt i32 %36, 0
%38 = add nsw i32 %36, 1000000000
%39 = ashr i32 %36, 31
%40 = add nsw i32 %31, %39
%41 = select i1 %37, i32 %38, i32 %36
%42 = icmp sgt i32 %40, 0
%43 = icmp sgt i32 %41, 3000000
%44 = select i1 %42, i1 true, i1 %43
br i1 %44, label %45, label %47
45: ; preds = %26
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %49
47: ; preds = %26
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
br label %49
49: ; preds = %47, %45, %24, %18, %10
%50 = phi i32 [ 2, %10 ], [ 2, %18 ], [ 2, %24 ], [ 0, %45 ], [ 1, %47 ]
ret i32 %50
}