#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;
};
typedef int pid_t;

/* Variables and functions */
int getpid();
int /*<<< orphan*/ memset(struct sched_param *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int sched_get_priority_max(int);
int sched_getscheduler(int);
int sched_setparam(int, struct sched_param *);
int main(void) {
  pid_t pid;
  struct sched_param sp;
  int policy, max, pr;
  pid = getpid();
  if (pid == -1) {
    perror("getpid");
  }
  policy = sched_getscheduler(pid);
  if (policy == -1) {
    return -1;
  }
  max = sched_get_priority_max(policy);
  if (max == -1) {
    return 1;
  }
  memset(&sp, 0, sizeof(struct sched_param));
  sp.sched_priority = max;
  pr = sched_setparam(pid, &sp);
  printf("New priority %d\n", pr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    119f <main+0x2f>
lea    0xe66(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1030 <sched_getscheduler@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c0 <main+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <main+0xac>
mov    -0x14(%rbp),%edi
call   1090 <sched_get_priority_max@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11e1 <main+0x71>
movl   $0x1,-0x4(%rbp)
jmp    121c <main+0xac>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <sched_setparam@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xdf7(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11ed <main+0x7d>
mov    %ebx,%edi
call   1030 <sched_getscheduler@plt>
cmp    $0xffffffff,%eax
je     11d1 <main+0x61>
mov    %eax,%edi
call   1090 <sched_get_priority_max@plt>
cmp    $0xffffffff,%eax
je     11d9 <main+0x69>
mov    %eax,%ebp
mov    %rsp,%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    %ebp,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
call   1080 <sched_setparam@plt>
lea    0xe3f(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
jmp    11df <main+0x6f>
mov    $0xffffffff,%r14d
jmp    11df <main+0x6f>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
jmp    1185 <main+0x15>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [7 x i8] c"getpid\00"
@.str.1 = [17 x i8] c"New priority %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sched_param
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (...) @getpid()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32, i32* %2
%14 = call i32 @sched_getscheduler(i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 -1, i32* %1
br label %32
18: ; preds = %12
%19 = load i32, i32* %4
%20 = call i32 @sched_get_priority_max(i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 1, i32* %1
br label %32
24: ; preds = %18
%25 = call i32 @memset(%struct.sched_param* %3, i32 0, i32 4)
%26 = load i32, i32* %5
%27 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %2
%29 = call i32 @sched_setparam(i32 %28, %struct.sched_param* %3)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %30)
store i32 0, i32* %1
br label %32
32: ; preds = %24, %23, %17
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str = [7 x i8] c"getpid\00"
@.str.1 = [17 x i8] c"New priority %d\0A\00"
define i32 @main() {
%1 = alloca %struct.sched_param
%2 = bitcast %struct.sched_param* %1 to i8*
%3 = tail call i32 (...) @getpid()
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = tail call i32 @sched_getscheduler(i32 %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %18, label %10
10: ; preds = %7
%11 = tail call i32 @sched_get_priority_max(i32 %8)
%12 = icmp eq i32 %11, -1
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = call i32 @memset(%struct.sched_param* %1, i32 0, i32 4)
%15 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %1, i64 0, i32 0
store i32 %11, i32* %15
%16 = call i32 @sched_setparam(i32 %3, %struct.sched_param* %1)
%17 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %10, %7, %13
%19 = phi i32 [ 0, %13 ], [ -1, %7 ], [ 1, %10 ]
ret i32 %19
}