// File: /workspace/github_cpp_makefile_star2_O3/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, 0x70(%rsp)
imulq %rsi, %rax
cmpq %rcx, %rax
jne 0x31e6
movq %rdi, %rbp
movq 0x6343(%rip), %r13      # 0x8fc8
leaq 0x3446(%rip), %rsi      # 0x60d2
movl $0x4, %edx
movq %r13, %rdi
callq 0x21f0
movb $0x3d, %al
leaq 0x38(%rsp), %rbx
movb %al, (%rbx)
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x21f0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movl $0x11, %r15d
movq %r15, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rdx
movl $0x3, %r12d
movq %r12, 0x8(%rax,%rdx)
movq -0x18(%rcx), %rcx
movl $0xffffff4f, %r14d      # imm = 0xFFFFFF4F
movl 0x18(%rax,%rcx), %edx
andl %r14d, %edx
orl $0x20, %edx
movl %edx, 0x18(%rax,%rcx)
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
movq %rax, %rdi
callq 0x21f0
leaq 0x33d5(%rip), %rsi      # 0x60d7
movl $0x7, %edx
movq %r13, %rdi
callq 0x21f0
movb $0x3d, %al
movb %al, (%rbx)
leaq 0x38(%rsp), %rbx
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x21f0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq %r15, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rdx
movq %r12, 0x8(%rax,%rdx)
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl %r14d, %edx
orl $0x20, %edx
movl %edx, 0x18(%rax,%rcx)
movq 0x20(%rbp), %rsi
movq %rax, %rdi
callq 0x2120
leaq 0x337d(%rip), %rsi      # 0x60df
movl $0xe, %edx
movq %r13, %rdi
callq 0x21f0
movb $0x3d, %al
movb %al, (%rbx)
leaq 0x38(%rsp), %rsi
movl $0x1, %edx
movq %r13, %rdi
callq 0x21f0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq %r15, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rdx
movq %r12, 0x8(%rax,%rdx)
movq -0x18(%rcx), %rcx
andl 0x18(%rax,%rcx), %r14d
orl $0x20, %r14d
movl %r14d, 0x18(%rax,%rcx)
movq 0x28(%rbp), %rsi
movq %rax, %rdi
callq 0x2120
movq %r13, %rdi
callq 0x2130
xorl %ebx, %ebx
movq %rbx, 0x58(%rsp)
callq 0x2040
movq %rax, 0x8(%rsp)
callq 0x4605
movsd %xmm0, 0x60(%rsp)
movl $0x20, %edi
callq 0x21c0
movq %rbx, (%rax)
leaq 0x23b4(%rip), %rcx      # 0x51a4
movq %rcx, 0x8(%rax)
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
callq 0x5138
movq %rbx, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
cmpq %rbx, 0x20(%rbp)
je 0x2f70
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %rbp, 0x68(%rsp)
leaq 0x70(%rsp), %rcx
movq %rcx, 0x38(%rsp)
leaq 0x18(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movq %rbp, 0x48(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq 0x30(%rsp), %r12
je 0x2e6f
movq %r12, %rdi
leaq 0x38(%rsp), %rsi
callq 0x4676
movq 0x28(%rsp), %r12
addq $0x8, %r12
movq %r12, 0x28(%rsp)
jmp 0x2f3c
movq 0x20(%rsp), %rbp
movq %r12, %r13
subq %rbp, %r13
movabsq $0x7ffffffffffffff8, %rax # imm = 0x7FFFFFFFFFFFFFF8
cmpq %rax, %r13
je 0x31da
sarq $0x3, %r13
cmpq $0x1, %r13
movq %r13, %rax
adcq $0x0, %rax
leaq (%rax,%r13), %r15
movabsq $0xfffffffffffffff, %rcx # imm = 0xFFFFFFFFFFFFFFF
cmpq %rcx, %r15
cmovaeq %rcx, %r15
addq %r13, %rax
cmovbq %rcx, %r15
testq %r15, %r15
je 0x2ed1
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x526a
movq %rax, %rbx
jmp 0x2ed3
xorl %ebx, %ebx
leaq (%rbx,%r13,8), %r13
movq %r13, %rdi
leaq 0x38(%rsp), %rsi
callq 0x4676
movq %rbx, %r13
cmpq %r12, %rbp
je 0x2f0d
movq %rbx, %r13
movq %rbp, %rax
movq (%rax), %rcx
movq %rcx, (%r13)
movq $0x0, (%rax)
addq $0x8, %rax
addq $0x8, %r13
cmpq %r12, %rax
jne 0x2ef2
testq %rbp, %rbp
je 0x2f22
movq 0x30(%rsp), %rsi
subq %rbp, %rsi
movq %rbp, %rdi
callq 0x21d0
addq $0x8, %r13
movq %rbx, 0x20(%rsp)
movq %r13, 0x28(%rsp)
leaq (%rbx,%r15,8), %rax
movq %rax, 0x30(%rsp)
movq %r13, %r12
incq %r14
movq 0x68(%rsp), %rbp
cmpq 0x20(%rbp), %r14
jne 0x2e25
movq 0x20(%rsp), %rbx
cmpq %r12, %rbx
movq 0x606b(%rip), %r13      # 0x8fc8
je 0x2f70
movq %rbx, %rdi
callq 0x2290
addq $0x8, %rbx
cmpq %r12, %rbx
jne 0x2f5f
leaq 0x20(%rsp), %rdi
callq 0x50c6
leaq 0x18(%rsp), %rdi
callq 0x5102
callq 0x4605
movsd %xmm0, 0x10(%rsp)
callq 0x2040
movq 0x58(%rsp), %rcx
movq 0x30(%rbp), %rdx
imulq 0x28(%rbp), %rdx
cmpq %rdx, %rcx
jne 0x3181
subq 0x8(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd 0x3048(%rip), %xmm0     # 0x6008
movsd %xmm0, 0x8(%rsp)
movsd 0x10(%rsp), %xmm0
subsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x328c(%rip), %rsi      # 0x626b
movl $0x12, %edx
movq %r13, %rdi
callq 0x21f0
movb $0x3d, %al
leaq 0x38(%rsp), %rbx
movb %al, (%rbx)
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x21f0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movl $0x11, %r15d
movq %r15, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rdx
movl $0x3, %r12d
movq %r12, 0x8(%rax,%rdx)
movq -0x18(%rcx), %rcx
movl $0xffffff4f, %r14d      # imm = 0xFFFFFF4F
movl 0x18(%rax,%rcx), %edx
andl %r14d, %edx
orl $0x20, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x2270
movsd 0x8(%rsp), %xmm2
mulsd 0x2fae(%rip), %xmm2     # 0x6008
movsd 0x30(%rbp), %xmm0
unpcklps 0x2fba(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2fc2(%rip), %xmm0     # 0x6030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
leaq 0x31f3(%rip), %rsi      # 0x627e
movl $0x18, %edx
movq %r13, %rdi
callq 0x21f0
movb $0x3d, %al
movb %al, (%rbx)
leaq 0x38(%rsp), %rbx
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x21f0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq %r15, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rdx
movq %r12, 0x8(%rax,%rdx)
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl %r14d, %edx
orl $0x20, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x2270
movsd 0x10(%rsp), %xmm2
mulsd 0x2f14(%rip), %xmm2     # 0x6008
movsd 0x30(%rbp), %xmm0
unpcklps 0x2f20(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2f28(%rip), %xmm0     # 0x6030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
leaq 0x3172(%rip), %rsi      # 0x6297
movl $0x17, %edx
movq %r13, %rdi
callq 0x21f0
movb $0x3d, %al
movb %al, (%rbx)
leaq 0x38(%rsp), %rsi
movl $0x1, %edx
movq %r13, %rdi
callq 0x21f0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq %r15, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rdx
movq %r12, 0x8(%rax,%rdx)
movq -0x18(%rcx), %rcx
andl 0x18(%rax,%rcx), %r14d
orl $0x20, %r14d
movl %r14d, 0x18(%rax,%rcx)
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x2270
xorl %ebx, %ebx
jmp 0x31a1
movq 0x5e68(%rip), %r13      # 0x8ff0
leaq 0x30cf(%rip), %rsi      # 0x625e
movl $0xc, %edx
movq %r13, %rdi
callq 0x21f0
movl $0x1, %ebx
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3301(%rip), %rdi      # 0x64e2
callq 0x20c0
callq 0x239e
movq %rax, %rdi
callq 0x2080
testq %rbx, %rbx
jne 0x3204
cmpq $0x0, (%r13)
je 0x3213
callq 0x2060
shlq $0x3, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x21d0
callq 0x2240
movq %rax, %rbx
callq 0x2280
jmp 0x3231
movq %rax, %rdi
callq 0x4e93
jmp 0x322e
jmp 0x322e
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x50c6
leaq 0x18(%rsp), %rdi
callq 0x5102
movq %rbx, %rdi
callq 0x22b0