#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*/ cpu_set_t;

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
size_t get_cpu_num();
int sched_setaffinity(int /*<<< orphan*/, size_t, int /*<<< orphan*/ *);
int bind_process_cpu(int cpu) {
  cpu_set_t cmask;
  size_t n;
  int ret;
  n = get_cpu_num();
  if (cpu < 0 || cpu >= (int)n) {
    errno = EINVAL;
    return -1;
  }
  CPU_ZERO(&cmask);
  CPU_SET(cpu, &cmask);
  ret = sched_setaffinity(0, n, &cmask);
  CPU_ZERO(&cmask);
  return ret;
}


x86 assembly (clang, -O0):
<bind_process_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <get_cpu_num@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     116f <bind_process_cpu+0x2f>
mov    -0x8(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jl     118d <bind_process_cpu+0x4d>
mov    0x2e62(%rip),%rax
mov    (%rax),%ecx
mov    0x2e61(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <bind_process_cpu+0x83>
lea    -0xc(%rbp),%rdi
call   1040 <CPU_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1060 <CPU_SET@plt>
mov    -0x18(%rbp),%rsi
xor    %edi,%edi
lea    -0xc(%rbp),%rdx
call   1030 <sched_setaffinity@plt>
mov    %eax,-0x1c(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <CPU_ZERO@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_process_cpu>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
xor    %eax,%eax
call   1050 <get_cpu_num@plt>
test   %ebx,%ebx
js     118e <bind_process_cpu+0x4e>
mov    %rax,%r14
cmp    %ebx,%r14d
jle    118e <bind_process_cpu+0x4e>
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   1040 <CPU_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1060 <CPU_SET@plt>
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <sched_setaffinity@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <CPU_ZERO@plt>
jmp    11a5 <bind_process_cpu+0x65>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    0x2e42(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @bind_process_cpu(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %3
%7 = call i64 (...) @get_cpu_num()
store i64 %7, i64* %5
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load i32, i32* %3
%12 = load i64, i64* %5
%13 = trunc i64 %12 to i32
%14 = icmp sge i32 %11, %13
br i1 %14, label %15, label %17
15: ; preds = %10, %1
%16 = load i32, i32* @EINVAL
store i32 %16, i32* @errno
store i32 -1, i32* %2
br label %25
17: ; preds = %10
%18 = call i32 @CPU_ZERO(i32* %4)
%19 = load i32, i32* %3
%20 = call i32 @CPU_SET(i32 %19, i32* %4)
%21 = load i64, i64* %5
%22 = call i32 @sched_setaffinity(i32 0, i64 %21, i32* %4)
store i32 %22, i32* %6
%23 = call i32 @CPU_ZERO(i32* %4)
%24 = load i32, i32* %6
store i32 %24, i32* %2
br label %25
25: ; preds = %17, %15
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @bind_process_cpu(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i64 (...) @get_cpu_num()
%5 = icmp sgt i32 %0, -1
%6 = trunc i64 %4 to i32
%7 = icmp sgt i32 %6, %0
%8 = select i1 %5, i1 %7, i1 false
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
br label %16
11: ; preds = %1
%12 = call i32 @CPU_ZERO(i32* %2)
%13 = call i32 @CPU_SET(i32 %0, i32* %2)
%14 = call i32 @sched_setaffinity(i32 0, i64 %4, i32* %2)
%15 = call i32 @CPU_ZERO(i32* %2)
br label %16
16: ; preds = %11, %9
%17 = phi i32 [ -1, %9 ], [ %14, %11 ]
ret i32 %17
}