#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;
};

/* Variables and functions */
int MCL_CURRENT;
int MCL_FUTURE;
int /*<<< orphan*/ SCHED_FIFO;
int SCHED_OTHER;
int /*<<< orphan*/ edlog(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int mlockall(int);
scalar_t__ pthread_getschedparam(int /*<<< orphan*/, int *,
                                 struct sched_param *);
int /*<<< orphan*/ pthread_self();
scalar_t__ pthread_setschedparam(int /*<<< orphan*/, int /*<<< orphan*/,
                                 struct sched_param *);
int /*<<< orphan*/ sched_get_priority_max(int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void invokerealtimeextensions() {
  struct sched_param sp;
  int policy;
  if ((pthread_getschedparam(pthread_self(), &policy, &sp) != 0)) {
    edlog("Scheduler changes failed with error: %s", strerror(errno));
  }
  if (policy == SCHED_OTHER) {
    sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
    if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp) != 0) {
      edlog("Scheduler changes failed with error: %s", strerror(errno));
    }
  }
  if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
    edlog("Memory page locking failed with error: %s", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<invokerealtimeextensions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%edi
lea    -0xc(%rbp),%rsi
lea    -0x8(%rbp),%rdx
call   1050 <pthread_getschedparam@plt>
cmp    $0x0,%rax
je     11b4 <invokerealtimeextensions+0x44>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xe51(%rip),%rdi
call   1040 <edlog@plt>
mov    -0xc(%rbp),%eax
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%eax
jne    121d <invokerealtimeextensions+0xad>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1080 <sched_get_priority_max@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1030 <pthread_setschedparam@plt>
cmp    $0x0,%rax
je     1218 <invokerealtimeextensions+0xa8>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xded(%rip),%rdi
call   1040 <edlog@plt>
jmp    121d <invokerealtimeextensions+0xad>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    0x2dc3(%rip),%rax
or     (%rax),%edi
call   1060 <mlockall@plt>
cmp    $0xffffffff,%eax
jne    1259 <invokerealtimeextensions+0xe9>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xdd4(%rip),%rdi
call   1040 <edlog@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invokerealtimeextensions>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1070 <pthread_self@plt>
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1050 <pthread_getschedparam@plt>
test   %rax,%rax
je     11ae <invokerealtimeextensions+0x3e>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe59(%rip),%rdi
mov    %eax,%esi
call   1040 <edlog@plt>
mov    0xc(%rsp),%eax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%eax
jne    1205 <invokerealtimeextensions+0x95>
mov    0x2e1c(%rip),%rbx
mov    (%rbx),%edi
call   1080 <sched_get_priority_max@plt>
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   1070 <pthread_self@plt>
mov    (%rbx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1030 <pthread_setschedparam@plt>
test   %rax,%rax
je     1205 <invokerealtimeextensions+0x95>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe02(%rip),%rdi
mov    %eax,%esi
call   1040 <edlog@plt>
mov    0x2db4(%rip),%rax
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1060 <mlockall@plt>
cmp    $0xffffffff,%eax
jne    123d <invokerealtimeextensions+0xcd>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf2(%rip),%rdi
mov    %eax,%esi
call   1040 <edlog@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [40 x i8] c"Scheduler changes failed with error: %s\00"
@errno = global i32 0
@SCHED_OTHER = global i32 0
@SCHED_FIFO = global i32 0
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
@.str.1 = [42 x i8] c"Memory page locking failed with error: %s\00"
define void @invokerealtimeextensions() {
%1 = alloca %struct.sched_param
%2 = alloca i32
%3 = call i32 (...) @pthread_self()
%4 = call i64 @pthread_getschedparam(i32 %3, i32* %2, %struct.sched_param* %1)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = call i32 @strerror(i32 %7)
%9 = call i32 @edlog(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %6, %0
%11 = load i32, i32* %2
%12 = load i32, i32* @SCHED_OTHER
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %27
14: ; preds = %10
%15 = load i32, i32* @SCHED_FIFO
%16 = call i32 @sched_get_priority_max(i32 %15)
%17 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i32 0, i32 0
store i32 %16, i32* %17
%18 = call i32 (...) @pthread_self()
%19 = load i32, i32* @SCHED_FIFO
%20 = call i64 @pthread_setschedparam(i32 %18, i32 %19, %struct.sched_param* %1)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = call i32 @edlog(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %22, %14
br label %27
27: ; preds = %26, %10
%28 = load i32, i32* @MCL_CURRENT
%29 = load i32, i32* @MCL_FUTURE
%30 = or i32 %28, %29
%31 = call i32 @mlockall(i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = load i32, i32* @errno
%35 = call i32 @strerror(i32 %34)
%36 = call i32 @edlog(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %27
ret void
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str = [40 x i8] c"Scheduler changes failed with error: %s\00"
@errno = global i32 0
@SCHED_OTHER = global i32 0
@SCHED_FIFO = global i32 0
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
@.str.1 = [42 x i8] c"Memory page locking failed with error: %s\00"
define void @invokerealtimeextensions() {
%1 = alloca %struct.sched_param
%2 = alloca i32
%3 = bitcast %struct.sched_param* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @pthread_self()
%6 = call i64 @pthread_getschedparam(i32 %5, i32* %2, %struct.sched_param* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = call i32 @edlog(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %8, %0
%13 = load i32, i32* %2
%14 = load i32, i32* @SCHED_OTHER
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %28
16: ; preds = %12
%17 = load i32, i32* @SCHED_FIFO
%18 = call i32 @sched_get_priority_max(i32 %17)
%19 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i64 0, i32 0
store i32 %18, i32* %19
%20 = call i32 (...) @pthread_self()
%21 = load i32, i32* @SCHED_FIFO
%22 = call i64 @pthread_setschedparam(i32 %20, i32 %21, %struct.sched_param* %1)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %16
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 @edlog(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %16, %24, %12
%29 = load i32, i32* @MCL_CURRENT
%30 = load i32, i32* @MCL_FUTURE
%31 = or i32 %30, %29
%32 = call i32 @mlockall(i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = load i32, i32* @errno
%36 = call i32 @strerror(i32 %35)
%37 = call i32 @edlog(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %34, %28
ret void
}