program main
    use mpi
    implicit none

    integer :: i, ierr, completed

    integer :: statuses(MPI_STATUS_SIZE, 2)
    integer :: requests(2)
    integer :: outcount, indices(2)

    integer, parameter :: tag = 123789

    call MPI_Init(ierr)

    call MPI_Isend(MPI_BOTTOM, 0, MPI_BYTE, 0, tag, MPI_COMM_WORLD, requests(1), ierr)
    call MPI_Irecv(MPI_BOTTOM, 0, MPI_BYTE, 0, tag, MPI_COMM_WORLD, requests(2), ierr)

    completed = 0

    do while(completed .lt. 2)
        call MPI_Testsome(2, requests, outcount, indices, statuses, ierr)
        print *, "MPI_Testsome found ", outcount, "completed requests"
        do i = 1,outcount
            print *, "Completed request ", indices(i)
            completed = completed + 1
        end do
    end do

!     do while (completed .lt. 2)
!         call MPI_Waitany(2, requests, indices(1), statuses(:,1), ierr)
!         print *, "Completed request ", indices(1)
!         completed = completed + 1
!     end do

    call MPI_Finalize(ierr)
end program
