#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_SET(int, int /*<<< orphan*/ *);
struct bitmask *numa_allocate_cpumask();
scalar_t__ numa_bitmask_isbitset(struct bitmask *, int);
int numa_bitmask_nbytes(struct bitmask *);
int /*<<< orphan*/ numa_free_cpumask(struct bitmask *);
int numa_node_to_cpus(int, struct bitmask *);
int get_node_cpu(cpu_set_t *set, int node, int cpu_index) {
  struct bitmask *cpumask = numa_allocate_cpumask();
  int ncpu = numa_bitmask_nbytes(cpumask) * 8;
  int ret;
  ret = numa_node_to_cpus(node, cpumask);
  if (ret < 0)
    return ret;
  for (int cpuid = 0; cpuid < ncpu; ++cpuid) {
    if (numa_bitmask_isbitset(cpumask, cpuid)) {
      if (cpu_index == 0) {
        CPU_SET(cpuid, set);
      }
      cpu_index--;
    }
  }
  numa_free_cpumask(cpumask);
}


x86 assembly (clang, -O0):
<get_node_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    $0x0,%al
call   1080 <numa_allocate_cpumask@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <numa_bitmask_nbytes@plt>
shl    $0x3,%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1030 <numa_node_to_cpus@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11b0 <get_node_cpu+0x50>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <get_node_cpu+0xb4>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    120b <get_node_cpu+0xab>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <numa_bitmask_isbitset@plt>
cmp    $0x0,%rax
je     11f8 <get_node_cpu+0x98>
cmpl   $0x0,-0x18(%rbp)
jne    11ef <get_node_cpu+0x8f>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1070 <CPU_SET@plt>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    11fd <get_node_cpu+0x9d>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b7 <get_node_cpu+0x57>
mov    -0x20(%rbp),%rdi
call   1060 <numa_free_cpumask@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_node_cpu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r12d
mov    %rdi,%r14
xor    %eax,%eax
call   1080 <numa_allocate_cpumask@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <numa_bitmask_nbytes@plt>
mov    %eax,%ebp
mov    %r12d,%edi
mov    %r15,%rsi
call   1030 <numa_node_to_cpus@plt>
mov    %eax,%r12d
test   %eax,%eax
js     11f2 <get_node_cpu+0x92>
test   %ebp,%ebp
jle    11ea <get_node_cpu+0x8a>
shl    $0x3,%ebp
cmp    $0x2,%ebp
mov    $0x1,%r13d
cmovge %ebp,%r13d
xor    %ebp,%ebp
jmp    11cb <get_node_cpu+0x6b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
add    $0x1,%ebp
cmp    %ebp,%r13d
je     11ea <get_node_cpu+0x8a>
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <numa_bitmask_isbitset@plt>
test   %rax,%rax
je     11c3 <get_node_cpu+0x63>
test   %ebx,%ebx
jne    11c0 <get_node_cpu+0x60>
mov    %ebp,%edi
mov    %r14,%rsi
call   1070 <CPU_SET@plt>
jmp    11c0 <get_node_cpu+0x60>
mov    %r15,%rdi
call   1060 <numa_free_cpumask@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bitmask = type { i32 }
define i32 @get_node_cpu(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.bitmask*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = call %struct.bitmask* (...) @numa_allocate_cpumask()
store %struct.bitmask* %12, %struct.bitmask** %8
%13 = load %struct.bitmask*, %struct.bitmask** %8
%14 = call i32 @numa_bitmask_nbytes(%struct.bitmask* %13)
%15 = mul nsw i32 %14, 8
store i32 %15, i32* %9
%16 = load i32, i32* %6
%17 = load %struct.bitmask*, %struct.bitmask** %8
%18 = call i32 @numa_node_to_cpus(i32 %16, %struct.bitmask* %17)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = load i32, i32* %10
store i32 %22, i32* %4
br label %50
23: ; preds = %3
store i32 0, i32* %11
br label %24
24: ; preds = %44, %23
%25 = load i32, i32* %11
%26 = load i32, i32* %9
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %47
28: ; preds = %24
%29 = load %struct.bitmask*, %struct.bitmask** %8
%30 = load i32, i32* %11
%31 = call i64 @numa_bitmask_isbitset(%struct.bitmask* %29, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %43
33: ; preds = %28
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* %11
%38 = load i32*, i32** %5
%39 = call i32 @CPU_SET(i32 %37, i32* %38)
br label %40
40: ; preds = %36, %33
%41 = load i32, i32* %7
%42 = add nsw i32 %41, -1
store i32 %42, i32* %7
br label %43
43: ; preds = %40, %28
br label %44
44: ; preds = %43
%45 = load i32, i32* %11
%46 = add nsw i32 %45, 1
store i32 %46, i32* %11
br label %24
47: ; preds = %24
%48 = load %struct.bitmask*, %struct.bitmask** %8
%49 = call i32 @numa_free_cpumask(%struct.bitmask* %48)
br label %50
50: ; preds = %47, %21
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
%struct.bitmask = type { i32 }
define i32 @get_node_cpu(i32* %0, i32 %1, i32 %2) {
%4 = tail call %struct.bitmask* (...) @numa_allocate_cpumask()
%5 = tail call i32 @numa_bitmask_nbytes(%struct.bitmask* %4)
%6 = shl i32 %5, 3
%7 = tail call i32 @numa_node_to_cpus(i32 %1, %struct.bitmask* %4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %30, label %9
9: ; preds = %3
%10 = icmp sgt i32 %5, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = call i32 @llvm.smax.i32(i32 %6, i32 1)
br label %15
13: ; preds = %26, %9
%14 = tail call i32 @numa_free_cpumask(%struct.bitmask* %4)
br label %30
15: ; preds = %11, %26
%16 = phi i32 [ %28, %26 ], [ 0, %11 ]
%17 = phi i32 [ %27, %26 ], [ %2, %11 ]
%18 = tail call i64 @numa_bitmask_isbitset(%struct.bitmask* %4, i32 %16)
%19 = icmp eq i64 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %15
%21 = icmp eq i32 %17, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = tail call i32 @CPU_SET(i32 %16, i32* %0)
br label %24
24: ; preds = %22, %20
%25 = add nsw i32 %17, -1
br label %26
26: ; preds = %15, %24
%27 = phi i32 [ %25, %24 ], [ %17, %15 ]
%28 = add nuw nsw i32 %16, 1
%29 = icmp eq i32 %28, %12
br i1 %29, label %13, label %15
30: ; preds = %3, %13
ret i32 %7
}