  MODULE tfc_mod
  USE ISO_C_BINDING, ONLY : C_NULL_CHAR
  IMPLICIT none (external, type)
  PRIVATE
!
  PUBLIC :: F_C_STRING
!
  CONTAINS
!
  FUNCTION F_C_STRING(string, asis)
  CHARACTER(LEN=:),ALLOCATABLE :: F_C_STRING  
  CHARACTER(LEN=*),INTENT(in)  :: string
  LOGICAL,OPTIONAL,INTENT(in)  :: asis    ! default: .false.
!
  INTEGER :: length
  LOGICAL :: do_asis
!
! the next line is the problem line with IFX if f_c_string is called without argument asis
!
  do_asis = MERGE(asis, .FALSE.,  PRESENT(asis)) 
!
  IF(do_asis) THEN
    length = LEN(string)+1
    ALLOCATE(CHARACTER(LEN=length) :: F_C_STRING)
    F_C_STRING = string//C_NULL_CHAR
  ELSE
    length = LEN_TRIM(string)+1
    ALLOCATE(CHARACTER(LEN=length) :: F_C_STRING)
    F_C_STRING = TRIM(string)//C_NULL_CHAR
  END IF
!
  RETURN
!
  END FUNCTION F_C_STRING

  END MODULE tfc_mod

  PROGRAM tfc
  USE tfc_mod,       ONLY : F_C_STRING
  USE ISO_C_BINDING, ONLY : C_NULL_CHAR
  IMPLICIT none (external, type)

  CHARACTER(LEN=32) :: input
  CHARACTER(LEN=64) :: result
  INTEGER :: length
!
  input = "This is a string"
!
!  The next lines work fine with the classic compiler, but fails with IFX
!
  result = F_C_STRING(input)                    !  problem line
  length = INDEX(result, C_NULL_CHAR)-1
  write(*,*) 'length', length
  write(*,*) 'result >>'//result(1:length)//"<<"
  write(*,*)
!
!  The next lines work fine with both the classic compiler and IFX
!
  result = F_C_STRING(input, asis = .TRUE.)
  length = INDEX(result, C_NULL_CHAR)-1
  write(*,*) 'length', length
  write(*,*) 'result >>'//result(1:length)//"<<"
  write(*,*)
!
!  The next lines work fine with both the classic compiler and IFX
!
  result = F_C_STRING(input, asis = .FALSE.)
  length = INDEX(result, C_NULL_CHAR)-1
  write(*,*) 'length', length
  write(*,*) 'result >>'//result(1:length)//"<<"
!
  END PROGRAM
