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

/* Variables and functions */
int /*<<< orphan*/ PTHREAD_EXPLICIT_SCHED;
int /*<<< orphan*/ SCHED_FIFO;
int /*<<< orphan*/ error(char *, int);
int pthread_attr_init(int /*<<< orphan*/ *);
int pthread_attr_setaffinity_np(int /*<<< orphan*/ *, int,
                                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 setup_thread_attr(pthread_attr_t *attr, int prio, cpu_set_t *mask) {
  int status;
  struct sched_param thread_param;
  status = pthread_attr_init(attr);
  if (status) {
    error("setup_thread_attr: initializing thread attribute: 0x%x\n", status);
    return 1;
  }
  status = pthread_attr_setschedpolicy(attr, SCHED_FIFO);
  if (status) {
    error("setup_thread_attr: setting attribute policy to SCHED_FIFO: 0x%x\n",
          status);
    return 1;
  }
  status = pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
  if (status) {
    error("setup_thread_attr: setting explicit scheduling inheritance: 0x%x\n",
          status);
    return 1;
  }
  thread_param.sched_priority = prio;
  status = pthread_attr_setschedparam(attr, &thread_param);
  if (status) {
    error("setup_thread_attr: setting scheduler param: 0x%x\n", status);
    return 1;
  }
  status = pthread_attr_setaffinity_np(attr, sizeof(cpu_set_t), mask);
  if (status) {
    error("setup_thread_attr: setting affinity attribute: 0x%x\n", status);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_thread_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <pthread_attr_init@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11a4 <setup_thread_attr+0x44>
mov    -0x24(%rbp),%esi
lea    0xe6d(%rip),%rdi
call   1070 <error@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <setup_thread_attr+0x134>
mov    -0x10(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1050 <pthread_attr_setschedpolicy@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11de <setup_thread_attr+0x7e>
mov    -0x24(%rbp),%esi
lea    0xe6b(%rip),%rdi
call   1070 <error@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <setup_thread_attr+0x134>
mov    -0x10(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
call   1040 <pthread_attr_setinheritsched@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1218 <setup_thread_attr+0xb8>
mov    -0x24(%rbp),%esi
lea    0xe72(%rip),%rdi
call   1070 <error@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <setup_thread_attr+0x134>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <pthread_attr_setschedparam@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1253 <setup_thread_attr+0xf3>
mov    -0x24(%rbp),%esi
lea    0xe79(%rip),%rdi
call   1070 <error@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <setup_thread_attr+0x134>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x4,%esi
call   1030 <pthread_attr_setaffinity_np@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     128d <setup_thread_attr+0x12d>
mov    -0x24(%rbp),%esi
lea    0xe71(%rip),%rdi
call   1070 <error@plt>
movl   $0x1,-0x4(%rbp)
jmp    1294 <setup_thread_attr+0x134>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_thread_attr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
call   1060 <pthread_attr_init@plt>
test   %eax,%eax
je     1182 <setup_thread_attr+0x22>
lea    0xe80(%rip),%rdi
jmp    11f7 <setup_thread_attr+0x97>
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <pthread_attr_setschedpolicy@plt>
test   %eax,%eax
je     11a0 <setup_thread_attr+0x40>
lea    0xe9a(%rip),%rdi
jmp    11f7 <setup_thread_attr+0x97>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <pthread_attr_setinheritsched@plt>
test   %eax,%eax
je     11be <setup_thread_attr+0x5e>
lea    0xebd(%rip),%rdi
jmp    11f7 <setup_thread_attr+0x97>
mov    %ebp,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <pthread_attr_setschedparam@plt>
test   %eax,%eax
je     11dc <setup_thread_attr+0x7c>
lea    0xee1(%rip),%rdi
jmp    11f7 <setup_thread_attr+0x97>
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r14,%rdx
call   1030 <pthread_attr_setaffinity_np@plt>
test   %eax,%eax
je     120c <setup_thread_attr+0xac>
lea    0xef6(%rip),%rdi
mov    %eax,%esi
call   1070 <error@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
jmp    1203 <setup_thread_attr+0xa3>

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [56 x i8] c"setup_thread_attr: initializing thread attribute: 0x%x\0A\00"
@SCHED_FIFO = global i32 0
@.str.1 = [65 x i8] c"setup_thread_attr: setting attribute policy to SCHED_FIFO: 0x%x\0A\00"
@PTHREAD_EXPLICIT_SCHED = global i32 0
@.str.2 = [66 x i8] c"setup_thread_attr: setting explicit scheduling inheritance: 0x%x\0A\00"
@.str.3 = [50 x i8] c"setup_thread_attr: setting scheduler param: 0x%x\0A\00"
@.str.4 = [53 x i8] c"setup_thread_attr: setting affinity attribute: 0x%x\0A\00"
define i32 @setup_thread_attr(i32* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.sched_param
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%10 = load i32*, i32** %5
%11 = call i32 @pthread_attr_init(i32* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i32, i32* %8
%16 = call i32 @error(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 1, i32* %4
br label %55
17: ; preds = %3
%18 = load i32*, i32** %5
%19 = load i32, i32* @SCHED_FIFO
%20 = call i32 @pthread_attr_setschedpolicy(i32* %18, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32, i32* %8
%25 = call i32 @error(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 1, i32* %4
br label %55
26: ; preds = %17
%27 = load i32*, i32** %5
%28 = load i32, i32* @PTHREAD_EXPLICIT_SCHED
%29 = call i32 @pthread_attr_setinheritsched(i32* %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32, i32* %8
%34 = call i32 @error(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 1, i32* %4
br label %55
35: ; preds = %26
%36 = load i32, i32* %6
%37 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %9, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32*, i32** %5
%39 = call i32 @pthread_attr_setschedparam(i32* %38, %struct.sched_param* %9)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = load i32, i32* %8
%44 = call i32 @error(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %43)
store i32 1, i32* %4
br label %55
45: ; preds = %35
%46 = load i32*, i32** %5
%47 = load i32*, i32** %7
%48 = call i32 @pthread_attr_setaffinity_np(i32* %46, i32 4, i32* %47)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = load i32, i32* %8
%53 = call i32 @error(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %52)
store i32 1, i32* %4
br label %55
54: ; preds = %45
store i32 0, i32* %4
br label %55
55: ; preds = %54, %51, %42, %32, %23, %14
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str = [56 x i8] c"setup_thread_attr: initializing thread attribute: 0x%x\0A\00"
@SCHED_FIFO = global i32 0
@.str.1 = [65 x i8] c"setup_thread_attr: setting attribute policy to SCHED_FIFO: 0x%x\0A\00"
@PTHREAD_EXPLICIT_SCHED = global i32 0
@.str.2 = [66 x i8] c"setup_thread_attr: setting explicit scheduling inheritance: 0x%x\0A\00"
@.str.3 = [50 x i8] c"setup_thread_attr: setting scheduler param: 0x%x\0A\00"
@.str.4 = [53 x i8] c"setup_thread_attr: setting affinity attribute: 0x%x\0A\00"
define i32 @setup_thread_attr(i32* %0, i32 %1, i32* %2) {
%4 = alloca %struct.sched_param
%5 = bitcast %struct.sched_param* %4 to i8*
%6 = tail call i32 @pthread_attr_init(i32* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @error(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %33
10: ; preds = %3
%11 = load i32, i32* @SCHED_FIFO
%12 = tail call i32 @pthread_attr_setschedpolicy(i32* %0, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @error(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %33
16: ; preds = %10
%17 = load i32, i32* @PTHREAD_EXPLICIT_SCHED
%18 = tail call i32 @pthread_attr_setinheritsched(i32* %0, i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 @error(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %33
22: ; preds = %16
%23 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %4, i64 0, i32 0
store i32 %1, i32* %23
%24 = call i32 @pthread_attr_setschedparam(i32* %0, %struct.sched_param* %4)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32 @error(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %33
28: ; preds = %22
%29 = call i32 @pthread_attr_setaffinity_np(i32* %0, i32 4, i32* %2)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @error(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %33
33: ; preds = %28, %31, %26, %20, %14, %8
%34 = phi i32 [ 1, %8 ], [ 1, %14 ], [ 1, %20 ], [ 1, %26 ], [ 1, %31 ], [ 0, %28 ]
ret i32 %34
}