#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*/ SIGABRT;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ 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) {
  struct timespec tssleep, tsbefore, tsafter;
  int pid;
  if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
    perror("clock_gettime() did not return success\n");
    return 2;
  }
  if ((pid = fork()) == 0) {
    tssleep.tv_sec = 30;
    tssleep.tv_nsec = 0;
    clock_nanosleep(CLOCK_REALTIME, 0, &tssleep, NULL);
  } else {
    int i;
    tst_process_state_wait3(pid, 'S', 1);
    if (kill(pid, SIGABRT) != 0) {
      printf("Could not raise signal being tested\n");
      return 2;
    }
    if (wait(&i) == -1) {
      perror("Error waiting for child to exit\n");
      return 2;
    }
    if (clock_gettime(CLOCK_REALTIME, &tsafter) != 0) {
      perror("clock_gettime() did not return success\n");
      return 2;
    }
    if ((tsafter.tv_sec - tsbefore.tv_sec) < 30) {
      printf("Test PASSED\n");
      return 0;
    } else {
      printf("Slept for too long: %d >= %d\n",
             (int)tsafter.tv_sec - (int)tsbefore.tv_sec, 30);
      printf("Test FAILED\n");
      return 1;
    }
  }
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     11c3 <main+0x43>
lea    0xe4e(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    130a <main+0x18a>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jne    1202 <main+0x82>
movl   $0x1e,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <clock_nanosleep@plt>
jmp    1303 <main+0x183>
mov    -0x3c(%rbp),%edi
mov    $0x53,%esi
mov    $0x1,%edx
call   1050 <tst_process_state_wait3@plt>
mov    -0x3c(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
cmp    $0x0,%rax
je     1249 <main+0xc9>
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    130a <main+0x18a>
lea    -0x40(%rbp),%rdi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
jne    1273 <main+0xf3>
lea    0xdeb(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    130a <main+0x18a>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     12a7 <main+0x127>
lea    0xd6a(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    130a <main+0x18a>
mov    -0x38(%rbp),%eax
sub    -0x28(%rbp),%eax
cmp    $0x1e,%eax
jge    12d0 <main+0x150>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    130a <main+0x18a>
mov    -0x38(%rbp),%esi
sub    -0x28(%rbp),%esi
lea    0xd9e(%rip),%rdi
mov    $0x1e,%edx
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130a <main+0x18a>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e42(%rip),%r14
mov    (%r14),%edi
lea    0x18(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %rax,%rax
jne    1282 <main+0xf2>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11f5 <main+0x65>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x53,%esi
mov    $0x1,%edx
call   1060 <tst_process_state_wait3@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <kill@plt>
test   %rax,%rax
je     1221 <main+0x91>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
jmp    1214 <main+0x84>
movl   $0x1e,(%rsp)
movq   $0x0,0x8(%rsp)
mov    (%r14),%edi
mov    %rsp,%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   1090 <clock_nanosleep@plt>
mov    $0x2,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0x14(%rsp),%rdi
call   10a0 <wait@plt>
cmp    $0xffffffff,%eax
je     1290 <main+0x100>
mov    (%r14),%edi
mov    %rsp,%rsi
call   1040 <clock_gettime@plt>
test   %rax,%rax
jne    1282 <main+0xf2>
mov    (%rsp),%esi
sub    0x18(%rsp),%esi
cmp    $0x1d,%esi
jg     125c <main+0xcc>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1219 <main+0x89>
lea    0xde6(%rip),%rdi
mov    $0x1e,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    1219 <main+0x89>
lea    0xd77(%rip),%rdi
call   1080 <perror@plt>
jmp    1214 <main+0x84>
lea    0xd91(%rip),%rdi
jmp    1289 <main+0xf9>

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"
@SIGABRT = global i32 0
@.str.1 = [37 x i8] c"Could not raise signal being tested\0A\00"
@.str.2 = [33 x i8] c"Error waiting for child to exit\0A\00"
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
@.str.4 = [30 x i8] c"Slept for too long: %d >= %d\0A\00"
@.str.5 = [13 x i8] c"Test FAILED\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
%7 = load i32, i32* @CLOCK_REALTIME
%8 = call i64 @clock_gettime(i32 %7, %struct.timespec* %3)
%9 = icmp ne i64 %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 %58
12: ; preds = %0
%13 = call i32 (...) @fork()
store i32 %13, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
store i32 30, i32* %16
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* @CLOCK_REALTIME
%19 = call i32 @clock_nanosleep(i32 %18, i32 0, %struct.timespec* %2, i32* null)
br label %57
20: ; preds = %12
%21 = load i32, i32* %5
%22 = call i32 @tst_process_state_wait3(i32 %21, i8 signext 83, i32 1)
%23 = load i32, i32* %5
%24 = load i32, i32* @SIGABRT
%25 = call i64 @kill(i32 %23, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %58
29: ; preds = %20
%30 = call i32 @wait(i32* %6)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %58
34: ; preds = %29
%35 = load i32, i32* @CLOCK_REALTIME
%36 = call i64 @clock_gettime(i32 %35, %struct.timespec* %4)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %58
40: ; preds = %34
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%44 = load i32, i32* %43
%45 = sub nsw i32 %42, %44
%46 = icmp slt i32 %45, 30
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %58
49: ; preds = %40
%50 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%53 = load i32, i32* %52
%54 = sub nsw i32 %51, %53
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %54, i32 30)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %58
57: ; preds = %15
store i32 2, i32* %1
br label %58
58: ; preds = %57, %49, %47, %38, %32, %27, %10
%59 = load i32, i32* %1
ret i32 %59
}

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"
@SIGABRT = global i32 0
@.str.2 = [33 x i8] c"Error waiting for child to exit\0A\00"
@.str.4 = [30 x i8] c"Slept for too long: %d >= %d\0A\00"
@str = [12 x i8] c"Test FAILED\00"
@str.6 = [12 x i8] c"Test PASSED\00"
@str.7 = [36 x i8] c"Could not raise signal being tested\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 = bitcast %struct.timespec* %3 to i8*
%8 = load i32, i32* @CLOCK_REALTIME
%9 = call i64 @clock_gettime(i32 %8, %struct.timespec* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %54
13: ; preds = %0
%14 = call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 30, i32* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* @CLOCK_REALTIME
%20 = call i32 @clock_nanosleep(i32 %19, i32 0, %struct.timespec* %1, i32* null)
br label %54
21: ; preds = %13
%22 = bitcast i32* %4 to i8*
%23 = call i32 @tst_process_state_wait3(i32 %14, i8 signext 83, i32 1)
%24 = load i32, i32* @SIGABRT
%25 = call i64 @kill(i32 %14, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %21
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.7, i64 0, i64 0))
br label %52
29: ; preds = %21
%30 = call i32 @wait(i32* %4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %52
34: ; preds = %29
%35 = load i32, i32* @CLOCK_REALTIME
%36 = call i64 @clock_gettime(i32 %35, %struct.timespec* %3)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %52
40: ; preds = %34
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%44 = load i32, i32* %43
%45 = sub nsw i32 %42, %44
%46 = icmp slt i32 %45, 30
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
br label %52
49: ; preds = %40
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %45, i32 30)
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %52
52: ; preds = %49, %47, %38, %32, %27
%53 = phi i32 [ 2, %27 ], [ 2, %32 ], [ 2, %38 ], [ 0, %47 ], [ 1, %49 ]
br label %54
54: ; preds = %16, %52, %11
%55 = phi i32 [ 2, %11 ], [ 2, %16 ], [ %53, %52 ]
ret i32 %55
}