// File: /workspace/github_cpp_makefile_star2_O2/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 $0x248, %rsp            # imm = 0x248
movq 0x3cba(%rip), %r15      # 0x6180
subq 0x3cab(%rip), %r15      # 0x6178
shrq $0x5, %r15
movq %rdi, -0x38(%rbp)
movslq %edi, %rbx
leaq -0x260(%rbp), %rdx
orl $-0x1, (%rdx)
leaq 0x3ca7(%rip), %rdi      # 0x6190
movq %rbx, %rsi
callq 0x2d1c
leaq 0x3cb0(%rip), %rdi      # 0x61a8
movq %rbx, %rsi
callq 0x2d56
movslq %r15d, %r14
leaq 0x3cb6(%rip), %rdi      # 0x61c0
movq %r14, %rsi
callq 0x2d8e
callq 0x2040
movq %rax, -0x48(%rbp)
xorl %ebx, %ebx
testl %r14d, %r14d
movl $0x0, %eax
movq %r15, -0x60(%rbp)
cmovgl %r15d, %eax
movq %rax, -0x58(%rbp)
movq 0x3a90(%rip), %r15      # 0x5fc8
xorl %r13d, %r13d
cmpq %r13, -0x58(%rbp)
je 0x2644
andl $0x0, -0x3c(%rbp)
leaq -0x260(%rbp), %rax
movq %rax, (%rsp)
leaq -0x3c(%rbp), %rdi
pushq $0x1
popq %rsi
movq %r15, %r14
movq %r15, %rdx
pushq $-0x1
popq %rcx
xorl %r8d, %r8d
movq 0x3a3a(%rip), %r9       # 0x5fa8
callq 0x2110
movslq -0x260(%rbp), %r12
movq 0x3c0f(%rip), %rax      # 0x6190
cmpl $-0x1, (%rax,%r12,4)
movq -0x48(%rbp), %r15
je 0x25c7
movq 0x3c15(%rip), %rax      # 0x61a8
movq (%rax,%r12,8), %rdi
callq 0x247c
movq 0x3bed(%rip), %rax      # 0x6190
movslq (%rax,%r12,4), %rax
movq 0x3c12(%rip), %rcx      # 0x61c0
movsd %xmm0, (%rcx,%rax,8)
movq %r15, %rdi
callq 0x247c
movq 0x3bce(%rip), %rax      # 0x6190
orl $-0x1, (%rax,%r12,4)
movl %r13d, (%rax,%r12,4)
callq 0x2040
movq 0x3bd1(%rip), %rcx      # 0x61a8
movq %rax, (%rcx,%r12,8)
movq %r15, %rdi
callq 0x247c
movq 0x3b8e(%rip), %rax      # 0x6178
movl 0x8(%rax,%rbx), %eax
incl %eax
movl %eax, -0x2c(%rbp)
leaq -0x2c(%rbp), %rdi
pushq $0x1
popq %rsi
movq %r14, %r15
movq %r14, %rdx
movl %r12d, %ecx
xorl %r8d, %r8d
movq 0x399b(%rip), %r14      # 0x5fa8
movq %r14, %r9
callq 0x2080
movq 0x3b5c(%rip), %rax      # 0x6178
movq (%rax,%rbx), %rdi
movl -0x2c(%rbp), %esi
movq 0x396e(%rip), %rdx      # 0x5f98
movl %r12d, %ecx
xorl %r8d, %r8d
movq %r14, %r9
callq 0x2080
incq %r13
addq $0x20, %rbx
jmp 0x253b
movq -0x38(%rbp), %rax
leal -0x1(%rax), %ebx
movq 0x3976(%rip), %r14      # 0x5fc8
movq 0x394f(%rip), %r15      # 0x5fa8
pushq $0x1
popq %r12
movl %ebx, -0x4c(%rbp)
testl %ebx, %ebx
jle 0x2706
andl $0x0, -0x3c(%rbp)
andl $0x0, -0x2c(%rbp)
leaq -0x260(%rbp), %rax
movq %rax, (%rsp)
leaq -0x2c(%rbp), %rdi
movl %r12d, %esi
movq %r14, %rdx
pushq $-0x1
popq %rcx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x2110
movl -0x260(%rbp), %r13d
leaq -0x3c(%rbp), %rdi
movl %r12d, %esi
movq %r14, %rdx
movl %r13d, %ecx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x2080
movslq %r13d, %r13
movq 0x3ad4(%rip), %rax      # 0x6190
cmpl $-0x1, (%rax,%r13,4)
je 0x26ff
movq 0x3ade(%rip), %rax      # 0x61a8
movq (%rax,%r13,8), %rdi
callq 0x247c
movq 0x3ab6(%rip), %rax      # 0x6190
movslq (%rax,%r13,4), %rax
movq 0x3adb(%rip), %rcx      # 0x61c0
movsd %xmm0, (%rcx,%rax,8)
movq -0x48(%rbp), %rdi
callq 0x247c
movq 0x3a96(%rip), %rax      # 0x6190
orl $-0x1, (%rax,%r13,4)
decl %ebx
jmp 0x2660
leaq 0x1913(%rip), %rsi      # 0x4020
leaq -0x260(%rbp), %r14
pushq $0x10
popq %rdx
movq %r14, %rdi
callq 0x2180
leaq 0x1902(%rip), %rsi      # 0x4028
movq %r14, %rdi
callq 0x2160
movq %rax, %rdi
movq -0x60(%rbp), %rsi
callq 0x2270
movq %rax, %rdi
callq 0x20d0
leaq 0x18f2(%rip), %rsi      # 0x403b
leaq -0x260(%rbp), %rdi
callq 0x2160
movq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x2270
movq %rax, %rdi
callq 0x20d0
movq 0x3a50(%rip), %rax      # 0x61c0
xorpd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movq 0x3a48(%rip), %rcx      # 0x61c8
movq -0x48(%rbp), %rbx
cmpq %rcx, %rax
je 0x279d
movsd -0x38(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
addq $0x8, %rax
jmp 0x2784
leaq 0x18ae(%rip), %rsi      # 0x4052
leaq -0x260(%rbp), %rdi
callq 0x2160
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x2230
leaq 0x18a5(%rip), %rsi      # 0x4069
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x247c
movsd %xmm0, -0x48(%rbp)
leaq 0x1886(%rip), %rsi      # 0x406e
leaq -0x260(%rbp), %rdi
callq 0x2160
movq %rax, %rdi
movsd -0x48(%rbp), %xmm0
callq 0x2230
leaq 0x1861(%rip), %rsi      # 0x4069
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20d0
leaq 0x185e(%rip), %rsi      # 0x407d
leaq -0x260(%rbp), %rdi
callq 0x2160
cvtsi2sdl -0x4c(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd -0x38(%rbp), %xmm0
divsd %xmm1, %xmm0
movq %rax, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x20d0
leaq -0x260(%rbp), %rdi
callq 0x20d0
leaq 0x182f(%rip), %rsi      # 0x4094
leaq -0x260(%rbp), %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20d0
leaq 0x181f(%rip), %rsi      # 0x409f
leaq -0x260(%rbp), %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20d0
xorl %r12d, %r12d
leaq -0x260(%rbp), %rbx
leaq 0x17a9(%rip), %r14      # 0x404e
leaq 0x17bd(%rip), %r15      # 0x4069
xorl %r13d, %r13d
cmpq %r13, -0x58(%rbp)
je 0x2903
movq 0x38bc(%rip), %rsi      # 0x6178
addq %r12, %rsi
movq %rbx, %rdi
callq 0x2140
movq %rax, %rdi
movq %r14, %rsi
callq 0x2160
movq 0x38e7(%rip), %rcx      # 0x61c0
movsd (%rcx,%r13,8), %xmm0
movq %rax, %rdi
callq 0x2230
movq %rax, %rdi
movq %r15, %rsi
callq 0x2160
movq %rax, %rdi
callq 0x20d0
incq %r13
addq $0x20, %r12
jmp 0x28af
leaq -0x260(%rbp), %rdi
callq 0x2210
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2929
jmp 0x2929
jmp 0x2929
jmp 0x2929
movq %rax, %rbx
leaq -0x260(%rbp), %rdi
callq 0x2210
movq %rbx, %rdi
callq 0x2280