#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*/ pthread_t;
typedef int /*<<< orphan*/ pthread_attr_t;

/* Variables and functions */
int /*<<< orphan*/ PTHREAD_EXPLICIT_SCHED;
int /*<<< orphan*/ PTHREAD_SCOPE_SYSTEM;
int /*<<< orphan*/ pthread_attr_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_attr_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_attr_setinheritsched(int /*<<< orphan*/ *,
                                                int /*<<< orphan*/);
int /*<<< orphan*/ pthread_attr_setschedparam(int /*<<< orphan*/ *,
                                              struct sched_param *);
int /*<<< orphan*/ pthread_attr_setschedpolicy(int /*<<< orphan*/ *, int const);
int /*<<< orphan*/ pthread_attr_setscope(int /*<<< orphan*/ *,
                                         int /*<<< orphan*/);
int /*<<< orphan*/ pthread_attr_setstacksize(int /*<<< orphan*/ *,
                                             size_t const);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), void *);
int sched_get_priority_max(int const);
int sched_get_priority_min(int const);
int _realtime_pthread_create(const int policy, int priority,
                             const size_t stacksize, pthread_t *thread,
                             void *(*start_routine)(void *), void *arg) {
  int rv;
  pthread_attr_t attr;
  struct sched_param parm;
  const int p_min = sched_get_priority_min(policy);
  const int p_max = sched_get_priority_max(policy);
  priority += p_max;
  if (priority > p_max)
    priority = p_max;
  if (priority < p_min)
    priority = p_min;
  parm.sched_priority = priority;
  pthread_attr_init(&attr);
  pthread_attr_setschedpolicy(&attr, policy);
  pthread_attr_setschedparam(&attr, &parm);
  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
  pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
  pthread_attr_setstacksize(&attr, stacksize);
  rv = pthread_create(thread, &attr, start_routine, arg);
  pthread_attr_destroy(&attr);
  return rv;
}


x86 assembly (clang, -O0):
<_realtime_pthread_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <sched_get_priority_min@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x4(%rbp),%edi
call   10b0 <sched_get_priority_max@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x40(%rbp),%eax
jle    11ef <_realtime_pthread_create+0x4f>
mov    -0x40(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1201 <_realtime_pthread_create+0x61>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <pthread_attr_init@plt>
mov    -0x4(%rbp),%esi
lea    -0x30(%rbp),%rdi
call   1040 <pthread_attr_setschedpolicy@plt>
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <pthread_attr_setschedparam@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1060 <pthread_attr_setscope@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1030 <pthread_attr_setinheritsched@plt>
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   10c0 <pthread_attr_setstacksize@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%rsi
call   1080 <pthread_create@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
call   1090 <pthread_attr_destroy@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_realtime_pthread_create>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x8(%rsp)
mov    %r8,%r15
mov    %rcx,%r12
mov    %rdx,%r13
mov    %esi,%ebx
mov    %edi,%ebp
call   1070 <sched_get_priority_min@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   10b0 <sched_get_priority_max@plt>
mov    %ebx,%ecx
sar    $0x1f,%ecx
and    %ebx,%ecx
add    %eax,%ecx
cmp    %r14d,%ecx
cmovl  %r14d,%ecx
mov    %ecx,(%rsp)
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <pthread_attr_init@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <pthread_attr_setschedpolicy@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10a0 <pthread_attr_setschedparam@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <pthread_attr_setscope@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <pthread_attr_setinheritsched@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10c0 <pthread_attr_setstacksize@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    0x8(%rsp),%rcx
call   1080 <pthread_create@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1090 <pthread_attr_destroy@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@PTHREAD_SCOPE_SYSTEM = global i32 0
@PTHREAD_EXPLICIT_SCHED = global i32 0
define i32 @_realtime_pthread_create(i32 %0, i32 %1, i64 %2, i32* %3, i8* (i8*)* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i8* (i8*)*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.sched_param
%16 = alloca i32
%17 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i64 %2, i64* %9
store i32* %3, i32** %10
store i8* (i8*)* %4, i8* (i8*)** %11
store i8* %5, i8** %12
%18 = load i32, i32* %7
%19 = call i32 @sched_get_priority_min(i32 %18)
store i32 %19, i32* %16
%20 = load i32, i32* %7
%21 = call i32 @sched_get_priority_max(i32 %20)
store i32 %21, i32* %17
%22 = load i32, i32* %17
%23 = load i32, i32* %8
%24 = add nsw i32 %23, %22
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = load i32, i32* %17
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %6
%29 = load i32, i32* %17
store i32 %29, i32* %8
br label %30
30: ; preds = %28, %6
%31 = load i32, i32* %8
%32 = load i32, i32* %16
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = load i32, i32* %16
store i32 %35, i32* %8
br label %36
36: ; preds = %34, %30
%37 = load i32, i32* %8
%38 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %15, i32 0, i32 0
store i32 %37, i32* %38
%39 = call i32 @pthread_attr_init(i32* %14)
%40 = load i32, i32* %7
%41 = call i32 @pthread_attr_setschedpolicy(i32* %14, i32 %40)
%42 = call i32 @pthread_attr_setschedparam(i32* %14, %struct.sched_param* %15)
%43 = load i32, i32* @PTHREAD_SCOPE_SYSTEM
%44 = call i32 @pthread_attr_setscope(i32* %14, i32 %43)
%45 = load i32, i32* @PTHREAD_EXPLICIT_SCHED
%46 = call i32 @pthread_attr_setinheritsched(i32* %14, i32 %45)
%47 = load i64, i64* %9
%48 = call i32 @pthread_attr_setstacksize(i32* %14, i64 %47)
%49 = load i32*, i32** %10
%50 = load i8* (i8*)*, i8* (i8*)** %11
%51 = load i8*, i8** %12
%52 = call i32 @pthread_create(i32* %49, i32* %14, i8* (i8*)* %50, i8* %51)
store i32 %52, i32* %13
%53 = call i32 @pthread_attr_destroy(i32* %14)
%54 = load i32, i32* %13
ret i32 %54
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@PTHREAD_SCOPE_SYSTEM = global i32 0
@PTHREAD_EXPLICIT_SCHED = global i32 0
define i32 @_realtime_pthread_create(i32 %0, i32 %1, i64 %2, i32* %3, i8* (i8*)* %4, i8* %5) {
%7 = alloca i32
%8 = alloca %struct.sched_param
%9 = bitcast i32* %7 to i8*
%10 = bitcast %struct.sched_param* %8 to i8*
%11 = tail call i32 @sched_get_priority_min(i32 %0)
%12 = tail call i32 @sched_get_priority_max(i32 %0)
%13 = icmp slt i32 %1, 0
%14 = select i1 %13, i32 %1, i32 0
%15 = add nsw i32 %12, %14
%16 = icmp slt i32 %15, %11
%17 = select i1 %16, i32 %11, i32 %15
%18 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %8, i64 0, i32 0
store i32 %17, i32* %18
%19 = call i32 @pthread_attr_init(i32* %7)
%20 = call i32 @pthread_attr_setschedpolicy(i32* %7, i32 %0)
%21 = call i32 @pthread_attr_setschedparam(i32* %7, %struct.sched_param* %8)
%22 = load i32, i32* @PTHREAD_SCOPE_SYSTEM
%23 = call i32 @pthread_attr_setscope(i32* %7, i32 %22)
%24 = load i32, i32* @PTHREAD_EXPLICIT_SCHED
%25 = call i32 @pthread_attr_setinheritsched(i32* %7, i32 %24)
%26 = call i32 @pthread_attr_setstacksize(i32* %7, i64 %2)
%27 = call i32 @pthread_create(i32* %3, i32* %7, i8* (i8*)* %4, i8* %5)
%28 = call i32 @pthread_attr_destroy(i32* %7)
ret i32 %27
}