program main
      implicit none
      integer(4) :: k, n, i, dis
      integer(4), allocatable :: a(:), diff(:)

      read(*,*) k, n
      allocate( a(n) )
      read(*,*) a
      allocate( diff(n) )

      do i = 1, n
        if ( i == n ) then
          diff(i) = a(1) + k - a(n)
        else
          diff(i) = a(i+1) - a(i)
        end if
      end do

      dis = k - maxval( diff )
      print *, dis 

end program main

; ModuleID = 'FIRModule'
source_filename = "FIRModule"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@_QFEa.0 = internal unnamed_addr global ptr null
@_QFEa.8 = internal unnamed_addr global i64 0
@_QFEdiff = internal unnamed_addr global { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } { ptr null, i64 ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64), i32 20180515, i8 1, i8 9, i8 2, i8 0, [1 x [3 x i64]] [[3 x i64] [i64 1, i64 0, i64 ptrtoint (ptr getelementptr (i32, ptr null, i32 1) to i64)]] }
@_QQcl.146542cf49c18460f57d8bd10b97de16 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_1974/source.f\00"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

define void @_QQmain() local_unnamed_addr {
  %1 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.146542cf49c18460f57d8bd10b97de16, i32 6)
  %5 = call i1 @_FortranAioInputInteger(ptr %4, ptr nonnull %2, i32 4)
  %6 = call i1 @_FortranAioInputInteger(ptr %4, ptr nonnull %3, i32 4)
  %7 = call i32 @_FortranAioEndIoStatement(ptr %4)
  %8 = load i32, ptr %3, align 4, !tbaa !3
  %9 = call i32 @llvm.smax.i32(i32 %8, i32 0)
  %10 = zext i32 %9 to i64
  %11 = shl nuw nsw i64 %10, 2
  %12 = call ptr @malloc(i64 %11)
  store ptr %12, ptr @_QFEa.0, align 8, !tbaa !7
  store i64 %10, ptr @_QFEa.8, align 8, !tbaa !7
  %13 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.146542cf49c18460f57d8bd10b97de16, i32 8)
  %.unpack = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %.unpack279.unpack.unpack281 = load i64, ptr @_QFEa.8, align 8, !tbaa !7
  store ptr %.unpack, ptr %1, align 8, !tbaa !7
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 1
  store i64 4, ptr %.fca.1.gep, align 8, !tbaa !7
  %.fca.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 2
  store i32 20180515, ptr %.fca.2.gep, align 8, !tbaa !7
  %.fca.3.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 3
  store i8 1, ptr %.fca.3.gep, align 4, !tbaa !7
  %.fca.4.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 4
  store i8 9, ptr %.fca.4.gep, align 1, !tbaa !7
  %.fca.5.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 5
  store i8 0, ptr %.fca.5.gep, align 2, !tbaa !7
  %.fca.6.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 6
  store i8 0, ptr %.fca.6.gep, align 1, !tbaa !7
  %.fca.7.0.0.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 0
  store i64 1, ptr %.fca.7.0.0.gep, align 8, !tbaa !7
  %.fca.7.0.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 1
  store i64 %.unpack279.unpack.unpack281, ptr %.fca.7.0.1.gep, align 8, !tbaa !7
  %.fca.7.0.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 2
  store i64 4, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %14 = call i1 @_FortranAioInputDescriptor(ptr %13, ptr nonnull %1)
  %15 = call i32 @_FortranAioEndIoStatement(ptr %13)
  %16 = load i32, ptr %3, align 4, !tbaa !3
  %17 = call i32 @llvm.smax.i32(i32 %16, i32 0)
  %18 = zext i32 %17 to i64
  %19 = shl nuw nsw i64 %18, 2
  %20 = call ptr @malloc(i64 %19)
  store ptr %20, ptr @_QFEdiff, align 16, !tbaa !7
  store i64 4, ptr getelementptr inbounds ({ ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEdiff, i64 0, i32 1), align 8, !tbaa !7
  store i32 20180515, ptr getelementptr inbounds ({ ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEdiff, i64 0, i32 2), align 16, !tbaa !7
  store i8 1, ptr getelementptr inbounds ({ ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEdiff, i64 0, i32 3), align 4, !tbaa !7
  store i8 9, ptr getelementptr inbounds ({ ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEdiff, i64 0, i32 4), align 1, !tbaa !7
  store i8 2, ptr getelementptr inbounds ({ ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEdiff, i64 0, i32 5), align 2, !tbaa !7
  store i8 0, ptr getelementptr inbounds ({ ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEdiff, i64 0, i32 6), align 1, !tbaa !7
  store i64 1, ptr getelementptr inbounds ({ ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEdiff, i64 0, i32 7), align 8, !tbaa !7
  store i64 %18, ptr getelementptr inbounds ({ ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEdiff, i64 0, i32 7, i64 0, i64 1), align 16, !tbaa !7
  store i64 4, ptr getelementptr inbounds ({ ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEdiff, i64 0, i32 7, i64 0, i64 2), align 8, !tbaa !7
  %21 = icmp sgt i32 %16, 0
  br i1 %21, label %.lr.ph, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %0
  %.pre = load i32, ptr %2, align 4, !tbaa !3
  br label %_FortranAMaxvalInteger4x1_contract_simplified.exit

.lr.ph:                                           ; preds = %0
  %22 = zext i32 %16 to i64
  %.unpack305 = load ptr, ptr @_QFEa.0, align 8
  %23 = load i32, ptr %2, align 4
  %24 = add nsw i64 %22, -1
  %25 = getelementptr i32, ptr %.unpack305, i64 %24
  %26 = zext i32 %16 to i64
  %27 = add nuw nsw i64 %22, 1
  %28 = add nsw i64 %26, -1
  %29 = getelementptr i32, ptr %20, i64 %28
  br label %30

30:                                               ; preds = %.lr.ph, %49
  %indvars.iv = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next.pre-phi, %49 ]
  %31 = icmp eq i64 %indvars.iv, %26
  br i1 %31, label %32, label %37

32:                                               ; preds = %30
  %33 = load i32, ptr %.unpack305, align 4, !tbaa !3
  %34 = add i32 %23, %33
  %35 = load i32, ptr %25, align 4, !tbaa !3
  %36 = sub i32 %34, %35
  store i32 %36, ptr %29, align 4, !tbaa !3
  %.pre354 = add nuw nsw i64 %indvars.iv, 1
  br label %49

37:                                               ; preds = %30
  %38 = add nuw nsw i64 %indvars.iv, 1
  %sext = shl i64 %38, 32
  %39 = ashr exact i64 %sext, 32
  %40 = add nsw i64 %39, -1
  %41 = getelementptr i32, ptr %.unpack305, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !3
  %43 = add i64 %indvars.iv, -1
  %44 = getelementptr i32, ptr %.unpack305, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !3
  %46 = sub i32 %42, %45
  %47 = add nsw i64 %indvars.iv, -1
  %48 = getelementptr i32, ptr %20, i64 %47
  store i32 %46, ptr %48, align 4, !tbaa !3
  br label %49

49:                                               ; preds = %32, %37
  %indvars.iv.next.pre-phi = phi i64 [ %.pre354, %32 ], [ %38, %37 ]
  %exitcond.not = icmp eq i64 %indvars.iv.next.pre-phi, %27
  br i1 %exitcond.not, label %._crit_edge, label %30

._crit_edge:                                      ; preds = %49
  %.not = icmp slt i32 %16, 1
  br i1 %.not, label %_FortranAMaxvalInteger4x1_contract_simplified.exit, label %.lr.ph.i.preheader

.lr.ph.i.preheader:                               ; preds = %._crit_edge
  %min.iters.check = icmp ult i32 %17, 8
  br i1 %min.iters.check, label %.lr.ph.i.preheader358, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.i.preheader
  %n.vec = and i64 %18, 2147483640
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ <i32 -2147483648, i32 -2147483648, i32 -2147483648, i32 -2147483648>, %vector.ph ], [ %53, %vector.body ]
  %vec.phi356 = phi <4 x i32> [ <i32 -2147483648, i32 -2147483648, i32 -2147483648, i32 -2147483648>, %vector.ph ], [ %54, %vector.body ]
  %50 = shl i64 %index, 2
  %51 = getelementptr i8, ptr %20, i64 %50
  %wide.load = load <4 x i32>, ptr %51, align 4, !tbaa !3
  %52 = getelementptr i32, ptr %51, i64 4
  %wide.load357 = load <4 x i32>, ptr %52, align 4, !tbaa !3
  %53 = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %wide.load, <4 x i32> %vec.phi)
  %54 = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %wide.load357, <4 x i32> %vec.phi356)
  %index.next = add nuw i64 %index, 8
  %55 = icmp eq i64 %index.next, %n.vec
  br i1 %55, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %rdx.minmax.select = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %53, <4 x i32> %54)
  %56 = call i32 @llvm.vector.reduce.smax.v4i32(<4 x i32> %rdx.minmax.select)
  %cmp.n = icmp eq i64 %n.vec, %18
  br i1 %cmp.n, label %_FortranAMaxvalInteger4x1_contract_simplified.exit, label %.lr.ph.i.preheader358

.lr.ph.i.preheader358:                            ; preds = %.lr.ph.i.preheader, %middle.block
  %.ph = phi i32 [ -2147483648, %.lr.ph.i.preheader ], [ %56, %middle.block ]
  %.ph359 = phi i64 [ 0, %.lr.ph.i.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph.i

.lr.ph.i:                                         ; preds = %.lr.ph.i.preheader358, %.lr.ph.i
  %57 = phi i32 [ %62, %.lr.ph.i ], [ %.ph, %.lr.ph.i.preheader358 ]
  %58 = phi i64 [ %63, %.lr.ph.i ], [ %.ph359, %.lr.ph.i.preheader358 ]
  %59 = shl i64 %58, 2
  %60 = getelementptr i8, ptr %20, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !3
  %62 = call i32 @llvm.smax.i32(i32 %61, i32 %57)
  %63 = add nuw nsw i64 %58, 1
  %exitcond.not.i = icmp eq i64 %63, %18
  br i1 %exitcond.not.i, label %_FortranAMaxvalInteger4x1_contract_simplified.exit, label %.lr.ph.i, !llvm.loop !12

_FortranAMaxvalInteger4x1_contract_simplified.exit: ; preds = %.lr.ph.i, %middle.block, %._crit_edge.thread, %._crit_edge
  %64 = phi i32 [ %23, %._crit_edge ], [ %.pre, %._crit_edge.thread ], [ %23, %middle.block ], [ %23, %.lr.ph.i ]
  %.lcssa.i = phi i32 [ -2147483648, %._crit_edge ], [ -2147483648, %._crit_edge.thread ], [ %56, %middle.block ], [ %62, %.lr.ph.i ]
  %65 = sub i32 %64, %.lcssa.i
  %66 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.146542cf49c18460f57d8bd10b97de16, i32 20)
  %67 = call i1 @_FortranAioOutputInteger32(ptr %66, i32 %65)
  %68 = call i32 @_FortranAioEndIoStatement(ptr %66)
  ret void
}

declare ptr @_FortranAioBeginExternalListInput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioInputInteger(ptr, ptr, i32) local_unnamed_addr

declare i32 @_FortranAioEndIoStatement(ptr) local_unnamed_addr

declare i1 @_FortranAioInputDescriptor(ptr, ptr) local_unnamed_addr

declare ptr @_FortranAioBeginExternalListOutput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.smax.v4i32(<4 x i32>, <4 x i32>) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i32 @llvm.vector.reduce.smax.v4i32(<4 x i32>) #3

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nocallback nofree nosync nounwind willreturn memory(none) }

!llvm.module.flags = !{!0, !1, !2}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{!4, !4, i64 0}
!4 = !{!"any data access", !5, i64 0}
!5 = !{!"any access", !6, i64 0}
!6 = !{!"Flang Type TBAA Root"}
!7 = !{!8, !8, i64 0}
!8 = !{!"descriptor member", !5, i64 0}
!9 = distinct !{!9, !10, !11}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !11, !10}
