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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned long);
int /*<<< orphan*/ printf(char *, unsigned long, unsigned long);
int sched_setaffinity(unsigned long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void set_affinity(unsigned long tid, unsigned long core_id) {
  cpu_set_t mask;
  CPU_ZERO(&mask);
  CPU_SET(core_id, &mask);
  int r = sched_setaffinity(tid, sizeof(mask), &mask);
  if (r < 0) {
    fprintf(stderr, "couldn't set affinity for %lu\n", core_id);
    exit(1);
  }
  printf("Setting affinity of thread %lu on core %lu\n", tid, core_id);
}


x86 assembly (clang, -O0):
<set_affinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1080 <CPU_SET@plt>
mov    -0x8(%rbp),%rdi
mov    $0x4,%esi
lea    -0x14(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11c8 <set_affinity+0x68>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe47(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe48(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_affinity>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   1060 <CPU_ZERO@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <CPU_SET@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
call   1040 <sched_setaffinity@plt>
test   %eax,%eax
js     11b7 <set_affinity+0x57>
lea    0xe7d(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"couldn't set affinity for %lu\0A\00"
@.str.1 = [44 x i8] c"Setting affinity of thread %lu on core %lu\0A\00"
define void @set_affinity(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i64 %0, i64* %3
store i64 %1, i64* %4
%7 = call i32 @CPU_ZERO(i32* %5)
%8 = load i64, i64* %4
%9 = call i32 @CPU_SET(i64 %8, i32* %5)
%10 = load i64, i64* %3
%11 = call i32 @sched_setaffinity(i64 %10, i32 4, i32* %5)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i64, i64* %4
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i64, i64* %3
%21 = load i64, i64* %4
%22 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %20, i64 %21)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"couldn't set affinity for %lu\0A\00"
@.str.1 = [44 x i8] c"Setting affinity of thread %lu on core %lu\0A\00"
define void @set_affinity(i64 %0, i64 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @CPU_ZERO(i32* %3)
%6 = call i32 @CPU_SET(i64 %1, i32* %3)
%7 = call i32 @sched_setaffinity(i64 %0, i32 4, i32* %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %1)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %0, i64 %1)
ret void
}