#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 MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ PTHREAD_EXPLICIT_SCHED;
int /*<<< orphan*/ PTHREAD_STACK_MIN;
int /*<<< orphan*/ SCHED_FIFO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int pthread_attr_init(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 /*<<< orphan*/);
int pthread_attr_setstack(int /*<<< orphan*/ *, void *, int /*<<< orphan*/);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), int /*<<< orphan*/ *);
void start_thread(int priority, pthread_t *thread, void *(*handler)(void *)) {
  struct sched_param param;
  pthread_attr_t attr;
  void *stack;
  int ret;
  stack = mmap(NULL, PTHREAD_STACK_MIN, PROT_READ | PROT_WRITE,
               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (stack == MAP_FAILED) {
    printf("mmap failed: %m\n");
    exit(-1);
  }
  memset(stack, 0, PTHREAD_STACK_MIN);
  ret = pthread_attr_init(&attr);
  if (ret) {
    printf("init pthread attributes failed\n");
    exit(-1);
  }
  ret = pthread_attr_setstack(&attr, stack, PTHREAD_STACK_MIN);
  if (ret) {
    printf("pthread setstack failed\n");
    exit(-1);
  }
  ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
  if (ret) {
    printf("pthread setschedpolicy failed\n");
    exit(-1);
  }
  param.sched_priority = priority;
  ret = pthread_attr_setschedparam(&attr, &param);
  if (ret) {
    printf("pthread setschedparam failed\n");
    exit(-1);
  }
  ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
  if (ret) {
    printf("pthread setinheritsched failed\n");
    exit(-1);
  }
  ret = pthread_create(thread, &attr, handler, NULL);
  if (ret) {
    printf("create pthread failed\n");
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<start_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
mov    0x2ddb(%rip),%rax
mov    (%rax),%ecx
mov    0x2df2(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2da7(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <start_thread+0x80>
lea    0xdef(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <memset@plt>
lea    -0x24(%rbp),%rdi
call   1080 <pthread_attr_init@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1260 <start_thread+0xc0>
lea    0xdc0(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rsi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rdi
call   1090 <pthread_attr_setstack@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1299 <start_thread+0xf9>
lea    0xda7(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   1060 <pthread_attr_setschedpolicy@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12ce <start_thread+0x12e>
lea    0xd8b(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <pthread_attr_setschedparam@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1304 <start_thread+0x164>
lea    0xd74(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   1030 <pthread_attr_setinheritsched@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1339 <start_thread+0x199>
lea    0xd5d(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   10a0 <pthread_create@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1371 <start_thread+0x1d1>
lea    0xd45(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_thread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
mov    0x2e14(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2e22(%rip),%rax
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dc8(%rip),%rax
mov    0x2de1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d9b(%rip),%rax
cmp    (%rax),%rbx
je     12b6 <start_thread+0x106>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
lea    0x4(%rsp),%rdi
call   1090 <pthread_attr_init@plt>
test   %eax,%eax
jne    12cc <start_thread+0x11c>
mov    0x0(%rbp),%edx
lea    0x4(%rsp),%rdi
mov    %rbx,%rsi
call   10a0 <pthread_attr_setstack@plt>
test   %eax,%eax
jne    12d5 <start_thread+0x125>
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdi
call   1070 <pthread_attr_setschedpolicy@plt>
test   %eax,%eax
jne    12de <start_thread+0x12e>
mov    %r12d,0x8(%rsp)
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   10c0 <pthread_attr_setschedparam@plt>
test   %eax,%eax
jne    12e7 <start_thread+0x137>
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdi
call   1030 <pthread_attr_setinheritsched@plt>
test   %eax,%eax
jne    12f0 <start_thread+0x140>
lea    0x4(%rsp),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
xor    %ecx,%ecx
call   10b0 <pthread_create@plt>
test   %eax,%eax
jne    12f9 <start_thread+0x149>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd43(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xdc6(%rip),%rdi
jmp    1300 <start_thread+0x150>
lea    0xda5(%rip),%rdi
jmp    1300 <start_thread+0x150>
lea    0xd7e(%rip),%rdi
jmp    1300 <start_thread+0x150>
lea    0xd58(%rip),%rdi
jmp    1300 <start_thread+0x150>
lea    0xd30(%rip),%rdi
jmp    1300 <start_thread+0x150>
lea    0xd11(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@PTHREAD_STACK_MIN = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [17 x i8] c"mmap failed: %m\0A\00"
@.str.1 = [32 x i8] c"init pthread attributes failed\0A\00"
@.str.2 = [25 x i8] c"pthread setstack failed\0A\00"
@SCHED_FIFO = global i32 0
@.str.3 = [31 x i8] c"pthread setschedpolicy failed\0A\00"
@.str.4 = [30 x i8] c"pthread setschedparam failed\0A\00"
@PTHREAD_EXPLICIT_SCHED = global i32 0
@.str.5 = [32 x i8] c"pthread setinheritsched failed\0A\00"
@.str.6 = [23 x i8] c"create pthread failed\0A\00"
define void @start_thread(i32 %0, i32* %1, i8* (i8*)* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8* (i8*)*
%7 = alloca %struct.sched_param
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* (i8*)* %2, i8* (i8*)** %6
%11 = load i32, i32* @PTHREAD_STACK_MIN
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_PRIVATE
%16 = load i32, i32* @MAP_ANONYMOUS
%17 = or i32 %15, %16
%18 = call i8* @mmap(i32* null, i32 %11, i32 %14, i32 %17, i32 -1, i32 0)
store i8* %18, i8** %9
%19 = load i8*, i8** %9
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %25
22: ; preds = %3
%23 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %3
%26 = load i8*, i8** %9
%27 = load i32, i32* @PTHREAD_STACK_MIN
%28 = call i32 @memset(i8* %26, i32 0, i32 %27)
%29 = call i32 @pthread_attr_init(i32* %8)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %25
%36 = load i8*, i8** %9
%37 = load i32, i32* @PTHREAD_STACK_MIN
%38 = call i32 @pthread_attr_setstack(i32* %8, i8* %36, i32 %37)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %35
%45 = load i32, i32* @SCHED_FIFO
%46 = call i32 @pthread_attr_setschedpolicy(i32* %8, i32 %45)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %44
%53 = load i32, i32* %4
%54 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %7, i32 0, i32 0
store i32 %53, i32* %54
%55 = call i32 @pthread_attr_setschedparam(i32* %8, %struct.sched_param* %7)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %52
%59 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %52
%62 = load i32, i32* @PTHREAD_EXPLICIT_SCHED
%63 = call i32 @pthread_attr_setinheritsched(i32* %8, i32 %62)
store i32 %63, i32* %10
%64 = load i32, i32* %10
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
%68 = call i32 @exit(i32 -1)
unreachable
69: ; preds = %61
%70 = load i32*, i32** %5
%71 = load i8* (i8*)*, i8* (i8*)** %6
%72 = call i32 @pthread_create(i32* %70, i32* %8, i8* (i8*)* %71, i32* null)
store i32 %72, i32* %10
%73 = load i32, i32* %10
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %69
%76 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%77 = call i32 @exit(i32 -1)
unreachable
78: ; preds = %69
ret void
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@PTHREAD_STACK_MIN = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [17 x i8] c"mmap failed: %m\0A\00"
@SCHED_FIFO = global i32 0
@PTHREAD_EXPLICIT_SCHED = global i32 0
@str = [22 x i8] c"create pthread failed\00"
@str.7 = [31 x i8] c"pthread setinheritsched failed\00"
@str.8 = [29 x i8] c"pthread setschedparam failed\00"
@str.9 = [30 x i8] c"pthread setschedpolicy failed\00"
@str.10 = [24 x i8] c"pthread setstack failed\00"
@str.11 = [31 x i8] c"init pthread attributes failed\00"
define void @start_thread(i32 %0, i32* %1, i8* (i8*)* %2) {
%4 = alloca %struct.sched_param
%5 = alloca i32
%6 = bitcast %struct.sched_param* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = load i32, i32* @PTHREAD_STACK_MIN
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_PRIVATE
%13 = load i32, i32* @MAP_ANONYMOUS
%14 = or i32 %13, %12
%15 = tail call i8* @mmap(i32* null, i32 %8, i32 %11, i32 %14, i32 -1, i32 0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %3
%22 = load i32, i32* @PTHREAD_STACK_MIN
%23 = tail call i32 @memset(i8* %15, i32 0, i32 %22)
%24 = call i32 @pthread_attr_init(i32* %5)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.11, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %21
%30 = load i32, i32* @PTHREAD_STACK_MIN
%31 = call i32 @pthread_attr_setstack(i32* %5, i8* %15, i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.10, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %29
%37 = load i32, i32* @SCHED_FIFO
%38 = call i32 @pthread_attr_setschedpolicy(i32* %5, i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.9, i64 0, i64 0))
%42 = call i32 @exit(i32 -1)
unreachable
43: ; preds = %36
%44 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i64 0, i32 0
store i32 %0, i32* %44
%45 = call i32 @pthread_attr_setschedparam(i32* %5, %struct.sched_param* %4)
%46 = icmp eq i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %43
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.8, i64 0, i64 0))
%49 = call i32 @exit(i32 -1)
unreachable
50: ; preds = %43
%51 = load i32, i32* @PTHREAD_EXPLICIT_SCHED
%52 = call i32 @pthread_attr_setinheritsched(i32* %5, i32 %51)
%53 = icmp eq i32 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %50
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.7, i64 0, i64 0))
%56 = call i32 @exit(i32 -1)
unreachable
57: ; preds = %50
%58 = call i32 @pthread_create(i32* %1, i32* %5, i8* (i8*)* %2, i32* null)
%59 = icmp eq i32 %58, 0
br i1 %59, label %63, label %60
60: ; preds = %57
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%62 = call i32 @exit(i32 -1)
unreachable
63: ; preds = %57
ret void
}