#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 member_0;
};
struct rlimit {
  int member_0;
  int member_1;
};

/* Variables and functions */
int MCL_CURRENT;
int MCL_FUTURE;
int /*<<< orphan*/ RLIMIT_RTTIME;
int SCHED_FIFO;
scalar_t__ mlockall(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ sched_setscheduler(int /*<<< orphan*/, int, struct sched_param *);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
int setup_realtime() {
  int min_prio, max_prio, my_prio;
  int policy = SCHED_FIFO;
  struct rlimit my_rlimit_rttime = {100000, 100000};
  if (setrlimit(RLIMIT_RTTIME, &my_rlimit_rttime) != 0) {
    perror("Failed to set rlimit");
    return -1;
  }
  my_prio = 10;
  struct sched_param my_sched_param = {my_prio};
  if (sched_setscheduler(0, policy, &my_sched_param) != 0) {
    perror("Failed to set scheduler");
    return -1;
  }
  if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
    perror("Failed to lock all memory pages");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_realtime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0xea5(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1040 <setrlimit@plt>
cmp    $0x0,%rax
je     1193 <setup_realtime+0x53>
lea    0xe86(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <setup_realtime+0xd0>
movl   $0xa,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1030 <sched_setscheduler@plt>
cmp    $0x0,%rax
je     11d0 <setup_realtime+0x90>
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <setup_realtime+0xd0>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    0x2e08(%rip),%rax
or     (%rax),%edi
call   1050 <mlockall@plt>
cmp    $0x0,%rax
je     1209 <setup_realtime+0xc9>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <setup_realtime+0xd0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_realtime>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e8b(%rip),%rax
mov    (%rax),%ebp
movabs $0x186a0000186a0,%rax
mov    %rax,0x10(%rsp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1040 <setrlimit@plt>
test   %rax,%rax
jne    11b8 <setup_realtime+0x78>
movl   $0xa,0x8(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
xor    %edi,%edi
mov    %ebp,%esi
call   1030 <sched_setscheduler@plt>
test   %rax,%rax
jne    11c1 <setup_realtime+0x81>
mov    0x2e2e(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1050 <mlockall@plt>
test   %rax,%rax
jne    11ca <setup_realtime+0x8a>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe41(%rip),%rdi
jmp    11d1 <setup_realtime+0x91>
lea    0xe4d(%rip),%rdi
jmp    11d1 <setup_realtime+0x91>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11af <setup_realtime+0x6f>

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@__const.setup_realtime.my_rlimit_rttime = %struct.rlimit { i32 100000, i32 100000 }
@RLIMIT_RTTIME = global i32 0
@.str = [21 x i8] c"Failed to set rlimit\00"
@.str.1 = [24 x i8] c"Failed to set scheduler\00"
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
@.str.2 = [32 x i8] c"Failed to lock all memory pages\00"
define i32 @setup_realtime() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.rlimit
%7 = alloca %struct.sched_param
%8 = load i32, i32* @SCHED_FIFO
store i32 %8, i32* %5
%9 = bitcast %struct.rlimit* %6 to i8*
%10 = load i32, i32* @RLIMIT_RTTIME
%11 = call i64 @setrlimit(i32 %10, %struct.rlimit* %6)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %32
15: ; preds = %0
store i32 10, i32* %4
%16 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %7, i32 0, i32 0
%17 = load i32, i32* %4
store i32 %17, i32* %16
%18 = load i32, i32* %5
%19 = call i64 @sched_setscheduler(i32 0, i32 %18, %struct.sched_param* %7)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %32
23: ; preds = %15
%24 = load i32, i32* @MCL_CURRENT
%25 = load i32, i32* @MCL_FUTURE
%26 = or i32 %24, %25
%27 = call i64 @mlockall(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %32
31: ; preds = %23
store i32 0, i32* %1
br label %32
32: ; preds = %31, %29, %21, %13
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@RLIMIT_RTTIME = global i32 0
@.str = [21 x i8] c"Failed to set rlimit\00"
@.str.1 = [24 x i8] c"Failed to set scheduler\00"
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
@.str.2 = [32 x i8] c"Failed to lock all memory pages\00"
define i32 @setup_realtime() {
%1 = alloca i64
%2 = bitcast i64* %1 to %struct.rlimit*
%3 = alloca %struct.sched_param
%4 = load i32, i32* @SCHED_FIFO
%5 = bitcast i64* %1 to i8*
store i64 429496729700000, i64* %1
%6 = load i32, i32* @RLIMIT_RTTIME
%7 = call i64 @setrlimit(i32 %6, %struct.rlimit* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %27
11: ; preds = %0
%12 = bitcast %struct.sched_param* %3 to i8*
%13 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i64 0, i32 0
store i32 10, i32* %13
%14 = call i64 @sched_setscheduler(i32 0, i32 %4, %struct.sched_param* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i32, i32* @MCL_CURRENT
%18 = load i32, i32* @MCL_FUTURE
%19 = or i32 %18, %17
%20 = call i64 @mlockall(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %16, %11
%23 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), %11 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), %16 ]
%24 = call i32 @perror(i8* %23)
br label %25
25: ; preds = %22, %16
%26 = phi i32 [ 0, %16 ], [ -1, %22 ]
br label %27
27: ; preds = %25, %9
%28 = phi i32 [ -1, %9 ], [ %26, %25 ]
ret i32 %28
}