#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*/ printf(char *, int);
int /*<<< orphan*/ pthread_self();
scalar_t__ pthread_setaffinity_np(int /*<<< orphan*/, int,
                                  int /*<<< orphan*/ *);
void bind_core(int core) {
  pthread_t tid = pthread_self();
  cpu_set_t cpuset;
  CPU_ZERO(&cpuset);
  CPU_SET(core, &cpuset);
  if (pthread_setaffinity_np(tid, sizeof(cpu_set_t), &cpuset) != 0)
    printf("Failed to bind to core %d\n", core);
}


x86 assembly (clang, -O0):
<bind_core>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <CPU_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1070 <CPU_SET@plt>
mov    -0x8(%rbp),%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1030 <pthread_setaffinity_np@plt>
cmp    $0x0,%rax
je     11a4 <bind_core+0x54>
mov    -0x4(%rbp),%esi
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_core>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
xor    %eax,%eax
call   1060 <pthread_self@plt>
mov    %eax,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <CPU_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1070 <CPU_SET@plt>
mov    %ebp,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1030 <pthread_setaffinity_np@plt>
test   %rax,%rax
je     119f <bind_core+0x4f>
lea    0xe69(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Failed to bind to core %d\0A\00"
define void @bind_core(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 (...) @pthread_self()
store i32 %5, i32* %3
%6 = call i32 @CPU_ZERO(i32* %4)
%7 = load i32, i32* %2
%8 = call i32 @CPU_SET(i32 %7, i32* %4)
%9 = load i32, i32* %3
%10 = call i64 @pthread_setaffinity_np(i32 %9, i32 4, i32* %4)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* %2
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %1
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"Failed to bind to core %d\0A\00"
define void @bind_core(i32 %0) {
%2 = alloca i32
%3 = tail call i32 (...) @pthread_self()
%4 = bitcast i32* %2 to i8*
%5 = call i32 @CPU_ZERO(i32* %2)
%6 = call i32 @CPU_SET(i32 %0, i32* %2)
%7 = call i64 @pthread_setaffinity_np(i32 %3, i32 4, i32* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %11
11: ; preds = %9, %1
ret void
}