#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*/ mask;
typedef int /*<<< orphan*/ cpu_set_t;

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SCHED_RR;
int sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int sched_setscheduler(int /*<<< orphan*/, int /*<<< orphan*/,
                       struct sched_param *);
int set_affinity_prio(int cpu, int prio) {
  cpu_set_t mask;
  struct sched_param p;
  int ret = 0;
  p.sched_priority = prio;
  CPU_ZERO(&mask);
  CPU_SET(cpu, &mask);
  ret = sched_setaffinity(0, sizeof(mask), &mask);
  if (ret)
    goto end;
  ret = sched_setscheduler(0, SCHED_RR, &p);
  if (ret)
    goto end;
end:
  return ret;
}


x86 assembly (clang, -O0):
<set_affinity_prio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <CPU_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1060 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1192 <set_affinity_prio+0x52>
jmp    11bd <set_affinity_prio+0x7d>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1030 <sched_setscheduler@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b8 <set_affinity_prio+0x78>
jmp    11bd <set_affinity_prio+0x7d>
jmp    11bd <set_affinity_prio+0x7d>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_affinity_prio>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %esi,(%rsp)
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1050 <CPU_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1040 <sched_setaffinity@plt>
test   %eax,%eax
jne    1186 <set_affinity_prio+0x46>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1030 <sched_setscheduler@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
define i32 @set_affinity_prio(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sched_param
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %7
%8 = load i32, i32* %4
%9 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %6, i32 0, i32 0
store i32 %8, i32* %9
%10 = call i32 @CPU_ZERO(i32* %5)
%11 = load i32, i32* %3
%12 = call i32 @CPU_SET(i32 %11, i32* %5)
%13 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %5)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
br label %24
17: ; preds = %2
%18 = load i32, i32* @SCHED_RR
%19 = call i32 @sched_setscheduler(i32 0, i32 %18, %struct.sched_param* %6)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
br label %24
23: ; preds = %17
br label %24
24: ; preds = %23, %22, %16
%25 = load i32, i32* %7
ret i32 %25
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
define i32 @set_affinity_prio(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sched_param
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sched_param* %4 to i8*
%7 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i64 0, i32 0
store i32 %1, i32* %7
%8 = call i32 @CPU_ZERO(i32* %3)
%9 = call i32 @CPU_SET(i32 %0, i32* %3)
%10 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %3)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @SCHED_RR
%14 = call i32 @sched_setscheduler(i32 0, i32 %13, %struct.sched_param* %4)
br label %15
15: ; preds = %12, %2
%16 = phi i32 [ %10, %2 ], [ %14, %12 ]
ret i32 %16
}