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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ __NR_gettid;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ pthread_self();
int pthread_setaffinity_np(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ syscall(int /*<<< orphan*/);
void _set_affinity(int cpu) {
  cpu_set_t mask;
  pthread_t thread = pthread_self();
  CPU_ZERO(&mask);
  CPU_SET(cpu, &mask);
  if (pthread_setaffinity_np(thread, sizeof(mask), &mask) == -1) {
    fprintf(stderr,
            "Could not set affinity for thread %ld, will ignore to avoid "
            "deadlock\n",
            syscall(__NR_gettid));
  }
}


x86 assembly (clang, -O0):
<_set_affinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,-0xc(%rbp)
lea    -0x8(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1080 <CPU_SET@plt>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
lea    -0x8(%rbp),%rdx
call   1030 <pthread_setaffinity_np@plt>
cmp    $0xffffffff,%eax
jne    11cf <_set_affinity+0x6f>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1050 <syscall@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%edx
lea    0xe36(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_set_affinity>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
xor    %eax,%eax
call   1070 <pthread_self@plt>
mov    %eax,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <CPU_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1080 <CPU_SET@plt>
mov    %ebp,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1030 <pthread_setaffinity_np@plt>
cmp    $0xffffffff,%eax
jne    11c7 <_set_affinity+0x67>
mov    0x2e31(%rip),%rax
mov    (%rax),%ebx
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1050 <syscall@plt>
lea    0xe42(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [70 x i8] c"Could not set affinity for thread %ld, will ignore to avoid deadlock\0A\00"
@__NR_gettid = global i32 0
define void @_set_affinity(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* %4
%6 = call i32 @CPU_ZERO(i32* %3)
%7 = load i32, i32* %2
%8 = call i32 @CPU_SET(i32 %7, i32* %3)
%9 = load i32, i32* %4
%10 = call i32 @pthread_setaffinity_np(i32 %9, i32 4, i32* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* @__NR_gettid
%15 = call i32 @syscall(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %12, %1
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [70 x i8] c"Could not set affinity for thread %ld, will ignore to avoid deadlock\0A\00"
@__NR_gettid = global i32 0
define void @_set_affinity(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (...) @pthread_self()
%5 = call i32 @CPU_ZERO(i32* %2)
%6 = call i32 @CPU_SET(i32 %0, i32* %2)
%7 = call i32 @pthread_setaffinity_np(i32 %4, i32 4, i32* %2)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @__NR_gettid
%12 = call i32 @syscall(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %9, %1
ret void
}