program main
  implicit none
  integer(8) i,N,min,fumi
  integer(8),allocatable :: A(:)
  read(*, *)N
  allocate(A(N))
  read(*, *) (A(i), i = 1,N)
  fumi = 0
  min = 0

  do i = 1,N
    if (min > A(i)) then
      fumi = fumi + (min - A(i))
    else
      min = A(i)
    end if
  end do
  
  write(*, *) fumi
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
@_QQcl.87527709064593a35e1b6646c2edf63e = linkonce constant [72 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_759/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 i64, align 8
  %2 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.87527709064593a35e1b6646c2edf63e, i32 5)
  %3 = call i1 @_FortranAioInputInteger(ptr %2, ptr nonnull %1, i32 8)
  %4 = call i32 @_FortranAioEndIoStatement(ptr %2)
  %5 = load i64, ptr %1, align 8, !tbaa !3
  %6 = call i64 @llvm.smax.i64(i64 %5, i64 0)
  %7 = shl i64 %6, 3
  %8 = call ptr @malloc(i64 %7)
  store ptr %8, ptr @_QFEa.0, align 8, !tbaa !7
  %9 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.87527709064593a35e1b6646c2edf63e, i32 7)
  %10 = load i64, ptr %1, align 8, !tbaa !3
  %11 = icmp sgt i64 %10, 0
  br i1 %11, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %.lr.ph
  %12 = phi i64 [ %16, %.lr.ph ], [ 1, %0 ]
  %.unpack122 = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %13 = add i64 %12, -1
  %14 = getelementptr i64, ptr %.unpack122, i64 %13
  %15 = call i1 @_FortranAioInputInteger(ptr %9, ptr %14, i32 8)
  %16 = add nuw i64 %12, 1
  %exitcond.not = icmp eq i64 %12, %10
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %0
  %17 = call i32 @_FortranAioEndIoStatement(ptr %9)
  %18 = load i64, ptr %1, align 8, !tbaa !3
  %19 = icmp sgt i64 %18, 0
  br i1 %19, label %.lr.ph137, label %._crit_edge138

.lr.ph137:                                        ; preds = %._crit_edge
  %.unpack = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %xtraiter = and i64 %18, 1
  %20 = icmp eq i64 %18, 1
  br i1 %20, label %._crit_edge138.loopexit.unr-lcssa, label %.lr.ph137.new

.lr.ph137.new:                                    ; preds = %.lr.ph137
  %unroll_iter = and i64 %18, -2
  br label %21

21:                                               ; preds = %21, %.lr.ph137.new
  %22 = phi i64 [ 1, %.lr.ph137.new ], [ %34, %21 ]
  %.0135 = phi i64 [ 0, %.lr.ph137.new ], [ %.1.1, %21 ]
  %.096134 = phi i64 [ 0, %.lr.ph137.new ], [ %.197.1, %21 ]
  %niter = phi i64 [ 0, %.lr.ph137.new ], [ %niter.next.1, %21 ]
  %23 = add nsw i64 %22, -1
  %24 = getelementptr i64, ptr %.unpack, i64 %23
  %25 = load i64, ptr %24, align 8, !tbaa !3
  %26 = icmp sgt i64 %.0135, %25
  %27 = sub i64 %.0135, %25
  %28 = select i1 %26, i64 %27, i64 0
  %.197 = add i64 %28, %.096134
  %.1 = call i64 @llvm.smax.i64(i64 %.0135, i64 %25)
  %29 = getelementptr i64, ptr %.unpack, i64 %22
  %30 = load i64, ptr %29, align 8, !tbaa !3
  %31 = icmp sgt i64 %.1, %30
  %32 = sub i64 %.1, %30
  %33 = select i1 %31, i64 %32, i64 0
  %.197.1 = add i64 %33, %.197
  %.1.1 = call i64 @llvm.smax.i64(i64 %.1, i64 %30)
  %34 = add nuw i64 %22, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge138.loopexit.unr-lcssa.loopexit, label %21

._crit_edge138.loopexit.unr-lcssa.loopexit:       ; preds = %21
  %35 = add i64 %22, 1
  br label %._crit_edge138.loopexit.unr-lcssa

._crit_edge138.loopexit.unr-lcssa:                ; preds = %._crit_edge138.loopexit.unr-lcssa.loopexit, %.lr.ph137
  %.197.lcssa.ph = phi i64 [ undef, %.lr.ph137 ], [ %.197.1, %._crit_edge138.loopexit.unr-lcssa.loopexit ]
  %.unr = phi i64 [ 0, %.lr.ph137 ], [ %35, %._crit_edge138.loopexit.unr-lcssa.loopexit ]
  %.0135.unr = phi i64 [ 0, %.lr.ph137 ], [ %.1.1, %._crit_edge138.loopexit.unr-lcssa.loopexit ]
  %.096134.unr = phi i64 [ 0, %.lr.ph137 ], [ %.197.1, %._crit_edge138.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge138, label %._crit_edge138.loopexit.epilog-lcssa

._crit_edge138.loopexit.epilog-lcssa:             ; preds = %._crit_edge138.loopexit.unr-lcssa
  %36 = getelementptr i64, ptr %.unpack, i64 %.unr
  %37 = load i64, ptr %36, align 8, !tbaa !3
  %38 = icmp sgt i64 %.0135.unr, %37
  %39 = sub i64 %.0135.unr, %37
  %40 = select i1 %38, i64 %39, i64 0
  %.197.epil = add i64 %40, %.096134.unr
  br label %._crit_edge138

._crit_edge138:                                   ; preds = %._crit_edge138.loopexit.epilog-lcssa, %._crit_edge138.loopexit.unr-lcssa, %._crit_edge
  %.096.lcssa = phi i64 [ 0, %._crit_edge ], [ %.197.lcssa.ph, %._crit_edge138.loopexit.unr-lcssa ], [ %.197.epil, %._crit_edge138.loopexit.epilog-lcssa ]
  %41 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.87527709064593a35e1b6646c2edf63e, i32 19)
  %42 = call i1 @_FortranAioOutputInteger64(ptr %41, i64 %.096.lcssa)
  %43 = call i32 @_FortranAioEndIoStatement(ptr %41)
  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 ptr @_FortranAioBeginExternalListOutput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger64(ptr, i64) local_unnamed_addr

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

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
attributes #1 = { nocallback nofree nosync nounwind speculatable 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}
