explain what is wrong with this code and fix it (it is currently also not outputting any log files):

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
#include <time.h>

#define SHIFT_ROW 0
#define SHIFT_COL 1
#define DISP 1

// Function to check if a number is prime
int is_prime(int num) {
    if (num <= 1) return 0;
    if (num <= 3) return 1;
    if (num % 2 == 0 || num % 3 == 0) return 0;

    for (int i = 5; i * i <= num; i += 6) {
        if (num % i == 0 || num % (i + 2) == 0) return 0;
    }
    return 1;
}

int main(int argc, char *argv[]) {
    int ndims = 2, size, my_rank, reorder, my_cart_rank, ierr;
    int nrows, ncols;
    int nbr_i_lo, nbr_i_hi;
    int nbr_j_lo, nbr_j_hi;
    MPI_Comm comm2D;
    int dims[ndims], coord[ndims];
    int wrap_around[ndims];
    int my_prime, recv_prime;
    char filename[20];

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    // Process command line arguments (same as Task 1)
    if (argc == 3) {
        nrows = atoi(argv[1]);
        ncols = atoi(argv[2]);
        dims[0] = nrows;
        dims[1] = ncols;
        if ((nrows * ncols) != size) {
            if (my_rank == 0) 
                printf("ERROR: nrows*ncols=%d * %d = %d != %d\n", 
                       nrows, ncols, nrows * ncols, size);
            MPI_Finalize();
            return 0;
        }
    } else {
        nrows = ncols = (int)sqrt(size);
        dims[0] = dims[1] = 0;
    }

    // Create Cartesian topology (same as Task 1)
    wrap_around[0] = wrap_around[1] = 0; 
    reorder = 1;
    ierr = MPI_Cart_create(MPI_COMM_WORLD, ndims, dims, wrap_around, 
                          reorder, &comm2D);
    if (ierr != 0) 
        printf("ERROR[%d] creating CART\n", ierr);

    MPI_Cart_coords(comm2D, my_rank, ndims, coord);
    MPI_Cart_rank(comm2D, coord, &my_cart_rank);

    MPI_Cart_shift(comm2D, SHIFT_ROW, DISP, &nbr_i_lo, &nbr_i_hi);
    MPI_Cart_shift(comm2D, SHIFT_COL, DISP, &nbr_j_lo, &nbr_j_hi);

    // Generate random prime number
    srand(time(NULL) + my_rank); 
    do {
        my_prime = rand() % 1000 + 2; 
    } while (!is_prime(my_prime));

    // Exchange and compare prime numbers for 500 iterations
    for (int iter = 0; iter < 500; iter++) {
        // Array to store neighbor ranks and received primes
        int neighbors[] = {nbr_j_lo, nbr_j_hi, nbr_i_lo, nbr_i_hi};
        int recv_primes[4];

        // Loop through neighbors
        for (int i = 0; i < 4; i++) {
            int neighbor = neighbors[i];

            // Correctly using MPI_Sendrecv: send to neighbor, receive from opposite
            MPI_Sendrecv(&my_prime, 1, MPI_INT, neighbor, 0,
                         &recv_primes[i], 1, MPI_INT, neighbor, 0,
                         comm2D, MPI_STATUS_IGNORE);

            // Compare and log matches
            if (recv_primes[i] == my_prime) {
                sprintf(filename, "rank_%d.txt", my_rank);
                FILE *fp = fopen(filename, "a");
                if (fp != NULL) {
                    fprintf(fp, "Prime: %d, Receiving Rank: %d, Sending Rank: %d\n",
                            my_prime, my_rank, neighbor);
                    fclose(fp);
                }
            }
        }
    }

    MPI_Comm_free(&comm2D);
    MPI_Finalize();
    return 0;
}