// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x18(%rbp)
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rcx
imulq -0x50(%rbp), %rcx
movq -0x8(%rbp), %rdx
imulq 0x20(%rdx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x38(%rbp)
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x30(%rbp)
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x30(%rbp)
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x48(%rbp)
movq %rax, -0xa8(%rbp)
movq $0x512310, %rax         # imm = 0x512310
cmpl $0x1, (%rax)
jle 0x40f718
movq -0x70(%rbp), %rsi
movq -0x60(%rbp), %rdx
leaq 0xca9a9(%rip), %rdi     # 0x4da0ba
movb $0x0, %al
callq 0x41b480
movq -0x50(%rbp), %rdi
imulq -0x70(%rbp), %rdi
shlq $0x3, %rdi
callq 0x43d330
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x40f764
movq $0x510708, %rax         # imm = 0x510708
movq (%rax), %rdi
movq -0x50(%rbp), %rdx
imulq -0x70(%rbp), %rdx
shlq $0x3, %rdx
leaq 0xca9ea(%rip), %rsi     # 0x4da13d
movb $0x0, %al
callq 0x41b3c0
movl $0x1, %edi
callq 0x41a3f0
movq $0x0, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x40f89f
movq -0x90(%rbp), %rax
imulq -0x70(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x60(%rbp), %rdi
shlq $0x3, %rdi
callq 0x43d330
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x40f7d5
movq $0x510708, %rax         # imm = 0x510708
movq (%rax), %rdi
movq -0x60(%rbp), %rdx
shlq $0x3, %rdx
leaq 0xca979(%rip), %rsi     # 0x4da13d
movb $0x0, %al
callq 0x41b3c0
movl $0x1, %edi
callq 0x41a3f0
movq -0x40(%rbp), %rax
movq -0x78(%rbp), %rdi
movq -0x60(%rbp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x40f861
movq -0x40(%rbp), %rax
movq -0x80(%rbp), %rdi
movq -0x90(%rbp), %rcx
imulq -0x70(%rbp), %rcx
movq -0x88(%rbp), %rdx
imulq -0x68(%rbp), %rdx
addq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rdi
movq -0x68(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq -0x88(%rbp), %r8
movq (%rcx,%r8,8), %rcx
imulq -0x68(%rbp), %rcx
addq %rcx, %rdx
callq *%rax
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x40f7f2
movq -0x78(%rbp), %rdi
callq 0x43d670
jmp 0x40f86c
movq $0x512310, %rax         # imm = 0x512310
cmpl $0x1, (%rax)
jle 0x40f886
leaq 0xca8dc(%rip), %rdi     # 0x4da15b
movb $0x0, %al
callq 0x41b480
jmp 0x40f888
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x40f76f
jmp 0x40f8a1
jmp 0x40f8a3
movq -0x50(%rbp), %rdi
imulq -0x70(%rbp), %rdi
shlq $0x3, %rdi
callq 0x43d330
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
jne 0x40f8f5
movq $0x510708, %rax         # imm = 0x510708
movq (%rax), %rdi
movq -0x50(%rbp), %rdx
imulq -0x70(%rbp), %rdx
shlq $0x3, %rdx
leaq 0xca859(%rip), %rsi     # 0x4da13d
movb $0x0, %al
callq 0x41b3c0
movl $0x1, %edi
callq 0x41a3f0
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq -0x50(%rbp), %rcx
imulq -0x70(%rbp), %rcx
cmpq %rcx, %rax
jae 0x40f94a
movq -0x88(%rbp), %rdx
movq -0xa0(%rbp), %rax
movq -0x80(%rbp), %rcx
movq -0x88(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x40f900
jmp 0x40f94c
jmp 0x40f94e
movq $0x512310, %rax         # imm = 0x512310
cmpl $0x1, (%rax)
jle 0x40f96c
movq -0x10(%rbp), %rsi
leaq 0xca7b1(%rip), %rdi     # 0x4da116
movb $0x0, %al
callq 0x41b480
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq -0x70(%rbp), %rcx
imulq -0x50(%rbp), %rcx
cmpq %rcx, %rax
jae 0x40fdec
jmp 0x40f992
jmp 0x40f994
movq -0x20(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x88(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
movq %rdx, %rcx
imulq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x88(%rbp), %rax
xorl %edx, %edx
divq -0x70(%rbp)
movq %rax, %rdx
movq -0xd0(%rbp), %rax
movq -0x8(%rbp), %rsi
imulq 0x20(%rsi), %rdx
shlq $0x3, %rdx
addq %rdx, %rax
movq %rax, -0xc0(%rbp)
movq -0x88(%rbp), %rax
xorl %edx, %edx
divq -0x70(%rbp)
movq -0xc8(%rbp), %rax
movq %rdx, %rsi
movq -0xc0(%rbp), %rdx
movq -0x8(%rbp), %rdi
movq 0x20(%rdi), %rdi
subq $0x1, %rdi
andq %rdi, %rsi
movq (%rdx,%rsi,8), %rdx
imulq -0xa8(%rbp), %rdx
addq %rdx, %rcx
cmpq $0x0, (%rax,%rcx)
jne 0x40fa34
jmp 0x40fa53
leaq 0xca740(%rip), %rdi     # 0x4da17b
leaq 0xca783(%rip), %rsi     # 0x4da1c5
movl $0x11f, %edx            # imm = 0x11F
leaq 0xca785(%rip), %rcx     # 0x4da1d3
callq 0x413d50
movq -0xa0(%rbp), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
cmpq $0x0, %rdx
jne 0x40fad6
movq -0xb0(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
movq %rax, -0xd8(%rbp)
movq -0x88(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jbe 0x40fad6
movq -0x88(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
imulq -0x70(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x40fae4
movq -0xb0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x80(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
cmpq $0x0, %rdx
jne 0x40fc97
movq -0x88(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
addq $0x1, %rax
imulq -0x70(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq -0x50(%rbp), %rcx
imulq -0x70(%rbp), %rcx
cmpq %rcx, %rax
jne 0x40fb52
xorl %eax, %eax
movq %rax, -0xe8(%rbp)
jmp 0x40fb60
movq -0xb8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0xb8(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
movq %rdx, %rcx
imulq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0xb8(%rbp), %rax
xorl %edx, %edx
divq -0x70(%rbp)
movq %rax, %rdx
movq -0x110(%rbp), %rax
movq -0x8(%rbp), %rsi
imulq 0x20(%rsi), %rdx
shlq $0x3, %rdx
addq %rdx, %rax
movq %rax, -0x100(%rbp)
movq -0xb8(%rbp), %rax
xorl %edx, %edx
divq -0x70(%rbp)
movq -0x108(%rbp), %rax
movq %rdx, %rsi
movq -0x100(%rbp), %rdx
movq -0x8(%rbp), %rdi
movq 0x20(%rdi), %rdi
subq $0x1, %rdi
andq %rdi, %rsi
movq (%rdx,%rsi,8), %rdx
imulq -0xa8(%rbp), %rdx
addq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0xf0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x88(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
movq %rdx, %rcx
imulq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rsi
movq -0x88(%rbp), %rax
xorl %edx, %edx
divq -0x70(%rbp)
movq -0x8(%rbp), %rdx
imulq 0x20(%rdx), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq -0x88(%rbp), %rax
xorl %edx, %edx
divq -0x70(%rbp)
movq -0xf8(%rbp), %rax
movq %rdx, %rdi
movq -0xf0(%rbp), %rdx
movq -0x8(%rbp), %r8
movq 0x20(%r8), %r8
subq $0x1, %r8
andq %r8, %rdi
movq (%rsi,%rdi,8), %rsi
imulq -0xa8(%rbp), %rsi
addq %rsi, %rcx
movq %rdx, (%rax,%rcx)
jmp 0x40fdd3
movq -0x20(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x80(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
movq %rdx, %rcx
imulq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x80(%rbp), %rax
movq -0xb0(%rbp), %rdx
movq (%rax,%rdx,8), %rax
xorl %edx, %edx
divq -0x70(%rbp)
movq %rax, %rdx
movq -0x138(%rbp), %rax
movq -0x8(%rbp), %rsi
imulq 0x20(%rsi), %rdx
shlq $0x3, %rdx
addq %rdx, %rax
movq %rax, -0x128(%rbp)
movq -0x80(%rbp), %rax
movq -0xb0(%rbp), %rdx
movq (%rax,%rdx,8), %rax
xorl %edx, %edx
divq -0x70(%rbp)
movq -0x130(%rbp), %rax
movq %rdx, %rsi
movq -0x128(%rbp), %rdx
movq -0x8(%rbp), %rdi
movq 0x20(%rdi), %rdi
subq $0x1, %rdi
andq %rdi, %rsi
movq (%rdx,%rsi,8), %rdx
imulq -0xa8(%rbp), %rdx
addq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x118(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x88(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
movq %rdx, %rcx
imulq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rsi
movq -0x88(%rbp), %rax
xorl %edx, %edx
divq -0x70(%rbp)
movq -0x8(%rbp), %rdx
imulq 0x20(%rdx), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq -0x88(%rbp), %rax
xorl %edx, %edx
divq -0x70(%rbp)
movq -0x120(%rbp), %rax
movq %rdx, %rdi
movq -0x118(%rbp), %rdx
movq -0x8(%rbp), %r8
movq 0x20(%r8), %r8
subq $0x1, %r8
andq %r8, %rdi
movq (%rsi,%rdi,8), %rsi
imulq -0xa8(%rbp), %rsi
addq %rsi, %rcx
movq %rdx, (%rax,%rcx)
jmp 0x40fdd5
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x40f977
movq -0x80(%rbp), %rdi
callq 0x43d670
movq -0xa0(%rbp), %rdi
callq 0x43d670
movq -0x20(%rbp), %rax
imulq $0x0, -0x30(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq -0x8(%rbp), %rsi
imulq $0x0, 0x20(%rsi), %rsi
shlq $0x3, %rsi
addq %rsi, %rdx
movq -0x8(%rbp), %rsi
movq 0x20(%rsi), %rsi
subq $0x1, %rsi
andq $0x0, %rsi
movq (%rdx,%rsi,8), %rdx
imulq -0xa8(%rbp), %rdx
addq %rdx, %rcx
addq %rcx, %rax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopl (%rax)