#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*/ EINVAL;
int /*<<< orphan*/ errno;
struct bitmask *numa_bitmask_alloc(int);
int /*<<< orphan*/ numa_bitmask_free(struct bitmask *);
scalar_t__ numa_bitmask_isbitset(struct bitmask *, int);
int numa_max_node();
int /*<<< orphan*/ numa_node_to_cpus_v2_int(int, struct bitmask *);
int numa_num_possible_cpus();
int numa_node_of_cpu(int cpu) {
  struct bitmask *bmp;
  int ncpus, nnodes, node, ret;
  ncpus = numa_num_possible_cpus();
  if (cpu > ncpus) {
    errno = EINVAL;
    return -1;
  }
  bmp = numa_bitmask_alloc(ncpus);
  nnodes = numa_max_node();
  for (node = 0; node <= nnodes; node++) {
    numa_node_to_cpus_v2_int(node, bmp);
    if (numa_bitmask_isbitset(bmp, cpu)) {
      ret = node;
      goto end;
    }
  }
  ret = -1;
  errno = EINVAL;
end:
  numa_bitmask_free(bmp);
  return ret;
}


x86 assembly (clang, -O0):
<numa_node_of_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <numa_num_possible_cpus@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    119f <numa_node_of_cpu+0x3f>
mov    0x2e50(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <numa_node_of_cpu+0xd0>
mov    -0x14(%rbp),%edi
call   1060 <numa_bitmask_alloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1080 <numa_max_node@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     1208 <numa_node_of_cpu+0xa8>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1070 <numa_node_to_cpus_v2_int@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <numa_bitmask_isbitset@plt>
cmp    $0x0,%rax
je     11f5 <numa_node_of_cpu+0x95>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1221 <numa_node_of_cpu+0xc1>
jmp    11fa <numa_node_of_cpu+0x9a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11bc <numa_node_of_cpu+0x5c>
movl   $0xffffffff,-0x20(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc1(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <numa_bitmask_free@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<numa_node_of_cpu>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
xor    %eax,%eax
call   1040 <numa_num_possible_cpus@plt>
cmp    %r14d,%eax
jge    118f <numa_node_of_cpu+0x2f>
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    0x2e5a(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
jmp    11f1 <numa_node_of_cpu+0x91>
mov    %eax,%edi
call   1060 <numa_bitmask_alloc@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <numa_max_node@plt>
test   %eax,%eax
js     11d2 <numa_node_of_cpu+0x72>
mov    %eax,%r15d
add    $0x1,%r15d
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <numa_node_to_cpus_v2_int@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <numa_bitmask_isbitset@plt>
test   %rax,%rax
jne    11e9 <numa_node_of_cpu+0x89>
add    $0x1,%ebp
cmp    %ebp,%r15d
jne    11b0 <numa_node_of_cpu+0x50>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    0x2dfe(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1050 <numa_bitmask_free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bitmask = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @numa_node_of_cpu(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.bitmask*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
%9 = call i32 (...) @numa_num_possible_cpus()
store i32 %9, i32* %5
%10 = load i32, i32* %3
%11 = load i32, i32* %5
%12 = icmp sgt i32 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
store i32 -1, i32* %2
br label %43
15: ; preds = %1
%16 = load i32, i32* %5
%17 = call %struct.bitmask* @numa_bitmask_alloc(i32 %16)
store %struct.bitmask* %17, %struct.bitmask** %4
%18 = call i32 (...) @numa_max_node()
store i32 %18, i32* %6
store i32 0, i32* %7
br label %19
19: ; preds = %34, %15
%20 = load i32, i32* %7
%21 = load i32, i32* %6
%22 = icmp sle i32 %20, %21
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = load i32, i32* %7
%25 = load %struct.bitmask*, %struct.bitmask** %4
%26 = call i32 @numa_node_to_cpus_v2_int(i32 %24, %struct.bitmask* %25)
%27 = load %struct.bitmask*, %struct.bitmask** %4
%28 = load i32, i32* %3
%29 = call i64 @numa_bitmask_isbitset(%struct.bitmask* %27, i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = load i32, i32* %7
store i32 %32, i32* %8
br label %39
33: ; preds = %23
br label %34
34: ; preds = %33
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
store i32 %36, i32* %7
br label %19
37: ; preds = %19
store i32 -1, i32* %8
%38 = load i32, i32* @EINVAL
store i32 %38, i32* @errno
br label %39
39: ; preds = %37, %31
%40 = load %struct.bitmask*, %struct.bitmask** %4
%41 = call i32 @numa_bitmask_free(%struct.bitmask* %40)
%42 = load i32, i32* %8
store i32 %42, i32* %2
br label %43
43: ; preds = %39, %13
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.bitmask = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @numa_node_of_cpu(i32 %0) {
%2 = tail call i32 (...) @numa_num_possible_cpus()
%3 = icmp slt i32 %2, %0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = load i32, i32* @EINVAL
store i32 %5, i32* @errno
br label %23
6: ; preds = %1
%7 = tail call %struct.bitmask* @numa_bitmask_alloc(i32 %2)
%8 = tail call i32 (...) @numa_max_node()
%9 = icmp slt i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %6, %15
%11 = phi i32 [ %16, %15 ], [ 0, %6 ]
%12 = tail call i32 @numa_node_to_cpus_v2_int(i32 %11, %struct.bitmask* %7)
%13 = tail call i64 @numa_bitmask_isbitset(%struct.bitmask* %7, i32 %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = add nuw i32 %11, 1
%17 = icmp eq i32 %11, %8
br i1 %17, label %18, label %10
18: ; preds = %15, %6
%19 = load i32, i32* @EINVAL
store i32 %19, i32* @errno
br label %20
20: ; preds = %10, %18
%21 = phi i32 [ -1, %18 ], [ %11, %10 ]
%22 = tail call i32 @numa_bitmask_free(%struct.bitmask* %7)
br label %23
23: ; preds = %20, %4
%24 = phi i32 [ -1, %4 ], [ %21, %20 ]
ret i32 %24
}