// File: /workspace/github_c_makefile_star2_O0/google[P]multichase/permutation.c
// Function: generate_chase
void *generate_chase(const struct generate_chase_common_args *args,
                     size_t mixer_idx) {
  char *arena = args->arena;
  size_t total_memory = args->total_memory;
  size_t stride = args->stride;
  size_t tlb_locality = args->tlb_locality;
  void (*gen_permutation)(perm_t *, size_t, size_t) = args->gen_permutation;
  const perm_t *mixer = args->mixer + mixer_idx * args->nr_mixers;
  size_t nr_mixer_indices = args->nr_mixer_indices;

  size_t nr_tlb_groups = total_memory / tlb_locality;
  size_t nr_elts_per_tlb = tlb_locality / stride;
  size_t nr_elts = total_memory / stride;
  perm_t *tlb_perm;
  perm_t *perm;
  size_t i;
  perm_t *perm_inverse;
  size_t mixer_scale = stride / nr_mixer_indices;

  if (verbosity > 1)
    printf("generating permutation of %zu elements (in %zu TLB groups)\n",
           nr_elts, nr_tlb_groups);
  tlb_perm = malloc(nr_tlb_groups * sizeof(*tlb_perm));
  gen_permutation(tlb_perm, nr_tlb_groups, 0);
  perm = malloc(nr_elts * sizeof(*perm));
  for (i = 0; i < nr_tlb_groups; ++i) {
    gen_permutation(&perm[i * nr_elts_per_tlb], nr_elts_per_tlb,
                    tlb_perm[i] * nr_elts_per_tlb);
  }
  free(tlb_perm);

  dassert(is_a_permutation(perm, nr_elts));

  if (verbosity > 1) printf("generating inverse permtuation\n");
  perm_inverse = malloc(nr_elts * sizeof(*perm));
  for (i = 0; i < nr_elts; ++i) {
    perm_inverse[perm[i]] = i;
  }

  dassert(is_a_permutation(perm_inverse, nr_elts));

#define MIXED(x) ((x)*stride + mixer[(x) & (args->nr_mixers - 1)] * mixer_scale)

  if (verbosity > 1)
    printf("threading the chase (mixer_idx = %zu)\n", mixer_idx);
  for (i = 0; i < nr_elts; ++i) {
    size_t next;
    dassert(perm[perm_inverse[i]] == i);
    next = perm_inverse[i] + 1;
    next = (next == nr_elts) ? 0 : next;
    *(void **)(arena + MIXED(i)) = (void *)(arena + MIXED(perm[next]));
  }

  free(perm);
  free(perm_inverse);

  return arena + MIXED(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rcx
movq -0x8(%rbp), %rdx
imulq 0x20(%rdx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x30(%rbp)
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x28(%rbp)
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x28(%rbp)
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x48(%rbp)
movq %rax, -0x88(%rbp)
movq $0x512310, %rax         # imm = 0x512310
cmpl $0x1, (%rax)
jle 0x40f3bf
movq -0x60(%rbp), %rsi
movq -0x50(%rbp), %rdx
leaq 0xcad02(%rip), %rdi     # 0x4da0ba
movb $0x0, %al
callq 0x41b480
movq -0x50(%rbp), %rdi
shlq $0x3, %rdi
callq 0x43d330
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movq -0x68(%rbp), %rdi
movq -0x50(%rbp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq -0x60(%rbp), %rdi
shlq $0x3, %rdi
callq 0x43d330
movq %rax, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x40f442
movq -0x38(%rbp), %rax
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rcx
imulq -0x58(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rdi
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq (%rcx,%rdx,8), %rdx
imulq -0x58(%rbp), %rdx
callq *%rax
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x40f3fb
movq -0x68(%rbp), %rdi
callq 0x43d670
jmp 0x40f44d
movq $0x512310, %rax         # imm = 0x512310
cmpl $0x1, (%rax)
jle 0x40f467
leaq 0xcac96(%rip), %rdi     # 0x4da0f6
movb $0x0, %al
callq 0x41b480
movq -0x60(%rbp), %rdi
shlq $0x3, %rdi
callq 0x43d330
movq %rax, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x40f4b0
movq -0x78(%rbp), %rdx
movq -0x80(%rbp), %rax
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x40f480
jmp 0x40f4b2
jmp 0x40f4b4
movq $0x512310, %rax         # imm = 0x512310
cmpl $0x1, (%rax)
jle 0x40f4d2
movq -0x10(%rbp), %rsi
leaq 0xcac4b(%rip), %rdi     # 0x4da116
movb $0x0, %al
callq 0x41b480
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x40f5cb
jmp 0x40f4ea
jmp 0x40f4ec
movq -0x80(%rbp), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x60(%rbp), %rax
jne 0x40f51b
xorl %eax, %eax
movq %rax, -0x98(%rbp)
jmp 0x40f529
movq -0x90(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rax
imulq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x70(%rbp), %rsi
movq -0x90(%rbp), %rdi
movq (%rsi,%rdi,8), %rsi
movq -0x8(%rbp), %rdi
movq 0x20(%rdi), %rdi
subq $0x1, %rdi
andq %rdi, %rsi
movq (%rcx,%rsi,8), %rcx
imulq -0x88(%rbp), %rcx
addq %rcx, %rax
addq %rax, %rdx
movq -0x18(%rbp), %rax
movq -0x78(%rbp), %rcx
imulq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rsi
movq -0x78(%rbp), %rdi
movq -0x8(%rbp), %r8
movq 0x20(%r8), %r8
subq $0x1, %r8
andq %r8, %rdi
movq (%rsi,%rdi,8), %rsi
imulq -0x88(%rbp), %rsi
addq %rsi, %rcx
movq %rdx, (%rax,%rcx)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x40f4da
movq -0x70(%rbp), %rdi
callq 0x43d670
movq -0x80(%rbp), %rdi
callq 0x43d670
movq -0x18(%rbp), %rax
imulq $0x0, -0x28(%rbp), %rcx
movq -0x40(%rbp), %rdx
movq -0x8(%rbp), %rsi
movq 0x20(%rsi), %rsi
subq $0x1, %rsi
andq $0x0, %rsi
movq (%rdx,%rsi,8), %rdx
imulq -0x88(%rbp), %rdx
addq %rdx, %rcx
addq %rcx, %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)