// File: /workspace/github_c_makefile_star2_O0/google[P]multichase/arena.c
// Function: arena_weighted_mbind
static void arena_weighted_mbind(size_t page_size, void *arena,
                                 size_t arena_size, uint16_t *weights,
                                 size_t nr_weights) {
  /* compute cumulative sum for weights
   * cumulative sum starts at -1
   * the method for determining a hit on a weight i is when the generated
   * random number (modulo sum of weights) <= weights_cumsum[i]
   */
  int64_t *weights_cumsum = malloc(nr_weights * sizeof(int64_t));
  if (!weights_cumsum) {
    fprintf(stderr, "Couldn't allocate memory for weights.\n");
    exit(1);
  }
  weights_cumsum[0] = weights[0] - 1;
  for (unsigned int i = 1; i < nr_weights; i++) {
    weights_cumsum[i] = weights_cumsum[i - 1] + weights[i];
  }
  const int32_t weight_sum = weights_cumsum[nr_weights - 1] + 1;

  uint64_t mask = 0;
  char *q = (char *)arena + arena_size;
  rng_init(1);
  for (char *p = arena; p < q; p += page_size) {
    uint32_t r = rng_int(1 << 31) % weight_sum;
    unsigned int node;
    for (node = 0; node < nr_weights; node++) {
      if (weights_cumsum[node] >= r) {
        break;
      }
    }
    mask = 1 << node;
    if (mbind(p, page_size, MPOL_BIND, &mask, nr_weights, MPOL_MF_STRICT)) {
      perror("mbind");
      exit(1);
    }
    *p = 0;
  }
  free(weights_cumsum);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x28(%rbp), %rdi
shlq $0x3, %rdi
callq 0x43d330
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x4102e6
movq $0x510708, %rax         # imm = 0x510708
movq (%rax), %rdi
leaq 0xca1bd(%rip), %rsi     # 0x4da492
movb $0x0, %al
callq 0x41b3c0
movl $0x1, %edi
callq 0x41a3f0
movq -0x20(%rbp), %rax
movzwl (%rax), %eax
subl $0x1, %eax
movslq %eax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpq -0x28(%rbp), %rax
jae 0x41033e
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
addq %rax, %rdx
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x410301
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
addq $0x1, %rax
movl %eax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x1, %edi
callq 0x410630
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x410438
movq $-0x80000000, %rdi      # imm = 0x80000000
callq 0x410720
movslq -0x38(%rbp), %rcx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpq -0x28(%rbp), %rax
jae 0x4103d5
movq -0x30(%rbp), %rax
movl -0x58(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl -0x54(%rbp), %ecx
cmpq %rcx, %rax
jl 0x4103c8
jmp 0x4103d5
jmp 0x4103ca
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x4103aa
movl -0x58(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
cltq
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x28(%rbp), %r8
movl $0x2, %edx
leaq -0x40(%rbp), %rcx
movl $0x1, %r9d
callq 0x410850
cmpl $0x0, %eax
je 0x410420
leaq 0xca0a8(%rip), %rdi     # 0x4da4b9
callq 0x41b6b0
movl $0x1, %edi
callq 0x41a3f0
movq -0x50(%rbp), %rax
movb $0x0, (%rax)
movq -0x8(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x41037b
movq -0x30(%rbp), %rdi
callq 0x43d670
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)