#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ cpu_set_t;

/* Variables and functions */
int /*<<< orphan*/ *CPU_ALLOC(int);
size_t CPU_ALLOC_SIZE(int);
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*/ *);
scalar_t__ gettid();
int num_online_cpus();
int sched_setaffinity(scalar_t__, size_t, int /*<<< orphan*/ *);
int be_migrate_thread_to_cpu(pid_t tid, int target_cpu) {
  cpu_set_t *cpu_set;
  size_t sz;
  int num_cpus;
  int ret;
  if (target_cpu < 0)
    return -1;
  num_cpus = num_online_cpus();
  if (num_cpus == -1)
    return -1;
  if (target_cpu >= num_cpus)
    return -1;
  cpu_set = CPU_ALLOC(num_cpus);
  sz = CPU_ALLOC_SIZE(num_cpus);
  CPU_ZERO_S(sz, cpu_set);
  CPU_SET_S(target_cpu, sz, cpu_set);
  if (tid == 0)
    tid = gettid();
  ret = sched_setaffinity(tid, sz, cpu_set);
  CPU_FREE(cpu_set);
  return ret;
}


x86 assembly (clang, -O0):
<be_migrate_thread_to_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a5 <be_migrate_thread_to_cpu+0x25>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <be_migrate_thread_to_cpu+0xcb>
mov    $0x0,%al
call   1060 <num_online_cpus@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11c5 <be_migrate_thread_to_cpu+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <be_migrate_thread_to_cpu+0xcb>
mov    -0x14(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jl     11dd <be_migrate_thread_to_cpu+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <be_migrate_thread_to_cpu+0xcb>
mov    -0x2c(%rbp),%edi
call   1030 <CPU_ALLOC@plt>
mov    %rax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <CPU_ALLOC_SIZE@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10a0 <CPU_ZERO_S@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1090 <CPU_SET_S@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1228 <be_migrate_thread_to_cpu+0xa8>
mov    $0x0,%al
call   1040 <gettid@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <sched_setaffinity@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <CPU_FREE@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<be_migrate_thread_to_cpu>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %esi,%esi
js     11fa <be_migrate_thread_to_cpu+0x7a>
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   1060 <num_online_cpus@plt>
cmp    $0xffffffff,%eax
je     11fa <be_migrate_thread_to_cpu+0x7a>
mov    %eax,%ebx
cmp    %ebp,%eax
jle    11fa <be_migrate_thread_to_cpu+0x7a>
mov    %ebx,%edi
call   1030 <CPU_ALLOC@plt>
mov    %rax,%r12
mov    %ebx,%edi
call   1050 <CPU_ALLOC_SIZE@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12,%rsi
call   10a0 <CPU_ZERO_S@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1090 <CPU_SET_S@plt>
test   %r14,%r14
jne    11de <be_migrate_thread_to_cpu+0x5e>
xor    %eax,%eax
call   1040 <gettid@plt>
mov    %rax,%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1070 <sched_setaffinity@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1080 <CPU_FREE@plt>
mov    %ebx,%eax
jmp    11ff <be_migrate_thread_to_cpu+0x7f>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @be_migrate_thread_to_cpu(i64 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i64 %0, i64* %4
store i32 %1, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %47
13: ; preds = %2
%14 = call i32 (...) @num_online_cpus()
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 -1, i32* %3
br label %47
18: ; preds = %13
%19 = load i32, i32* %5
%20 = load i32, i32* %8
%21 = icmp sge i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %3
br label %47
23: ; preds = %18
%24 = load i32, i32* %8
%25 = call i32* @CPU_ALLOC(i32 %24)
store i32* %25, i32** %6
%26 = load i32, i32* %8
%27 = call i64 @CPU_ALLOC_SIZE(i32 %26)
store i64 %27, i64* %7
%28 = load i64, i64* %7
%29 = load i32*, i32** %6
%30 = call i32 @CPU_ZERO_S(i64 %28, i32* %29)
%31 = load i32, i32* %5
%32 = load i64, i64* %7
%33 = load i32*, i32** %6
%34 = call i32 @CPU_SET_S(i32 %31, i64 %32, i32* %33)
%35 = load i64, i64* %4
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %23
%38 = call i64 (...) @gettid()
store i64 %38, i64* %4
br label %39
39: ; preds = %37, %23
%40 = load i64, i64* %4
%41 = load i64, i64* %7
%42 = load i32*, i32** %6
%43 = call i32 @sched_setaffinity(i64 %40, i64 %41, i32* %42)
store i32 %43, i32* %9
%44 = load i32*, i32** %6
%45 = call i32 @CPU_FREE(i32* %44)
%46 = load i32, i32* %9
store i32 %46, i32* %3
br label %47
47: ; preds = %39, %22, %17, %12
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
define i32 @be_migrate_thread_to_cpu(i64 %0, i32 %1) {
%3 = icmp slt i32 %1, 0
br i1 %3, label %21, label %4
4: ; preds = %2
%5 = tail call i32 (...) @num_online_cpus()
%6 = icmp ne i32 %5, -1
%7 = icmp sgt i32 %5, %1
%8 = and i1 %6, %7
br i1 %8, label %9, label %21
9: ; preds = %4
%10 = tail call i32* @CPU_ALLOC(i32 %5)
%11 = tail call i64 @CPU_ALLOC_SIZE(i32 %5)
%12 = tail call i32 @CPU_ZERO_S(i64 %11, i32* %10)
%13 = tail call i32 @CPU_SET_S(i32 %1, i64 %11, i32* %10)
%14 = icmp eq i64 %0, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i64 (...) @gettid()
br label %17
17: ; preds = %15, %9
%18 = phi i64 [ %16, %15 ], [ %0, %9 ]
%19 = tail call i32 @sched_setaffinity(i64 %18, i64 %11, i32* %10)
%20 = tail call i32 @CPU_FREE(i32* %10)
br label %21
21: ; preds = %4, %2, %17
%22 = phi i32 [ %19, %17 ], [ -1, %2 ], [ -1, %4 ]
ret i32 %22
}