#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int pthread_getaffinity_np(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_self();
int /*<<< orphan*/ pthread_setaffinity_np(int /*<<< orphan*/, int,
                                          int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void pin_thread(int where) {
  int j;
  cpu_set_t cpuset;
  pthread_t thread;
  thread = pthread_self();
  CPU_ZERO(&cpuset);
  CPU_SET(where, &cpuset);
  pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
  int s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
  if (s != 0) {
    fprintf(stderr, "error: pthread_getaffinity_np");
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<pin_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,-0x10(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <CPU_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1090 <CPU_SET@plt>
mov    -0x10(%rbp),%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1030 <pthread_setaffinity_np@plt>
mov    -0x10(%rbp),%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1080 <pthread_getaffinity_np@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11e8 <pin_thread+0x78>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pin_thread>:
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   1090 <CPU_SET@plt>
mov    %ebp,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1030 <pthread_setaffinity_np@plt>
mov    %ebp,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1080 <pthread_getaffinity_np@plt>
test   %eax,%eax
jne    11c7 <pin_thread+0x57>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"error: pthread_getaffinity_np\00"
define void @pin_thread(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = call i32 (...) @pthread_self()
store i32 %7, i32* %5
%8 = call i32 @CPU_ZERO(i32* %4)
%9 = load i32, i32* %2
%10 = call i32 @CPU_SET(i32 %9, i32* %4)
%11 = load i32, i32* %5
%12 = call i32 @pthread_setaffinity_np(i32 %11, i32 4, i32* %4)
%13 = load i32, i32* %5
%14 = call i32 @pthread_getaffinity_np(i32 %13, i32 4, i32* %4)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %1
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %1
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"error: pthread_getaffinity_np\00"
define void @pin_thread(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 = call i32 @pthread_getaffinity_np(i32 %4, i32 4, i32* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %1
ret void
}