#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*/ schp;
typedef int /*<<< orphan*/ mask;

/* Variables and functions */
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ memset(struct sched_param *, int /*<<< orphan*/, int);
scalar_t__ sched_setaffinity(int, unsigned int, unsigned long *);
scalar_t__ sched_setscheduler(int, int, struct sched_param *);
int set_real_time_priority(int pid, unsigned long mask, int sched, int prio) {
  struct sched_param schp;
  unsigned int len;
  len = sizeof(mask);
  if (sched_setaffinity(pid, len, &mask)) {
    fatal("could not set pid's affinity.\n");
  }
  memset(&schp, 0, sizeof(schp));
  schp.sched_priority = prio;
  if (sched_setscheduler(pid, sched, &schp) != 0) {
    fatal("sched_setscheduler");
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_real_time_priority>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x8,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x24(%rbp),%esi
lea    -0x10(%rbp),%rdx
call   1050 <sched_setaffinity@plt>
cmp    $0x0,%rax
je     1181 <set_real_time_priority+0x41>
lea    0xe84(%rip),%rdi
call   1060 <fatal@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdx
call   1030 <sched_setscheduler@plt>
cmp    $0x0,%rax
je     11bc <set_real_time_priority+0x7c>
lea    0xe68(%rip),%rdi
call   1060 <fatal@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_real_time_priority>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %edi,%ebx
mov    %rsi,0x10(%rsp)
lea    0x10(%rsp),%rdx
mov    $0x8,%esi
call   1050 <sched_setaffinity@plt>
test   %rax,%rax
je     1177 <set_real_time_priority+0x37>
lea    0xe8e(%rip),%rdi
call   1060 <fatal@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %r15d,0x8(%rsp)
mov    %ebx,%edi
mov    %r14d,%esi
mov    %rbp,%rdx
call   1030 <sched_setscheduler@plt>
test   %rax,%rax
je     11ae <set_real_time_priority+0x6e>
lea    0xe76(%rip),%rdi
call   1060 <fatal@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [31 x i8] c"could not set pid's affinity.\0A\00"
@.str.1 = [19 x i8] c"sched_setscheduler\00"
define i32 @set_real_time_priority(i32 %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sched_param
%10 = alloca i32
store i32 %0, i32* %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 8, i32* %10
%11 = load i32, i32* %5
%12 = load i32, i32* %10
%13 = call i64 @sched_setaffinity(i32 %11, i32 %12, i64* %6)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = call i32 @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %4
%18 = call i32 @memset(%struct.sched_param* %9, i32 0, i32 4)
%19 = load i32, i32* %8
%20 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %9, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %5
%22 = load i32, i32* %7
%23 = call i64 @sched_setscheduler(i32 %21, i32 %22, %struct.sched_param* %9)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %17
ret i32 0
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str = [31 x i8] c"could not set pid's affinity.\0A\00"
@.str.1 = [19 x i8] c"sched_setscheduler\00"
define i32 @set_real_time_priority(i32 %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = alloca %struct.sched_param
store i64 %1, i64* %5
%7 = bitcast %struct.sched_param* %6 to i8*
%8 = call i64 @sched_setaffinity(i32 %0, i32 8, i64* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %4
%11 = call i32 @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %4
%13 = call i32 @memset(%struct.sched_param* %6, i32 0, i32 4)
%14 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %6, i64 0, i32 0
store i32 %3, i32* %14
%15 = call i64 @sched_setscheduler(i32 %0, i32 %2, %struct.sched_param* %6)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = call i32 @fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
ret i32 0
}