#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_CREATE_DETACHED;
int /*<<< orphan*/ PTHREAD_EXPLICIT_SCHED;
int SCHED_FIFO;
int SCHED_OTHER;
int SCHED_RR;
int /*<<< orphan*/ pthread_attr_destroy(int /*<<< orphan*/ *);
int pthread_attr_init(int /*<<< orphan*/ *);
int pthread_attr_setdetachstate(int /*<<< orphan*/ *, int /*<<< orphan*/);
int pthread_attr_setinheritsched(int /*<<< orphan*/ *, int /*<<< orphan*/);
int pthread_attr_setschedparam(int /*<<< orphan*/ *, struct sched_param *);
int pthread_attr_setschedpolicy(int /*<<< orphan*/ *, int);
int pthread_attr_setstacksize(int /*<<< orphan*/ *, size_t);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), void *);
int os_thread_create(int policy, int priority, size_t stack_size,
                     void *(*start)(void *), void *arg) {
  int ret;
  pthread_t tid;
  pthread_attr_t attr;
  struct sched_param params;
  if (start == NULL) {
    return -1;
  }
  switch (policy) {
  case 1:
    policy = SCHED_FIFO;
    break;
  case 2:
    policy = SCHED_RR;
    break;
  case 0:
  default:
    policy = SCHED_OTHER;
    break;
  }
  ret = pthread_attr_init(&attr);
  if (ret != 0) {
    return -2;
  }
  if (stack_size > 0) {
    ret = pthread_attr_setstacksize(&attr, stack_size);
    if (ret != 0) {
      ret = -3;
      goto out;
    }
  }
  ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  if (0 != ret) {
    ret = -4;
    goto out;
  }
  ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
  if (0 != ret) {
    ret = -5;
    goto out;
  }
  ret = pthread_attr_setschedpolicy(&attr, policy);
  if (0 != ret) {
    ret = -6;
    goto out;
  }
  params.sched_priority = priority;
  ret = pthread_attr_setschedparam(&attr, &params);
  if (0 != ret) {
    ret = -7;
    goto out;
  }
  ret = pthread_create(&tid, &attr, start, arg);
  if (0 != ret) {
    ret = -8;
    goto out;
  }
out:
  pthread_attr_destroy(&attr);
  return ret;
}


x86 assembly (clang, -O0):
<os_thread_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b1 <os_thread_create+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <os_thread_create+0x1df>
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
test   %eax,%eax
je     1208 <os_thread_create+0x88>
jmp    11c4 <os_thread_create+0x44>
mov    -0x3c(%rbp),%eax
sub    $0x1,%eax
je     11e6 <os_thread_create+0x66>
jmp    11d5 <os_thread_create+0x55>
mov    -0x3c(%rbp),%eax
sub    $0x2,%eax
je     11f7 <os_thread_create+0x77>
jmp    120d <os_thread_create+0x8d>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
jmp    1219 <os_thread_create+0x99>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
jmp    1219 <os_thread_create+0x99>
jmp    120d <os_thread_create+0x8d>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    -0x34(%rbp),%rdi
call   1050 <pthread_attr_init@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     123b <os_thread_create+0xbb>
movl   $0xfffffffe,-0x4(%rbp)
jmp    135f <os_thread_create+0x1df>
cmpq   $0x0,-0x18(%rbp)
jbe    1271 <os_thread_create+0xf1>
mov    -0x18(%rbp),%rsi
lea    -0x34(%rbp),%rdi
call   10a0 <pthread_attr_setstacksize@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     126c <os_thread_create+0xec>
movl   $0xfffffffd,-0x2c(%rbp)
jmp    1350 <os_thread_create+0x1d0>
jmp    1271 <os_thread_create+0xf1>
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
lea    -0x34(%rbp),%rdi
call   1060 <pthread_attr_setdetachstate@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
je     129d <os_thread_create+0x11d>
movl   $0xfffffffc,-0x2c(%rbp)
jmp    1350 <os_thread_create+0x1d0>
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
lea    -0x34(%rbp),%rdi
call   1030 <pthread_attr_setinheritsched@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
je     12c9 <os_thread_create+0x149>
movl   $0xfffffffb,-0x2c(%rbp)
jmp    1350 <os_thread_create+0x1d0>
mov    -0x8(%rbp),%esi
lea    -0x34(%rbp),%rdi
call   1040 <pthread_attr_setschedpolicy@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
je     12ef <os_thread_create+0x16f>
movl   $0xfffffffa,-0x2c(%rbp)
jmp    1350 <os_thread_create+0x1d0>
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
lea    -0x34(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1090 <pthread_attr_setschedparam@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
je     131c <os_thread_create+0x19c>
movl   $0xfffffff9,-0x2c(%rbp)
jmp    1350 <os_thread_create+0x1d0>
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1070 <pthread_create@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
je     134b <os_thread_create+0x1cb>
movl   $0xfffffff8,-0x2c(%rbp)
jmp    1350 <os_thread_create+0x1d0>
jmp    1350 <os_thread_create+0x1d0>
lea    -0x34(%rbp),%rdi
call   1080 <pthread_attr_destroy@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_thread_create>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rcx,%rcx
je     11b2 <os_thread_create+0x32>
mov    %r8,%r14
mov    %rcx,%r13
mov    %rdx,%rbp
mov    %esi,%r12d
cmp    $0x2,%edi
jne    11bc <os_thread_create+0x3c>
mov    0x2e0d(%rip),%rax
cmp    $0x1,%edi
je     11c8 <os_thread_create+0x48>
jmp    11cf <os_thread_create+0x4f>
mov    $0xffffffff,%ebx
jmp    12a0 <os_thread_create+0x120>
mov    0x2e05(%rip),%rax
cmp    $0x1,%edi
jne    11cf <os_thread_create+0x4f>
mov    0x2e11(%rip),%rax
mov    (%rax),%r15d
lea    0xc(%rsp),%rdi
call   1050 <pthread_attr_init@plt>
mov    $0xfffffffe,%ebx
test   %eax,%eax
jne    12a0 <os_thread_create+0x120>
test   %rbp,%rbp
je     1208 <os_thread_create+0x88>
lea    0xc(%rsp),%rdi
mov    %rbp,%rsi
call   10a0 <pthread_attr_setstacksize@plt>
mov    $0xfffffffd,%ebx
test   %eax,%eax
jne    1296 <os_thread_create+0x116>
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdi
call   1060 <pthread_attr_setdetachstate@plt>
mov    $0xfffffffc,%ebx
test   %eax,%eax
jne    1296 <os_thread_create+0x116>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdi
call   1030 <pthread_attr_setinheritsched@plt>
mov    $0xfffffffb,%ebx
test   %eax,%eax
jne    1296 <os_thread_create+0x116>
lea    0xc(%rsp),%rdi
mov    %r15d,%esi
call   1040 <pthread_attr_setschedpolicy@plt>
mov    $0xfffffffa,%ebx
test   %eax,%eax
jne    1296 <os_thread_create+0x116>
mov    %r12d,0x10(%rsp)
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1090 <pthread_attr_setschedparam@plt>
mov    $0xfffffff9,%ebx
test   %eax,%eax
jne    1296 <os_thread_create+0x116>
lea    0x14(%rsp),%rdi
lea    0xc(%rsp),%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   1070 <pthread_create@plt>
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
lea    -0x8(,%rcx,8),%ebx
lea    0xc(%rsp),%rdi
call   1080 <pthread_attr_destroy@plt>
mov    %ebx,%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 }
@SCHED_FIFO = global i32 0
@SCHED_RR = global i32 0
@SCHED_OTHER = global i32 0
@PTHREAD_CREATE_DETACHED = global i32 0
@PTHREAD_EXPLICIT_SCHED = global i32 0
define i32 @os_thread_create(i32 %0, i32 %1, i64 %2, i8* (i8*)* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8* (i8*)*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.sched_param
store i32 %0, i32* %7
store i32 %1, i32* %8
store i64 %2, i64* %9
store i8* (i8*)* %3, i8* (i8*)** %10
store i8* %4, i8** %11
%16 = load i8* (i8*)*, i8* (i8*)** %10
%17 = icmp eq i8* (i8*)* %16, null
br i1 %17, label %18, label %19
18: ; preds = %5
store i32 -1, i32* %6
br label %79
19: ; preds = %5
%20 = load i32, i32* %7
switch i32 %20, label %26 [
i32 1, label %21
i32 2, label %23
i32 0, label %25
]
21: ; preds = %19
%22 = load i32, i32* @SCHED_FIFO
store i32 %22, i32* %7
br label %28
23: ; preds = %19
%24 = load i32, i32* @SCHED_RR
store i32 %24, i32* %7
br label %28
25: ; preds = %19
br label %26
26: ; preds = %19, %25
%27 = load i32, i32* @SCHED_OTHER
store i32 %27, i32* %7
br label %28
28: ; preds = %26, %23, %21
%29 = call i32 @pthread_attr_init(i32* %14)
store i32 %29, i32* %12
%30 = load i32, i32* %12
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 -2, i32* %6
br label %79
33: ; preds = %28
%34 = load i64, i64* %9
%35 = icmp ugt i64 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load i64, i64* %9
%38 = call i32 @pthread_attr_setstacksize(i32* %14, i64 %37)
store i32 %38, i32* %12
%39 = load i32, i32* %12
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 -3, i32* %12
br label %76
42: ; preds = %36
br label %43
43: ; preds = %42, %33
%44 = load i32, i32* @PTHREAD_CREATE_DETACHED
%45 = call i32 @pthread_attr_setdetachstate(i32* %14, i32 %44)
store i32 %45, i32* %12
%46 = load i32, i32* %12
%47 = icmp ne i32 0, %46
br i1 %47, label %48, label %49
48: ; preds = %43
store i32 -4, i32* %12
br label %76
49: ; preds = %43
%50 = load i32, i32* @PTHREAD_EXPLICIT_SCHED
%51 = call i32 @pthread_attr_setinheritsched(i32* %14, i32 %50)
store i32 %51, i32* %12
%52 = load i32, i32* %12
%53 = icmp ne i32 0, %52
br i1 %53, label %54, label %55
54: ; preds = %49
store i32 -5, i32* %12
br label %76
55: ; preds = %49
%56 = load i32, i32* %7
%57 = call i32 @pthread_attr_setschedpolicy(i32* %14, i32 %56)
store i32 %57, i32* %12
%58 = load i32, i32* %12
%59 = icmp ne i32 0, %58
br i1 %59, label %60, label %61
60: ; preds = %55
store i32 -6, i32* %12
br label %76
61: ; preds = %55
%62 = load i32, i32* %8
%63 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %15, i32 0, i32 0
store i32 %62, i32* %63
%64 = call i32 @pthread_attr_setschedparam(i32* %14, %struct.sched_param* %15)
store i32 %64, i32* %12
%65 = load i32, i32* %12
%66 = icmp ne i32 0, %65
br i1 %66, label %67, label %68
67: ; preds = %61
store i32 -7, i32* %12
br label %76
68: ; preds = %61
%69 = load i8* (i8*)*, i8* (i8*)** %10
%70 = load i8*, i8** %11
%71 = call i32 @pthread_create(i32* %13, i32* %14, i8* (i8*)* %69, i8* %70)
store i32 %71, i32* %12
%72 = load i32, i32* %12
%73 = icmp ne i32 0, %72
br i1 %73, label %74, label %75
74: ; preds = %68
store i32 -8, i32* %12
br label %76
75: ; preds = %68
br label %76
76: ; preds = %75, %74, %67, %60, %54, %48, %41
%77 = call i32 @pthread_attr_destroy(i32* %14)
%78 = load i32, i32* %12
store i32 %78, i32* %6
br label %79
79: ; preds = %76, %32, %18
%80 = load i32, i32* %6
ret i32 %80
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@SCHED_FIFO = global i32 0
@SCHED_RR = global i32 0
@SCHED_OTHER = global i32 0
@PTHREAD_CREATE_DETACHED = global i32 0
@PTHREAD_EXPLICIT_SCHED = global i32 0
define i32 @os_thread_create(i32 %0, i32 %1, i64 %2, i8* (i8*)* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sched_param
%9 = bitcast i32* %6 to i8*
%10 = bitcast i32* %7 to i8*
%11 = bitcast %struct.sched_param* %8 to i8*
%12 = icmp eq i8* (i8*)* %3, null
br i1 %12, label %50, label %13
13: ; preds = %5
%14 = icmp eq i32 %0, 2
%15 = icmp eq i32 %0, 1
%16 = load i32, i32* @SCHED_FIFO
%17 = load i32, i32* @SCHED_RR
%18 = load i32, i32* @SCHED_OTHER
%19 = select i1 %14, i32 %17, i32 %18
%20 = select i1 %15, i32 %16, i32 %19
%21 = call i32 @pthread_attr_init(i32* %7)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %50
23: ; preds = %13
%24 = icmp eq i64 %2, 0
br i1 %24, label %28, label %25
25: ; preds = %23
%26 = call i32 @pthread_attr_setstacksize(i32* %7, i64 %2)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %47
28: ; preds = %25, %23
%29 = load i32, i32* @PTHREAD_CREATE_DETACHED
%30 = call i32 @pthread_attr_setdetachstate(i32* %7, i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %47
32: ; preds = %28
%33 = load i32, i32* @PTHREAD_EXPLICIT_SCHED
%34 = call i32 @pthread_attr_setinheritsched(i32* %7, i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %47
36: ; preds = %32
%37 = call i32 @pthread_attr_setschedpolicy(i32* %7, i32 %20)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %8, i64 0, i32 0
store i32 %1, i32* %40
%41 = call i32 @pthread_attr_setschedparam(i32* %7, %struct.sched_param* %8)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = call i32 @pthread_create(i32* %6, i32* %7, i8* (i8*)* %3, i8* %4)
%45 = icmp eq i32 %44, 0
%46 = select i1 %45, i32 0, i32 -8
br label %47
47: ; preds = %39, %36, %32, %28, %25, %43
%48 = phi i32 [ %46, %43 ], [ -3, %25 ], [ -4, %28 ], [ -5, %32 ], [ -6, %36 ], [ -7, %39 ]
%49 = call i32 @pthread_attr_destroy(i32* %7)
br label %50
50: ; preds = %13, %5, %47
%51 = phi i32 [ %48, %47 ], [ -1, %5 ], [ -2, %13 ]
ret i32 %51
}