#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;
};
struct sched_param {
  int /*<<< orphan*/ sched_priority;
};

/* Variables and functions */
int ESRCH;
int /*<<< orphan*/ SCHED_RR;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sched_get_priority_min(int /*<<< orphan*/);
int sched_rr_get_interval(int, struct timespec *);
int sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                       struct sched_param *);
char *strerror(int);
int wait(int *);
int main(void) {
  struct timespec interval;
  int result = -2, child_pid, stat_loc;
  struct sched_param param;
  param.sched_priority = sched_get_priority_min(SCHED_RR);
  if (sched_setscheduler(0, SCHED_RR, &param) == -1) {
    printf("sched_setscheduler failed: %d (%s)\n", errno, strerror(errno));
    return 2;
  }
  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    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1050 <sched_get_priority_min@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1040 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
jne    120e <main+0x7e>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1c(%rbp),%esi
mov    %rax,%rdx
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    132c <main+0x19c>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    123a <main+0xaa>
lea    0xdfb(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    132c <main+0x19c>
cmpl   $0x0,-0x10(%rbp)
jne    124b <main+0xbb>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1250 <main+0xc0>
lea    -0x14(%rbp),%rdi
call   10a0 <wait@plt>
cmp    $0xffffffff,%eax
jne    127a <main+0xea>
lea    0xdc7(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    132c <main+0x19c>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1060 <sched_rr_get_interval@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12c5 <main+0x135>
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
mov    0x2d25(%rip),%rcx
cmp    (%rcx),%eax
jne    12c5 <main+0x135>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132c <main+0x19c>
mov    0x2d14(%rip),%rax
mov    (%rax),%eax
mov    0x2cf3(%rip),%rcx
cmp    (%rcx),%eax
je     12f5 <main+0x165>
lea    0xd65(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    132c <main+0x19c>
cmpl   $0x0,-0xc(%rbp)
jne    1319 <main+0x189>
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132c <main+0x19c>
lea    0xd5a(%rip),%rdi
call   1070 <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
mov    0x2e24(%rip),%rbx
mov    (%rbx),%edi
call   1060 <sched_get_priority_min@plt>
mov    %eax,(%rsp)
mov    (%rbx),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1050 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
je     1232 <main+0x92>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     1274 <main+0xd4>
mov    %eax,%ebx
test   %eax,%eax
je     12a7 <main+0x107>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
je     127d <main+0xdd>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1070 <sched_rr_get_interval@plt>
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2db8(%rip),%rdx
mov    (%rdx),%edx
cmp    $0xffffffff,%eax
je     125c <main+0xbc>
cmp    %edx,%ecx
jne    1286 <main+0xe6>
test   %eax,%eax
jne    1299 <main+0xf9>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    126e <main+0xce>
mov    0x2da7(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xdb7(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%eax
jmp    126e <main+0xce>
cmp    %edx,%ecx
jne    1286 <main+0xe6>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xda9(%rip),%rdi
jmp    12a0 <main+0x100>
lea    0xdac(%rip),%rdi
jmp    12a0 <main+0x100>
lea    0xdaf(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    126e <main+0xce>
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
jmp    1255 <main+0xb5>
xor    %edi,%edi
call   1090 <exit@plt>

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

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