#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_ALLOC(int);
int /*<<< orphan*/ CPU_FREE(int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_SET_S(int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO_S(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ _SC_NPROCESSORS_ONLN;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
int AffinitizeThreadToCore(int core) {
  cpu_set_t *cmask;
  int n, ret;
  n = sysconf(_SC_NPROCESSORS_ONLN);
  if (core < 0 || core >= n) {
    fprintf(stderr, "%d: invalid CPU number.\n", core);
    return -1;
  }
  cmask = CPU_ALLOC(n);
  if (cmask == NULL) {
    fprintf(stderr, "%d: uexpected cmask.\n", n);
    return -1;
  }
  CPU_ZERO_S(n, cmask);
  CPU_SET_S(core, n, cmask);
  ret = sched_setaffinity(0, n, cmask);
  CPU_FREE(cmask);
  return ret;
}


x86 assembly (clang, -O0):
<AffinitizeThreadToCore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11a2 <AffinitizeThreadToCore+0x32>
mov    -0x8(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     11c6 <AffinitizeThreadToCore+0x56>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <AffinitizeThreadToCore+0xcc>
mov    -0x14(%rbp),%edi
call   1030 <CPU_ALLOC@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1201 <AffinitizeThreadToCore+0x91>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe29(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <AffinitizeThreadToCore+0xcc>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1090 <CPU_ZERO_S@plt>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1080 <CPU_SET_S@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
call   1040 <sched_setaffinity@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <CPU_FREE@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AffinitizeThreadToCore>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
test   %ebp,%ebp
js     11cd <AffinitizeThreadToCore+0x5d>
mov    %eax,%ebx
cmp    %ebp,%eax
jle    11cd <AffinitizeThreadToCore+0x5d>
mov    %ebx,%edi
call   1030 <CPU_ALLOC@plt>
test   %rax,%rax
je     11e1 <AffinitizeThreadToCore+0x71>
mov    %rax,%r14
mov    %ebx,%edi
mov    %rax,%rsi
call   1090 <CPU_ZERO_S@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r14,%rdx
call   1080 <CPU_SET_S@plt>
xor    %edi,%edi
mov    %ebx,%esi
mov    %r14,%rdx
call   1040 <sched_setaffinity@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1060 <CPU_FREE@plt>
mov    %ebx,%eax
jmp    11fd <AffinitizeThreadToCore+0x8d>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    %ebp,%edx
jmp    11f3 <AffinitizeThreadToCore+0x83>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
mov    %ebx,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@_SC_NPROCESSORS_ONLN = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"%d: invalid CPU number.\0A\00"
@.str.1 = [22 x i8] c"%d: uexpected cmask.\0A\00"
define i32 @AffinitizeThreadToCore(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* @_SC_NPROCESSORS_ONLN
%8 = call i32 @sysconf(i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = load i32, i32* %3
%13 = load i32, i32* %5
%14 = icmp sge i32 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %11, %1
%16 = load i32, i32* @stderr
%17 = load i32, i32* %3
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 -1, i32* %2
br label %42
19: ; preds = %11
%20 = load i32, i32* %5
%21 = call i32* @CPU_ALLOC(i32 %20)
store i32* %21, i32** %4
%22 = load i32*, i32** %4
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i32, i32* %5
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 -1, i32* %2
br label %42
28: ; preds = %19
%29 = load i32, i32* %5
%30 = load i32*, i32** %4
%31 = call i32 @CPU_ZERO_S(i32 %29, i32* %30)
%32 = load i32, i32* %3
%33 = load i32, i32* %5
%34 = load i32*, i32** %4
%35 = call i32 @CPU_SET_S(i32 %32, i32 %33, i32* %34)
%36 = load i32, i32* %5
%37 = load i32*, i32** %4
%38 = call i32 @sched_setaffinity(i32 0, i32 %36, i32* %37)
store i32 %38, i32* %6
%39 = load i32*, i32** %4
%40 = call i32 @CPU_FREE(i32* %39)
%41 = load i32, i32* %6
store i32 %41, i32* %2
br label %42
42: ; preds = %28, %24, %15
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@_SC_NPROCESSORS_ONLN = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"%d: invalid CPU number.\0A\00"
@.str.1 = [22 x i8] c"%d: uexpected cmask.\0A\00"
define i32 @AffinitizeThreadToCore(i32 %0) {
%2 = load i32, i32* @_SC_NPROCESSORS_ONLN
%3 = tail call i32 @sysconf(i32 %2)
%4 = icmp sgt i32 %0, -1
%5 = icmp sgt i32 %3, %0
%6 = select i1 %4, i1 %5, i1 false
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %21
10: ; preds = %1
%11 = tail call i32* @CPU_ALLOC(i32 %3)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %3)
br label %21
16: ; preds = %10
%17 = tail call i32 @CPU_ZERO_S(i32 %3, i32* %11)
%18 = tail call i32 @CPU_SET_S(i32 %0, i32 %3, i32* %11)
%19 = tail call i32 @sched_setaffinity(i32 0, i32 %3, i32* %11)
%20 = tail call i32 @CPU_FREE(i32* %11)
br label %21
21: ; preds = %16, %13, %7
%22 = phi i32 [ -1, %7 ], [ -1, %13 ], [ %19, %16 ]
ret i32 %22
}