PROGRAM Main
    IMPLICIT NONE
    INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(12)
    REAL(KIND=dp), ALLOCATABLE :: data(:,:)
    REAL(KIND=dp) :: wall, cpu, x
    INTEGER :: size, m, n
    CHARACTER(LEN=100) :: filename1, filename2, filename3

    CALL SYSTEM_CLOCK(m,n)
    wall = m/REAL(n,KIND=dp)
    CALL CPU_TIME(cpu)

    CALL GET_COMMAND_ARGUMENT(1,filename1)
    CALL GET_COMMAND_ARGUMENT(2,filename2)
    CALL GET_COMMAND_ARGUMENT(3,filename3,STATUS=n)
    IF (n == 0) THEN
        PRINT *, 'Too many arguments'
        STOP 1
    END IF
    OPEN(69,FILE=filename1,ACTION='READ',ACCESS='STREAM',FORM='UNFORMATTED')
    OPEN(42,FILE=filename2,ACTION='WRITE',FORM='UNFORMATTED')
    READ (69) size
    IF (size < 1 .OR. size > 1000000) THEN
        WRITE (*,'("Invalid value of size: ",I0)') size
        STOP 1
    END IF
    WRITE (42) size
    ALLOCATE(data(size,size))
    READ (69) data
    WRITE (42) data
    READ (69) data
    WRITE (42) data
    CLOSE(42)

    CALL SYSTEM_CLOCK(m,n)
    wall = m/REAL(n,KIND=dp)-wall
    CALL CPU_TIME(x)
    cpu = x-cpu
    WRITE (*,'("Time = ",F0.2," seconds, CPU = ",F0.2," seconds")') wall,cpu
END PROGRAM Main
