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

/* Variables and functions */
int /*<<< orphan*/ SCHED_RR;
int errno;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sched_get_priority_min(int /*<<< orphan*/);
int sched_rr_get_interval(int /*<<< orphan*/, struct timespec *);
int sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                       struct sched_param *);
char *strerror(int);
int main(void) {
  struct timespec interval0;
  struct timespec interval1;
  int result0 = -1;
  int result1 = -1;
  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;
  }
  interval0.tv_sec = -1;
  interval0.tv_nsec = -1;
  interval1.tv_sec = -1;
  interval1.tv_nsec = -1;
  result0 = sched_rr_get_interval(0, &interval0);
  result1 = sched_rr_get_interval(getpid(), &interval1);
  if (result0 == result1 && interval0.tv_sec == interval1.tv_sec &&
      interval0.tv_nsec == interval1.tv_nsec && errno == 0) {
    printf("Test PASSED\n");
    return 0;
  } else if (errno != 0) {
    perror("Unexpected error");
    return 1;
  } else {
    printf("Different results between pid == 0 and pid == getpid().\n");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1060 <sched_get_priority_min@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1050 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
jne    11f5 <main+0x85>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%esi
mov    %rax,%rdx
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12bf <main+0x14f>
movl   $0xffffffff,-0x10(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x14(%rbp)
xor    %edi,%edi
lea    -0x10(%rbp),%rsi
call   1070 <sched_rr_get_interval@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   1070 <sched_rr_get_interval@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    1282 <main+0x112>
mov    -0x10(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    1282 <main+0x112>
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1282 <main+0x112>
mov    0x2d81(%rip),%rax
cmpl   $0x0,(%rax)
jne    1282 <main+0x112>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bf <main+0x14f>
mov    0x2d57(%rip),%rax
cmpl   $0x0,(%rax)
je     12aa <main+0x13a>
lea    0xd98(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <main+0x14f>
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e44(%rip),%rbx
mov    (%rbx),%edi
call   1070 <sched_get_priority_min@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1060 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
je     1236 <main+0xb6>
movq   $0xffffffffffffffff,0x18(%rsp)
ff ff
movq   $0xffffffffffffffff,0x10(%rsp)
ff ff
lea    0x18(%rsp),%rsi
xor    %edi,%edi
call   1080 <sched_rr_get_interval@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   1080 <sched_rr_get_interval@plt>
cmp    %eax,%ebx
jne    1216 <main+0x96>
mov    0x18(%rsp),%eax
cmp    0x10(%rsp),%eax
jne    1216 <main+0x96>
mov    0x1c(%rsp),%ecx
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
cmp    0x14(%rsp),%ecx
jne    121f <main+0x9f>
test   %eax,%eax
jne    121f <main+0x9f>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    125e <main+0xde>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
test   %eax,%eax
jne    1264 <main+0xe4>
lea    0xe0b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    125e <main+0xde>
mov    0x2da3(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xdb3(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdb9(%rip),%rdi
call   1090 <perror@plt>
jmp    122f <main+0xaf>

LLVM IR (-O0):
%struct.timespec = type { i32, 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 = [13 x i8] c"Test PASSED\0A\00"
@.str.2 = [17 x i8] c"Unexpected error\00"
@.str.3 = [57 x i8] c"Different results between pid == 0 and pid == getpid().\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sched_param
store i32 0, i32* %1
store i32 -1, i32* %4
store i32 -1, i32* %5
%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 %53
18: ; preds = %0
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
store i32 -1, i32* %19
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
store i32 -1, i32* %20
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 -1, i32* %21
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 -1, i32* %22
%23 = call i32 @sched_rr_get_interval(i32 0, %struct.timespec* %2)
store i32 %23, i32* %4
%24 = call i32 (...) @getpid()
%25 = call i32 @sched_rr_get_interval(i32 %24, %struct.timespec* %3)
store i32 %25, i32* %5
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %46
29: ; preds = %18
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%33 = load i32, i32* %32
%34 = icmp eq i32 %31, %33
br i1 %34, label %35, label %46
35: ; preds = %29
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%39 = load i32, i32* %38
%40 = icmp eq i32 %37, %39
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = load i32, i32* @errno
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %53
46: ; preds = %41, %35, %29, %18
%47 = load i32, i32* @errno
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %53
51: ; preds = %46
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %53
53: ; preds = %51, %49, %44, %13
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.timespec = type { i32, 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.2 = [17 x i8] c"Unexpected error\00"
@str = [56 x i8] c"Different results between pid == 0 and pid == getpid().\00"
@str.4 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca %struct.sched_param
%4 = bitcast %struct.timespec* %1 to i8*
%5 = bitcast %struct.timespec* %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 %48
17: ; preds = %0
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 -1, i32* %18
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i32 -1, i32* %19
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 -1, i32* %20
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 -1, i32* %21
%22 = call i32 @sched_rr_get_interval(i32 0, %struct.timespec* %1)
%23 = call i32 (...) @getpid()
%24 = call i32 @sched_rr_get_interval(i32 %23, %struct.timespec* %2)
%25 = icmp eq i32 %22, %24
br i1 %25, label %26, label %39
26: ; preds = %17
%27 = load i32, i32* %18
%28 = load i32, i32* %20
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = load i32, i32* %19
%32 = load i32, i32* %21
%33 = icmp eq i32 %31, %32
%34 = load i32, i32* @errno
%35 = icmp eq i32 %34, 0
%36 = select i1 %33, i1 %35, i1 false
br i1 %36, label %37, label %41
37: ; preds = %30
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %48
39: ; preds = %17, %26
%40 = load i32, i32* @errno
br label %41
41: ; preds = %39, %30
%42 = phi i32 [ %40, %39 ], [ %34, %30 ]
%43 = icmp eq i32 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %48
46: ; preds = %41
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str, i64 0, i64 0))
br label %48
48: ; preds = %46, %44, %37, %13
%49 = phi i32 [ 2, %13 ], [ 0, %37 ], [ 1, %44 ], [ 1, %46 ]
ret i32 %49
}