#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*/ cpu_set_t;

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int gettid();
scalar_t__ sched_setaffinity(int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void set_cpu_affinity(int cpu) {
  cpu_set_t coremask;
  CPU_ZERO(&coremask);
  CPU_SET(cpu, &coremask);
  if (sched_setaffinity(gettid(), sizeof(cpu_set_t), &coremask) != 0) {
    fprintf(stderr, "Unable to set thread %d affinity. %s\n", gettid(),
            strerror(errno));
  }
}


x86 assembly (clang, -O0):
<set_cpu_affinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1080 <CPU_SET@plt>
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,%edi
mov    $0x4,%esi
lea    -0x8(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
cmp    $0x0,%rax
je     11da <set_cpu_affinity+0x7a>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%edx
mov    %rax,%rcx
lea    0xe2b(%rip),%rsi
call   1050 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cpu_affinity>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1060 <CPU_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <CPU_SET@plt>
xor    %eax,%eax
call   1030 <gettid@plt>
mov    %eax,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1040 <sched_setaffinity@plt>
test   %rax,%rax
je     11cf <set_cpu_affinity+0x6f>
mov    0x2e3d(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <gettid@plt>
mov    %eax,%ebp
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe3d(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1050 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"Unable to set thread %d affinity. %s\0A\00"
@errno = global i32 0
define void @set_cpu_affinity(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = call i32 @CPU_ZERO(i32* %3)
%5 = load i32, i32* %2
%6 = call i32 @CPU_SET(i32 %5, i32* %3)
%7 = call i32 (...) @gettid()
%8 = call i64 @sched_setaffinity(i32 %7, i32 4, i32* %3)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 (...) @gettid()
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %14)
br label %16
16: ; preds = %10, %1
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"Unable to set thread %d affinity. %s\0A\00"
@errno = global i32 0
define void @set_cpu_affinity(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 (...) @gettid()
%7 = call i64 @sched_setaffinity(i32 %6, i32 4, i32* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 (...) @gettid()
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %11, i8* %13)
br label %15
15: ; preds = %9, %1
ret void
}