program main

  integer :: N, add

  integer, allocatable, dimension ( : ) :: A, B

  read ( *, * ) N

  allocate ( A ( 1:N-1 ) )

  allocate ( B ( 1:N ) )

  read ( *, * ) A ( 1:N-1 )

  B ( : ) = 0

  B ( 1 ) = A ( 1 )

  B ( N ) = A ( N - 1 )
  
  do i = 2, N - 1

     B ( i ) = min ( A ( i - 1 ), A ( i ) )
     
  end do
  
  print '(i0)', sum ( B ( 1:N ) )

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
@_QFEb.0 = internal unnamed_addr global ptr null
@_QFEb.8 = internal unnamed_addr global i64 0
@_QQcl.a83e9796702270deefa298f854793476 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_3313/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

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 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.a83e9796702270deefa298f854793476, i32 7)
  %4 = call i1 @_FortranAioInputInteger(ptr %3, ptr nonnull %2, i32 4)
  %5 = call i32 @_FortranAioEndIoStatement(ptr %3)
  %6 = load i32, ptr %2, align 4, !tbaa !3
  %7 = add i32 %6, -1
  %8 = call i32 @llvm.smax.i32(i32 %7, i32 0)
  %9 = zext i32 %8 to i64
  %10 = shl nuw nsw i64 %9, 2
  %11 = call ptr @malloc(i64 %10)
  store ptr %11, ptr @_QFEa.0, align 8, !tbaa !7
  %12 = call i32 @llvm.smax.i32(i32 %6, i32 0)
  %13 = zext i32 %12 to i64
  %14 = shl nuw nsw i64 %13, 2
  %15 = call ptr @malloc(i64 %14)
  store ptr %15, ptr @_QFEb.0, align 8, !tbaa !7
  store i64 %13, ptr @_QFEb.8, align 8, !tbaa !7
  %16 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.a83e9796702270deefa298f854793476, i32 13)
  %.unpack = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %17 = load i32, ptr %2, align 4, !tbaa !3
  %18 = add i32 %17, -1
  %19 = call i32 @llvm.smax.i32(i32 %18, i32 0)
  %20 = zext i32 %19 to i64
  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 %20, 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
  %21 = call i1 @_FortranAioInputDescriptor(ptr %16, ptr nonnull %1)
  %22 = call i32 @_FortranAioEndIoStatement(ptr %16)
  %.unpack369 = load ptr, ptr @_QFEb.0, align 8, !tbaa !7
  %.unpack376.unpack.unpack378 = load i64, ptr @_QFEb.8, align 8, !tbaa !7
  %.not468 = icmp slt i64 %.unpack376.unpack.unpack378, 1
  br i1 %.not468, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %23 = shl nuw i64 %.unpack376.unpack.unpack378, 2
  call void @llvm.memset.p0.i64(ptr align 4 %.unpack369, i8 0, i64 %23, i1 false), !tbaa !3
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %0
  %.unpack381 = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %24 = load i32, ptr %.unpack381, align 4, !tbaa !3
  store i32 %24, ptr %.unpack369, align 4, !tbaa !3
  %25 = load i32, ptr %2, align 4, !tbaa !3
  %26 = add i32 %25, -1
  %27 = sext i32 %26 to i64
  %28 = add nsw i64 %27, -1
  %29 = getelementptr i32, ptr %.unpack381, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !3
  %31 = sext i32 %25 to i64
  %32 = add nsw i64 %31, -1
  %33 = getelementptr i32, ptr %.unpack369, i64 %32
  store i32 %30, ptr %33, align 4, !tbaa !3
  %34 = load i32, ptr %2, align 4, !tbaa !3
  %35 = add i32 %34, -1
  %36 = icmp sgt i32 %35, 1
  br i1 %36, label %.lr.ph466.preheader, label %._crit_edge467

.lr.ph466.preheader:                              ; preds = %._crit_edge
  %37 = zext i32 %35 to i64
  %38 = add nsw i64 %37, -1
  %min.iters.check = icmp ult i32 %35, 9
  br i1 %min.iters.check, label %.lr.ph466.preheader493, label %vector.memcheck

vector.memcheck:                                  ; preds = %.lr.ph466.preheader
  %uglygep = getelementptr i8, ptr %.unpack369, i64 4
  %39 = shl nuw nsw i64 %37, 2
  %uglygep470 = getelementptr i8, ptr %.unpack369, i64 %39
  %uglygep471 = getelementptr i8, ptr %.unpack381, i64 %39
  %bound0 = icmp ult ptr %uglygep, %uglygep471
  %bound1 = icmp ult ptr %.unpack381, %uglygep470
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph466.preheader493, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %38, -8
  %ind.end = or i64 %n.vec, 2
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %40 = getelementptr i32, ptr %.unpack381, i64 %index
  %wide.load = load <4 x i32>, ptr %40, align 4, !tbaa !3, !alias.scope !9
  %41 = getelementptr i32, ptr %40, i64 4
  %wide.load472 = load <4 x i32>, ptr %41, align 4, !tbaa !3, !alias.scope !9
  %42 = or i64 %index, 1
  %43 = getelementptr i32, ptr %.unpack381, i64 %42
  %wide.load473 = load <4 x i32>, ptr %43, align 4, !tbaa !3, !alias.scope !9
  %44 = getelementptr i32, ptr %43, i64 4
  %wide.load474 = load <4 x i32>, ptr %44, align 4, !tbaa !3, !alias.scope !9
  %45 = call <4 x i32> @llvm.smin.v4i32(<4 x i32> %wide.load, <4 x i32> %wide.load473)
  %46 = call <4 x i32> @llvm.smin.v4i32(<4 x i32> %wide.load472, <4 x i32> %wide.load474)
  %47 = or i64 %index, 1
  %48 = getelementptr i32, ptr %.unpack369, i64 %47
  store <4 x i32> %45, ptr %48, align 4, !tbaa !3, !alias.scope !12, !noalias !9
  %49 = getelementptr i32, ptr %48, i64 4
  store <4 x i32> %46, ptr %49, align 4, !tbaa !3, !alias.scope !12, !noalias !9
  %index.next = add nuw i64 %index, 8
  %50 = icmp eq i64 %index.next, %n.vec
  br i1 %50, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %38, %n.vec
  br i1 %cmp.n, label %._crit_edge467, label %.lr.ph466.preheader493

.lr.ph466.preheader493:                           ; preds = %vector.memcheck, %.lr.ph466.preheader, %middle.block
  %indvars.iv.ph = phi i64 [ 2, %vector.memcheck ], [ 2, %.lr.ph466.preheader ], [ %ind.end, %middle.block ]
  %51 = add nuw nsw i64 %37, 1
  %52 = sub nsw i64 %51, %indvars.iv.ph
  %xtraiter = and i64 %52, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph466.prol.loopexit, label %.lr.ph466.prol

.lr.ph466.prol:                                   ; preds = %.lr.ph466.preheader493
  %53 = add nsw i64 %indvars.iv.ph, -2
  %54 = getelementptr i32, ptr %.unpack381, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !3
  %56 = add nsw i64 %indvars.iv.ph, -1
  %57 = getelementptr i32, ptr %.unpack381, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !3
  %59 = call i32 @llvm.smin.i32(i32 %55, i32 %58)
  %60 = add nsw i64 %indvars.iv.ph, -1
  %61 = getelementptr i32, ptr %.unpack369, i64 %60
  store i32 %59, ptr %61, align 4, !tbaa !3
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.ph, 1
  br label %.lr.ph466.prol.loopexit

.lr.ph466.prol.loopexit:                          ; preds = %.lr.ph466.prol, %.lr.ph466.preheader493
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %.lr.ph466.preheader493 ], [ %indvars.iv.next.prol, %.lr.ph466.prol ]
  %62 = icmp eq i64 %indvars.iv.ph, %37
  br i1 %62, label %._crit_edge467, label %.lr.ph466

.lr.ph466:                                        ; preds = %.lr.ph466.prol.loopexit, %.lr.ph466
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %.lr.ph466 ], [ %indvars.iv.unr, %.lr.ph466.prol.loopexit ]
  %63 = add i64 %indvars.iv, -2
  %64 = getelementptr i32, ptr %.unpack381, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !3
  %66 = add i64 %indvars.iv, -1
  %67 = getelementptr i32, ptr %.unpack381, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !3
  %69 = call i32 @llvm.smin.i32(i32 %65, i32 %68)
  %70 = add i64 %indvars.iv, -1
  %71 = getelementptr i32, ptr %.unpack369, i64 %70
  store i32 %69, ptr %71, align 4, !tbaa !3
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %72 = add i64 %indvars.iv, -1
  %73 = getelementptr i32, ptr %.unpack381, i64 %72
  %74 = load i32, ptr %73, align 4, !tbaa !3
  %75 = getelementptr i32, ptr %.unpack381, i64 %indvars.iv
  %76 = load i32, ptr %75, align 4, !tbaa !3
  %77 = call i32 @llvm.smin.i32(i32 %74, i32 %76)
  %78 = getelementptr i32, ptr %.unpack369, i64 %indvars.iv
  store i32 %77, ptr %78, align 4, !tbaa !3
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next, %37
  br i1 %exitcond.not.1, label %._crit_edge467, label %.lr.ph466, !llvm.loop !17

._crit_edge467:                                   ; preds = %.lr.ph466.prol.loopexit, %.lr.ph466, %middle.block, %._crit_edge
  %79 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.a83e9796702270deefa298f854793476, i32 27)
  %.unpack429 = load ptr, ptr @_QFEb.0, align 8, !tbaa !7
  %80 = load i32, ptr %2, align 4, !tbaa !3
  %81 = call i32 @llvm.smax.i32(i32 %80, i32 0)
  %82 = zext i32 %81 to i64
  %.not = icmp slt i32 %80, 1
  br i1 %.not, label %_FortranASumInteger4x1_contract_simplified.exit, label %.lr.ph.i.preheader

.lr.ph.i.preheader:                               ; preds = %._crit_edge467
  %min.iters.check477 = icmp ult i32 %81, 8
  br i1 %min.iters.check477, label %.lr.ph.i.preheader489, label %vector.ph478

vector.ph478:                                     ; preds = %.lr.ph.i.preheader
  %n.vec480 = and i64 %82, 2147483640
  br label %vector.body483

vector.body483:                                   ; preds = %vector.body483, %vector.ph478
  %index484 = phi i64 [ 0, %vector.ph478 ], [ %index.next488, %vector.body483 ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph478 ], [ %86, %vector.body483 ]
  %vec.phi485 = phi <4 x i32> [ zeroinitializer, %vector.ph478 ], [ %87, %vector.body483 ]
  %83 = shl i64 %index484, 2
  %84 = getelementptr i8, ptr %.unpack429, i64 %83
  %wide.load486 = load <4 x i32>, ptr %84, align 4, !tbaa !3
  %85 = getelementptr i32, ptr %84, i64 4
  %wide.load487 = load <4 x i32>, ptr %85, align 4, !tbaa !3
  %86 = add <4 x i32> %wide.load486, %vec.phi
  %87 = add <4 x i32> %wide.load487, %vec.phi485
  %index.next488 = add nuw i64 %index484, 8
  %88 = icmp eq i64 %index.next488, %n.vec480
  br i1 %88, label %middle.block475, label %vector.body483, !llvm.loop !18

middle.block475:                                  ; preds = %vector.body483
  %bin.rdx = add <4 x i32> %87, %86
  %89 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n482 = icmp eq i64 %n.vec480, %82
  br i1 %cmp.n482, label %_FortranASumInteger4x1_contract_simplified.exit, label %.lr.ph.i.preheader489

.lr.ph.i.preheader489:                            ; preds = %.lr.ph.i.preheader, %middle.block475
  %.ph = phi i32 [ 0, %.lr.ph.i.preheader ], [ %89, %middle.block475 ]
  %.ph490 = phi i64 [ 0, %.lr.ph.i.preheader ], [ %n.vec480, %middle.block475 ]
  br label %.lr.ph.i

.lr.ph.i:                                         ; preds = %.lr.ph.i.preheader489, %.lr.ph.i
  %90 = phi i32 [ %95, %.lr.ph.i ], [ %.ph, %.lr.ph.i.preheader489 ]
  %91 = phi i64 [ %96, %.lr.ph.i ], [ %.ph490, %.lr.ph.i.preheader489 ]
  %92 = shl i64 %91, 2
  %93 = getelementptr i8, ptr %.unpack429, i64 %92
  %94 = load i32, ptr %93, align 4, !tbaa !3
  %95 = add i32 %94, %90
  %96 = add nuw nsw i64 %91, 1
  %exitcond.not.i = icmp eq i64 %96, %82
  br i1 %exitcond.not.i, label %_FortranASumInteger4x1_contract_simplified.exit, label %.lr.ph.i, !llvm.loop !19

_FortranASumInteger4x1_contract_simplified.exit:  ; preds = %.lr.ph.i, %middle.block475, %._crit_edge467
  %.lcssa.i = phi i32 [ 0, %._crit_edge467 ], [ %89, %middle.block475 ], [ %95, %.lr.ph.i ]
  %97 = call i1 @_FortranAioOutputInteger32(ptr %79, i32 %.lcssa.i)
  %98 = call i32 @_FortranAioEndIoStatement(ptr %79)
  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 @_FortranAioBeginExternalFormattedOutput(ptr, i64, ptr, i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

; Function Attrs: 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 i32 @llvm.smin.i32(i32, i32) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

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

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) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!10}
!10 = distinct !{!10, !11}
!11 = distinct !{!11, !"LVerDomain"}
!12 = !{!13}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !15, !16}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = distinct !{!17, !15}
!18 = distinct !{!18, !15, !16}
!19 = distinct !{!19, !16, !15}
