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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ SIGCONT;
int /*<<< orphan*/ SIGSTOP;
int /*<<< orphan*/ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                           struct timespec *, int /*<<< orphan*/ *);
int fork();
scalar_t__ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ tst_process_state_wait3(int, char, int);
int wait(int *);
int main(void) {
  int pid, slepts;
  struct timespec tsbefore, tsafter;
  if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
    perror("clock_gettime() did not return success\n");
    return 2;
  }
  if ((pid = fork()) == 0) {
    struct timespec tssleep;
    tssleep.tv_sec = 5;
    tssleep.tv_nsec = 0;
    if (clock_nanosleep(CLOCK_REALTIME, 0, &tssleep, NULL) == 0) {
      printf("clock_nanosleep() returned success\n");
      return 1;
    } else {
      printf("clock_nanosleep() did not return success\n");
      return 0;
    }
    return 0;
  } else {
    int i;
    tst_process_state_wait3(pid, 'S', 1);
    if (kill(pid, SIGSTOP) != 0) {
      printf("Could not raise SIGSTOP\n");
      return 2;
    }
    if (kill(pid, SIGCONT) != 0) {
      printf("Could not raise SIGCONT\n");
      return 2;
    }
    if (wait(&i) == -1) {
      perror("Error waiting for child to exit\n");
      return 2;
    }
    if (!WIFEXITED(i) || !WEXITSTATUS(i)) {
      printf("Test FAILED\n");
      return 1;
    }
    if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
      perror("Error in clock_gettime()\n");
      return 2;
    }
    slepts = tsafter.tv_sec - tsbefore.tv_sec;
    if (slepts >= 5) {
      printf("Test PASSED\n");
      return 0;
    } else {
      printf("clock_nanosleep() did not sleep long enough\n");
      return 1;
    }
  }
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%eax
je     11e2 <main+0x42>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a3 <main+0x203>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jne    1256 <main+0xb6>
movl   $0x5,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x40(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10a0 <clock_nanosleep@plt>
cmp    $0x0,%rax
jne    123e <main+0x9e>
lea    0xdfb(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a3 <main+0x203>
lea    0xe07(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a3 <main+0x203>
mov    -0x8(%rbp),%edi
mov    $0x53,%esi
mov    $0x1,%edx
call   1050 <tst_process_state_wait3@plt>
mov    -0x8(%rbp),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
je     129b <main+0xfb>
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a3 <main+0x203>
mov    -0x8(%rbp),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
je     12ce <main+0x12e>
lea    0xdd2(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a3 <main+0x203>
lea    -0x44(%rbp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
jne    12f8 <main+0x158>
lea    0xdc1(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a3 <main+0x203>
mov    -0x44(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%eax
je     131a <main+0x17a>
mov    -0x44(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%eax
jne    1332 <main+0x192>
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a3 <main+0x203>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    1365 <main+0x1c5>
lea    0xd82(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13a3 <main+0x203>
mov    -0x30(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jl     1390 <main+0x1f0>
lea    0xd71(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a3 <main+0x203>
lea    0xd66(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e2a(%rip),%r14
mov    (%r14),%edi
lea    0x18(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %eax,%eax
jne    12ca <main+0x12a>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     11ff <main+0x5f>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x53,%esi
mov    $0x1,%edx
call   1050 <tst_process_state_wait3@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <kill@plt>
test   %rax,%rax
je     1242 <main+0xa2>
lea    0xec2(%rip),%rdi
jmp    125e <main+0xbe>
movl   $0x5,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    (%r14),%edi
lea    0x8(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   10a0 <clock_nanosleep@plt>
xor    %ebx,%ebx
test   %rax,%rax
sete   %bl
lea    0xed0(%rip),%rax
lea    0xea0(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
jmp    1268 <main+0xc8>
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <kill@plt>
test   %rax,%rax
je     1272 <main+0xd2>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0x4(%rsp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
je     12f7 <main+0x157>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
je     12d8 <main+0x138>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
je     12d8 <main+0x138>
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     1300 <main+0x160>
mov    0x8(%rsp),%eax
sub    0x18(%rsp),%eax
cmp    $0x5,%eax
jl     12e1 <main+0x141>
lea    0xdda(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    1268 <main+0xc8>
lea    0xd2f(%rip),%rdi
call   1080 <perror@plt>
jmp    1263 <main+0xc3>
lea    0xd84(%rip),%rdi
jmp    12e8 <main+0x148>
lea    0xd87(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    1268 <main+0xc8>
lea    0xd2a(%rip),%rdi
jmp    12d1 <main+0x131>
lea    0xd42(%rip),%rdi
jmp    12d1 <main+0x131>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@CLOCK_REALTIME = global i32 0
@.str = [40 x i8] c"clock_gettime() did not return success\0A\00"
@.str.1 = [36 x i8] c"clock_nanosleep() returned success\0A\00"
@.str.2 = [42 x i8] c"clock_nanosleep() did not return success\0A\00"
@SIGSTOP = global i32 0
@.str.3 = [25 x i8] c"Could not raise SIGSTOP\0A\00"
@SIGCONT = global i32 0
@.str.4 = [25 x i8] c"Could not raise SIGCONT\0A\00"
@.str.5 = [33 x i8] c"Error waiting for child to exit\0A\00"
@.str.6 = [13 x i8] c"Test FAILED\0A\00"
@.str.7 = [26 x i8] c"Error in clock_gettime()\0A\00"
@.str.8 = [13 x i8] c"Test PASSED\0A\00"
@.str.9 = [45 x i8] c"clock_nanosleep() did not sleep long enough\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @CLOCK_REALTIME
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %4)
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %75
13: ; preds = %0
%14 = call i32 (...) @fork()
store i32 %14, i32* %2
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %13
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 5, i32* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* @CLOCK_REALTIME
%20 = call i64 @clock_nanosleep(i32 %19, i32 0, %struct.timespec* %6, i32* null)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %75
24: ; preds = %16
%25 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %75
26: ; preds = %13
%27 = load i32, i32* %2
%28 = call i32 @tst_process_state_wait3(i32 %27, i8 signext 83, i32 1)
%29 = load i32, i32* %2
%30 = load i32, i32* @SIGSTOP
%31 = call i64 @kill(i32 %29, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %75
35: ; preds = %26
%36 = load i32, i32* %2
%37 = load i32, i32* @SIGCONT
%38 = call i64 @kill(i32 %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %1
br label %75
42: ; preds = %35
%43 = call i32 @wait(i32* %7)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %1
br label %75
47: ; preds = %42
%48 = load i32, i32* %7
%49 = call i32 @WIFEXITED(i32 %48)
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i32, i32* %7
%53 = call i32 @WEXITSTATUS(i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %57, label %55
55: ; preds = %51, %47
%56 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %75
57: ; preds = %51
%58 = load i32, i32* @CLOCK_REALTIME
%59 = call i32 @clock_gettime(i32 %58, %struct.timespec* %5)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
store i32 2, i32* %1
br label %75
63: ; preds = %57
%64 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%65 = load i32, i32* %64
%66 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%67 = load i32, i32* %66
%68 = sub nsw i32 %65, %67
store i32 %68, i32* %3
%69 = load i32, i32* %3
%70 = icmp sge i32 %69, 5
br i1 %70, label %71, label %73
71: ; preds = %63
%72 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %75
73: ; preds = %63
%74 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %1
br label %75
75: ; preds = %73, %71, %61, %55, %45, %40, %33, %24, %22, %11
%76 = load i32, i32* %1
ret i32 %76
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@CLOCK_REALTIME = global i32 0
@.str = [40 x i8] c"clock_gettime() did not return success\0A\00"
@SIGSTOP = global i32 0
@SIGCONT = global i32 0
@.str.5 = [33 x i8] c"Error waiting for child to exit\0A\00"
@.str.7 = [26 x i8] c"Error in clock_gettime()\0A\00"
@str = [12 x i8] c"Test FAILED\00"
@str.10 = [44 x i8] c"clock_nanosleep() did not sleep long enough\00"
@str.11 = [12 x i8] c"Test PASSED\00"
@str.12 = [24 x i8] c"Could not raise SIGCONT\00"
@str.13 = [24 x i8] c"Could not raise SIGSTOP\00"
@str.14 = [41 x i8] c"clock_nanosleep() did not return success\00"
@str.15 = [35 x i8] c"clock_nanosleep() returned success\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = bitcast %struct.timespec* %1 to i8*
%6 = bitcast %struct.timespec* %2 to i8*
%7 = load i32, i32* @CLOCK_REALTIME
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %1)
%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 %73
12: ; preds = %0
%13 = call i32 (...) @fork()
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %12
%16 = bitcast %struct.timespec* %3 to i8*
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 5, i32* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* @CLOCK_REALTIME
%20 = call i64 @clock_nanosleep(i32 %19, i32 0, %struct.timespec* %3, i32* null)
%21 = icmp eq i64 %20, 0
%22 = select i1 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @str.15, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @str.14, i64 0, i64 0)
%23 = zext i1 %21 to i32
%24 = call i32 @puts(i8* dereferenceable(1) %22)
br label %73
25: ; preds = %12
%26 = bitcast i32* %4 to i8*
%27 = call i32 @tst_process_state_wait3(i32 %13, i8 signext 83, i32 1)
%28 = load i32, i32* @SIGSTOP
%29 = call i64 @kill(i32 %13, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.13, i64 0, i64 0))
br label %71
33: ; preds = %25
%34 = load i32, i32* @SIGCONT
%35 = call i64 @kill(i32 %13, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.12, i64 0, i64 0))
br label %71
39: ; preds = %33
%40 = call i32 @wait(i32* %4)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %71
44: ; preds = %39
%45 = load i32, i32* %4
%46 = call i32 @WIFEXITED(i32 %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %44
%49 = load i32, i32* %4
%50 = call i32 @WEXITSTATUS(i32 %49)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48, %44
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %71
54: ; preds = %48
%55 = load i32, i32* @CLOCK_REALTIME
%56 = call i32 @clock_gettime(i32 %55, %struct.timespec* %2)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
br label %71
60: ; preds = %54
%61 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%62 = load i32, i32* %61
%63 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%64 = load i32, i32* %63
%65 = sub nsw i32 %62, %64
%66 = icmp sgt i32 %65, 4
br i1 %66, label %67, label %69
67: ; preds = %60
%68 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.11, i64 0, i64 0))
br label %71
69: ; preds = %60
%70 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.10, i64 0, i64 0))
br label %71
71: ; preds = %69, %67, %58, %52, %42, %37, %31
%72 = phi i32 [ 2, %31 ], [ 2, %37 ], [ 2, %42 ], [ 2, %58 ], [ 0, %67 ], [ 1, %69 ], [ 1, %52 ]
br label %73
73: ; preds = %71, %15, %10
%74 = phi i32 [ 2, %10 ], [ %23, %15 ], [ %72, %71 ]
ret i32 %74
}