// File: /workspace/github_cpp_makefile_star2_O2/romkatv[P]action-chain/src/action_chain_test.cc
// Function: int romkatv::(anonymous namespace)::Benchmark<romkatv::ActionChain>(romkatv::(anonymous namespace)::Flags const&)
int Benchmark(const Flags& flags) {
  const std::uint64_t actions_per_thread = flags.actions / flags.threads;
  CHECK(actions_per_thread * flags.threads == flags.actions);

  auto PrintCol = [](const char* name, const auto& val) {
    std::cout << name << '=' << std::setw(17) << std::setprecision(3) << std::left << val;
  };

  PrintCol("sync", flags.sync);
  PrintCol("threads", flags.threads);
  PrintCol("ops-per-action", flags.ops_per_action);
  std::cout << std::flush;

  volatile std::uint64_t counter = 0;
  auto wall_time_start = std::chrono::high_resolution_clock::now();
  double cpu_time_start = CpuTimeSec();
  {
    std::mutex m;
    Sync sync;
    std::vector<std::thread> threads;
    for (std::uint64_t i = 0; i != flags.threads; ++i) {
      threads.emplace_back([&] {
        typename Sync::Mem mem;
        for (std::uint64_t i = 0; i != actions_per_thread; ++i) {
          sync.Run(&mem, [&] {
            for (std::uint64_t j = 0; j != flags.ops_per_action; ++j) ++counter;
          });
        }
      });
    }
    for (std::thread& t : threads) t.join();
  }
  double cpu_time_end = CpuTimeSec();
  auto wall_time_end = std::chrono::high_resolution_clock::now();

  if (counter != flags.ops_per_action * flags.actions) {
    std::cerr << "TEST FAILURE" << std::endl;
    return 1;
  }

  double wall = std::chrono::duration<double>(wall_time_end - wall_time_start).count();
  double cpu = cpu_time_end - cpu_time_start;
  PrintCol("total-wall-time(s)", wall);
  PrintCol("wall-time-per-action(ns)", 1e9 * wall / flags.actions);
  PrintCol("cpu-time-per-action(ns)", 1e9 * cpu / flags.actions);
  std::cout << std::endl;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq 0x20(%rdi), %rsi
movq 0x30(%rdi), %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
movq %rax, 0x50(%rsp)
imulq %rsi, %rax
cmpq %rcx, %rax
jne 0x2c9b
movq %rdi, %r13
leaq 0x20(%rdi), %r12
movq 0x5657(%rip), %rbx      # 0x7fc0
leaq 0x2762(%rip), %rsi      # 0x50d2
movq %rbx, %rdi
callq 0x21e0
pushq $0x3d
popq %rsi
movq %rax, %rdi
callq 0x2210
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x11, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rdx
movq $0x3, 0x8(%rax,%rdx)
movq -0x18(%rcx), %rcx
movl $0xffffff4f, %edx       # imm = 0xFFFFFF4F
andl 0x18(%rax,%rcx), %edx
orl $0x20, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movq %r13, %rsi
callq 0x21c0
leaq 0x2711(%rip), %rdi      # 0x50d7
movq %r12, %rsi
callq 0x3c02
leaq 0x28(%r13), %rsi
leaq 0x2706(%rip), %rdi      # 0x50df
callq 0x3c02
movq %rbx, %rdi
callq 0x2150
leaq 0x30(%rsp), %rax
movq $0x0, (%rax)
callq 0x2030
movq %rax, 0x38(%rsp)
callq 0x3c5d
movsd %xmm0, 0x40(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x4990
movq %rsp, %rax
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
xorl %ebp, %ebp
xorl %eax, %eax
movq %r12, 0x48(%rsp)
cmpq (%r12), %rax
je 0x2b38
movq %rax, 0x20(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x60(%rsp)
movq %r13, 0x68(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq 0x10(%rsp), %rbp
je 0x2a83
movq %rbp, %rdi
leaq 0x58(%rsp), %rsi
callq 0x3d52
movq 0x8(%rsp), %rbp
addq $0x8, %rbp
movq %rbp, 0x8(%rsp)
jmp 0x2b2b
movq %rsp, %rdi
pushq $0x1
popq %rsi
leaq 0x2ab4(%rip), %rdx      # 0x5544
callq 0x4b3c
movq %rax, 0x18(%rsp)
movq (%rsp), %rbx
movq 0x8(%rsp), %r14
movq %rsp, %rdi
movq 0x18(%rsp), %rsi
callq 0x4b80
movq %rax, %r15
movq %r13, %r12
movq %rbp, %r13
subq %rbx, %r13
addq %rax, %r13
movq %r13, %rdi
leaq 0x58(%rsp), %rsi
callq 0x3d52
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %rsp, %r13
movq %r13, %rcx
callq 0x4bd1
leaq 0x8(%rax), %rdx
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rcx
callq 0x4bd1
movq %rax, %rbp
movq 0x10(%rsp), %rdx
subq %rbx, %rdx
sarq $0x3, %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4b8c
movq %r12, %r13
movq 0x48(%rsp), %r12
movq %r15, (%rsp)
movq %rbp, 0x8(%rsp)
movq 0x18(%rsp), %rax
leaq (%r15,%rax,8), %rax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rax
incq %rax
jmp 0x2a2a
movq (%rsp), %rbx
cmpq %rbp, %rbx
je 0x2b4f
movq %rbx, %rdi
callq 0x22c0
addq $0x8, %rbx
jmp 0x2b3c
movq %rsp, %rdi
callq 0x49b8
leaq 0x28(%rsp), %rdi
callq 0x49da
callq 0x3c5d
movsd %xmm0, 0x20(%rsp)
callq 0x2030
movq 0x30(%rsp), %rcx
movq 0x30(%r13), %rdx
imulq 0x28(%r13), %rdx
cmpq %rdx, %rcx
jne 0x2c6e
subq 0x38(%rsp), %rax
cvtsi2sd %rax, %xmm0
divsd 0x246e(%rip), %xmm0     # 0x5008
movsd %xmm0, 0x18(%rsp)
leaq 0x58(%rsp), %rsi
movsd %xmm0, (%rsi)
movsd 0x20(%rsp), %xmm0
subsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x26a9(%rip), %rdi      # 0x526b
callq 0x3cf6
movsd 0x18(%rsp), %xmm2
mulsd 0x2433(%rip), %xmm2     # 0x5008
movsd 0x30(%r13), %xmm0
unpcklps 0x243e(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2446(%rip), %xmm0     # 0x5030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
movq %rsp, %rbx
movsd %xmm0, (%rbx)
leaq 0x2672(%rip), %rdi      # 0x527e
movq %rbx, %rsi
callq 0x3cf6
movsd 0x20(%rsp), %xmm2
mulsd 0x23e6(%rip), %xmm2     # 0x5008
movsd 0x30(%r13), %xmm0
unpcklps 0x23f1(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x23f9(%rip), %xmm0     # 0x5030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rbx)
leaq 0x2641(%rip), %rdi      # 0x5297
movq %rsp, %rsi
callq 0x3cf6
movq 0x535b(%rip), %rdi      # 0x7fc0
callq 0x20d0
xorl %eax, %eax
jmp 0x2c8c
movq 0x537b(%rip), %rdi      # 0x7ff0
leaq 0x25e2(%rip), %rsi      # 0x525e
callq 0x21e0
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5326(%rip), %rax      # 0x7fc8
movq (%rax), %rdi
leaq 0x23ce(%rip), %rsi      # 0x507a
leaq 0x23d8(%rip), %rdx      # 0x508b
leaq 0x2570(%rip), %r8       # 0x522a
movl $0xa6, %ecx
xorl %eax, %eax
callq 0x2250
callq 0x2100
movq %rax, %rdi
callq 0x2070
testq %r15, %r15
jne 0x2ce2
movq %r13, %rdi
callq 0x4c24
jmp 0x2ced
movq %r15, %rdi
movq %r15, %rsi
callq 0x4c32
movq %rsp, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
callq 0x4b8c
callq 0x2270
movq %rax, %rbx
callq 0x22b0
jmp 0x2d19
movq %rax, %rdi
callq 0x475f
jmp 0x2d16
movq %rax, %rbx
movq %rsp, %rdi
callq 0x49b8
leaq 0x28(%rsp), %rdi
callq 0x49da
movq %rbx, %rdi
callq 0x22e0