#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 /*<<< orphan*/);
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, 0);
  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    12c1 <bind_cpu+0x101>
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    12c1 <bind_cpu+0x101>
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    12c1 <bind_cpu+0x101>
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
xor    %esi,%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   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <get_num_cpus@plt>
test   %r15d,%r15d
js     1269 <bind_cpu+0xa9>
mov    %rax,%rbx
cmp    %r15d,%ebx
jle    1269 <bind_cpu+0xa9>
mov    %rbx,%rdi
call   1030 <CPU_ALLOC@plt>
test   %rax,%rax
je     127b <bind_cpu+0xbb>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %rax,%rsi
call   10c0 <CPU_ZERO_S@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10a0 <CPU_SET_S@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <sched_setaffinity@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1090 <CPU_FREE@plt>
xor    %eax,%eax
call   10e0 <numa_max_node@plt>
test   %rax,%rax
je     1280 <bind_cpu+0xc0>
mov    $0x10,%edi
call   10d0 <numa_bitmask_alloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1070 <assert@plt>
mov    %rbp,%rdi
xor    %esi,%esi
call   1040 <numa_bitmask_setbit@plt>
mov    %rbp,%rdi
call   1050 <numa_set_membind@plt>
mov    %rbp,%rdi
call   10b0 <numa_bitmask_free@plt>
jmp    1280 <bind_cpu+0xc0>
mov    0x2d68(%rip),%rax
sub    (%rax),%ebp
mov    0x2d67(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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 %53
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 %53
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 %53
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 = call i32 @numa_bitmask_setbit(%struct.bitmask* %46, i32 0)
%48 = load %struct.bitmask*, %struct.bitmask** %5
%49 = call i32 @numa_set_membind(%struct.bitmask* %48)
%50 = load %struct.bitmask*, %struct.bitmask** %5
%51 = call i32 @numa_bitmask_free(%struct.bitmask* %50)
%52 = load i32, i32* %7
store i32 %52, i32* %2
br label %53
53: ; preds = %42, %40, %24, %16
%54 = load i32, i32* %2
ret i32 %54
}

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 %26
10: ; preds = %1
%11 = tail call i32* @CPU_ALLOC(i64 %2)
%12 = icmp eq i32* %11, null
br i1 %12, label %26, 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 %26, label %20
20: ; preds = %13
%21 = tail call %struct.bitmask* @numa_bitmask_alloc(i32 16)
%22 = tail call i32 @assert(%struct.bitmask* %21)
%23 = tail call i32 @numa_bitmask_setbit(%struct.bitmask* %21, i32 0)
%24 = tail call i32 @numa_set_membind(%struct.bitmask* %21)
%25 = tail call i32 @numa_bitmask_free(%struct.bitmask* %21)
br label %26
26: ; preds = %13, %10, %20, %7
%27 = phi i32 [ -1, %7 ], [ %16, %20 ], [ -1, %10 ], [ %16, %13 ]
ret i32 %27
}