#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_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 /*<<< orphan*/ usleep(int);
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);
  usleep(1000);
  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    12cd <bind_cpu+0x10d>
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    12cd <bind_cpu+0x10d>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <CPU_ZERO_S@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1090 <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    $0x3e8,%edi
call   10e0 <usleep@plt>
mov    $0x0,%al
call   10d0 <numa_max_node@plt>
cmp    $0x0,%rax
jne    1288 <bind_cpu+0xc8>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12cd <bind_cpu+0x10d>
mov    $0x10,%edi
call   10c0 <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   10a0 <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     126d <bind_cpu+0xad>
mov    %rax,%rbp
cmp    %r14d,%ebp
jle    126d <bind_cpu+0xad>
mov    %rbp,%rdi
call   1030 <CPU_ALLOC@plt>
test   %rax,%rax
je     127f <bind_cpu+0xbf>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
call   10b0 <CPU_ZERO_S@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1090 <CPU_SET_S@plt>
xor    %edi,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1060 <sched_setaffinity@plt>
mov    %eax,%ebp
mov    $0x3e8,%edi
call   10e0 <usleep@plt>
xor    %eax,%eax
call   10d0 <numa_max_node@plt>
test   %rax,%rax
je     1284 <bind_cpu+0xc4>
mov    $0x10,%edi
call   10c0 <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   10a0 <numa_bitmask_free@plt>
jmp    1284 <bind_cpu+0xc4>
mov    0x2d64(%rip),%rax
sub    (%rax),%ebx
mov    0x2d63(%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 %54
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 %54
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 = call i32 @usleep(i32 1000)
%37 = call i64 (...) @numa_max_node()
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %25
%40 = load i32, i32* %7
store i32 %40, i32* %2
br label %54
41: ; preds = %25
%42 = call %struct.bitmask* @numa_bitmask_alloc(i32 16)
store %struct.bitmask* %42, %struct.bitmask** %5
%43 = load %struct.bitmask*, %struct.bitmask** %5
%44 = call i32 @assert(%struct.bitmask* %43)
%45 = load %struct.bitmask*, %struct.bitmask** %5
%46 = load i32, i32* %3
%47 = srem i32 %46, 2
%48 = call i32 @numa_bitmask_setbit(%struct.bitmask* %45, i32 %47)
%49 = load %struct.bitmask*, %struct.bitmask** %5
%50 = call i32 @numa_set_membind(%struct.bitmask* %49)
%51 = load %struct.bitmask*, %struct.bitmask** %5
%52 = call i32 @numa_bitmask_free(%struct.bitmask* %51)
%53 = load i32, i32* %7
store i32 %53, i32* %2
br label %54
54: ; preds = %41, %39, %24, %16
%55 = load i32, i32* %2
ret i32 %55
}

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 @usleep(i32 1000)
%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
}