#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PRIO_PROCESS;
int SCHED_OTHER;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, char *);
int sched_setparam(int /*<<< orphan*/, struct sched_param *);
int setpriority(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int set_priority(pid_t pid, int policy, int priority) {
  int ret;
  if (policy != SCHED_OTHER) {
    struct sched_param sched_p;
    sched_p.__sched_priority = priority;
    ret = sched_setparam(pid, &sched_p);
    if (ret)
      fprintf(stderr, "sched_setparam returned %d errno=%d (%s).\n", ret, errno,
              strerror(errno));
  } else {
    ret = setpriority(PRIO_PROCESS, pid, priority);
    if (ret)
      fprintf(stderr, "setpriority returned %d errno=%d (%s).\n", ret, errno,
              strerror(errno));
  }
  return -1;
}


x86 assembly (clang, -O0):
<set_priority>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%eax
je     11cb <set_priority+0x8b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <sched_setparam@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11c6 <set_priority+0x86>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    %rax,%r8
lea    0xe3f(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1235 <set_priority+0xf5>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <setpriority@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1230 <set_priority+0xf0>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    %rax,%r8
lea    0xe00(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1235 <set_priority+0xf5>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_priority>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%eax
mov    0x2e7f(%rip),%rcx
cmp    %esi,(%rcx)
jne    118e <set_priority+0x4e>
mov    0x2e84(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%esi
call   1030 <setpriority@plt>
test   %eax,%eax
je     11d4 <set_priority+0x94>
mov    %eax,%ebx
mov    0x2e66(%rip),%rax
mov    (%rax),%r14d
mov    0x2e4c(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1060 <strerror@plt>
lea    0xe9f(%rip),%rsi
jmp    11c5 <set_priority+0x85>
mov    %edx,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1050 <sched_setparam@plt>
test   %eax,%eax
je     11d4 <set_priority+0x94>
mov    %eax,%ebx
mov    0x2e2d(%rip),%rax
mov    (%rax),%r14d
mov    0x2e13(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1060 <strerror@plt>
lea    0xe3b(%rip),%rsi
mov    %r14d,%edi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %rax,%r8
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_OTHER = global i32 0
@stderr = global i32 0
@.str = [43 x i8] c"sched_setparam returned %d errno=%d (%s).\0A\00"
@errno = global i32 0
@PRIO_PROCESS = global i32 0
@.str.1 = [40 x i8] c"setpriority returned %d errno=%d (%s).\0A\00"
define i32 @set_priority(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sched_param
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %5
%10 = load i32, i32* @SCHED_OTHER
%11 = icmp ne i32 %9, %10
br i1 %11, label %12, label %27
12: ; preds = %3
%13 = load i32, i32* %6
%14 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %8, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* %4
%16 = call i32 @sched_setparam(i32 %15, %struct.sched_param* %8)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i32, i32* %7
%22 = load i32, i32* @errno
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %22, i8* %24)
br label %26
26: ; preds = %19, %12
br label %42
27: ; preds = %3
%28 = load i32, i32* @PRIO_PROCESS
%29 = load i32, i32* %4
%30 = load i32, i32* %6
%31 = call i32 @setpriority(i32 %28, i32 %29, i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = load i32, i32* %7
%37 = load i32, i32* @errno
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %37, i8* %39)
br label %41
41: ; preds = %34, %27
br label %42
42: ; preds = %41, %26
ret i32 -1
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_OTHER = global i32 0
@stderr = global i32 0
@.str = [43 x i8] c"sched_setparam returned %d errno=%d (%s).\0A\00"
@errno = global i32 0
@PRIO_PROCESS = global i32 0
@.str.1 = [40 x i8] c"setpriority returned %d errno=%d (%s).\0A\00"
define i32 @set_priority(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.sched_param
%5 = load i32, i32* @SCHED_OTHER
%6 = icmp eq i32 %5, %1
br i1 %6, label %18, label %7
7: ; preds = %3
%8 = bitcast %struct.sched_param* %4 to i8*
%9 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i64 0, i32 0
store i32 %2, i32* %9
%10 = call i32 @sched_setparam(i32 %0, %struct.sched_param* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %14, i8* %15)
br label %17
17: ; preds = %12, %7
br label %27
18: ; preds = %3
%19 = load i32, i32* @PRIO_PROCESS
%20 = tail call i32 @setpriority(i32 %19, i32 %0, i32 %2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %24, i8* %25)
br label %27
27: ; preds = %18, %22, %17
ret i32 -1
}