#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__ nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(void) {
  struct timespec tssleepfor, tsstorage, tsbefore, tsafter;
  int sleepnsec = 3;
  int slepts = 0, sleptns = 0;
  if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
    perror("Error in clock_gettime()\n");
    return 2;
  }
  tssleepfor.tv_sec = 0;
  tssleepfor.tv_nsec = sleepnsec;
  if (nanosleep(&tssleepfor, &tsstorage) != 0) {
    printf("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 > sleepnsec)) {
    printf("Test PASSED\n");
    return 0;
  } else {
    printf("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    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x3,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    1197 <main+0x57>
lea    0xe7a(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1277 <main+0x137>
movl   $0x0,-0x10(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <nanosleep@plt>
cmp    $0x0,%rax
je     11d3 <main+0x93>
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1277 <main+0x137>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    1206 <main+0xc6>
lea    0xe0b(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1277 <main+0x137>
mov    -0x28(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1236 <main+0xf6>
mov    -0x34(%rbp),%eax
add    $0x3b9aca00,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jg     124c <main+0x10c>
mov    -0x34(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jle    1264 <main+0x124>
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1277 <main+0x137>
lea    0xde0(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e94(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     11f5 <main+0xb5>
movabs $0x300000000,%rax
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   1050 <nanosleep@plt>
test   %rax,%rax
je     119b <main+0x5b>
lea    0xec1(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
add    $0x20,%rsp
pop    %rbx
ret
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1040 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     11f5 <main+0xb5>
mov    (%rsp),%ecx
mov    0x4(%rsp),%edx
sub    0x8(%rsp),%ecx
sub    0xc(%rsp),%edx
lea    0x3b9aca00(%rdx),%eax
mov    %edx,%esi
sar    $0x1f,%esi
test   %edx,%edx
cmovns %edx,%eax
add    %ecx,%esi
jg     11e5 <main+0xa5>
cmp    $0x4,%eax
jge    11e5 <main+0xa5>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    1195 <main+0x55>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1195 <main+0x55>
lea    0xe04(%rip),%rdi
call   1060 <perror@plt>
jmp    1190 <main+0x50>

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

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