// File: /workspace/github_cpp_makefile_star2_O1/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 0x6341(%rip), %r13      # 0x8fc0
leaq 0x344f(%rip), %rsi      # 0x60d5
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 0x33de(%rip), %rsi      # 0x60da
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 0x3386(%rip), %rsi      # 0x60e2
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 0x4613
movsd %xmm0, 0x60(%rsp)
movl $0x20, %edi
callq 0x21c0
movq %rbx, (%rax)
leaq 0x2408(%rip), %rcx      # 0x51f2
movq %rcx, 0x8(%rax)
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
callq 0x5186
movq %rbx, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
cmpq %rbx, 0x20(%rbp)
je 0x2f52
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %rbp, 0x68(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rbp, 0x48(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x18(%rsp), %r13
cmpq 0x20(%rsp), %r13
je 0x2e65
movq %r13, %rdi
leaq 0x38(%rsp), %rsi
callq 0x4684
addq $0x8, 0x18(%rsp)
jmp 0x2f40
movq 0x10(%rsp), %r15
movq %r13, %r12
subq %r15, %r12
movabsq $0x7ffffffffffffff8, %rax # imm = 0x7FFFFFFFFFFFFFF8
cmpq %rax, %r12
je 0x31da
sarq $0x3, %r12
cmpq $0x1, %r12
movq %r12, %rax
adcq $0x0, %rax
leaq (%rax,%r12), %rbp
movabsq $0xfffffffffffffff, %rcx # imm = 0xFFFFFFFFFFFFFFF
cmpq %rcx, %rbp
cmovaeq %rcx, %rbp
addq %r12, %rax
cmovbq %rcx, %rbp
testq %rbp, %rbp
movq %rbp, (%rsp)
je 0x2ec8
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x52b8
jmp 0x2eca
xorl %eax, %eax
leaq (%rax,%r12,8), %r12
movq %rax, %rbp
movq %r12, %rdi
leaq 0x38(%rsp), %rsi
callq 0x4684
movq %rbp, %r12
cmpq %r13, %r15
movq %rbp, %rdx
je 0x2f0a
movq %rdx, %r12
movq %r15, %rax
movq %r14, (%r12)
movq (%rax), %rcx
movq %rcx, (%r12)
movq %r14, (%rax)
addq $0x8, %rax
addq $0x8, %r12
cmpq %r13, %rax
jne 0x2eef
testq %r15, %r15
movq (%rsp), %rbp
je 0x2f29
movq 0x20(%rsp), %rsi
subq %r15, %rsi
movq %rdx, %r13
movq %r15, %rdi
callq 0x21d0
movq %r13, %rdx
addq $0x8, %r12
movq %rdx, 0x10(%rsp)
movq %r12, 0x18(%rsp)
leaq (%rdx,%rbp,8), %rax
movq %rax, 0x20(%rsp)
incq %rbx
movq 0x68(%rsp), %rbp
cmpq 0x20(%rbp), %rbx
jne 0x2e1e
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0x2f6f
movq %rbx, %rdi
callq 0x2290
addq $0x8, %rbx
jmp 0x2f5c
leaq 0x10(%rsp), %rdi
callq 0x5114
leaq 0x30(%rsp), %rdi
callq 0x5150
callq 0x4613
movsd %xmm0, (%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 0x304a(%rip), %xmm0     # 0x6008
movsd %xmm0, 0x8(%rsp)
movsd (%rsp), %xmm0
subsd 0x60(%rsp), %xmm0
movsd %xmm0, (%rsp)
leaq 0x3293(%rip), %rsi      # 0x626e
movl $0x12, %edx
movq 0x5fd9(%rip), %r13      # 0x8fc0
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 0x2fab(%rip), %xmm2     # 0x6008
movsd 0x30(%rbp), %xmm0
unpcklps 0x2fb7(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2fbf(%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      # 0x6281
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 (%rsp), %xmm2
mulsd 0x2f12(%rip), %xmm2     # 0x6008
movsd 0x30(%rbp), %xmm0
unpcklps 0x2f1e(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2f26(%rip), %xmm0     # 0x6030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, (%rsp)
leaq 0x3174(%rip), %rsi      # 0x629a
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 (%rsp), %xmm0
callq 0x2270
xorl %ebx, %ebx
jmp 0x31a1
movq 0x5e68(%rip), %r13      # 0x8ff0
leaq 0x30d2(%rip), %rsi      # 0x6261
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 0x3304(%rip), %rdi      # 0x64e5
callq 0x20c0
callq 0x239e
movq %rax, %rdi
callq 0x2080
testq %rbp, %rbp
jne 0x3204
cmpq $0x0, (%r12)
je 0x3214
callq 0x2060
movq %rbp, %rdi
movq (%rsp), %rsi
shlq $0x3, %rsi
callq 0x21d0
callq 0x2240
movq %rax, %rbx
callq 0x2280
jmp 0x3232
movq %rax, %rdi
callq 0x4eae
jmp 0x322f
jmp 0x322f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5114
leaq 0x30(%rsp), %rdi
callq 0x5150
movq %rbx, %rdi
callq 0x22b0