Index: m4/ifunction.m4
===================================================================
--- m4/ifunction.m4	(Revision 250720)
+++ m4/ifunction.m4	(Arbeitskopie)
@@ -45,6 +45,13 @@ name`'rtype_qual`_'atype_code (rtype * const restr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in u_name intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -193,8 +200,8 @@ void
   rtype_name * restrict dest;
   const atype_name * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -204,6 +211,14 @@ void
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in u_name intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -380,6 +395,13 @@ void
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in u_name intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iall_i1.c
===================================================================
--- generated/iall_i1.c	(Revision 250720)
+++ generated/iall_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iall_i1 (gfc_array_i1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miall_i1 (gfc_array_i1 * const restrict retarray,
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miall_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siall_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iall_i16.c
===================================================================
--- generated/iall_i16.c	(Revision 250720)
+++ generated/iall_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iall_i16 (gfc_array_i16 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miall_i16 (gfc_array_i16 * const restrict retarray
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miall_i16 (gfc_array_i16 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siall_i16 (gfc_array_i16 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iall_i2.c
===================================================================
--- generated/iall_i2.c	(Revision 250720)
+++ generated/iall_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iall_i2 (gfc_array_i2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miall_i2 (gfc_array_i2 * const restrict retarray,
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miall_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siall_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iall_i4.c
===================================================================
--- generated/iall_i4.c	(Revision 250720)
+++ generated/iall_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iall_i4 (gfc_array_i4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miall_i4 (gfc_array_i4 * const restrict retarray,
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miall_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siall_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iall_i8.c
===================================================================
--- generated/iall_i8.c	(Revision 250720)
+++ generated/iall_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iall_i8 (gfc_array_i8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miall_i8 (gfc_array_i8 * const restrict retarray,
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miall_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siall_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IALL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iany_i1.c
===================================================================
--- generated/iany_i1.c	(Revision 250720)
+++ generated/iany_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iany_i1 (gfc_array_i1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miany_i1 (gfc_array_i1 * const restrict retarray,
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miany_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siany_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iany_i16.c
===================================================================
--- generated/iany_i16.c	(Revision 250720)
+++ generated/iany_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iany_i16 (gfc_array_i16 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miany_i16 (gfc_array_i16 * const restrict retarray
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miany_i16 (gfc_array_i16 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siany_i16 (gfc_array_i16 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iany_i2.c
===================================================================
--- generated/iany_i2.c	(Revision 250720)
+++ generated/iany_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iany_i2 (gfc_array_i2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miany_i2 (gfc_array_i2 * const restrict retarray,
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miany_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siany_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iany_i4.c
===================================================================
--- generated/iany_i4.c	(Revision 250720)
+++ generated/iany_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iany_i4 (gfc_array_i4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miany_i4 (gfc_array_i4 * const restrict retarray,
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miany_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siany_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iany_i8.c
===================================================================
--- generated/iany_i8.c	(Revision 250720)
+++ generated/iany_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iany_i8 (gfc_array_i8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miany_i8 (gfc_array_i8 * const restrict retarray,
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miany_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siany_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IANY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iparity_i1.c
===================================================================
--- generated/iparity_i1.c	(Revision 250720)
+++ generated/iparity_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iparity_i1 (gfc_array_i1 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miparity_i1 (gfc_array_i1 * const restrict retarra
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miparity_i1 (gfc_array_i1 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siparity_i1 (gfc_array_i1 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iparity_i16.c
===================================================================
--- generated/iparity_i16.c	(Revision 250720)
+++ generated/iparity_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iparity_i16 (gfc_array_i16 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miparity_i16 (gfc_array_i16 * const restrict retar
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miparity_i16 (gfc_array_i16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siparity_i16 (gfc_array_i16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iparity_i2.c
===================================================================
--- generated/iparity_i2.c	(Revision 250720)
+++ generated/iparity_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iparity_i2 (gfc_array_i2 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miparity_i2 (gfc_array_i2 * const restrict retarra
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miparity_i2 (gfc_array_i2 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siparity_i2 (gfc_array_i2 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iparity_i4.c
===================================================================
--- generated/iparity_i4.c	(Revision 250720)
+++ generated/iparity_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iparity_i4 (gfc_array_i4 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miparity_i4 (gfc_array_i4 * const restrict retarra
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miparity_i4 (gfc_array_i4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siparity_i4 (gfc_array_i4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/iparity_i8.c
===================================================================
--- generated/iparity_i8.c	(Revision 250720)
+++ generated/iparity_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ iparity_i8 (gfc_array_i8 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ miparity_i8 (gfc_array_i8 * const restrict retarra
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ miparity_i8 (gfc_array_i8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ siparity_i8 (gfc_array_i8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in IPARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_i1.c
===================================================================
--- generated/maxloc1_16_i1.c	(Revision 250720)
+++ generated/maxloc1_16_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_i1 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_i1 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_i1 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_i1 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_i16.c
===================================================================
--- generated/maxloc1_16_i16.c	(Revision 250720)
+++ generated/maxloc1_16_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_i16 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_i16 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_i16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_i16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_i2.c
===================================================================
--- generated/maxloc1_16_i2.c	(Revision 250720)
+++ generated/maxloc1_16_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_i2 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_i2 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_i2 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_i2 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_i4.c
===================================================================
--- generated/maxloc1_16_i4.c	(Revision 250720)
+++ generated/maxloc1_16_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_i4 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_i4 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_i4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_i4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_i8.c
===================================================================
--- generated/maxloc1_16_i8.c	(Revision 250720)
+++ generated/maxloc1_16_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_i8 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_i8 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_i8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_i8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_r10.c
===================================================================
--- generated/maxloc1_16_r10.c	(Revision 250720)
+++ generated/maxloc1_16_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_r10 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_r10 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_r10 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_r10 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_r16.c
===================================================================
--- generated/maxloc1_16_r16.c	(Revision 250720)
+++ generated/maxloc1_16_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_r16 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_r16 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_r16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_r16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_r4.c
===================================================================
--- generated/maxloc1_16_r4.c	(Revision 250720)
+++ generated/maxloc1_16_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_r4 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_r4 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_r4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_r4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_16_r8.c
===================================================================
--- generated/maxloc1_16_r8.c	(Revision 250720)
+++ generated/maxloc1_16_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_16_r8 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_16_r8 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_16_r8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_16_r8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_i1.c
===================================================================
--- generated/maxloc1_4_i1.c	(Revision 250720)
+++ generated/maxloc1_4_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_i1 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_i1 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_i1 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_i1 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_i16.c
===================================================================
--- generated/maxloc1_4_i16.c	(Revision 250720)
+++ generated/maxloc1_4_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_i16 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_i16 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_i16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_i16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_i2.c
===================================================================
--- generated/maxloc1_4_i2.c	(Revision 250720)
+++ generated/maxloc1_4_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_i2 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_i2 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_i2 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_i2 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_i4.c
===================================================================
--- generated/maxloc1_4_i4.c	(Revision 250720)
+++ generated/maxloc1_4_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_i4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_i8.c
===================================================================
--- generated/maxloc1_4_i8.c	(Revision 250720)
+++ generated/maxloc1_4_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_i8 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_i8 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_i8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_i8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_r10.c
===================================================================
--- generated/maxloc1_4_r10.c	(Revision 250720)
+++ generated/maxloc1_4_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_r10 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_r10 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_r10 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_r10 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_r16.c
===================================================================
--- generated/maxloc1_4_r16.c	(Revision 250720)
+++ generated/maxloc1_4_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_r16 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_r16 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_r16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_r16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_r4.c
===================================================================
--- generated/maxloc1_4_r4.c	(Revision 250720)
+++ generated/maxloc1_4_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_r4 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_r4 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_r4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_r4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_4_r8.c
===================================================================
--- generated/maxloc1_4_r8.c	(Revision 250720)
+++ generated/maxloc1_4_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_4_r8 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_4_r8 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_4_r8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_4_r8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_i1.c
===================================================================
--- generated/maxloc1_8_i1.c	(Revision 250720)
+++ generated/maxloc1_8_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_i1 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_i1 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_i1 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_i1 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_i16.c
===================================================================
--- generated/maxloc1_8_i16.c	(Revision 250720)
+++ generated/maxloc1_8_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_i16 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_i16 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_i16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_i16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_i2.c
===================================================================
--- generated/maxloc1_8_i2.c	(Revision 250720)
+++ generated/maxloc1_8_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_i2 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_i2 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_i2 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_i2 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_i4.c
===================================================================
--- generated/maxloc1_8_i4.c	(Revision 250720)
+++ generated/maxloc1_8_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_i4 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_i4 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_i4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_i4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_i8.c
===================================================================
--- generated/maxloc1_8_i8.c	(Revision 250720)
+++ generated/maxloc1_8_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_i8 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_i8 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_i8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_i8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_r10.c
===================================================================
--- generated/maxloc1_8_r10.c	(Revision 250720)
+++ generated/maxloc1_8_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_r10 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_r10 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_r10 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_r10 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_r16.c
===================================================================
--- generated/maxloc1_8_r16.c	(Revision 250720)
+++ generated/maxloc1_8_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_r16 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_r16 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_r16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_r16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_r4.c
===================================================================
--- generated/maxloc1_8_r4.c	(Revision 250720)
+++ generated/maxloc1_8_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_r4 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_r4 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_r4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_r4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxloc1_8_r8.c
===================================================================
--- generated/maxloc1_8_r8.c	(Revision 250720)
+++ generated/maxloc1_8_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxloc1_8_r8 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mmaxloc1_8_r8 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mmaxloc1_8_r8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ smaxloc1_8_r8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_i1.c
===================================================================
--- generated/maxval_i1.c	(Revision 250720)
+++ generated/maxval_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_i1 (gfc_array_i1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_i1 (gfc_array_i1 * const restrict retarray
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_i1 (gfc_array_i1 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_i1 (gfc_array_i1 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_i16.c
===================================================================
--- generated/maxval_i16.c	(Revision 250720)
+++ generated/maxval_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_i16 (gfc_array_i16 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_i16 (gfc_array_i16 * const restrict retarr
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_i16 (gfc_array_i16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_i16 (gfc_array_i16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_i2.c
===================================================================
--- generated/maxval_i2.c	(Revision 250720)
+++ generated/maxval_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_i2 (gfc_array_i2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_i2 (gfc_array_i2 * const restrict retarray
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_i2 (gfc_array_i2 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_i2 (gfc_array_i2 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_i4.c
===================================================================
--- generated/maxval_i4.c	(Revision 250720)
+++ generated/maxval_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_i4 (gfc_array_i4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_i4 (gfc_array_i4 * const restrict retarray
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_i4 (gfc_array_i4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_i4 (gfc_array_i4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_i8.c
===================================================================
--- generated/maxval_i8.c	(Revision 250720)
+++ generated/maxval_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_i8 (gfc_array_i8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_i8 (gfc_array_i8 * const restrict retarray
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_i8 (gfc_array_i8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_i8 (gfc_array_i8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_r10.c
===================================================================
--- generated/maxval_r10.c	(Revision 250720)
+++ generated/maxval_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_r10 (gfc_array_r10 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_r10 (gfc_array_r10 * const restrict retarr
   GFC_REAL_10 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_r10 (gfc_array_r10 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_r10 (gfc_array_r10 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_r16.c
===================================================================
--- generated/maxval_r16.c	(Revision 250720)
+++ generated/maxval_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_r16 (gfc_array_r16 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_r16 (gfc_array_r16 * const restrict retarr
   GFC_REAL_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_r16 (gfc_array_r16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_r16 (gfc_array_r16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_r4.c
===================================================================
--- generated/maxval_r4.c	(Revision 250720)
+++ generated/maxval_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_r4 (gfc_array_r4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_r4 (gfc_array_r4 * const restrict retarray
   GFC_REAL_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_r4 (gfc_array_r4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_r4 (gfc_array_r4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/maxval_r8.c
===================================================================
--- generated/maxval_r8.c	(Revision 250720)
+++ generated/maxval_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ maxval_r8 (gfc_array_r8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mmaxval_r8 (gfc_array_r8 * const restrict retarray
   GFC_REAL_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mmaxval_r8 (gfc_array_r8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ smaxval_r8 (gfc_array_r8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_i1.c
===================================================================
--- generated/minloc1_16_i1.c	(Revision 250720)
+++ generated/minloc1_16_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_i1 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_i1 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_i1 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_i1 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_i16.c
===================================================================
--- generated/minloc1_16_i16.c	(Revision 250720)
+++ generated/minloc1_16_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_i16 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_i16 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_i16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_i16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_i2.c
===================================================================
--- generated/minloc1_16_i2.c	(Revision 250720)
+++ generated/minloc1_16_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_i2 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_i2 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_i2 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_i2 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_i4.c
===================================================================
--- generated/minloc1_16_i4.c	(Revision 250720)
+++ generated/minloc1_16_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_i4 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_i4 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_i4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_i4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_i8.c
===================================================================
--- generated/minloc1_16_i8.c	(Revision 250720)
+++ generated/minloc1_16_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_i8 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_i8 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_i8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_i8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_r10.c
===================================================================
--- generated/minloc1_16_r10.c	(Revision 250720)
+++ generated/minloc1_16_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_r10 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_r10 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_r10 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_r10 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_r16.c
===================================================================
--- generated/minloc1_16_r16.c	(Revision 250720)
+++ generated/minloc1_16_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_r16 (gfc_array_i16 * const restrict ret
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_r16 (gfc_array_i16 * const restrict re
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_r16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_r16 (gfc_array_i16 * const restrict re
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_r4.c
===================================================================
--- generated/minloc1_16_r4.c	(Revision 250720)
+++ generated/minloc1_16_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_r4 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_r4 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_r4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_r4 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_16_r8.c
===================================================================
--- generated/minloc1_16_r8.c	(Revision 250720)
+++ generated/minloc1_16_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_16_r8 (gfc_array_i16 * const restrict reta
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_16_r8 (gfc_array_i16 * const restrict ret
   GFC_INTEGER_16 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_16_r8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_16_r8 (gfc_array_i16 * const restrict ret
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_i1.c
===================================================================
--- generated/minloc1_4_i1.c	(Revision 250720)
+++ generated/minloc1_4_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_i1 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_i1 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_i1 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_i1 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_i16.c
===================================================================
--- generated/minloc1_4_i16.c	(Revision 250720)
+++ generated/minloc1_4_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_i16 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_i16 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_i16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_i16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_i2.c
===================================================================
--- generated/minloc1_4_i2.c	(Revision 250720)
+++ generated/minloc1_4_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_i2 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_i2 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_i2 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_i2 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_i4.c
===================================================================
--- generated/minloc1_4_i4.c	(Revision 250720)
+++ generated/minloc1_4_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_i4 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_i4 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_i4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_i4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_i8.c
===================================================================
--- generated/minloc1_4_i8.c	(Revision 250720)
+++ generated/minloc1_4_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_i8 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_i8 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_i8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_i8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_r10.c
===================================================================
--- generated/minloc1_4_r10.c	(Revision 250720)
+++ generated/minloc1_4_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_r10 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_r10 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_r10 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_r10 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_r16.c
===================================================================
--- generated/minloc1_4_r16.c	(Revision 250720)
+++ generated/minloc1_4_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_r16 (gfc_array_i4 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_r16 (gfc_array_i4 * const restrict reta
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_r16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_r16 (gfc_array_i4 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_r4.c
===================================================================
--- generated/minloc1_4_r4.c	(Revision 250720)
+++ generated/minloc1_4_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_r4 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_r4 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_r4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_r4 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_4_r8.c
===================================================================
--- generated/minloc1_4_r8.c	(Revision 250720)
+++ generated/minloc1_4_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_4_r8 (gfc_array_i4 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_4_r8 (gfc_array_i4 * const restrict retar
   GFC_INTEGER_4 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_4_r8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_4_r8 (gfc_array_i4 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_i1.c
===================================================================
--- generated/minloc1_8_i1.c	(Revision 250720)
+++ generated/minloc1_8_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_i1 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_i1 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_i1 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_i1 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_i16.c
===================================================================
--- generated/minloc1_8_i16.c	(Revision 250720)
+++ generated/minloc1_8_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_i16 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_i16 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_i16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_i16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_i2.c
===================================================================
--- generated/minloc1_8_i2.c	(Revision 250720)
+++ generated/minloc1_8_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_i2 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_i2 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_i2 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_i2 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_i4.c
===================================================================
--- generated/minloc1_8_i4.c	(Revision 250720)
+++ generated/minloc1_8_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_i4 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_i4 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_i4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_i4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_i8.c
===================================================================
--- generated/minloc1_8_i8.c	(Revision 250720)
+++ generated/minloc1_8_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_i8 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_i8 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_i8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_i8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_r10.c
===================================================================
--- generated/minloc1_8_r10.c	(Revision 250720)
+++ generated/minloc1_8_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_r10 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_r10 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_r10 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_r10 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_r16.c
===================================================================
--- generated/minloc1_8_r16.c	(Revision 250720)
+++ generated/minloc1_8_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_r16 (gfc_array_i8 * const restrict retar
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_r16 (gfc_array_i8 * const restrict reta
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_r16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_r16 (gfc_array_i8 * const restrict reta
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_r4.c
===================================================================
--- generated/minloc1_8_r4.c	(Revision 250720)
+++ generated/minloc1_8_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_r4 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_r4 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_r4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_r4 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minloc1_8_r8.c
===================================================================
--- generated/minloc1_8_r8.c	(Revision 250720)
+++ generated/minloc1_8_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minloc1_8_r8 (gfc_array_i8 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -224,8 +231,8 @@ mminloc1_8_r8 (gfc_array_i8 * const restrict retar
   GFC_INTEGER_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -235,6 +242,14 @@ mminloc1_8_r8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -445,6 +460,13 @@ sminloc1_8_r8 (gfc_array_i8 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_i1.c
===================================================================
--- generated/minval_i1.c	(Revision 250720)
+++ generated/minval_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_i1 (gfc_array_i1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_i1 (gfc_array_i1 * const restrict retarray
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_i1 (gfc_array_i1 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_i1 (gfc_array_i1 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_i16.c
===================================================================
--- generated/minval_i16.c	(Revision 250720)
+++ generated/minval_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_i16 (gfc_array_i16 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_i16 (gfc_array_i16 * const restrict retarr
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_i16 (gfc_array_i16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_i16 (gfc_array_i16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_i2.c
===================================================================
--- generated/minval_i2.c	(Revision 250720)
+++ generated/minval_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_i2 (gfc_array_i2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_i2 (gfc_array_i2 * const restrict retarray
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_i2 (gfc_array_i2 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_i2 (gfc_array_i2 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_i4.c
===================================================================
--- generated/minval_i4.c	(Revision 250720)
+++ generated/minval_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_i4 (gfc_array_i4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_i4 (gfc_array_i4 * const restrict retarray
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_i4 (gfc_array_i4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_i4 (gfc_array_i4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_i8.c
===================================================================
--- generated/minval_i8.c	(Revision 250720)
+++ generated/minval_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_i8 (gfc_array_i8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_i8 (gfc_array_i8 * const restrict retarray
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_i8 (gfc_array_i8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_i8 (gfc_array_i8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_r10.c
===================================================================
--- generated/minval_r10.c	(Revision 250720)
+++ generated/minval_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_r10 (gfc_array_r10 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_r10 (gfc_array_r10 * const restrict retarr
   GFC_REAL_10 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_r10 (gfc_array_r10 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_r10 (gfc_array_r10 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_r16.c
===================================================================
--- generated/minval_r16.c	(Revision 250720)
+++ generated/minval_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_r16 (gfc_array_r16 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_r16 (gfc_array_r16 * const restrict retarr
   GFC_REAL_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_r16 (gfc_array_r16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_r16 (gfc_array_r16 * const restrict retarr
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_r4.c
===================================================================
--- generated/minval_r4.c	(Revision 250720)
+++ generated/minval_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_r4 (gfc_array_r4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_r4 (gfc_array_r4 * const restrict retarray
   GFC_REAL_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_r4 (gfc_array_r4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_r4 (gfc_array_r4 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/minval_r8.c
===================================================================
--- generated/minval_r8.c	(Revision 250720)
+++ generated/minval_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ minval_r8 (gfc_array_r8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -217,8 +224,8 @@ mminval_r8 (gfc_array_r8 * const restrict retarray
   GFC_REAL_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -228,6 +235,14 @@ mminval_r8 (gfc_array_r8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -433,6 +448,13 @@ sminval_r8 (gfc_array_r8 * const restrict retarray
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/norm2_r10.c
===================================================================
--- generated/norm2_r10.c	(Revision 250720)
+++ generated/norm2_r10.c	(Arbeitskopie)
@@ -55,9 +55,16 @@ norm2_r10 (gfc_array_r10 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in NORM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/norm2_r16.c
===================================================================
--- generated/norm2_r16.c	(Revision 250720)
+++ generated/norm2_r16.c	(Arbeitskopie)
@@ -59,9 +59,16 @@ norm2_r16 (gfc_array_r16 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in NORM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/norm2_r4.c
===================================================================
--- generated/norm2_r4.c	(Revision 250720)
+++ generated/norm2_r4.c	(Arbeitskopie)
@@ -55,9 +55,16 @@ norm2_r4 (gfc_array_r4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in NORM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/norm2_r8.c
===================================================================
--- generated/norm2_r8.c	(Revision 250720)
+++ generated/norm2_r8.c	(Arbeitskopie)
@@ -55,9 +55,16 @@ norm2_r8 (gfc_array_r8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in NORM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/parity_l1.c
===================================================================
--- generated/parity_l1.c	(Revision 250720)
+++ generated/parity_l1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ parity_l1 (gfc_array_l1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/parity_l16.c
===================================================================
--- generated/parity_l16.c	(Revision 250720)
+++ generated/parity_l16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ parity_l16 (gfc_array_l16 * const restrict retarra
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/parity_l2.c
===================================================================
--- generated/parity_l2.c	(Revision 250720)
+++ generated/parity_l2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ parity_l2 (gfc_array_l2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/parity_l4.c
===================================================================
--- generated/parity_l4.c	(Revision 250720)
+++ generated/parity_l4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ parity_l4 (gfc_array_l4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/parity_l8.c
===================================================================
--- generated/parity_l8.c	(Revision 250720)
+++ generated/parity_l8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ parity_l8 (gfc_array_l8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PARITY intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
Index: generated/product_c10.c
===================================================================
--- generated/product_c10.c	(Revision 250720)
+++ generated/product_c10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_c10 (gfc_array_c10 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_c10 (gfc_array_c10 * const restrict retar
   GFC_COMPLEX_10 * restrict dest;
   const GFC_COMPLEX_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_c10 (gfc_array_c10 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_c10 (gfc_array_c10 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_c16.c
===================================================================
--- generated/product_c16.c	(Revision 250720)
+++ generated/product_c16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_c16 (gfc_array_c16 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_c16 (gfc_array_c16 * const restrict retar
   GFC_COMPLEX_16 * restrict dest;
   const GFC_COMPLEX_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_c16 (gfc_array_c16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_c16 (gfc_array_c16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_c4.c
===================================================================
--- generated/product_c4.c	(Revision 250720)
+++ generated/product_c4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_c4 (gfc_array_c4 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_c4 (gfc_array_c4 * const restrict retarra
   GFC_COMPLEX_4 * restrict dest;
   const GFC_COMPLEX_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_c4 (gfc_array_c4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_c4 (gfc_array_c4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_c8.c
===================================================================
--- generated/product_c8.c	(Revision 250720)
+++ generated/product_c8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_c8 (gfc_array_c8 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_c8 (gfc_array_c8 * const restrict retarra
   GFC_COMPLEX_8 * restrict dest;
   const GFC_COMPLEX_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_c8 (gfc_array_c8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_c8 (gfc_array_c8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_i1.c
===================================================================
--- generated/product_i1.c	(Revision 250720)
+++ generated/product_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_i1 (gfc_array_i1 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_i1 (gfc_array_i1 * const restrict retarra
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_i1 (gfc_array_i1 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_i1 (gfc_array_i1 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_i16.c
===================================================================
--- generated/product_i16.c	(Revision 250720)
+++ generated/product_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_i16 (gfc_array_i16 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_i16 (gfc_array_i16 * const restrict retar
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_i16 (gfc_array_i16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_i16 (gfc_array_i16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_i2.c
===================================================================
--- generated/product_i2.c	(Revision 250720)
+++ generated/product_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_i2 (gfc_array_i2 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_i2 (gfc_array_i2 * const restrict retarra
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_i2 (gfc_array_i2 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_i2 (gfc_array_i2 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_i4.c
===================================================================
--- generated/product_i4.c	(Revision 250720)
+++ generated/product_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_i4 (gfc_array_i4 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_i4 (gfc_array_i4 * const restrict retarra
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_i4 (gfc_array_i4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_i4 (gfc_array_i4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_i8.c
===================================================================
--- generated/product_i8.c	(Revision 250720)
+++ generated/product_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_i8 (gfc_array_i8 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_i8 (gfc_array_i8 * const restrict retarra
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_i8 (gfc_array_i8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_i8 (gfc_array_i8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_r10.c
===================================================================
--- generated/product_r10.c	(Revision 250720)
+++ generated/product_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_r10 (gfc_array_r10 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_r10 (gfc_array_r10 * const restrict retar
   GFC_REAL_10 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_r10 (gfc_array_r10 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_r10 (gfc_array_r10 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_r16.c
===================================================================
--- generated/product_r16.c	(Revision 250720)
+++ generated/product_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_r16 (gfc_array_r16 * const restrict retarr
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_r16 (gfc_array_r16 * const restrict retar
   GFC_REAL_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_r16 (gfc_array_r16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_r16 (gfc_array_r16 * const restrict retar
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_r4.c
===================================================================
--- generated/product_r4.c	(Revision 250720)
+++ generated/product_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_r4 (gfc_array_r4 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_r4 (gfc_array_r4 * const restrict retarra
   GFC_REAL_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_r4 (gfc_array_r4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_r4 (gfc_array_r4 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/product_r8.c
===================================================================
--- generated/product_r8.c	(Revision 250720)
+++ generated/product_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ product_r8 (gfc_array_r8 * const restrict retarray
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ mproduct_r8 (gfc_array_r8 * const restrict retarra
   GFC_REAL_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ mproduct_r8 (gfc_array_r8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ sproduct_r8 (gfc_array_r8 * const restrict retarra
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in PRODUCT intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_c10.c
===================================================================
--- generated/sum_c10.c	(Revision 250720)
+++ generated/sum_c10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_c10 (gfc_array_c10 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_c10 (gfc_array_c10 * const restrict retarray,
   GFC_COMPLEX_10 * restrict dest;
   const GFC_COMPLEX_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_c10 (gfc_array_c10 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_c10 (gfc_array_c10 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_c16.c
===================================================================
--- generated/sum_c16.c	(Revision 250720)
+++ generated/sum_c16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_c16 (gfc_array_c16 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_c16 (gfc_array_c16 * const restrict retarray,
   GFC_COMPLEX_16 * restrict dest;
   const GFC_COMPLEX_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_c16 (gfc_array_c16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_c16 (gfc_array_c16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_c4.c
===================================================================
--- generated/sum_c4.c	(Revision 250720)
+++ generated/sum_c4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_c4 (gfc_array_c4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_c4 (gfc_array_c4 * const restrict retarray,
   GFC_COMPLEX_4 * restrict dest;
   const GFC_COMPLEX_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_c4 (gfc_array_c4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_c4 (gfc_array_c4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_c8.c
===================================================================
--- generated/sum_c8.c	(Revision 250720)
+++ generated/sum_c8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_c8 (gfc_array_c8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_c8 (gfc_array_c8 * const restrict retarray,
   GFC_COMPLEX_8 * restrict dest;
   const GFC_COMPLEX_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_c8 (gfc_array_c8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_c8 (gfc_array_c8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_i1.c
===================================================================
--- generated/sum_i1.c	(Revision 250720)
+++ generated/sum_i1.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_i1 (gfc_array_i1 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_i1 (gfc_array_i1 * const restrict retarray,
   GFC_INTEGER_1 * restrict dest;
   const GFC_INTEGER_1 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_i1 (gfc_array_i1 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_i16.c
===================================================================
--- generated/sum_i16.c	(Revision 250720)
+++ generated/sum_i16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_i16 (gfc_array_i16 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_i16 (gfc_array_i16 * const restrict retarray,
   GFC_INTEGER_16 * restrict dest;
   const GFC_INTEGER_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_i16 (gfc_array_i16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_i16 (gfc_array_i16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_i2.c
===================================================================
--- generated/sum_i2.c	(Revision 250720)
+++ generated/sum_i2.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_i2 (gfc_array_i2 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_i2 (gfc_array_i2 * const restrict retarray,
   GFC_INTEGER_2 * restrict dest;
   const GFC_INTEGER_2 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_i2 (gfc_array_i2 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_i4.c
===================================================================
--- generated/sum_i4.c	(Revision 250720)
+++ generated/sum_i4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_i4 (gfc_array_i4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_i4 (gfc_array_i4 * const restrict retarray,
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_i4 (gfc_array_i4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_i8.c
===================================================================
--- generated/sum_i8.c	(Revision 250720)
+++ generated/sum_i8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_i8 (gfc_array_i8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_i8 (gfc_array_i8 * const restrict retarray,
   GFC_INTEGER_8 * restrict dest;
   const GFC_INTEGER_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_i8 (gfc_array_i8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_r10.c
===================================================================
--- generated/sum_r10.c	(Revision 250720)
+++ generated/sum_r10.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_r10 (gfc_array_r10 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_r10 (gfc_array_r10 * const restrict retarray,
   GFC_REAL_10 * restrict dest;
   const GFC_REAL_10 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_r10 (gfc_array_r10 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_r10 (gfc_array_r10 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_r16.c
===================================================================
--- generated/sum_r16.c	(Revision 250720)
+++ generated/sum_r16.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_r16 (gfc_array_r16 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_r16 (gfc_array_r16 * const restrict retarray,
   GFC_REAL_16 * restrict dest;
   const GFC_REAL_16 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_r16 (gfc_array_r16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_r16 (gfc_array_r16 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_r4.c
===================================================================
--- generated/sum_r4.c	(Revision 250720)
+++ generated/sum_r4.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_r4 (gfc_array_r4 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_r4 (gfc_array_r4 * const restrict retarray,
   GFC_REAL_4 * restrict dest;
   const GFC_REAL_4 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_r4 (gfc_array_r4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_r4 (gfc_array_r4 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
Index: generated/sum_r8.c
===================================================================
--- generated/sum_r8.c	(Revision 250720)
+++ generated/sum_r8.c	(Arbeitskopie)
@@ -52,9 +52,16 @@ sum_r8 (gfc_array_r8 * const restrict retarray,
   int continue_loop;
 
   /* Make dim zero based to avoid confusion.  */
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
   dim = (*pdim) - 1;
-  rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len < 0)
     len = 0;
@@ -203,8 +210,8 @@ msum_r8 (gfc_array_r8 * const restrict retarray,
   GFC_REAL_8 * restrict dest;
   const GFC_REAL_8 * restrict base;
   const GFC_LOGICAL_1 * restrict mbase;
-  int rank;
-  int dim;
+  index_type rank;
+  index_type dim;
   index_type n;
   index_type len;
   index_type delta;
@@ -214,6 +221,14 @@ msum_r8 (gfc_array_r8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   len = GFC_DESCRIPTOR_EXTENT(array,dim);
   if (len <= 0)
     return;
@@ -391,6 +406,13 @@ ssum_r8 (gfc_array_r8 * const restrict retarray,
   dim = (*pdim) - 1;
   rank = GFC_DESCRIPTOR_RANK (array) - 1;
 
+  if (unlikely (dim < 0 || dim > rank))
+    {
+      runtime_error ("Dim argument incorrect in SUM intrinsic: "
+ 		     "is %ld, should be between 1 and %ld",
+		     (long int) dim + 1, (long int) rank + 1);
+    }
+
   for (n = 0; n < dim; n++)
     {
       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
