// File: 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:
subq $0x88, %rsp
movl %edi, 0x84(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x64(%rsp), %rsi
callq 0xf050
movq 0x68(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xece0
movl 0x60(%rsp), %edi
callq 0x119d0
cvttsd2si %xmm0, %eax
movl %eax, 0x5c(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x54(%rsp), %rdx
leaq 0x44(%rsp), %rcx
leaq 0x4c(%rsp), %r8
movl $0x2, %esi
callq 0xe8f0
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0xe510
movq 0x68(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0xf4b0
movslq 0x60(%rsp), %rdi
shlq $0x2, %rdi
callq 0xef20
movq %rax, 0x30(%rsp)
movslq 0x60(%rsp), %rdi
shlq $0x2, %rdi
callq 0xef20
movq %rax, 0x28(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x40(%rsp), %eax
jne 0x4cfb1
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jge 0x4cfaf
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jge 0x4cf9d
cmpl $0x0, 0x20(%rsp)
je 0x4cef4
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x5c(%rsp), %ecx
addl 0x20(%rsp), %ecx
movslq %ecx, %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0x4cf22
movl 0x84(%rsp), %edi
movl 0x5c(%rsp), %esi
movl 0x24(%rsp), %edx
callq 0x4e0e0
movl %eax, %edx
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x5c(%rsp), %ecx
addl 0x20(%rsp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
cmpl $0x0, 0x24(%rsp)
jne 0x4cf3d
cmpl $0x0, 0x20(%rsp)
jne 0x4cf3d
movq 0x28(%rsp), %rax
movl $0x0, (%rax)
jmp 0x4cf8b
movq 0x28(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x5c(%rsp), %ecx
addl 0x20(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x5c(%rsp), %ecx
addl 0x20(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addl (%rax,%rcx,4), %edx
movq 0x28(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x5c(%rsp), %ecx
addl 0x20(%rsp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x4cf8d
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x4cec1
jmp 0x4cf9f
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x4ceab
jmp 0x4cfb1
movl $0x0, 0x1c(%rsp)
cmpl $0x0, 0x50(%rsp)
jne 0x4cfd8
movl 0x84(%rsp), %edi
movl 0x5c(%rsp), %esi
movl 0x4c(%rsp), %edx
callq 0x4e0e0
movl %eax, 0x1c(%rsp)
movq 0x78(%rsp), %rdi
movl 0x1c(%rsp), %esi
movq 0x70(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r9
movl 0x40(%rsp), %r10d
movq 0x68(%rsp), %rax
movq 0x1cfa7(%rip), %r11     # 0x69fa8
movq %r11, %rdx
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xf520
movq 0x30(%rsp), %rdi
callq 0xed30
movq 0x28(%rsp), %rdi
callq 0xed30
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)