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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ SCHED_FIFO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ run_loop(char *);
int sched_get_priority_max(int /*<<< orphan*/);
int sched_get_priority_min(int /*<<< orphan*/);
int sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                       struct sched_param *);
int /*<<< orphan*/ wait(int *);
int main(__attribute__((unused)) int argc,
         __attribute__((unused)) char *argv[]) {
  struct sched_param param;
  cpu_set_t mask;
  int prio_min, prio_max;
  if ((prio_min = sched_get_priority_min(SCHED_FIFO)) == -1)
    pexit("sched_get_priority_min");
  if ((prio_max = sched_get_priority_max(SCHED_FIFO)) == -1)
    pexit("sched_get_priority_max");
  param.sched_priority = (prio_min + prio_max) / 2;
  if (sched_setscheduler(0, SCHED_FIFO, &param) == -1)
    pexit("sched_setscheduler");
  CPU_ZERO(&mask);
  CPU_SET(0, &mask);
  if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1)
    pexit("sched_setaffinity");
  switch (fork()) {
  case -1:
    pexit("fork");
    break;
  case 0:
    run_loop("Parent");
    break;
  default:
    run_loop("Child");
    break;
  }
  int status;
  wait(&status);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1060 <sched_get_priority_min@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11ec <main+0x3c>
lea    0xe19(%rip),%rdi
call   10a0 <pexit@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1090 <sched_get_priority_max@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1212 <main+0x62>
lea    0xe0a(%rip),%rdi
call   10a0 <pexit@plt>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1040 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
jne    124c <main+0x9c>
lea    0xde7(%rip),%rdi
call   10a0 <pexit@plt>
lea    -0x1c(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   10b0 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0x1c(%rbp),%rdx
call   1050 <sched_setaffinity@plt>
cmp    $0xffffffff,%eax
jne    1285 <main+0xd5>
lea    0xdc1(%rip),%rdi
call   10a0 <pexit@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     12af <main+0xff>
jmp    129f <main+0xef>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     12c0 <main+0x110>
jmp    12d1 <main+0x121>
lea    0xd9d(%rip),%rdi
call   10a0 <pexit@plt>
jmp    12dd <main+0x12d>
lea    0xd91(%rip),%rdi
call   1030 <run_loop@plt>
jmp    12dd <main+0x12d>
lea    0xd87(%rip),%rdi
call   1030 <run_loop@plt>
lea    -0x28(%rbp),%rdi
call   10c0 <wait@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e29(%rip),%r14
mov    (%r14),%edi
call   1060 <sched_get_priority_min@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11da <main+0x2a>
lea    0xe2b(%rip),%rdi
call   10a0 <pexit@plt>
mov    (%r14),%edi
call   1090 <sched_get_priority_max@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11f5 <main+0x45>
lea    0xe27(%rip),%rdi
call   10a0 <pexit@plt>
add    %ebx,%ebp
mov    %ebp,%eax
shr    $0x1f,%eax
add    %ebp,%eax
sar    %eax
mov    %eax,(%rsp)
mov    (%r14),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1040 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
jne    1221 <main+0x71>
lea    0xe12(%rip),%rdi
call   10a0 <pexit@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <CPU_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10b0 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1050 <sched_setaffinity@plt>
cmp    $0xffffffff,%eax
jne    1258 <main+0xa8>
lea    0xdee(%rip),%rdi
call   10a0 <pexit@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     1276 <main+0xc6>
cmp    $0xffffffff,%eax
jne    127f <main+0xcf>
lea    0xde4(%rip),%rdi
call   10a0 <pexit@plt>
jmp    128b <main+0xdb>
lea    0xddb(%rip),%rdi
jmp    1286 <main+0xd6>
lea    0xdd9(%rip),%rdi
call   1030 <run_loop@plt>
lea    0x8(%rsp),%rdi
call   10c0 <wait@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@.str = [23 x i8] c"sched_get_priority_min\00"
@.str.1 = [23 x i8] c"sched_get_priority_max\00"
@.str.2 = [19 x i8] c"sched_setscheduler\00"
@.str.3 = [18 x i8] c"sched_setaffinity\00"
@.str.4 = [5 x i8] c"fork\00"
@.str.5 = [7 x i8] c"Parent\00"
@.str.6 = [6 x i8] c"Child\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sched_param
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @SCHED_FIFO
%12 = call i32 @sched_get_priority_min(i32 %11)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @pexit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* @SCHED_FIFO
%18 = call i32 @sched_get_priority_max(i32 %17)
store i32 %18, i32* %9
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @pexit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i32, i32* %8
%24 = load i32, i32* %9
%25 = add nsw i32 %23, %24
%26 = sdiv i32 %25, 2
%27 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %6, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @SCHED_FIFO
%29 = call i32 @sched_setscheduler(i32 0, i32 %28, %struct.sched_param* %6)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = call i32 @pexit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %22
%34 = call i32 @CPU_ZERO(i32* %7)
%35 = call i32 @CPU_SET(i32 0, i32* %7)
%36 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %7)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 @pexit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %33
%41 = call i32 (...) @fork()
switch i32 %41, label %46 [
i32 -1, label %42
i32 0, label %44
]
42: ; preds = %40
%43 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %48
44: ; preds = %40
%45 = call i32 @run_loop(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %48
46: ; preds = %40
%47 = call i32 @run_loop(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %48
48: ; preds = %46, %44, %42
%49 = call i32 @wait(i32* %10)
%50 = load i32, i32* @EXIT_SUCCESS
%51 = call i32 @exit(i32 %50)
unreachable
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@.str = [23 x i8] c"sched_get_priority_min\00"
@.str.1 = [23 x i8] c"sched_get_priority_max\00"
@.str.2 = [19 x i8] c"sched_setscheduler\00"
@.str.3 = [18 x i8] c"sched_setaffinity\00"
@.str.4 = [5 x i8] c"fork\00"
@.str.5 = [7 x i8] c"Parent\00"
@.str.6 = [6 x i8] c"Child\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sched_param
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.sched_param* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = load i32, i32* @SCHED_FIFO
%9 = tail call i32 @sched_get_priority_min(i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @pexit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* @SCHED_FIFO
%15 = tail call i32 @sched_get_priority_max(i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 @pexit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = add nsw i32 %15, %9
%21 = sdiv i32 %20, 2
%22 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @SCHED_FIFO
%24 = call i32 @sched_setscheduler(i32 0, i32 %23, %struct.sched_param* %3)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @pexit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %19
%29 = call i32 @CPU_ZERO(i32* %4)
%30 = call i32 @CPU_SET(i32 0, i32* %4)
%31 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %4)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @pexit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = call i32 (...) @fork()
switch i32 %36, label %41 [
i32 -1, label %37
i32 0, label %39
]
37: ; preds = %35
%38 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %43
39: ; preds = %35
%40 = call i32 @run_loop(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %43
41: ; preds = %35
%42 = call i32 @run_loop(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %41, %39, %37
%44 = bitcast i32* %5 to i8*
%45 = call i32 @wait(i32* %5)
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
}