#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 */
typedef int /*<<< orphan*/ pthread_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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ get_cluster(int);
int /*<<< orphan*/ numa_set_preferred(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_self();
scalar_t__ pthread_setaffinity_np(int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void set_cpu(int cpu) {
  cpu_set_t mask;
  CPU_ZERO(&mask);
  CPU_SET(cpu, &mask);
  numa_set_preferred(get_cluster(cpu));
  pthread_t thread = pthread_self();
  if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &mask) != 0) {
    fprintf(stderr, "Error setting thread affinity\n");
  }
}


x86 assembly (clang, -O0):
<set_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1090 <CPU_SET@plt>
mov    -0x4(%rbp),%edi
call   1040 <get_cluster@plt>
mov    %eax,%edi
call   1030 <numa_set_preferred@plt>
mov    $0x0,%al
call   1080 <pthread_self@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
lea    -0x8(%rbp),%rdx
call   1050 <pthread_setaffinity_np@plt>
cmp    $0x0,%rax
je     11d9 <set_cpu+0x69>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
call   1060 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cpu>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1070 <CPU_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1090 <CPU_SET@plt>
mov    %ebx,%edi
call   1040 <get_cluster@plt>
mov    %eax,%edi
call   1030 <numa_set_preferred@plt>
xor    %eax,%eax
call   1080 <pthread_self@plt>
mov    %eax,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1050 <pthread_setaffinity_np@plt>
test   %rax,%rax
je     11cb <set_cpu+0x5b>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1060 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"Error setting thread affinity\0A\00"
define void @set_cpu(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 @CPU_ZERO(i32* %3)
%6 = load i32, i32* %2
%7 = call i32 @CPU_SET(i32 %6, i32* %3)
%8 = load i32, i32* %2
%9 = call i32 @get_cluster(i32 %8)
%10 = call i32 @numa_set_preferred(i32 %9)
%11 = call i32 (...) @pthread_self()
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = call i64 @pthread_setaffinity_np(i32 %12, i32 4, i32* %3)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %15, %1
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Error setting thread affinity\0A\00"
define void @set_cpu(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @CPU_ZERO(i32* %2)
%5 = call i32 @CPU_SET(i32 %0, i32* %2)
%6 = call i32 @get_cluster(i32 %0)
%7 = call i32 @numa_set_preferred(i32 %6)
%8 = call i32 (...) @pthread_self()
%9 = call i64 @pthread_setaffinity_np(i32 %8, i32 4, i32* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %1
ret void
}