// File: /workspace/github_cpp_makefile_star2_O1/kaityo256[P]cps/cps.cpp
// Function: manager(int)
void manager(const int procs) {
  const int num_tasks = command_list.size();
  assign_list.resize(procs, -1);
  start_time.resize(procs);
  ellapsed_time.resize(num_tasks);
  int task_index = 0;
  auto timer_start = std::chrono::system_clock::now();
  // Distribute Tasks
  while (task_index < num_tasks) {
    MPI_Status st;
    int isReady = 0;
    MPI_Recv(&isReady, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &st);
    int i = st.MPI_SOURCE;
    if (assign_list[i] != -1) {
      auto start = start_time[i];
      double elapsed = get_time(start);
      ellapsed_time[assign_list[i]] = elapsed;
      debug_printf("task %d assigned to %d is finished at %f\n", task_index, i, get_time(timer_start));
      assign_list[i] = -1;
    }
    // Assign task_index-th task to the i-th process
    assign_list[i] = task_index;
    start_time[i] = std::chrono::system_clock::now();
    debug_printf("task %d is assignd to %d at %f\n", task_index, i, get_time(timer_start));
    int len = command_list[task_index].length() + 1;
    MPI_Send(&len, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
    MPI_Send(command_list[task_index].data(), len, MPI_CHAR, i, 0, MPI_COMM_WORLD);
    task_index++;
  }
  // Complete Notification
  int finish_check = procs - 1;

  while (finish_check > 0) {
    MPI_Status st;
    int dummy = 0;
    int recv = 0;
    MPI_Recv(&recv, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &st);
    int i = st.MPI_SOURCE;
    MPI_Send(&dummy, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
    finish_check--;
    if (assign_list[i] != -1) {
      auto start = start_time[i];
      double elapsed = get_time(start);
      ellapsed_time[assign_list[i]] = elapsed;
      debug_printf("task %d assigned to %d is finished at %f\n", task_index, i, get_time(timer_start));
      assign_list[i] = -1;
    }
  }
  // Generate Log
  std::ofstream ofs("cps.log");
  ofs << "Number of tasks : " << num_tasks << std::endl;
  ofs << "Number of processes : " << procs << std::endl;
  double total = std::accumulate(ellapsed_time.begin(), ellapsed_time.end(), 0.0);
  ofs << "Total execution time: " << total << " [s]" << std::endl;
  double elapsed = get_time(timer_start);
  ofs << "Elapsed time: " << elapsed << " [s]" << std::endl;
  double eff = total / (elapsed * (procs - 1));
  ofs << "Parallel Efficiency : " << eff << std::endl;
  ofs << std::endl;
  ofs << "Task list:" << std::endl;
  ofs << "Command : Elapsed time" << std::endl;
  for (int i = 0; i < num_tasks; i++) {
    ofs << command_list[i] << " : " << ellapsed_time[i] << " [s]" << std::endl;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq 0x3bf9(%rip), %rbx      # 0x61a0
subq 0x3bea(%rip), %rbx      # 0x6198
shrq $0x5, %rbx
movl %edi, -0x2c(%rbp)
movslq %edi, %r14
leaq -0x250(%rbp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x3be4(%rip), %rdi      # 0x61b0
movq %r14, %rsi
callq 0x3128
leaq 0x3bed(%rip), %rdi      # 0x61c8
movq %r14, %rsi
callq 0x3162
movq %rbx, -0x40(%rbp)
movslq %ebx, %r14
leaq 0x3bef(%rip), %rdi      # 0x61e0
movq %r14, %rsi
callq 0x319a
callq 0x2050
movq %rax, -0x50(%rbp)
testl %r14d, %r14d
jle 0x2761
movq -0x40(%rbp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x48(%rbp)
xorl %r14d, %r14d
movq 0x39a6(%rip), %rbx      # 0x5fc8
movq 0x3977(%rip), %r15      # 0x5fa0
xorl %r13d, %r13d
movl $0x0, -0x34(%rbp)
leaq -0x250(%rbp), %rax
movq %rax, (%rsp)
leaq -0x34(%rbp), %rdi
movl $0x1, %esi
movq %rbx, %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movq %r15, %r9
callq 0x2130
movslq -0x250(%rbp), %r12
movq 0x3b48(%rip), %rax      # 0x61b0
cmpl $-0x1, (%rax,%r12,4)
je 0x26e2
movq 0x3b52(%rip), %rax      # 0x61c8
movq %rbx, %r15
movq (%rax,%r12,8), %rbx
callq 0x2050
subq %rbx, %rax
movq %r15, %rbx
movq 0x3911(%rip), %r15      # 0x5fa0
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
cvtsi2sd %rdx, %xmm0
divsd 0x1951(%rip), %xmm0     # 0x4008
movq 0x3af2(%rip), %rax      # 0x61b0
movslq (%rax,%r12,4), %rax
movq 0x3b17(%rip), %rcx      # 0x61e0
movsd %xmm0, (%rcx,%rax,8)
callq 0x2050
movq 0x3ad6(%rip), %rax      # 0x61b0
movl $0xffffffff, (%rax,%r12,4) # imm = 0xFFFFFFFF
movq 0x3ac7(%rip), %rax      # 0x61b0
movl %r13d, (%rax,%r12,4)
callq 0x2050
movq 0x3acf(%rip), %rcx      # 0x61c8
movq %rax, (%rcx,%r12,8)
callq 0x2050
movq 0x3a8f(%rip), %rax      # 0x6198
movl 0x8(%rax,%r14), %eax
incl %eax
movl %eax, -0x30(%rbp)
leaq -0x30(%rbp), %rdi
movl $0x1, %esi
movq %rbx, %rdx
movl %r12d, %ecx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x2090
movq 0x3a64(%rip), %rax      # 0x6198
movq (%rax,%r14), %rdi
movl -0x30(%rbp), %esi
movq 0x384e(%rip), %rdx      # 0x5f90
movl %r12d, %ecx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x2090
incq %r13
addq $0x20, %r14
cmpq %r13, -0x48(%rbp)
jne 0x262c
cmpl $0x2, -0x2c(%rbp)
jl 0x2854
movq 0x3856(%rip), %r14      # 0x5fc8
movq 0x3827(%rip), %r15      # 0x5fa0
movl -0x2c(%rbp), %r13d
xorl %eax, %eax
movl %eax, -0x34(%rbp)
movl %eax, -0x30(%rbp)
leaq -0x250(%rbp), %rax
movq %rax, (%rsp)
leaq -0x30(%rbp), %rdi
movl $0x1, %esi
movq %r14, %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movq %r15, %r9
callq 0x2130
movl -0x250(%rbp), %r12d
leaq -0x34(%rbp), %rdi
movl $0x1, %esi
movq %r14, %rdx
movl %r12d, %ecx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x2090
movslq %r12d, %r12
movq 0x39d9(%rip), %rax      # 0x61b0
cmpl $-0x1, (%rax,%r12,4)
je 0x2847
movq 0x39e3(%rip), %rax      # 0x61c8
movq (%rax,%r12,8), %rbx
callq 0x2050
subq %rbx, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
divsd 0x17ec(%rip), %xmm0     # 0x4008
movq 0x398d(%rip), %rax      # 0x61b0
movslq (%rax,%r12,4), %rax
movq 0x39b2(%rip), %rcx      # 0x61e0
movsd %xmm0, (%rcx,%rax,8)
callq 0x2050
movq 0x3971(%rip), %rax      # 0x61b0
movl $0xffffffff, (%rax,%r12,4) # imm = 0xFFFFFFFF
decl %r13d
cmpl $0x1, %r13d
jg 0x277d
leaq 0x17c5(%rip), %rsi      # 0x4020
leaq -0x250(%rbp), %r14
movq %r14, %rdi
movl $0x10, %edx
callq 0x21b0
leaq 0x17b2(%rip), %rsi      # 0x4028
movl $0x12, %edx
movq %r14, %rdi
callq 0x21c0
leaq -0x250(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x2290
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20f0
leaq 0x1777(%rip), %rsi      # 0x403b
leaq -0x250(%rbp), %rdi
movl $0x16, %edx
callq 0x21c0
leaq -0x250(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x2290
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20f0
movq 0x38cb(%rip), %rax      # 0x61e0
movq 0x38cc(%rip), %rcx      # 0x61e8
xorpd %xmm0, %xmm0
cmpq %rcx, %rax
je 0x292f
addsd (%rax), %xmm0
addq $0x8, %rax
jmp 0x2920
movsd %xmm0, -0x48(%rbp)
leaq 0x1717(%rip), %rsi      # 0x4052
leaq -0x250(%rbp), %rdi
movl $0x16, %edx
callq 0x21c0
leaq -0x250(%rbp), %rdi
movsd -0x48(%rbp), %xmm0
callq 0x2250
movq %rax, %r14
leaq 0x1702(%rip), %rsi      # 0x4069
movl $0x4, %edx
movq %rax, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20f0
callq 0x2050
subq -0x50(%rbp), %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %r14
leaq 0x16b3(%rip), %rsi      # 0x406e
leaq -0x250(%rbp), %rdi
movl $0xe, %edx
callq 0x21c0
movq %r14, %rax
shrq $0x3f, %rax
sarq $0x12, %r14
addq %rax, %r14
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
divsd 0x161e(%rip), %xmm0     # 0x4008
leaq -0x250(%rbp), %rdi
movsd %xmm0, -0x50(%rbp)
callq 0x2250
movq %rax, %r14
leaq 0x1664(%rip), %rsi      # 0x4069
movl $0x4, %edx
movq %rax, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20f0
leaq 0x163d(%rip), %rsi      # 0x407d
leaq -0x250(%rbp), %rdi
movl $0x16, %edx
callq 0x21c0
movl -0x2c(%rbp), %eax
decl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd -0x50(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd -0x48(%rbp), %xmm0
divsd %xmm1, %xmm0
leaq -0x250(%rbp), %r14
movq %r14, %rdi
callq 0x2250
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20f0
movq -0x250(%rbp), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
leaq -0x250(%rbp), %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20f0
leaq 0x15b6(%rip), %rsi      # 0x4094
leaq -0x250(%rbp), %r14
movl $0xa, %edx
movq %r14, %rdi
callq 0x21c0
movq -0x250(%rbp), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
leaq -0x250(%rbp), %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20f0
leaq 0x1577(%rip), %rsi      # 0x409f
leaq -0x250(%rbp), %r14
movl $0x16, %edx
movq %r14, %rdi
callq 0x21c0
movq -0x250(%rbp), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
leaq -0x250(%rbp), %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20f0
cmpl $0x0, -0x40(%rbp)
jle 0x2c21
movq -0x40(%rbp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x40(%rbp)
xorl %ebx, %ebx
leaq -0x250(%rbp), %r14
leaq 0x14bc(%rip), %r15      # 0x404e
leaq 0x14d0(%rip), %r12      # 0x4069
movq 0x35f8(%rip), %rax      # 0x6198
movq (%rax,%rbx,4), %rsi
movq 0x8(%rax,%rbx,4), %rdx
movq %r14, %rdi
callq 0x21c0
movq %rax, %r13
movl $0x3, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21c0
movq 0x3615(%rip), %rax      # 0x61e0
movsd (%rax,%rbx), %xmm0
movq %r13, %rdi
callq 0x2250
movq %rax, %r13
movl $0x4, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x21c0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20f0
addq $0x8, %rbx
decq -0x40(%rbp)
jne 0x2b99
movq 0x3390(%rip), %rax      # 0x5fb8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x248(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x250(%rbp,%rcx)
callq 0x2150
leaq -0x158(%rbp), %rdi
callq 0x2080
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c71
jmp 0x2c71
jmp 0x2c71
jmp 0x2c71
movq %rax, %rbx
leaq -0x250(%rbp), %rdi
callq 0x2230
movq %rbx, %rdi
callq 0x22a0