#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 */
struct bitmask {
  int dummy;
};
typedef int /*<<< orphan*/ cpu_set_t;

/* Variables and functions */
int /*<<< orphan*/ *CPU_ALLOC(size_t);
int /*<<< orphan*/ CPU_FREE(int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_SET_S(int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO_S(size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ assert(struct bitmask *);
int /*<<< orphan*/ errno;
size_t get_num_cpus();
struct bitmask *numa_bitmask_alloc(int);
int /*<<< orphan*/ numa_bitmask_free(struct bitmask *);
int /*<<< orphan*/ numa_bitmask_setbit(struct bitmask *, int);
scalar_t__ numa_max_node();
int /*<<< orphan*/ numa_set_membind(struct bitmask *);
int sched_setaffinity(int /*<<< orphan*/, size_t, int /*<<< orphan*/ *);
int bind_cpu(int cpu) {
  cpu_set_t *cmask;
  struct bitmask *bmask;
  size_t n;
  int ret;
  n = get_num_cpus();
  if (cpu < 0 || cpu >= (int)n) {
    errno = -EINVAL;
    return -1;
  }
  cmask = CPU_ALLOC(n);
  if (cmask == NULL)
    return -1;
  CPU_ZERO_S(n, cmask);
  CPU_SET_S(cpu, n, cmask);
  ret = sched_setaffinity(0, n, cmask);
  CPU_FREE(cmask);
  if (numa_max_node() == 0)
    return ret;
  bmask = numa_bitmask_alloc(16);
  assert(bmask);
  numa_bitmask_setbit(bmask, cpu % 2);
  numa_set_membind(bmask);
  numa_bitmask_free(bmask);
  return ret;
}


x86 assembly (clang, -O0):
<bind_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1080 <get_num_cpus@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11ef <bind_cpu+0x2f>
mov    -0x8(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    %ecx,%eax
jl     120f <bind_cpu+0x4f>
xor    %ecx,%ecx
mov    0x2de0(%rip),%rax
sub    (%rax),%ecx
mov    0x2ddf(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <bind_cpu+0x10c>
mov    -0x20(%rbp),%rdi
call   1030 <CPU_ALLOC@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1233 <bind_cpu+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <bind_cpu+0x10c>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <CPU_ZERO_S@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   10a0 <CPU_SET_S@plt>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
call   1060 <sched_setaffinity@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <CPU_FREE@plt>
mov    $0x0,%al
call   10e0 <numa_max_node@plt>
cmp    $0x0,%rax
jne    1287 <bind_cpu+0xc7>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12cc <bind_cpu+0x10c>
mov    $0x10,%edi
call   10d0 <numa_bitmask_alloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
call   1040 <numa_bitmask_setbit@plt>
mov    -0x18(%rbp),%rdi
call   1050 <numa_set_membind@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <numa_bitmask_free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_cpu>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <get_num_cpus@plt>
test   %r14d,%r14d
js     126b <bind_cpu+0xab>
mov    %rax,%rbp
cmp    %r14d,%ebp
jle    126b <bind_cpu+0xab>
mov    %rbp,%rdi
call   1030 <CPU_ALLOC@plt>
test   %rax,%rax
je     127d <bind_cpu+0xbd>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
call   10c0 <CPU_ZERO_S@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   10a0 <CPU_SET_S@plt>
xor    %edi,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1060 <sched_setaffinity@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1090 <CPU_FREE@plt>
xor    %eax,%eax
call   10e0 <numa_max_node@plt>
test   %rax,%rax
je     1282 <bind_cpu+0xc2>
mov    $0x10,%edi
call   10d0 <numa_bitmask_alloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <assert@plt>
and    $0x1,%r14d
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <numa_bitmask_setbit@plt>
mov    %rbx,%rdi
call   1050 <numa_set_membind@plt>
mov    %rbx,%rdi
call   10b0 <numa_bitmask_free@plt>
jmp    1282 <bind_cpu+0xc2>
mov    0x2d66(%rip),%rax
sub    (%rax),%ebx
mov    0x2d65(%rip),%rax
mov    %ebx,(%rax)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.bitmask = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @bind_cpu(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.bitmask*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
%8 = call i64 (...) @get_num_cpus()
store i64 %8, i64* %6
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %1
%12 = load i32, i32* %3
%13 = load i64, i64* %6
%14 = trunc i64 %13 to i32
%15 = icmp sge i32 %12, %14
br i1 %15, label %16, label %19
16: ; preds = %11, %1
%17 = load i32, i32* @EINVAL
%18 = sub nsw i32 0, %17
store i32 %18, i32* @errno
store i32 -1, i32* %2
br label %55
19: ; preds = %11
%20 = load i64, i64* %6
%21 = call i32* @CPU_ALLOC(i64 %20)
store i32* %21, i32** %4
%22 = load i32*, i32** %4
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %2
br label %55
25: ; preds = %19
%26 = load i64, i64* %6
%27 = load i32*, i32** %4
%28 = call i32 @CPU_ZERO_S(i64 %26, i32* %27)
%29 = load i32, i32* %3
%30 = load i64, i64* %6
%31 = load i32*, i32** %4
%32 = call i32 @CPU_SET_S(i32 %29, i64 %30, i32* %31)
%33 = load i64, i64* %6
%34 = load i32*, i32** %4
%35 = call i32 @sched_setaffinity(i32 0, i64 %33, i32* %34)
store i32 %35, i32* %7
%36 = load i32*, i32** %4
%37 = call i32 @CPU_FREE(i32* %36)
%38 = call i64 (...) @numa_max_node()
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %25
%41 = load i32, i32* %7
store i32 %41, i32* %2
br label %55
42: ; preds = %25
%43 = call %struct.bitmask* @numa_bitmask_alloc(i32 16)
store %struct.bitmask* %43, %struct.bitmask** %5
%44 = load %struct.bitmask*, %struct.bitmask** %5
%45 = call i32 @assert(%struct.bitmask* %44)
%46 = load %struct.bitmask*, %struct.bitmask** %5
%47 = load i32, i32* %3
%48 = srem i32 %47, 2
%49 = call i32 @numa_bitmask_setbit(%struct.bitmask* %46, i32 %48)
%50 = load %struct.bitmask*, %struct.bitmask** %5
%51 = call i32 @numa_set_membind(%struct.bitmask* %50)
%52 = load %struct.bitmask*, %struct.bitmask** %5
%53 = call i32 @numa_bitmask_free(%struct.bitmask* %52)
%54 = load i32, i32* %7
store i32 %54, i32* %2
br label %55
55: ; preds = %42, %40, %24, %16
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
%struct.bitmask = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @bind_cpu(i32 %0) {
%2 = tail call i64 (...) @get_num_cpus()
%3 = icmp sgt i32 %0, -1
%4 = trunc i64 %2 to i32
%5 = icmp sgt i32 %4, %0
%6 = select i1 %3, i1 %5, i1 false
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
store i32 %9, i32* @errno
br label %27
10: ; preds = %1
%11 = tail call i32* @CPU_ALLOC(i64 %2)
%12 = icmp eq i32* %11, null
br i1 %12, label %27, label %13
13: ; preds = %10
%14 = tail call i32 @CPU_ZERO_S(i64 %2, i32* %11)
%15 = tail call i32 @CPU_SET_S(i32 %0, i64 %2, i32* %11)
%16 = tail call i32 @sched_setaffinity(i32 0, i64 %2, i32* %11)
%17 = tail call i32 @CPU_FREE(i32* %11)
%18 = tail call i64 (...) @numa_max_node()
%19 = icmp eq i64 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %13
%21 = tail call %struct.bitmask* @numa_bitmask_alloc(i32 16)
%22 = tail call i32 @assert(%struct.bitmask* %21)
%23 = and i32 %0, 1
%24 = tail call i32 @numa_bitmask_setbit(%struct.bitmask* %21, i32 %23)
%25 = tail call i32 @numa_set_membind(%struct.bitmask* %21)
%26 = tail call i32 @numa_bitmask_free(%struct.bitmask* %21)
br label %27
27: ; preds = %13, %10, %20, %7
%28 = phi i32 [ -1, %7 ], [ %16, %20 ], [ -1, %10 ], [ %16, %13 ]
ret i32 %28
}