// File: /workspace/github_c_makefile_star2_O1/google[P]multichase/permutation.c
// Function: generate_chase_long
void *generate_chase_long(const struct generate_chase_common_args *args,
                     size_t mixer_idx, size_t total_par) {
  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;
  size_t nr_mixer_indices = args->nr_mixer_indices;
  size_t nr_iteration = nr_mixer_indices / total_par;
  const perm_t *mixer = args->mixer + mixer_idx * nr_iteration * args->nr_mixers;

  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;
  size_t j;
  size_t base;
  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);

  perm = malloc(nr_iteration * nr_elts * sizeof(*perm));
  if (perm == NULL) {
    fprintf(stderr, "Could not allocate %lu bytes\n",
            nr_iteration * nr_elts * sizeof(*perm));
    exit(1);
  }

  // Generate nr_iteration number of permutations.
  for (j = 0; j < nr_iteration; j++) {
    base = j * nr_elts;

    tlb_perm = malloc(nr_tlb_groups * sizeof(*tlb_perm));
    if (tlb_perm == NULL) {
      fprintf(stderr, "Could not allocate %lu bytes\n",
              nr_tlb_groups * sizeof(*tlb_perm));
      exit(1);
    }

    gen_permutation(tlb_perm, nr_tlb_groups, 0);

    for (i = 0; i < nr_tlb_groups; ++i) {
      gen_permutation(&perm[j * nr_elts + i * nr_elts_per_tlb], nr_elts_per_tlb,
                    base + tlb_perm[i] * nr_elts_per_tlb);
    }
    free(tlb_perm);

    dassert(is_a_permutation(perm, nr_elts));
    if (verbosity > 1)
      printf("generating inverse permutation\n");
  }

  dassert(is_a_permutation(perm, nr_iteration * nr_elts));

  perm_inverse = malloc(nr_iteration * nr_elts * sizeof(*perm_inverse));
  if (perm_inverse == NULL) {
    fprintf(stderr, "Could not allocate %lu bytes\n",
            nr_iteration * nr_elts * sizeof(*perm_inverse));
    exit(1);
  }

  for (i = 0; i < nr_iteration * nr_elts; ++i) {
    perm_inverse[perm[i]] = i;
  }

  dassert(is_a_permutation(perm_inverse, nr_iteration, nr_elts));

// Get the [(x mod NR_MIXER)th element in the jth row of mixer]th element
// in the xth stride of the array.
#define MIXED_2(x,j,n) ((x)*stride + (mixer + j*n)[(x) & (n-1)] * mixer_scale)

  if (verbosity > 1)
    printf("threading the chase (mixer_idx = %zu)\n", mixer_idx);

  // Generate the final permutation, which connects nr_iteration * nr_elts
  // number of permutations together into one.
  for (i = 0; i < nr_elts * nr_iteration; ++i) {
    size_t next;
    dassert(perm[perm_inverse[i]] == i);
    assert(*(void **)(arena + MIXED_2(i%nr_elts,i/nr_elts, args->nr_mixers)) == NULL);
    next = perm_inverse[i] + 1;
    // If next is the position representing the start of a new iteration of
    // permutation, set next to be the position representing the start of
    // current iteration, because we want to finish current permutation before
    // proceeding to the next.
    next = (next%nr_elts == 0 && next/nr_elts > i/nr_elts) ? i/nr_elts*nr_elts : next;

    if (perm[next]%nr_elts == 0) {
      // If current iteration of permutation is finished,
      // new position is the start of next iteration.
      size_t new = (i/nr_elts + 1) * nr_elts;
      new = (new == nr_iteration * nr_elts) ? 0 : new;
      *(void **)(arena + MIXED_2(i%nr_elts,i/nr_elts, args->nr_mixers)) =
        (void *)(arena + MIXED_2(new%nr_elts,new/nr_elts, args->nr_mixers));
    } else {
      *(void **)(arena + MIXED_2(i%nr_elts,i/nr_elts, args->nr_mixers)) =
        (void *)(arena + MIXED_2(perm[next]%nr_elts,perm[next]/nr_elts, args->nr_mixers));
    }
  }

  free(perm);
  free(perm_inverse);

  return arena + MIXED_2(0,0, args->nr_mixers);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %r10
movq (%rdi), %rax
movq %rax, 0x20(%rsp)
movq 0x8(%rdi), %r8
movq 0x10(%rdi), %rcx
movq 0x18(%rdi), %r9
movq 0x28(%rdi), %r14
movq 0x30(%rdi), %r15
movq %r15, %rax
xorl %edx, %edx
movq %r10, 0x8(%rsp)
divq %r10
movq %rax, 0x48(%rsp)
movq %rax, %r12
movq %rsi, 0x50(%rsp)
imulq %rsi, %r12
imulq 0x20(%rdi), %r12
shlq $0x3, %r12
movq %rdi, 0x58(%rsp)
addq 0x38(%rdi), %r12
movq %r8, %rax
xorl %edx, %edx
divq %r9
movq %rax, %rbp
movq %r9, 0x78(%rsp)
movq %r9, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %rbx
movq %r8, 0x80(%rsp)
movq %r8, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
movq %rcx, 0x60(%rsp)
movq %rcx, %rax
xorl %edx, %edx
divq %r15
movq %rax, %r13
movq $0x50b310, %rax         # imm = 0x50B310
cmpl $0x2, (%rax)
movq %rsi, 0x18(%rsp)
jl 0x4080b0
leaq 0xcaff7(%rip), %rdi     # 0x4d3098
movq %rbp, %rdx
xorl %eax, %eax
callq 0x413340
movq 0x18(%rsp), %rsi
imulq 0x48(%rsp), %rsi
movq %rsi, 0x28(%rsp)
leaq (,%rsi,8), %rdi
movq %rdi, 0x38(%rsp)
callq 0x435520
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x40838b
cmpq 0x8(%rsp), %r15
jb 0x4081c7
leaq (,%rbp,8), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x30(%rsp)
movq %r12, 0x70(%rsp)
movq %r13, 0x68(%rsp)
movq 0x40(%rsp), %rdi
callq 0x435520
testq %rax, %rax
je 0x408381
movq %rax, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %rax, 0x8(%rsp)
callq *%r14
movq 0x78(%rsp), %rax
cmpq 0x80(%rsp), %rax
movq %rbp, %r15
movq %r14, %rbp
ja 0x40817d
movq 0x30(%rsp), %r14
imulq 0x18(%rsp), %r14
movq 0x10(%rsp), %rax
leaq (%rax,%r14,8), %r12
xorl %r13d, %r13d
movq %r13, %rax
imulq %rbx, %rax
leaq (%r12,%rax,8), %rdi
movq 0x8(%rsp), %rax
movq (%rax,%r13,8), %rdx
imulq %rbx, %rdx
addq %r14, %rdx
movq %rbx, %rsi
callq *%rbp
incq %r13
cmpq %r15, %r13
jb 0x408155
movq 0x8(%rsp), %rdi
callq 0x435860
movq $0x50b310, %rax         # imm = 0x50B310
cmpl $0x2, (%rax)
jl 0x40819f
leaq 0xcb054(%rip), %rdi     # 0x4d31ee
callq 0x4225c0
movq 0x30(%rsp), %rcx
incq %rcx
movq %rcx, 0x30(%rsp)
cmpq 0x48(%rsp), %rcx
movq 0x70(%rsp), %r12
movq 0x68(%rsp), %r13
movq %rbp, %r14
movq %r15, %rbp
jb 0x408106
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x435520
testq %rax, %rax
movq 0x20(%rsp), %r8
movq 0x60(%rsp), %rbp
je 0x408395
movq 0x28(%rsp), %rbx
testq %rbx, %rbx
movq 0x10(%rsp), %rdi
je 0x408208
xorl %edx, %edx
movq (%rdi,%rdx,8), %rcx
movq %rdx, (%rax,%rcx,8)
incq %rdx
cmpq %rbx, %rdx
jb 0x4081f8
movq %rax, 0x8(%rsp)
movq $0x50b310, %rax         # imm = 0x50B310
cmpl $0x2, (%rax)
jl 0x408236
leaq 0xcaeb4(%rip), %rdi     # 0x4d30d4
movq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x413340
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %rdi
testq %rbx, %rbx
movq 0x18(%rsp), %r15
je 0x408333
movq 0x58(%rsp), %rax
movq 0x20(%rax), %r10
leaq -0x1(%r10), %r11
xorl %ecx, %ecx
movq %rcx, %rax
xorl %edx, %edx
divq %r15
movq %rax, %r14
movq %rdx, %rsi
movq %r10, %rax
imulq %r14, %rax
leaq (%r12,%rax,8), %rax
movq %r11, %rdx
andq %rsi, %rdx
imulq %rbp, %rsi
movq (%rax,%rdx,8), %rbx
imulq %r13, %rbx
addq %r8, %rsi
cmpq $0x0, (%rbx,%rsi)
jne 0x408362
movq 0x8(%rsp), %rax
movq (%rax,%rcx,8), %r8
incq %r8
movq %r8, %rax
xorl %edx, %edx
divq %r15
cmpq %r14, %rax
ja 0x4082aa
movq %r8, %rax
jmp 0x4082b1
movq %r14, %rax
imulq %r15, %rax
testq %rdx, %rdx
je 0x4082b9
movq %r8, %rax
movq (%rdi,%rax,8), %r8
movq %r8, %rax
xorl %edx, %edx
divq %r15
movq %rdx, %r9
testq %rdx, %rdx
jne 0x4082ef
incq %r14
imulq %r15, %r14
movl $0x0, %r8d
cmpq 0x28(%rsp), %r14
je 0x4082e4
movq %r14, %r8
movq %r8, %rax
xorl %edx, %edx
divq %r15
movq %rdx, %r9
movq %r9, %rdi
imulq %rbp, %rdi
movq %r8, %rax
xorl %edx, %edx
divq %r15
imulq %r10, %rax
leaq (%r12,%rax,8), %rax
andq %r11, %r9
movq (%rax,%r9,8), %rax
imulq %r13, %rax
movq 0x20(%rsp), %r8
addq %r8, %rdi
addq %rax, %rdi
movq %rdi, (%rsi,%rbx)
incq %rcx
cmpq 0x28(%rsp), %rcx
movq 0x10(%rsp), %rdi
jb 0x408253
movq %r8, %rbx
callq 0x435860
movq 0x8(%rsp), %rdi
callq 0x435860
imulq (%r12), %r13
addq %r13, %rbx
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xcadb0(%rip), %rdi     # 0x4d3119
leaq 0xcadf3(%rip), %rsi     # 0x4d3163
leaq 0xcadfa(%rip), %rcx     # 0x4d3171
movl $0x11f, %edx            # imm = 0x11F
callq 0x40bc10
movq 0x40(%rsp), %rdi
callq 0x401216
movq 0x38(%rsp), %rdi
callq 0x40125e
movq %rbx, %rdi
callq 0x40123a
nopl (%rax)