#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*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ PTHREAD_EXPLICIT_SCHED;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pthread_attr_init(int /*<<< orphan*/ *);
scalar_t__ pthread_attr_setaffinity_np(int /*<<< orphan*/ *, int,
                                       int /*<<< orphan*/ *);
scalar_t__ pthread_attr_setinheritsched(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/);
scalar_t__ pthread_attr_setschedparam(int /*<<< orphan*/ *,
                                      struct sched_param *);
scalar_t__ pthread_attr_setschedpolicy(int /*<<< orphan*/ *, int);
int set_thread_attr(pthread_attr_t *attr, int policy, int priority, int cpuid) {
  struct sched_param param;
  cpu_set_t cpu_info;
  pthread_attr_init(attr);
  if (pthread_attr_setschedpolicy(attr, policy) != 0) {
    perror("pthread_attr_setschedpolicy");
    return -1;
  }
  param.sched_priority = priority;
  if (pthread_attr_setschedparam(attr, &param) != 0) {
    perror("pthread_attr_setschedparam");
    return -1;
  }
  CPU_ZERO(&cpu_info);
  CPU_SET(cpuid, &cpu_info);
  if (pthread_attr_setaffinity_np(attr, sizeof(cpu_set_t), &cpu_info)) {
    perror("pthread_attr_setaffinity_np");
    return -1;
  }
  if (pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED) != 0) {
    perror("pthread_attr_setinheritsched");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_thread_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <pthread_attr_init@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <pthread_attr_setschedpolicy@plt>
cmp    $0x0,%rax
je     11cc <set_thread_attr+0x4c>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <set_thread_attr+0x105>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <pthread_attr_setschedparam@plt>
cmp    $0x0,%rax
je     1201 <set_thread_attr+0x81>
lea    0xe2c(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <set_thread_attr+0x105>
lea    -0x24(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   10a0 <CPU_SET@plt>
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
lea    -0x24(%rbp),%rdx
call   1030 <pthread_attr_setaffinity_np@plt>
cmp    $0x0,%rax
je     124a <set_thread_attr+0xca>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <set_thread_attr+0x105>
mov    -0x10(%rbp),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
call   1040 <pthread_attr_setinheritsched@plt>
cmp    $0x0,%rax
je     127e <set_thread_attr+0xfe>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <set_thread_attr+0x105>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_thread_attr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%rbx
call   1060 <pthread_attr_init@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <pthread_attr_setschedpolicy@plt>
test   %rax,%rax
jne    120b <set_thread_attr+0x8b>
mov    %r15d,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <pthread_attr_setschedparam@plt>
test   %rax,%rax
jne    1214 <set_thread_attr+0x94>
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <CPU_ZERO@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   10a0 <CPU_SET@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %rbp,%rdx
call   1030 <pthread_attr_setaffinity_np@plt>
test   %rax,%rax
jne    121d <set_thread_attr+0x9d>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <pthread_attr_setinheritsched@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     122e <set_thread_attr+0xae>
lea    0xe4a(%rip),%rdi
jmp    1224 <set_thread_attr+0xa4>
lea    0xdee(%rip),%rdi
jmp    1224 <set_thread_attr+0xa4>
lea    0xe01(%rip),%rdi
jmp    1224 <set_thread_attr+0xa4>
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sched_param = type { i32 }
@.str = [28 x i8] c"pthread_attr_setschedpolicy\00"
@.str.1 = [27 x i8] c"pthread_attr_setschedparam\00"
@.str.2 = [28 x i8] c"pthread_attr_setaffinity_np\00"
@PTHREAD_EXPLICIT_SCHED = global i32 0
@.str.3 = [29 x i8] c"pthread_attr_setinheritsched\00"
define i32 @set_thread_attr(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sched_param
%11 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32*, i32** %6
%13 = call i32 @pthread_attr_init(i32* %12)
%14 = load i32*, i32** %6
%15 = load i32, i32* %7
%16 = call i64 @pthread_attr_setschedpolicy(i32* %14, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %45
20: ; preds = %4
%21 = load i32, i32* %8
%22 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %10, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32*, i32** %6
%24 = call i64 @pthread_attr_setschedparam(i32* %23, %struct.sched_param* %10)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %45
28: ; preds = %20
%29 = call i32 @CPU_ZERO(i32* %11)
%30 = load i32, i32* %9
%31 = call i32 @CPU_SET(i32 %30, i32* %11)
%32 = load i32*, i32** %6
%33 = call i64 @pthread_attr_setaffinity_np(i32* %32, i32 4, i32* %11)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %45
37: ; preds = %28
%38 = load i32*, i32** %6
%39 = load i32, i32* @PTHREAD_EXPLICIT_SCHED
%40 = call i64 @pthread_attr_setinheritsched(i32* %38, i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
br label %45
44: ; preds = %37
store i32 0, i32* %5
br label %45
45: ; preds = %44, %42, %35, %26, %18
%46 = load i32, i32* %5
ret i32 %46
}

LLVM IR (-O2):
%struct.sched_param = type { i32 }
@.str = [28 x i8] c"pthread_attr_setschedpolicy\00"
@.str.1 = [27 x i8] c"pthread_attr_setschedparam\00"
@.str.2 = [28 x i8] c"pthread_attr_setaffinity_np\00"
@PTHREAD_EXPLICIT_SCHED = global i32 0
@.str.3 = [29 x i8] c"pthread_attr_setinheritsched\00"
define i32 @set_thread_attr(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sched_param
%6 = alloca i32
%7 = bitcast %struct.sched_param* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = tail call i32 @pthread_attr_init(i32* %0)
%10 = tail call i64 @pthread_attr_setschedpolicy(i32* %0, i32 %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %4
%13 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %33
14: ; preds = %4
%15 = getelementptr inbounds %struct.sched_param, %struct.sched_param* %5, i64 0, i32 0
store i32 %2, i32* %15
%16 = call i64 @pthread_attr_setschedparam(i32* %0, %struct.sched_param* %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %33
20: ; preds = %14
%21 = call i32 @CPU_ZERO(i32* %6)
%22 = call i32 @CPU_SET(i32 %3, i32* %6)
%23 = call i64 @pthread_attr_setaffinity_np(i32* %0, i32 4, i32* %6)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %33
27: ; preds = %20
%28 = load i32, i32* @PTHREAD_EXPLICIT_SCHED
%29 = call i64 @pthread_attr_setinheritsched(i32* %0, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %27, %31, %25, %18, %12
%34 = phi i32 [ -1, %12 ], [ -1, %18 ], [ -1, %25 ], [ -1, %31 ], [ 0, %27 ]
ret i32 %34
}