; LLVM IR from kazunetakahashi/atcoder :: 2017/0122_AGC009/C2.ll (Z11calc_sum_DPiii)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@A = external dso_local global i64, align 8
@S = external dso_local global [100010 x i64], align 16
@underA = external dso_local global [100010 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z6calc_Xi(i32 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z11calc_sum_DPiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !9
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100010 x i32], ptr @underA, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = add nsw i32 %15, 1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [100010 x i32], ptr @underA, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = sub nsw i32 %14, %19
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %22, label %23

22:                                               ; preds = %3
  store i64 0, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %58

23:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %24 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %24, ptr %10, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %52, %23
  %26 = load i32, ptr %10, align 4, !tbaa !5
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  %29 = icmp sge i32 %26, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %25
  store i32 2, ptr %9, align 4
  br label %55

31:                                               ; preds = %25
  %32 = load i32, ptr %10, align 4, !tbaa !5
  %33 = call noundef i64 @_Z6calc_Xi(i32 noundef %32)
  %34 = load i64, ptr %8, align 8, !tbaa !9
  %35 = add nsw i64 %34, %33
  store i64 %35, ptr %8, align 8, !tbaa !9
  %36 = load i64, ptr %8, align 8, !tbaa !9
  %37 = srem i64 %36, 1000000007
  store i64 %37, ptr %8, align 8, !tbaa !9
  %38 = load i32, ptr %10, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [100010 x i64], ptr @S, i64 0, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !9
  %43 = load i32, ptr %10, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [100010 x i64], ptr @S, i64 0, i64 %44
  %46 = load i64, ptr %45, align 8, !tbaa !9
  %47 = sub nsw i64 %42, %46
  %48 = load i64, ptr @A, align 8, !tbaa !9
  %49 = icmp slt i64 %47, %48
  br i1 %49, label %50, label %51

50:                                               ; preds = %31
  store i32 2, ptr %9, align 4
  br label %55

51:                                               ; preds = %31
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %10, align 4, !tbaa !5
  %54 = add nsw i32 %53, -1
  store i32 %54, ptr %10, align 4, !tbaa !5
  br label %25, !llvm.loop !11

55:                                               ; preds = %50, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %56

56:                                               ; preds = %55
  %57 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %57, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %58

58:                                               ; preds = %56, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %59 = load i64, ptr %4, align 8
  ret i64 %59
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }

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

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@A = external dso_local local_unnamed_addr global i64, align 8
@S = external dso_local local_unnamed_addr global [100010 x i64], align 16
@underA = external dso_local local_unnamed_addr global [100010 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z6calc_Xi(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i64 -1000000006, 1000000007) i64 @_Z11calc_sum_DPiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = sext i32 %0 to i64
  %5 = getelementptr inbounds [100010 x i32], ptr @underA, i64 0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = add nsw i32 %2, 1
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [100010 x i32], ptr @underA, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = icmp sle i32 %6, %10
  %.not.not15 = icmp sgt i32 %2, %1
  %or.cond = and i1 %11, %.not.not15
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %3
  %12 = sext i32 %2 to i64
  %13 = sext i32 %1 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader
  %indvars.iv = phi i64 [ %12, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %.01316 = phi i64 [ 0, %.lr.ph.preheader ], [ %17, %.lr.ph ]
  %14 = trunc nsw i64 %indvars.iv to i32
  %15 = tail call noundef i64 @_Z6calc_Xi(i32 noundef %14)
  %16 = add nsw i64 %15, %.01316
  %17 = srem i64 %16, 1000000007
  %18 = add nsw i64 %indvars.iv, 1
  %19 = getelementptr inbounds [100010 x i64], ptr @S, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !9
  %21 = getelementptr inbounds [100010 x i64], ptr @S, i64 0, i64 %indvars.iv
  %22 = load i64, ptr %21, align 8, !tbaa !9
  %23 = sub nsw i64 %20, %22
  %24 = load i64, ptr @A, align 8, !tbaa !9
  %25 = icmp sge i64 %23, %24
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %.not.not = icmp sgt i64 %indvars.iv.next, %13
  %or.cond21 = select i1 %25, i1 %.not.not, i1 false
  br i1 %or.cond21, label %.lr.ph, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph, %3
  %.014 = phi i64 [ 0, %3 ], [ %17, %.lr.ph ]
  ret i64 %.014
}

attributes #0 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

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

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass