program main
  implicit none
  integer(8) :: n, x, i
  integer(8), allocatable :: a(:)
  read(*, *) n, x
  allocate(a(n))
  read(*, *) (a(i), i = 1, n)

  write(*, "(i0)") processes(n, x, a)

  deallocate(a)
  contains

  function processes(n, x, a)
    implicit none
    integer(8), intent(in) :: n, x
    integer(8), intent(inout) :: a(:)
    integer(8) :: i, processes, eat
    processes = 0
    do i = 2, n
      eat = max(0, a(i) + a(i - 1) - x)
      processes = processes + eat
      a(i) = max(0, a(i) - eat)
    end do
  end function processes

end

; 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.2816acb8ef93c601c858363c985d3af9 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_7720/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 i64, align 8
  %2 = alloca i64, align 8
  %3 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.2816acb8ef93c601c858363c985d3af9, i32 5)
  %4 = call i1 @_FortranAioInputInteger(ptr %3, ptr nonnull %1, i32 8)
  %5 = call i1 @_FortranAioInputInteger(ptr %3, ptr nonnull %2, i32 8)
  %6 = call i32 @_FortranAioEndIoStatement(ptr %3)
  %7 = load i64, ptr %1, align 8, !tbaa !3
  %8 = call i64 @llvm.smax.i64(i64 %7, i64 0)
  %9 = shl i64 %8, 3
  %10 = call ptr @malloc(i64 %9)
  store ptr %10, ptr @_QFEa.0, align 8, !tbaa !7
  %11 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.2816acb8ef93c601c858363c985d3af9, i32 7)
  %12 = load i64, ptr %1, align 8, !tbaa !3
  %13 = icmp sgt i64 %12, 0
  br i1 %13, label %.lr.ph, label %._crit_edge

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

._crit_edge:                                      ; preds = %.lr.ph, %0
  %19 = call i32 @_FortranAioEndIoStatement(ptr %11)
  %20 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.2816acb8ef93c601c858363c985d3af9, i32 9)
  %.unpack = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %21 = load i64, ptr %1, align 8, !tbaa !3
  %22 = add i64 %21, -1
  %23 = icmp sgt i64 %22, 0
  br i1 %23, label %.lr.ph.i.preheader, label %_QFPprocesses.exit

.lr.ph.i.preheader:                               ; preds = %._crit_edge
  %load_initial = load i64, ptr %.unpack, align 8
  br label %.lr.ph.i

.lr.ph.i:                                         ; preds = %.lr.ph.i.preheader, %.lr.ph.i
  %store_forwarded = phi i64 [ %load_initial, %.lr.ph.i.preheader ], [ %35, %.lr.ph.i ]
  %24 = phi i64 [ 2, %.lr.ph.i.preheader ], [ %36, %.lr.ph.i ]
  %.08.i = phi i64 [ 0, %.lr.ph.i.preheader ], [ %33, %.lr.ph.i ]
  %25 = shl i64 %24, 3
  %26 = add i64 %25, -8
  %27 = getelementptr i8, ptr %.unpack, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !3
  %29 = add i64 %store_forwarded, %28
  %30 = load i64, ptr %2, align 8, !tbaa !3
  %31 = sub i64 %29, %30
  %32 = call i64 @llvm.smax.i64(i64 %31, i64 0)
  %33 = add i64 %32, %.08.i
  %34 = sub i64 %28, %32
  %35 = call i64 @llvm.smax.i64(i64 %34, i64 0)
  store i64 %35, ptr %27, align 8, !tbaa !3
  %36 = add nuw i64 %24, 1
  %exitcond.not.i = icmp eq i64 %24, %21
  br i1 %exitcond.not.i, label %_QFPprocesses.exit, label %.lr.ph.i

_QFPprocesses.exit:                               ; preds = %.lr.ph.i, %._crit_edge
  %.0.lcssa.i = phi i64 [ 0, %._crit_edge ], [ %33, %.lr.ph.i ]
  %37 = call i1 @_FortranAioOutputInteger64(ptr %20, i64 %.0.lcssa.i)
  %38 = call i32 @_FortranAioEndIoStatement(ptr %20)
  %.unpack157 = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  call void @free(ptr %.unpack157)
  store ptr null, ptr @_QFEa.0, align 8, !tbaa !7
  ret void
}

; Function Attrs: nofree nosync nounwind memory(readwrite, inaccessiblemem: none)
define i64 @_QFPprocesses(ptr nocapture readonly %0, ptr nocapture readonly %1, ptr nocapture readonly %2) local_unnamed_addr #2 {
  %4 = load i64, ptr %0, align 8, !tbaa !3
  %5 = add i64 %4, -1
  %6 = icmp sgt i64 %5, 0
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %7 = load ptr, ptr %2, align 8, !tbaa !7
  %8 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, i64 0, i32 7, i64 0, i64 2
  %9 = load i64, ptr %8, align 8, !tbaa !7
  br label %10

10:                                               ; preds = %.lr.ph, %10
  %11 = phi i64 [ 2, %.lr.ph ], [ %27, %10 ]
  %.08 = phi i64 [ 0, %.lr.ph ], [ %24, %10 ]
  %12 = add i64 %11, -1
  %13 = mul i64 %9, %12
  %14 = getelementptr i8, ptr %7, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !3
  %16 = add i64 %11, -2
  %17 = mul i64 %9, %16
  %18 = getelementptr i8, ptr %7, i64 %17
  %19 = load i64, ptr %18, align 8, !tbaa !3
  %20 = add i64 %19, %15
  %21 = load i64, ptr %1, align 8, !tbaa !3
  %22 = sub i64 %20, %21
  %23 = tail call i64 @llvm.smax.i64(i64 %22, i64 0)
  %24 = add i64 %23, %.08
  %25 = sub i64 %15, %23
  %26 = tail call i64 @llvm.smax.i64(i64 %25, i64 0)
  store i64 %26, ptr %14, align 8, !tbaa !3
  %27 = add nuw i64 %11, 1
  %exitcond.not = icmp eq i64 %11, %4
  br i1 %exitcond.not, label %._crit_edge, label %10

._crit_edge:                                      ; preds = %10, %3
  %.0.lcssa = phi i64 [ 0, %3 ], [ %24, %10 ]
  ret i64 %.0.lcssa
}

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 @_FortranAioBeginExternalFormattedOutput(ptr, i64, ptr, 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) #3

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 = { nofree nosync nounwind memory(readwrite, inaccessiblemem: none) }
attributes #3 = { 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}
