// File: /workspace/github_cpp_makefile_star2_O2/sid1607[P]avx2-merge-sort/test_sort.cpp
// Function: test_merge_sort(int)
bool test_merge_sort(int len) {
  bool status = true;
  int *a = nullptr, *temp = nullptr;

  // 32-byte aligned allocation
  if (posix_memalign((void **)&a, 32, len*sizeof(int)) != 0) {
    throw std::bad_alloc();
  }

  if (posix_memalign((void **)&temp, 32, len*sizeof(int)) != 0) {
    throw std::bad_alloc();
  }

  std::vector<int> ref(len);
  generate_random_array(a, len);
  std::copy(a, a+len, &ref[0]);

  auto std_start = get_time();
  generate_reference(ref);
  auto std_end = get_time();

  auto res = merge_sort(a, temp, len);
  auto merge_end = get_time();
  // print_array(&res.first[0], "Out", len);
  if (is_sorted_array(&res.first[0], &ref[0], len) == false) {
      print_array(&ref[0], "Ref", len);
      print_array(&res.first[0], "Out", len);
      status = false;
  } else {
    status = true;
    std::cout << "std time:" << std_end-std_start << " Merge time:" 
      << merge_end - std_end << std::endl;
  }

  delete a;
  delete temp;
  return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x68, %rsp
leaq 0x8(%rsp), %rax
andq $0x0, (%rax)
andq $0x0, 0x10(%rsp)
movl %edi, %ebx
movslq %edi, %r14
leaq (,%r14,4), %r15
pushq $0x20
popq %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x2090
testl %eax, %eax
jne 0x2f4a
leaq 0x10(%rsp), %rdi
pushq $0x20
popq %rsi
movq %r15, %rdx
callq 0x2090
testl %eax, %eax
jne 0x2f4a
leaq 0x50(%rsp), %rdi
leaq 0x20(%rsp), %rdx
movq %r14, %rsi
callq 0x306e
movq 0x8(%rsp), %rdi
movl %r14d, %esi
callq 0x2cb5
testl %r14d, %r14d
je 0x2e15
movq 0x8(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq %r15, %rdx
callq 0x2190
callq 0x30c1
vmovsd %xmm0, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x2d75
callq 0x30c1
vmovsd %xmm0, 0x18(%rsp)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x26cb
movq %rax, %r14
callq 0x30c1
vmovsd %xmm0, 0x40(%rsp)
movq 0x50(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x2d3e
movl %eax, %ebp
testb %al, %al
je 0x2ec1
movq 0x315c(%rip), %rdi      # 0x5fd0
leaq 0x124f(%rip), %rsi      # 0x40ca
callq 0x2130
vmovsd 0x18(%rsp), %xmm0
vsubsd 0x48(%rsp), %xmm0, %xmm0
movq %rax, %rdi
callq 0x2180
leaq 0x1239(%rip), %rsi      # 0x40d4
movq %rax, %rdi
callq 0x2130
vmovsd 0x40(%rsp), %xmm0
vsubsd 0x18(%rsp), %xmm0, %xmm0
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x2070
jmp 0x2f1f
leaq 0x11fa(%rip), %rsi      # 0x40c2
leaq 0x20(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x2050
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
movl %ebx, %edx
callq 0x2b70
leaq 0x20(%rsp), %rdi
callq 0x2060
leaq 0x11cf(%rip), %rsi      # 0x40c6
leaq 0x20(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x2050
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x2b70
leaq 0x20(%rsp), %rdi
callq 0x2060
movq 0x8(%rsp), %rdi
callq 0x2110
movq 0x10(%rsp), %rdi
callq 0x2110
leaq 0x50(%rsp), %rdi
callq 0x369c
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x2080
movq 0x306f(%rip), %rcx      # 0x5fc8
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x3071(%rip), %rsi      # 0x5fd8
movq 0x304a(%rip), %rdx      # 0x5fb8
movq %rax, %rdi
callq 0x21a0
jmp 0x2f7a
jmp 0x2f91
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2060
jmp 0x2f94
jmp 0x2f91
jmp 0x2f91
jmp 0x2f91
jmp 0x2f91
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x369c
movq %rbx, %rdi
callq 0x21c0