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

/* Variables and functions */
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ SCHED_RR;
int /*<<< orphan*/ memset(struct sched_param *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pthread_self();
int pthread_setschedparam(int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sched_param *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int cras_set_thread_priority(int priority) {
  struct sched_param sched_param;
  int err;
  memset(&sched_param, 0, sizeof(sched_param));
  sched_param.sched_priority = priority;
  err = pthread_setschedparam(pthread_self(), SCHED_RR, &sched_param);
  if (err < 0)
    syslog(LOG_WARNING, "Set sched params for thread\n");
  return err;
}


x86 assembly (clang, -O0):
<cras_set_thread_priority>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1030 <pthread_setschedparam@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    119e <cras_set_thread_priority+0x5e>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1050 <syslog@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cras_set_thread_priority>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %ebx,(%rsp)
xor    %eax,%eax
call   1060 <pthread_self@plt>
mov    0x2e67(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
mov    %r14,%rdx
call   1030 <pthread_setschedparam@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1190 <cras_set_thread_priority+0x50>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
call   1050 <syslog@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
@LOG_WARNING = global i32 0
@.str = [29 x i8] c"Set sched params for thread\0A\00"
define i32 @cras_set_thread_priority(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sched_param
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 @memset(%struct.sched_param* %3, i32 0, i32 4)
%6 = load i32, i32* %2
%7 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %3, i32 0, i32 0
store i32 %6, i32* %7
%8 = call i32 (...) @pthread_self()
%9 = load i32, i32* @SCHED_RR
%10 = call i32 @pthread_setschedparam(i32 %8, i32 %9, %struct.sched_param* %3)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i32, i32* @LOG_WARNING
%15 = call i32 @syslog(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %13, %1
%17 = load i32, i32* %4
ret i32 %17
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_RR = global i32 0
@LOG_WARNING = global i32 0
@.str = [29 x i8] c"Set sched params for thread\0A\00"
define i32 @cras_set_thread_priority(i32 %0) {
%2 = alloca %struct.sched_param
%3 = bitcast %struct.sched_param* %2 to i8*
%4 = call i32 @memset(%struct.sched_param* %2, i32 0, i32 4)
%5 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %2, i64 0, i32 0
store i32 %0, i32* %5
%6 = call i32 (...) @pthread_self()
%7 = load i32, i32* @SCHED_RR
%8 = call i32 @pthread_setschedparam(i32 %6, i32 %7, %struct.sched_param* %2)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @LOG_WARNING
%12 = call i32 @syslog(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %10, %1
ret i32 %8
}