#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*/ pid_t;
typedef int /*<<< orphan*/ my_mask;

/* Variables and functions */
int /*<<< orphan*/ __NR_sched_setaffinity;
int /*<<< orphan*/ errx(int, char *, int);
int /*<<< orphan*/ memset(unsigned long long *, int /*<<< orphan*/, int);
int syscall(int /*<<< orphan*/, int /*<<< orphan*/, int, unsigned long long **);
int pin_cpu(pid_t pid, unsigned int cpu) {
  unsigned long long my_mask[(2048 >> 3)];
  memset(my_mask, 0, sizeof(my_mask));
  if (cpu >= 2048)
    errx(1, "this program supports only up to %d CPUs", 2048);
  my_mask[cpu >> 6] = 1ULL << (cpu & 63);
  return syscall(__NR_sched_setaffinity, pid, sizeof(my_mask), &my_mask);
}


x86 assembly (clang, -O0):
<pin_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x810,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1040 <memset@plt>
cmpl   $0x800,-0x8(%rbp)
jb     1177 <pin_cpu+0x47>
mov    $0x1,%edi
lea    0xe93(%rip),%rsi
mov    $0x800,%edx
call   1030 <errx@plt>
mov    -0x8(%rbp),%eax
and    $0x3f,%eax
mov    %eax,%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
mov    %rax,%rcx
mov    -0x8(%rbp),%eax
shr    $0x6,%eax
mov    %eax,%eax
mov    %rcx,-0x810(%rbp,%rax,8)
ff
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
lea    -0x810(%rbp),%rcx
mov    $0x800,%edx
call   1050 <syscall@plt>
add    $0x810,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pin_cpu>:
push   %rbp
push   %rbx
sub    $0x808,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1040 <memset@plt>
cmp    $0x800,%ebx
jb     116a <pin_cpu+0x3a>
lea    0xea5(%rip),%rsi
mov    $0x1,%edi
mov    $0x800,%edx
call   1030 <errx@plt>
mov    $0x1,%eax
mov    %ebx,%ecx
shl    %cl,%rax
shr    $0x6,%ebx
mov    %rax,(%rsp,%rbx,8)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rcx
mov    %ebp,%esi
mov    $0x800,%edx
call   1050 <syscall@plt>
add    $0x808,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"this program supports only up to %d CPUs\00"
@__NR_sched_setaffinity = global i32 0
define i32 @pin_cpu(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i64]
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = getelementptr inbounds [256 x i64], [256 x i64]* %5, i64 0, i64 0
%7 = call i32 @memset(i64* %6, i32 0, i32 2048)
%8 = load i32, i32* %4
%9 = icmp uge i32 %8, 2048
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @errx(i32 1, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 2048)
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* %4
%14 = and i32 %13, 63
%15 = zext i32 %14 to i64
%16 = shl i64 1, %15
%17 = load i32, i32* %4
%18 = lshr i32 %17, 6
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [256 x i64], [256 x i64]* %5, i64 0, i64 %19
store i64 %16, i64* %20
%21 = load i32, i32* @__NR_sched_setaffinity
%22 = load i32, i32* %3
%23 = bitcast [256 x i64]* %5 to i64**
%24 = call i32 @syscall(i32 %21, i32 %22, i32 2048, i64** %23)
ret i32 %24
}

LLVM IR (-O2):
@.str = [41 x i8] c"this program supports only up to %d CPUs\00"
@__NR_sched_setaffinity = global i32 0
define i32 @pin_cpu(i32 %0, i32 %1) {
%3 = alloca [256 x i64]
%4 = bitcast [256 x i64]* %3 to i8*
%5 = getelementptr inbounds [256 x i64], [256 x i64]* %3, i64 0, i64 0
%6 = call i32 @memset(i64* %5, i32 0, i32 2048)
%7 = icmp ugt i32 %1, 2047
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @errx(i32 1, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 2048)
br label %10
10: ; preds = %8, %2
%11 = and i32 %1, 63
%12 = zext i32 %11 to i64
%13 = shl nuw i64 1, %12
%14 = lshr i32 %1, 6
%15 = zext i32 %14 to i64
%16 = getelementptr inbounds [256 x i64], [256 x i64]* %3, i64 0, i64 %15
store i64 %13, i64* %16
%17 = load i32, i32* @__NR_sched_setaffinity
%18 = bitcast [256 x i64]* %3 to i64**
%19 = call i32 @syscall(i32 %17, i32 %0, i32 2048, i64** %18)
ret i32 %19
}