// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movq %rdx, %r8
movq (%rdi), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rdi), %r10
movq 0x10(%rdi), %r12
movq 0x18(%rdi), %r9
movq 0x28(%rdi), %rax
movq %rax, 0x48(%rsp)
movq 0x30(%rdi), %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq %rax, 0x38(%rsp)
movq %rax, %r13
movq %rsi, 0x50(%rsp)
imulq %rsi, %r13
imulq 0x20(%rdi), %r13
shlq $0x3, %r13
movq %rdi, 0x58(%rsp)
addq 0x38(%rdi), %r13
movq %r10, %rax
xorl %edx, %edx
divq %r9
movq %rax, %rbx
movq %r9, %rax
xorl %edx, %edx
divq %r12
movq %rax, %r15
movq %r10, %rax
xorl %edx, %edx
divq %r12
movq %rax, %rsi
movq %r12, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %rbp
movq $0x50a310, %rax         # imm = 0x50A310
cmpl $0x2, (%rax)
movq %rsi, 0x8(%rsp)
jl 0x407ba1
leaq 0xca506(%rip), %rdi     # 0x4d2098
movq %rbx, %rdx
xorl %eax, %eax
callq 0x412ec0
movq 0x8(%rsp), %rsi
imulq 0x38(%rsp), %rsi
movq %rsi, 0x20(%rsp)
leaq (,%rsi,8), %rdi
movq %rdi, 0x28(%rsp)
callq 0x4350a0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x407e6e
leaq (,%rbx,8), %rax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
movq %r12, 0x70(%rsp)
movq %r13, 0x68(%rsp)
movq %rbp, 0x60(%rsp)
cmpq 0x38(%rsp), %rax
jae 0x407c9d
movq %rax, 0x40(%rsp)
movq 0x30(%rsp), %rdi
callq 0x4350a0
testq %rax, %rax
je 0x407e2a
movq %rax, %r14
movq 0x40(%rsp), %rbp
imulq 0x8(%rsp), %rbp
movq %rax, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq *0x48(%rsp)
movq 0x10(%rsp), %rax
leaq (%rax,%rbp,8), %r13
xorl %r12d, %r12d
cmpq %rbx, %r12
jae 0x407c61
movq %r12, %rax
imulq %r15, %rax
leaq (,%rax,8), %rdi
addq %r13, %rdi
movq (%r14,%r12,8), %rdx
imulq %r15, %rdx
addq %rbp, %rdx
movq %r15, %rsi
callq *0x48(%rsp)
incq %r12
jmp 0x407c33
movq %r14, %rdi
callq 0x4353e0
movq $0x50a310, %rax         # imm = 0x50A310
cmpl $0x2, (%rax)
jl 0x407c81
leaq 0xca572(%rip), %rdi     # 0x4d21ee
callq 0x422140
movq 0x40(%rsp), %rax
incq %rax
movq 0x70(%rsp), %r12
movq 0x68(%rsp), %r13
movq 0x60(%rsp), %rbp
jmp 0x407bea
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x4350a0
testq %rax, %rax
je 0x407e86
movq %rax, %r15
xorl %eax, %eax
movq 0x8(%rsp), %r9
cmpq 0x20(%rsp), %rax
jae 0x407cd6
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq %rax, (%r15,%rcx,8)
incq %rax
jmp 0x407cbd
movq $0x50a310, %rax         # imm = 0x50A310
cmpl $0x2, (%rax)
jl 0x407cfa
leaq 0xca3eb(%rip), %rdi     # 0x4d20d4
movq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x412ec0
movq 0x8(%rsp), %r9
xorl %ecx, %ecx
cmpq 0x20(%rsp), %rcx
jae 0x407dfc
movq %rcx, %rax
xorl %edx, %edx
divq %r9
movq %rax, %rdi
movq %rdx, %rsi
movq 0x58(%rsp), %rax
movq 0x20(%rax), %r10
movq %r10, %rax
imulq %rdi, %rax
leaq (,%rax,8), %rax
addq %r13, %rax
leaq -0x1(%r10), %rbx
movq %rbx, %rdx
andq %rsi, %rdx
imulq %r12, %rsi
movq (%rax,%rdx,8), %r11
imulq %rbp, %r11
addq 0x18(%rsp), %rsi
cmpq $0x0, (%r11,%rsi)
jne 0x407e4f
movq (%r15,%rcx,8), %r8
incq %r8
movq %r8, %rax
xorl %edx, %edx
divq %r9
cmpq %rdi, %rax
ja 0x407d6f
movq %r8, %rax
jmp 0x407d76
movq %rdi, %rax
imulq %r9, %rax
testq %rdx, %rdx
je 0x407d7e
movq %r8, %rax
movq 0x10(%rsp), %rdx
movq (%rdx,%rax,8), %r8
movq %r8, %rax
xorl %edx, %edx
divq %r9
movq %r9, %r14
movq %rdx, %r9
testq %rdx, %rdx
jne 0x407dbc
incq %rdi
imulq %r14, %rdi
movl $0x0, %r8d
cmpq 0x20(%rsp), %rdi
je 0x407db1
movq %rdi, %r8
movq %r8, %rax
xorl %edx, %edx
divq %r14
movq %rdx, %r9
movq %r9, %rdi
imulq %r12, %rdi
movq %r8, %rax
xorl %edx, %edx
divq %r14
imulq %r10, %rax
leaq (,%rax,8), %rax
addq %r13, %rax
andq %r9, %rbx
movq (%rax,%rbx,8), %rax
imulq %rbp, %rax
addq 0x18(%rsp), %rdi
addq %rax, %rdi
movq %rdi, (%rsi,%r11)
incq %rcx
movq %r14, %r9
jmp 0x407cfc
movq 0x10(%rsp), %rdi
callq 0x4353e0
movq %r15, %rdi
callq 0x4353e0
imulq (%r13), %rbp
movq 0x18(%rsp), %rax
addq %rbp, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x508708, %rax         # imm = 0x508708
movq (%rax), %rdi
leaq 0xca2c0(%rip), %rsi     # 0x4d20fb
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x412e00
pushq $0x1
popq %rdi
callq 0x411e30
leaq 0xca2c3(%rip), %rdi     # 0x4d2119
leaq 0xca306(%rip), %rsi     # 0x4d2163
leaq 0xca30d(%rip), %rcx     # 0x4d2171
movl $0x11f, %edx            # imm = 0x11F
callq 0x40b790
movq $0x508708, %rax         # imm = 0x508708
movq (%rax), %rdi
leaq 0xca27c(%rip), %rsi     # 0x4d20fb
movq 0x28(%rsp), %rdx
jmp 0x407e40
movq $0x508708, %rax         # imm = 0x508708
movq (%rax), %rdi
leaq 0xca264(%rip), %rsi     # 0x4d20fb
movq %rbx, %rdx
jmp 0x407e40