#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, unsigned 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 [got %x]", 2048, cpu);
  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     117a <pin_cpu+0x4a>
mov    -0x8(%rbp),%ecx
mov    $0x1,%edi
lea    0xe90(%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    0x2e42(%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     116c <pin_cpu+0x3c>
lea    0xea5(%rip),%rsi
mov    $0x1,%edi
mov    $0x800,%edx
mov    %ebx,%ecx
call   1030 <errx@plt>
mov    $0x1,%eax
mov    %ebx,%ecx
shl    %cl,%rax
shr    $0x6,%ebx
mov    %rax,(%rsp,%rbx,8)
mov    0x2e64(%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 = [50 x i8] c"this program supports only up to %d CPUs [got %x]\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 %13
10: ; preds = %2
%11 = load i32, i32* %4
%12 = call i32 @errx(i32 1, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 2048, i32 %11)
br label %13
13: ; preds = %10, %2
%14 = load i32, i32* %4
%15 = and i32 %14, 63
%16 = zext i32 %15 to i64
%17 = shl i64 1, %16
%18 = load i32, i32* %4
%19 = lshr i32 %18, 6
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds [256 x i64], [256 x i64]* %5, i64 0, i64 %20
store i64 %17, i64* %21
%22 = load i32, i32* @__NR_sched_setaffinity
%23 = load i32, i32* %3
%24 = bitcast [256 x i64]* %5 to i64**
%25 = call i32 @syscall(i32 %22, i32 %23, i32 2048, i64** %24)
ret i32 %25
}

LLVM IR (-O2):
@.str = [50 x i8] c"this program supports only up to %d CPUs [got %x]\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 ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 2048, i32 %1)
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
}