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

/* Variables and functions */
int /*<<< orphan*/ SCHED_FIFO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
int sched_get_priority_max(int /*<<< orphan*/);
scalar_t__ sched_getparam(int /*<<< orphan*/, struct sched_param *);
scalar_t__ sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                              struct sched_param *);
int /*<<< orphan*/ stderr;
int go_realtime(void) {
  int max_pri;
  struct sched_param sp;
  if (sched_getparam(0, &sp)) {
    perror("sched_getparam");
    return -1;
  }
  max_pri = sched_get_priority_max(SCHED_FIFO);
  sp.sched_priority = 80;
  if (sp.sched_priority > max_pri) {
    fprintf(stderr, "Invalid priority (maximum %d)\n", max_pri);
    return -1;
  }
  if (sched_setscheduler(0, SCHED_FIFO, &sp)) {
    perror("sched_setscheduler");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<go_realtime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %edi,%edi
lea    -0x10(%rbp),%rsi
call   1030 <sched_getparam@plt>
cmp    $0x0,%rax
je     1185 <go_realtime+0x35>
lea    0xe8c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <go_realtime+0xba>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1070 <sched_get_priority_max@plt>
mov    %eax,-0x8(%rbp)
movl   $0x50,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    11cd <go_realtime+0x7d>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe53(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <go_realtime+0xba>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1040 <sched_setscheduler@plt>
cmp    $0x0,%rax
je     1203 <go_realtime+0xb3>
lea    0xe3c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <go_realtime+0xba>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<go_realtime>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1030 <sched_getparam@plt>
test   %rax,%rax
jne    11bc <go_realtime+0x6c>
mov    0x2e7b(%rip),%rbx
mov    (%rbx),%edi
call   1070 <sched_get_priority_max@plt>
movl   $0x50,0x8(%rsp)
cmp    $0x4f,%eax
jg     119f <go_realtime+0x4f>
mov    0x2e50(%rip),%rcx
mov    (%rcx),%edi
lea    0xe7e(%rip),%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    11b4 <go_realtime+0x64>
mov    (%rbx),%esi
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1040 <sched_setscheduler@plt>
test   %rax,%rax
jne    11c5 <go_realtime+0x75>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe3d(%rip),%rdi
jmp    11cc <go_realtime+0x7c>
lea    0xe62(%rip),%rdi
call   1060 <perror@plt>
jmp    1198 <go_realtime+0x48>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [15 x i8] c"sched_getparam\00"
@SCHED_FIFO = global i32 0
@stderr = global i32 0
@.str.1 = [31 x i8] c"Invalid priority (maximum %d)\0A\00"
@.str.2 = [19 x i8] c"sched_setscheduler\00"
define i32 @go_realtime() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sched_param
%4 = call i64 @sched_getparam(i32 0, %struct.sched_param* %3)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %27
8: ; preds = %0
%9 = load i32, i32* @SCHED_FIFO
%10 = call i32 @sched_get_priority_max(i32 %9)
store i32 %10, i32* %2
%11 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
store i32 80, i32* %11
%12 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* %2
%15 = icmp sgt i32 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %8
%17 = load i32, i32* @stderr
%18 = load i32, i32* %2
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 -1, i32* %1
br label %27
20: ; preds = %8
%21 = load i32, i32* @SCHED_FIFO
%22 = call i64 @sched_setscheduler(i32 0, i32 %21, %struct.sched_param* %3)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %27
26: ; preds = %20
store i32 0, i32* %1
br label %27
27: ; preds = %26, %24, %16, %6
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str = [15 x i8] c"sched_getparam\00"
@SCHED_FIFO = global i32 0
@stderr = global i32 0
@.str.1 = [31 x i8] c"Invalid priority (maximum %d)\0A\00"
@.str.2 = [19 x i8] c"sched_setscheduler\00"
define i32 @go_realtime() {
%1 = alloca %struct.sched_param
%2 = bitcast %struct.sched_param* %1 to i8*
%3 = call i64 @sched_getparam(i32 0, %struct.sched_param* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %21
7: ; preds = %0
%8 = load i32, i32* @SCHED_FIFO
%9 = call i32 @sched_get_priority_max(i32 %8)
%10 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i64 0, i32 0
store i32 80, i32* %10
%11 = icmp slt i32 %9, 80
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %21
15: ; preds = %7
%16 = load i32, i32* @SCHED_FIFO
%17 = call i64 @sched_setscheduler(i32 0, i32 %16, %struct.sched_param* %1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %15, %19, %12, %5
%22 = phi i32 [ -1, %5 ], [ -1, %12 ], [ -1, %19 ], [ 0, %15 ]
ret i32 %22
}