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

; 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
@X = external dso_local global [100010 x i64], align 16
@kB = external dso_local global [100010 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z6calc_Xi(i32 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [100010 x i64], ptr @X, i64 0, i64 %5
  %7 = load i64, ptr %6, align 8, !tbaa !9
  %8 = icmp sge i64 %7, 0
  br i1 %8, label %9, label %14

9:                                                ; preds = %1
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [100010 x i64], ptr @X, i64 0, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !9
  store i64 %13, ptr %2, align 8
  br label %68

14:                                               ; preds = %1
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sub nsw i32 %15, 1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [100010 x i64], ptr @S, i64 0, i64 %17
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = sub nsw i32 %20, 2
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100010 x i64], ptr @S, i64 0, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = sub nsw i64 %19, %24
  %26 = load i64, ptr @A, align 8, !tbaa !9
  %27 = icmp slt i64 %25, %26
  br i1 %27, label %28, label %47

28:                                               ; preds = %14
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = sub nsw i32 %29, 2
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100010 x i32], ptr @kB, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = icmp sle i32 %30, %34
  br i1 %35, label %36, label %43

36:                                               ; preds = %28
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sub nsw i32 %37, 2
  %39 = call noundef i64 @_Z6calc_Xi(i32 noundef %38)
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100010 x i64], ptr @X, i64 0, i64 %41
  store i64 %39, ptr %42, align 8, !tbaa !9
  store i64 %39, ptr %2, align 8
  br label %68

43:                                               ; preds = %28
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [100010 x i64], ptr @X, i64 0, i64 %45
  store i64 0, ptr %46, align 8, !tbaa !9
  store i64 0, ptr %2, align 8
  br label %68

47:                                               ; preds = %14
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = sub nsw i32 %48, 1
  %50 = call noundef i64 @_Z6calc_Xi(i32 noundef %49)
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = sub nsw i32 %51, 1
  %53 = load i32, ptr %3, align 4, !tbaa !5
  %54 = sub nsw i32 %53, 1
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [100010 x i32], ptr @kB, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [100010 x i32], ptr @kB, i64 0, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !5
  %62 = call noundef i64 @_Z11calc_sum_DPiii(i32 noundef %52, i32 noundef %57, i32 noundef %61)
  %63 = add nsw i64 %50, %62
  %64 = srem i64 %63, 1000000007
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [100010 x i64], ptr @X, i64 0, i64 %66
  store i64 %64, ptr %67, align 8, !tbaa !9
  store i64 %64, ptr %2, align 8
  br label %68

68:                                               ; preds = %47, %43, %36, %9
  %69 = load i64, ptr %2, align 8
  ret i64 %69
}

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

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}


; 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
@X = external dso_local local_unnamed_addr global [100010 x i64], align 16
@kB = external dso_local local_unnamed_addr global [100010 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i64 -1000000006, -9223372036854775808) i64 @_Z6calc_Xi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [100010 x i64], ptr @X, i64 0, i64 %2
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = icmp sgt i64 %4, -1
  br i1 %5, label %common.ret, label %6

6:                                                ; preds = %1
  %7 = add nsw i32 %0, -1
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [100010 x i64], ptr @S, i64 0, i64 %8
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = add nsw i32 %0, -2
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100010 x i64], ptr @S, i64 0, i64 %12
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = sub nsw i64 %10, %14
  %16 = load i64, ptr @A, align 8, !tbaa !5
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %18, label %23

18:                                               ; preds = %6
  %19 = getelementptr inbounds [100010 x i32], ptr @kB, i64 0, i64 %2
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %.not = icmp sgt i32 %11, %20
  br i1 %.not, label %common.ret.sink.split, label %21

common.ret.sink.split:                            ; preds = %18, %21, %23
  %.sink = phi i64 [ %31, %23 ], [ %22, %21 ], [ 0, %18 ]
  store i64 %.sink, ptr %3, align 8, !tbaa !5
  br label %common.ret

common.ret:                                       ; preds = %common.ret.sink.split, %1
  %common.ret.op = phi i64 [ %4, %1 ], [ %.sink, %common.ret.sink.split ]
  ret i64 %common.ret.op

21:                                               ; preds = %18
  %22 = tail call noundef i64 @_Z6calc_Xi(i32 noundef %11)
  br label %common.ret.sink.split

23:                                               ; preds = %6
  %24 = tail call noundef i64 @_Z6calc_Xi(i32 noundef %7)
  %25 = getelementptr inbounds [100010 x i32], ptr @kB, i64 0, i64 %8
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = getelementptr inbounds [100010 x i32], ptr @kB, i64 0, i64 %2
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = tail call noundef i64 @_Z11calc_sum_DPiii(i32 noundef %7, i32 noundef %26, i32 noundef %28)
  %30 = add nsw i64 %29, %24
  %31 = srem i64 %30, 1000000007
  br label %common.ret.sink.split
}

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

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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass