#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;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int get_nprocs();
struct bitmask *numa_allocate_cpumask();
scalar_t__ numa_bitmask_isbitset(struct bitmask *, int);
int /*<<< orphan*/ numa_node_to_cpus(unsigned long, struct bitmask *);
int /*<<< orphan*/ printf(char *);
unsigned long get_first_core_of_node(unsigned long node) {
  struct bitmask *bm;
  bm = numa_allocate_cpumask();
  numa_node_to_cpus(node, bm);
  for (int i = 0; i < get_nprocs(); i++) {
    if (numa_bitmask_isbitset(bm, i)) {
      return i;
    }
  }
  printf("Oops\n");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<get_first_core_of_node>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <numa_allocate_cpumask@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <numa_node_to_cpus@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <get_nprocs@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jge    11d8 <get_first_core_of_node+0x78>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <numa_bitmask_isbitset@plt>
cmp    $0x0,%rax
je     11c5 <get_first_core_of_node+0x65>
movslq -0x14(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
jmp    11ca <get_first_core_of_node+0x6a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    118b <get_first_core_of_node+0x2b>
lea    0xe21(%rip),%rdi
call   1030 <printf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<get_first_core_of_node>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <numa_allocate_cpumask@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <numa_node_to_cpus@plt>
xor    %eax,%eax
call   1080 <get_nprocs@plt>
test   %eax,%eax
jle    11ae <get_first_core_of_node+0x4e>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <numa_bitmask_isbitset@plt>
test   %rax,%rax
jne    11c8 <get_first_core_of_node+0x68>
xor    %eax,%eax
call   1080 <get_nprocs@plt>
add    $0x1,%rbx
cmp    %eax,%ebx
jl     1190 <get_first_core_of_node+0x30>
lea    0xe4b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.bitmask = type { i32 }
@.str = [6 x i8] c"Oops\0A\00"
@EXIT_FAILURE = global i32 0
define i64 @get_first_core_of_node(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.bitmask*
%4 = alloca i32
store i64 %0, i64* %2
%5 = call %struct.bitmask* (...) @numa_allocate_cpumask()
store %struct.bitmask* %5, %struct.bitmask** %3
%6 = load i64, i64* %2
%7 = load %struct.bitmask*, %struct.bitmask** %3
%8 = call i32 @numa_node_to_cpus(i64 %6, %struct.bitmask* %7)
store i32 0, i32* %4
br label %9
9: ; preds = %22, %1
%10 = load i32, i32* %4
%11 = call i32 (...) @get_nprocs()
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %25
13: ; preds = %9
%14 = load %struct.bitmask*, %struct.bitmask** %3
%15 = load i32, i32* %4
%16 = call i64 @numa_bitmask_isbitset(%struct.bitmask* %14, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
ret i64 %20
21: ; preds = %13
br label %22
22: ; preds = %21
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %9
25: ; preds = %9
%26 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
}

LLVM IR (-O2):
%struct.bitmask = type { i32 }
@EXIT_FAILURE = global i32 0
@str = [5 x i8] c"Oops\00"
define i64 @get_first_core_of_node(i64 %0) {
%2 = tail call %struct.bitmask* (...) @numa_allocate_cpumask()
%3 = tail call i32 @numa_node_to_cpus(i64 %0, %struct.bitmask* %2)
%4 = tail call i32 (...) @get_nprocs()
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %1, %10
%7 = phi i32 [ %11, %10 ], [ 0, %1 ]
%8 = tail call i64 @numa_bitmask_isbitset(%struct.bitmask* %2, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %6
%11 = add nuw nsw i32 %7, 1
%12 = tail call i32 (...) @get_nprocs()
%13 = icmp slt i32 %11, %12
br i1 %13, label %6, label %14
14: ; preds = %10, %1
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %6
%19 = zext i32 %7 to i64
ret i64 %19
}