#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 dummy;
};

/* Variables and functions */
scalar_t__ ESRCH;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sched_rr_get_interval(int, struct timespec *);
int wait(int *);
int main(void) {
  struct timespec interval;
  int result = -2, child_pid, stat_loc;
  child_pid = fork();
  if (child_pid == -1) {
    perror("fork failed");
    return 2;
  } else if (child_pid == 0) {
    exit(0);
  }
  if (wait(&stat_loc) == -1) {
    perror("wait failed");
    return 2;
  }
  result = sched_rr_get_interval(child_pid, &interval);
  if (result == -1 && errno == ESRCH) {
    printf("Test PASSED\n");
    return 0;
  }
  if (errno != ESRCH) {
    perror("Returned error is not ESRCH");
    return 1;
  }
  if (result == 0) {
    printf("Returned code == 0.\n");
    return 1;
  } else {
    perror("Unresolved test error");
    return 2;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xfffffffe,-0xc(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11a2 <main+0x42>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1294 <main+0x134>
cmpl   $0x0,-0x10(%rbp)
jne    11b3 <main+0x53>
xor    %edi,%edi
call   1060 <exit@plt>
jmp    11b8 <main+0x58>
lea    -0x14(%rbp),%rdi
call   1070 <wait@plt>
cmp    $0xffffffff,%eax
jne    11e2 <main+0x82>
lea    0xe3b(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1294 <main+0x134>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1040 <sched_rr_get_interval@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    122d <main+0xcd>
mov    0x2dde(%rip),%rax
mov    (%rax),%rax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%rax
jne    122d <main+0xcd>
lea    0xdfc(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1294 <main+0x134>
mov    0x2dac(%rip),%rax
mov    (%rax),%rax
mov    0x2d92(%rip),%rcx
cmp    (%rcx),%rax
je     125f <main+0xff>
lea    0xdd7(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <main+0x134>
cmpl   $0x0,-0xc(%rbp)
jne    1281 <main+0x121>
lea    0xdd1(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <main+0x134>
lea    0xdce(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     11e0 <main+0x80>
mov    %eax,%ebx
test   %eax,%eax
je     121c <main+0xbc>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
cmp    $0xffffffff,%eax
je     11e9 <main+0x89>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1040 <sched_rr_get_interval@plt>
mov    0x2e43(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e29(%rip),%rdx
mov    (%rdx),%rdx
cmp    $0xffffffff,%eax
je     11cb <main+0x6b>
cmp    %rdx,%rcx
jne    11f2 <main+0x92>
test   %eax,%eax
jne    1205 <main+0xa5>
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    1216 <main+0xb6>
cmp    %rdx,%rcx
jne    11f2 <main+0x92>
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1216 <main+0xb6>
lea    0xe19(%rip),%rdi
jmp    120c <main+0xac>
lea    0xe1c(%rip),%rdi
jmp    120c <main+0xac>
lea    0xe1f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
jmp    1216 <main+0xb6>
lea    0xe28(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [12 x i8] c"wait failed\00"
@errno = global i64 0
@ESRCH = global i64 0
@.str.2 = [13 x i8] c"Test PASSED\0A\00"
@.str.3 = [28 x i8] c"Returned error is not ESRCH\00"
@.str.4 = [21 x i8] c"Returned code == 0.\0A\00"
@.str.5 = [22 x i8] c"Unresolved test error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 -2, i32* %3
%6 = call i32 (...) @fork()
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %46
11: ; preds = %0
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %11
br label %17
17: ; preds = %16
%18 = call i32 @wait(i32* %5)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %46
22: ; preds = %17
%23 = load i32, i32* %4
%24 = call i32 @sched_rr_get_interval(i32 %23, %struct.timespec* %2)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i64, i64* @errno
%29 = load i64, i64* @ESRCH
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = 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 %46
33: ; preds = %27, %22
%34 = load i64, i64* @errno
%35 = load i64, i64* @ESRCH
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %46
39: ; preds = %33
%40 = load i32, i32* %3
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %46
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %1
br label %46
46: ; preds = %44, %42, %37, %31, %20, %9
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [12 x i8] c"wait failed\00"
@errno = global i64 0
@ESRCH = global i64 0
@.str.3 = [28 x i8] c"Returned error is not ESRCH\00"
@.str.5 = [22 x i8] c"Unresolved test error\00"
@str = [20 x i8] c"Returned code == 0.\00"
@str.6 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca i32
%3 = bitcast %struct.timespec* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @fork()
switch i32 %5, label %10 [
i32 -1, label %6
i32 0, label %8
]
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %33
8: ; preds = %0
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = call i32 @wait(i32* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %33
15: ; preds = %10
%16 = call i32 @sched_rr_get_interval(i32 %5, %struct.timespec* %1)
%17 = icmp eq i32 %16, -1
%18 = load i64, i64* @errno
%19 = load i64, i64* @ESRCH
%20 = icmp eq i64 %18, %19
br i1 %17, label %21, label %24
21: ; preds = %15
br i1 %20, label %22, label %25
22: ; preds = %21
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
br label %33
24: ; preds = %15
br i1 %20, label %27, label %25
25: ; preds = %21, %24
%26 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %33
27: ; preds = %24
%28 = icmp eq i32 %16, 0
br i1 %28, label %29, label %31
29: ; preds = %27
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %33
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %31, %29, %25, %22, %13, %6
%34 = phi i32 [ 2, %6 ], [ 2, %13 ], [ 0, %22 ], [ 1, %25 ], [ 1, %29 ], [ 2, %31 ]
ret i32 %34
}