#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*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int sched_get_priority_max(int /*<<< orphan*/);
int sched_getparam(int /*<<< orphan*/, struct sched_param *);
int sched_getscheduler(int /*<<< orphan*/);
int sched_setparam(int /*<<< orphan*/, struct sched_param *);
int sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                       struct sched_param *);
int main(int argc, char const *argv[]) {
  int sts;
  printf("\n");
  struct sched_param sched_params;
  sts = sched_getparam(0, &sched_params);
  if (sts == -1) {
    perror("sched_getparam");
    exit(-1);
  };
  int old_policy = sched_getscheduler(0);
  printf("initial policy: %d\n", old_policy);
  printf("initial priority: %d\n", sched_params.sched_priority);
  int max = sched_get_priority_max(SCHED_FIFO);
  printf("max=%d\n", max);
  struct sched_param new_sched_params;
  new_sched_params.sched_priority = max;
  sts = sched_setscheduler(0, SCHED_FIFO, &new_sched_params);
  if (sts == -1) {
    perror("sched_setscheduler");
    exit(-1);
  };
  sts = sched_setparam(0, &new_sched_params);
  if (sts == -1) {
    perror("sched_setparam");
    exit(-1);
  };
  sched_getparam(0, &new_sched_params);
  printf("new priority: %d\n", new_sched_params.sched_priority);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1050 <sched_getparam@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d2 <main+0x52>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1030 <sched_getscheduler@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   10a0 <sched_get_priority_max@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1060 <sched_setscheduler@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    125d <main+0xdd>
lea    0xdf3(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
xor    %edi,%edi
lea    -0x28(%rbp),%rsi
call   1080 <sched_setparam@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    128b <main+0x10b>
lea    0xdd8(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
xor    %edi,%edi
lea    -0x28(%rbp),%rsi
call   1050 <sched_getparam@plt>
mov    -0x28(%rbp),%esi
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
call   1060 <sched_getparam@plt>
cmp    $0xffffffff,%eax
je     1251 <main+0xc1>
xor    %edi,%edi
call   1040 <sched_getscheduler@plt>
lea    0xe4b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%esi
lea    0xe4b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e02(%rip),%r14
mov    (%r14),%edi
call   10b0 <sched_get_priority_max@plt>
mov    %eax,%ebx
lea    0xe42(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,0x8(%rsp)
mov    (%r14),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1070 <sched_setscheduler@plt>
cmp    $0xffffffff,%eax
je     125a <main+0xca>
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1090 <sched_setparam@plt>
cmp    $0xffffffff,%eax
je     1263 <main+0xd3>
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1060 <sched_getparam@plt>
mov    0x8(%rsp),%esi
lea    0xe23(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xda8(%rip),%rdi
jmp    126a <main+0xda>
lea    0xde0(%rip),%rdi
jmp    126a <main+0xda>
lea    0xdea(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [2 x i8] c"\0A\00"
@.str.1 = [15 x i8] c"sched_getparam\00"
@.str.2 = [20 x i8] c"initial policy: %d\0A\00"
@.str.3 = [22 x i8] c"initial priority: %d\0A\00"
@SCHED_FIFO = global i32 0
@.str.4 = [8 x i8] c"max=%d\0A\00"
@.str.5 = [19 x i8] c"sched_setscheduler\00"
@.str.6 = [15 x i8] c"sched_setparam\00"
@.str.7 = [18 x i8] c"new priority: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sched_param
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sched_param
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @sched_getparam(i32 0, %struct.sched_param* %7)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = call i32 @sched_getscheduler(i32 0)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %7, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @SCHED_FIFO
%26 = call i32 @sched_get_priority_max(i32 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %9
%30 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %10, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* @SCHED_FIFO
%32 = call i32 @sched_setscheduler(i32 0, i32 %31, %struct.sched_param* %10)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %18
%36 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %18
%39 = call i32 @sched_setparam(i32 0, %struct.sched_param* %10)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %38
%46 = call i32 @sched_getparam(i32 0, %struct.sched_param* %10)
%47 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %10, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i32 %48)
ret i32 0
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str.1 = [15 x i8] c"sched_getparam\00"
@.str.2 = [20 x i8] c"initial policy: %d\0A\00"
@.str.3 = [22 x i8] c"initial priority: %d\0A\00"
@SCHED_FIFO = global i32 0
@.str.4 = [8 x i8] c"max=%d\0A\00"
@.str.5 = [19 x i8] c"sched_setscheduler\00"
@.str.6 = [15 x i8] c"sched_setparam\00"
@.str.7 = [18 x i8] c"new priority: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sched_param
%4 = alloca %struct.sched_param
%5 = tail call i32 @putchar(i32 10)
%6 = bitcast %struct.sched_param* %3 to i8*
%7 = call i32 @sched_getparam(i32 0, %struct.sched_param* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = call i32 @sched_getscheduler(i32 0)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @SCHED_FIFO
%19 = call i32 @sched_get_priority_max(i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = bitcast %struct.sched_param* %4 to i8*
%22 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i64 0, i32 0
store i32 %19, i32* %22
%23 = load i32, i32* @SCHED_FIFO
%24 = call i32 @sched_setscheduler(i32 0, i32 %23, %struct.sched_param* %4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %12
%27 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %12
%30 = call i32 @sched_setparam(i32 0, %struct.sched_param* %4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %29
%36 = call i32 @sched_getparam(i32 0, %struct.sched_param* %4)
%37 = load i32, i32* %22
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i32 %37)
ret i32 0
}