program grand_garden
  implicit none
  integer :: n, h(100) = 0, i
  read(*,*) n
  read(*,*) h(1:n)
  write(*,'(i0)') watering(h(1:n))
contains
  recursive function watering(h) result(res)
    integer, intent(in) :: h(:)
    integer :: n, m, l, r, i, res
    res = 0
    if (maxval(h) == 0) return
    n = size(h)
    m = minval(h)
    res = m
    l = 0
    do r = 1, n
      if (r == n .or. h(r) == m) then
        i = merge(r-1,n,h(r) == m)
        if (l+1 <= i) res = res+watering(h(l+1:i)-m)
        l = r
      end if
    end do
  end
end program grand_garden
; 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"

@_QFEh = internal global [100 x i32] zeroinitializer
@_QQcl.5243fa386434665936886f2aa1d24411 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_5084/source.f\00"
@_QQcl.28693029 = linkonce constant [4 x i8] c"(i0)"
@_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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

define void @_QQmain() local_unnamed_addr {
  %1 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %2 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %3 = alloca i32, align 4
  %4 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.5243fa386434665936886f2aa1d24411, i32 4)
  %5 = call i1 @_FortranAioInputInteger(ptr %4, ptr nonnull %3, i32 4)
  %6 = call i32 @_FortranAioEndIoStatement(ptr %4)
  %7 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.5243fa386434665936886f2aa1d24411, i32 5)
  %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
  store ptr @_QFEh, ptr %2, align 8, !tbaa !7
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, 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 %2, 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 %2, 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 %2, 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 %2, 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 %2, 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 %2, 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 %2, i64 0, i32 7, i64 0, i64 1
  store i64 %10, 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 %2, i64 0, i32 7, i64 0, i64 2
  store i64 4, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %11 = call i1 @_FortranAioInputDescriptor(ptr %7, ptr nonnull %2)
  %12 = call i32 @_FortranAioEndIoStatement(ptr %7)
  %13 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.5243fa386434665936886f2aa1d24411, i32 6)
  %14 = load i32, ptr %3, align 4, !tbaa !3
  %15 = call i32 @llvm.smax.i32(i32 %14, i32 0)
  %16 = zext i32 %15 to i64
  store ptr @_QFEh, ptr %1, align 8, !tbaa !7
  %.fca.1.gep4 = 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.gep4, align 8, !tbaa !7
  %.fca.2.gep6 = 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.gep6, align 8, !tbaa !7
  %.fca.3.gep8 = 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.gep8, align 4, !tbaa !7
  %.fca.4.gep10 = 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.gep10, align 1, !tbaa !7
  %.fca.5.gep12 = 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.gep12, align 2, !tbaa !7
  %.fca.6.gep14 = 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.gep14, align 1, !tbaa !7
  %.fca.7.0.0.gep16 = 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.gep16, align 8, !tbaa !7
  %.fca.7.0.1.gep18 = 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 %16, ptr %.fca.7.0.1.gep18, align 8, !tbaa !7
  %.fca.7.0.2.gep20 = 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.gep20, align 8, !tbaa !7
  %17 = call i32 @_QFPwatering(ptr nonnull %1)
  %18 = call i1 @_FortranAioOutputInteger32(ptr %13, i32 %17)
  %19 = call i32 @_FortranAioEndIoStatement(ptr %13)
  ret void
}

define i32 @_QFPwatering(ptr %0) local_unnamed_addr {
  %2 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %3 = tail call i32 @_FortranAMaxvalInteger4(ptr %0, ptr nonnull @_QQcl.5243fa386434665936886f2aa1d24411, i32 11, i32 0, ptr null)
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %.loopexit, label %5

5:                                                ; preds = %1
  %6 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %0, i64 0, i32 7, i64 0, i64 1
  %7 = load i64, ptr %6, align 8, !tbaa !7
  %8 = tail call i32 @_FortranAMinvalInteger4(ptr %0, ptr nonnull @_QQcl.5243fa386434665936886f2aa1d24411, i32 14, i32 0, ptr null)
  %9 = trunc i64 %7 to i32
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.lr.ph26, label %.loopexit

.lr.ph26:                                         ; preds = %5
  %11 = and i64 %7, 4294967295
  %12 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %0, i64 0, i32 7, i64 0, i64 2
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, i64 0, i32 1
  %.fca.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, i64 0, i32 2
  %.fca.3.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, i64 0, i32 3
  %.fca.4.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, i64 0, i32 4
  %.fca.5.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, i64 0, i32 5
  %.fca.6.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, i64 0, i32 6
  %.fca.7.0.0.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, i64 0, i32 7, i64 0, i64 0
  %.fca.7.0.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, i64 0, i32 7, i64 0, i64 1
  %.fca.7.0.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, i64 0, i32 7, i64 0, i64 2
  %13 = and i64 %7, 4294967295
  br label %14

14:                                               ; preds = %.lr.ph26, %53
  %indvars.iv = phi i64 [ 1, %.lr.ph26 ], [ %indvars.iv.next, %53 ]
  %15 = phi i64 [ %11, %.lr.ph26 ], [ %54, %53 ]
  %.024 = phi i32 [ %8, %.lr.ph26 ], [ %.2, %53 ]
  %.01723 = phi i32 [ 0, %.lr.ph26 ], [ %.118, %53 ]
  %16 = icmp eq i64 %indvars.iv, %13
  %17 = add nsw i64 %indvars.iv, -1
  %18 = load ptr, ptr %0, align 8, !tbaa !7
  %19 = load i64, ptr %12, align 8, !tbaa !7
  %20 = mul i64 %19, %17
  %21 = getelementptr i8, ptr %18, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !3
  %23 = icmp eq i32 %22, %8
  %24 = or i1 %16, %23
  br i1 %24, label %25, label %53

25:                                               ; preds = %14
  %.v = select i1 %23, i64 %17, i64 %7
  %26 = trunc i64 %.v to i32
  %27 = add i32 %.01723, 1
  %.not = icmp sgt i32 %27, %26
  %28 = trunc i64 %indvars.iv to i32
  br i1 %.not, label %53, label %29

29:                                               ; preds = %25
  %30 = sext i32 %27 to i64
  %sext28 = shl i64 %.v, 32
  %31 = ashr exact i64 %sext28, 32
  %32 = sub nsw i64 %31, %30
  %33 = call i64 @llvm.smax.i64(i64 %32, i64 -1)
  %34 = add nsw i64 %33, 1
  %35 = shl nuw nsw i64 %34, 2
  %36 = call ptr @malloc(i64 %35)
  %37 = icmp sgt i64 %32, -1
  br i1 %37, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %29
  %38 = add nsw i64 %30, -1
  br label %39

39:                                               ; preds = %.lr.ph, %39
  %40 = phi i64 [ %34, %.lr.ph ], [ %49, %39 ]
  %41 = phi i64 [ 0, %.lr.ph ], [ %42, %39 ]
  %42 = add nuw nsw i64 %41, 1
  %43 = add nsw i64 %38, %41
  %44 = mul i64 %43, %19
  %45 = getelementptr i8, ptr %18, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !3
  %47 = sub i32 %46, %8
  %48 = getelementptr i32, ptr %36, i64 %41
  store i32 %47, ptr %48, align 4, !tbaa !3
  %49 = add nsw i64 %40, -1
  %50 = icmp ugt i64 %40, 1
  br i1 %50, label %39, label %._crit_edge

._crit_edge:                                      ; preds = %39, %29
  store ptr %36, ptr %2, align 8, !tbaa !7
  store i64 4, ptr %.fca.1.gep, align 8, !tbaa !7
  store i32 20180515, ptr %.fca.2.gep, align 8, !tbaa !7
  store i8 1, ptr %.fca.3.gep, align 4, !tbaa !7
  store i8 9, ptr %.fca.4.gep, align 1, !tbaa !7
  store i8 0, ptr %.fca.5.gep, align 2, !tbaa !7
  store i8 0, ptr %.fca.6.gep, align 1, !tbaa !7
  store i64 1, ptr %.fca.7.0.0.gep, align 8, !tbaa !7
  store i64 %34, ptr %.fca.7.0.1.gep, align 8, !tbaa !7
  store i64 4, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %51 = call i32 @_QFPwatering(ptr nonnull %2)
  %52 = add i32 %51, %.024
  call void @free(ptr %36)
  br label %53

53:                                               ; preds = %25, %._crit_edge, %14
  %.118 = phi i32 [ %.01723, %14 ], [ %28, %._crit_edge ], [ %28, %25 ]
  %.2 = phi i32 [ %.024, %14 ], [ %52, %._crit_edge ], [ %.024, %25 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %54 = add nsw i64 %15, -1
  %55 = icmp sgt i64 %15, 1
  br i1 %55, label %14, label %.loopexit

.loopexit:                                        ; preds = %53, %5, %1
  %.3 = phi i32 [ 0, %1 ], [ %8, %5 ], [ %.2, %53 ]
  ret i32 %.3
}

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 @_FortranAioBeginExternalFormattedOutput(ptr, i64, ptr, i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

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

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

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

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

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