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

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int numa_num_configured_cpus();
int numa_num_configured_nodes();
int /*<<< orphan*/ perror(char *);
int sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int set_cpu_affinity(int thread_no) {
  cpu_set_t set;
  int cpu = 0, ret = 0;
  int nb_numa_nodes = numa_num_configured_nodes();
  int nb_cores = numa_num_configured_cpus();
  int nb_cores_per_nodes = nb_cores / nb_numa_nodes;
  cpu = (thread_no / nb_numa_nodes) +
        ((thread_no % nb_numa_nodes) * nb_cores_per_nodes);
  CPU_ZERO(&set);
  CPU_SET(cpu, &set);
  ret = sched_setaffinity(0, sizeof(set), &set);
  if (ret == -1) {
    perror("sched_setaffinity");
    return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_cpu_affinity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1030 <numa_num_configured_nodes@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <numa_num_configured_cpus@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    -0x24(%rbp),%eax
imul   -0x20(%rbp),%edx
add    %edx,%eax
mov    %eax,-0x10(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <CPU_ZERO@plt>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1080 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0xc(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fd <set_cpu_affinity+0x9d>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <set_cpu_affinity+0xa4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cpu_affinity>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <numa_num_configured_nodes@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <numa_num_configured_cpus@plt>
cltd
idiv   %ebx
mov    %eax,%ebp
mov    %r15d,%eax
cltd
idiv   %ebx
imul   %edx,%ebp
add    %eax,%ebp
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <CPU_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1040 <sched_setaffinity@plt>
cmp    $0xffffffff,%eax
je     11c6 <set_cpu_affinity+0x66>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11b8 <set_cpu_affinity+0x58>

LLVM IR (-O0):
@.str = [18 x i8] c"sched_setaffinity\00"
define i32 @set_cpu_affinity(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %5
store i32 0, i32* %6
%10 = call i32 (...) @numa_num_configured_nodes()
store i32 %10, i32* %7
%11 = call i32 (...) @numa_num_configured_cpus()
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = load i32, i32* %7
%14 = sdiv i32 %12, %13
store i32 %14, i32* %9
%15 = load i32, i32* %3
%16 = load i32, i32* %7
%17 = sdiv i32 %15, %16
%18 = load i32, i32* %3
%19 = load i32, i32* %7
%20 = srem i32 %18, %19
%21 = load i32, i32* %9
%22 = mul nsw i32 %20, %21
%23 = add nsw i32 %17, %22
store i32 %23, i32* %5
%24 = call i32 @CPU_ZERO(i32* %4)
%25 = load i32, i32* %5
%26 = call i32 @CPU_SET(i32 %25, i32* %4)
%27 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %4)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %1
%31 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* %6
store i32 %32, i32* %2
br label %34
33: ; preds = %1
store i32 0, i32* %2
br label %34
34: ; preds = %33, %30
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [18 x i8] c"sched_setaffinity\00"
define i32 @set_cpu_affinity(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (...) @numa_num_configured_nodes()
%5 = tail call i32 (...) @numa_num_configured_cpus()
%6 = sdiv i32 %5, %4
%7 = sdiv i32 %0, %4
%8 = srem i32 %0, %4
%9 = mul nsw i32 %8, %6
%10 = add nsw i32 %9, %7
%11 = call i32 @CPU_ZERO(i32* %2)
%12 = call i32 @CPU_SET(i32 %10, i32* %2)
%13 = call i32 @sched_setaffinity(i32 0, i32 4, i32* %2)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %1, %15
%18 = phi i32 [ -1, %15 ], [ 0, %1 ]
ret i32 %18
}