// File: /workspace/github_cpp_makefile_star2_O2/WeihuaZhu[P]hpc_prog2/mpi_jacobi.cpp
// Function: gather_vector(int, double*, double*, ompi_communicator_t*)
void gather_vector(const int n, double* local_vector, double* output_vector, MPI_Comm comm)
{
    // obtain the necessary parameters required in thie method
    int rank;
    MPI_Comm_rank(comm, &rank);
    int p;
    MPI_Comm_size(comm, &p);
    int q = sqrt(p);
    int dims[2];
    int coords[2];
    int periods[2];
    MPI_Cart_get(comm, 2, dims, periods, coords);
    int sourceRank;
    int sourceCoord[2] = {0, 0};
    MPI_Cart_rank(comm, sourceCoord, &sourceRank);

    //initialization of sendcounts and displs
    int *count_first_col = (int*) malloc(sizeof(int) * p);
    int *displs = (int*) malloc(sizeof(int) * p);
    if (rank == sourceRank) {
        for (int i = 0; i < q; i++) {
            for (int j = 0; j < q; j++) {
                if (j != 0) {
                    count_first_col[i * q + j] = 0;
                } else {
                    count_first_col[i * q + j] = block_decompose(n, q, i);
                }
                if (i == 0 && j == 0) {
                    displs[0] = 0;
                } else {
                    displs[i * q + j] = displs[i * q + j - 1] + count_first_col[i * q + j - 1];
                }
            }
        }
    }

    // perform the gather operation using Gatherv, and store the result in output_vector
    int sendcounts = 0;
    if (coords[1] == 0) {
        sendcounts = block_decompose(n, q, coords[0]);
    }
    MPI_Gatherv(local_vector, sendcounts, MPI_DOUBLE, output_vector, count_first_col, displs, MPI_DOUBLE, sourceRank, comm);

    // free the useless memory
    free(count_first_col);
    free(displs);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movl %edi, %ebp
leaq 0x4(%rsp), %rsi
movq %rcx, %rdi
callq 0x67d0
movq %rsp, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6600
cvtsi2sdl (%r12), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq %r14, 0x20(%rsp)
movq %r15, 0x18(%rsp)
jb 0x1d616
sqrtsd %xmm0, %xmm0
jmp 0x1d61b
callq 0x63b0
cvttsd2si %xmm0, %r15d
pushq $0x2
popq %rsi
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x8(%rsp), %r8
movq %rbx, %rdi
callq 0x6430
leaq 0x40(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0x3c(%rsp), %r14
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
movq %r14, %rdx
callq 0x6a10
movslq (%rsp), %r13
shlq $0x2, %r13
movq %r13, %rdi
callq 0x6750
movq %rax, %r12
movq %r13, %rdi
callq 0x6750
movq %rax, %r13
movl (%r14), %eax
movq %rax, 0x10(%rsp)
cmpl %eax, 0x4(%rsp)
jne 0x1d711
movslq %r15d, %rax
xorl %esi, %esi
testl %eax, %eax
movl $0x0, %edi
cmovgl %r15d, %edi
movl %eax, %r8d
movq %rax, 0x30(%rsp)
leaq (,%rax,4), %r9
movq %r13, %r11
movq %r12, %r14
cmpq %rdi, %rsi
je 0x1d711
movq %rsi, %r10
imulq 0x30(%rsp), %r10
xorl %ebx, %ebx
cmpq %rbx, %r8
je 0x1d706
testq %rbx, %rbx
je 0x1d6cf
andl $0x0, (%r14,%rbx,4)
jmp 0x1d6e6
movl %ebp, %eax
cltd
idivl %r15d
movslq %edx, %rdx
xorl %ecx, %ecx
cmpq %rdx, %rsi
setl %cl
addl %eax, %ecx
movl %ecx, (%r12,%r10,4)
movl %ebx, %eax
orl %esi, %eax
je 0x1d6fc
movl -0x4(%r14,%rbx,4), %eax
addl -0x4(%r11,%rbx,4), %eax
movl %eax, (%r11,%rbx,4)
jmp 0x1d701
andl $0x0, (%r13)
incq %rbx
jmp 0x1d6be
incq %rsi
addq %r9, %r14
addq %r9, %r11
jmp 0x1d6ae
xorl %esi, %esi
cmpl $0x0, 0xc(%rsp)
jne 0x1d72c
movl %ebp, %eax
cltd
idivl %r15d
xorl %esi, %esi
cmpl 0x8(%rsp), %edx
setg %sil
addl %eax, %esi
subq $0x8, %rsp
movq 0x13871(%rip), %rdx     # 0x30fa8
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq %r12, %r8
movq %r13, %r9
pushq 0x30(%rsp)
pushq 0x20(%rsp)
pushq %rdx
callq 0x6a40
addq $0x20, %rsp
movq %r12, %rdi
callq 0x6650
movq %r13, %rdi
callq 0x6650
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq