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

/* Variables and functions */
int MCL_CURRENT;
int MCL_FUTURE;
int /*<<< orphan*/ SCHED_FIFO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ geteuid();
int /*<<< orphan*/ memset(struct sched_param *, int /*<<< orphan*/, int);
scalar_t__ mlockall(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sched_get_priority_max(int /*<<< orphan*/);
scalar_t__ sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                              struct sched_param *);
int /*<<< orphan*/ stderr;
void prepare_system_for_benchmarking() {
  if (geteuid() == 0) {
    struct sched_param sp;
    memset(&sp, 0, sizeof(sp));
    sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
    if (sched_setscheduler(0, SCHED_FIFO, &sp) != 0)
      fprintf(stderr, "Error changing scheduling policy to RT class\n");
    else
      printf("# Scheduling policy is changed to RT class with max priority\n");
    if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
      fprintf(stderr, "Error locking pages\n");
    else
      printf("# All pages of process are locked (paging is disabled)\n");
  } else {
    fprintf(stderr, "# Process is launched without ROOT permissions\n");
  }
}


x86 assembly (clang, -O0):
<prepare_system_for_benchmarking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <geteuid@plt>
cmp    $0x0,%rax
jne    123a <prepare_system_for_benchmarking+0xca>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1090 <sched_get_priority_max@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x8(%rbp),%rdx
call   1040 <sched_setscheduler@plt>
cmp    $0x0,%rax
je     11e2 <prepare_system_for_benchmarking+0x72>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
call   1070 <fprintf@plt>
jmp    11ee <prepare_system_for_benchmarking+0x7e>
lea    0xe45(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2df2(%rip),%rax
or     (%rax),%edi
call   1080 <mlockall@plt>
cmp    $0x0,%rax
je     1229 <prepare_system_for_benchmarking+0xb9>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1235 <prepare_system_for_benchmarking+0xc5>
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
jmp    124f <prepare_system_for_benchmarking+0xdf>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1070 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_system_for_benchmarking>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <geteuid@plt>
test   %rax,%rax
je     119c <prepare_system_for_benchmarking+0x2c>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xeb3(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <fprintf@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e2b(%rip),%rbx
mov    (%rbx),%edi
call   1090 <sched_get_priority_max@plt>
mov    %eax,(%rsp)
mov    (%rbx),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1040 <sched_setscheduler@plt>
test   %rax,%rax
je     11e7 <prepare_system_for_benchmarking+0x77>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1070 <fprintf@plt>
jmp    11f3 <prepare_system_for_benchmarking+0x83>
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dce(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1080 <mlockall@plt>
test   %rax,%rax
je     1226 <prepare_system_for_benchmarking+0xb6>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1232 <prepare_system_for_benchmarking+0xc2>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"Error changing scheduling policy to RT class\0A\00"
@.str.1 = [62 x i8] c"# Scheduling policy is changed to RT class with max priority\0A\00"
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
@.str.2 = [21 x i8] c"Error locking pages\0A\00"
@.str.3 = [56 x i8] c"# All pages of process are locked (paging is disabled)\0A\00"
@.str.4 = [48 x i8] c"# Process is launched without ROOT permissions\0A\00"
define void @prepare_system_for_benchmarking() {
%1 = alloca %struct.sched_param
%2 = call i64 (...) @geteuid()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %29
4: ; preds = %0
%5 = call i32 @memset(%struct.sched_param* %1, i32 0, i32 4)
%6 = load i32, i32* @SCHED_FIFO
%7 = call i32 @sched_get_priority_max(i32 %6)
%8 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i32 0, i32 0
store i32 %7, i32* %8
%9 = load i32, i32* @SCHED_FIFO
%10 = call i64 @sched_setscheduler(i32 0, i32 %9, %struct.sched_param* %1)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %17
15: ; preds = %4
%16 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* @MCL_CURRENT
%19 = load i32, i32* @MCL_FUTURE
%20 = or i32 %18, %19
%21 = call i64 @mlockall(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %28
26: ; preds = %17
%27 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
br label %32
29: ; preds = %0
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %29, %28
ret void
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"Error changing scheduling policy to RT class\0A\00"
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
@.str.2 = [21 x i8] c"Error locking pages\0A\00"
@.str.4 = [48 x i8] c"# Process is launched without ROOT permissions\0A\00"
@str = [61 x i8] c"# Scheduling policy is changed to RT class with max priority\00"
@str.5 = [55 x i8] c"# All pages of process are locked (paging is disabled)\00"
define void @prepare_system_for_benchmarking() {
%1 = alloca %struct.sched_param
%2 = tail call i64 (...) @geteuid()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %30
4: ; preds = %0
%5 = bitcast %struct.sched_param* %1 to i8*
%6 = call i32 @memset(%struct.sched_param* %1, i32 0, i32 4)
%7 = load i32, i32* @SCHED_FIFO
%8 = call i32 @sched_get_priority_max(i32 %7)
%9 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i64 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @SCHED_FIFO
%11 = call i64 @sched_setscheduler(i32 0, i32 %10, %struct.sched_param* %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %4
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %18
16: ; preds = %4
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = load i32, i32* @MCL_CURRENT
%20 = load i32, i32* @MCL_FUTURE
%21 = or i32 %20, %19
%22 = call i64 @mlockall(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %29
27: ; preds = %18
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.5, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
br label %33
30: ; preds = %0
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %30, %29
ret void
}