; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x42-树状数组/241. 楼兰图腾.ll (Z4calcb)

; 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"

@n = external dso_local global i64, align 8
@a = external dso_local global [200010 x i64], align 16
@c = external dso_local global [200010 x i64], align 16
@maxA = external dso_local global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3askx(i64 noundef) #0

; 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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z3addxx(i64 noundef, i64 noundef) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4calcb(i1 noundef zeroext %0) #0 {
  %2 = alloca i8, align 1
  %3 = alloca i64, align 8
  %4 = alloca [200010 x i64], align 16
  %5 = alloca [200010 x i64], align 16
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = zext i1 %0 to i8
  store i8 %8, ptr %2, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1600080, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 1600080, ptr %5) #3
  call void @llvm.memset.p0.i64(ptr align 16 @c, i8 0, i64 1600080, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %9 = load i64, ptr @n, align 8, !tbaa !9
  %10 = trunc i64 %9 to i32
  store i32 %10, ptr %6, align 4, !tbaa !11
  br label %11

11:                                               ; preds = %45, %1
  %12 = load i32, ptr %6, align 4, !tbaa !11
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %48

15:                                               ; preds = %11
  %16 = load i8, ptr %2, align 1, !tbaa !5, !range !13, !noundef !14
  %17 = trunc i8 %16 to i1
  br i1 %17, label %18, label %30

18:                                               ; preds = %15
  %19 = load i64, ptr @maxA, align 8, !tbaa !9
  %20 = call noundef i64 @_Z3askx(i64 noundef %19)
  %21 = load i32, ptr %6, align 4, !tbaa !11
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [200010 x i64], ptr @a, i64 0, i64 %22
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = call noundef i64 @_Z3askx(i64 noundef %24)
  %26 = sub nsw i64 %20, %25
  %27 = load i32, ptr %6, align 4, !tbaa !11
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [200010 x i64], ptr %5, i64 0, i64 %28
  store i64 %26, ptr %29, align 8, !tbaa !9
  br label %40

30:                                               ; preds = %15
  %31 = load i32, ptr %6, align 4, !tbaa !11
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [200010 x i64], ptr @a, i64 0, i64 %32
  %34 = load i64, ptr %33, align 8, !tbaa !9
  %35 = sub nsw i64 %34, 1
  %36 = call noundef i64 @_Z3askx(i64 noundef %35)
  %37 = load i32, ptr %6, align 4, !tbaa !11
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [200010 x i64], ptr %5, i64 0, i64 %38
  store i64 %36, ptr %39, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %30, %18
  %41 = load i32, ptr %6, align 4, !tbaa !11
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [200010 x i64], ptr @a, i64 0, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !9
  call void @_Z3addxx(i64 noundef %44, i64 noundef 1)
  br label %45

45:                                               ; preds = %40
  %46 = load i32, ptr %6, align 4, !tbaa !11
  %47 = add nsw i32 %46, -1
  store i32 %47, ptr %6, align 4, !tbaa !11
  br label %11, !llvm.loop !15

48:                                               ; preds = %14
  call void @llvm.memset.p0.i64(ptr align 16 @c, i8 0, i64 1600080, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1, ptr %7, align 4, !tbaa !11
  br label %49

49:                                               ; preds = %96, %48
  %50 = load i32, ptr %7, align 4, !tbaa !11
  %51 = sext i32 %50 to i64
  %52 = load i64, ptr @n, align 8, !tbaa !9
  %53 = icmp sle i64 %51, %52
  br i1 %53, label %55, label %54

54:                                               ; preds = %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %99

55:                                               ; preds = %49
  %56 = load i8, ptr %2, align 1, !tbaa !5, !range !13, !noundef !14
  %57 = trunc i8 %56 to i1
  br i1 %57, label %58, label %70

58:                                               ; preds = %55
  %59 = load i64, ptr @maxA, align 8, !tbaa !9
  %60 = call noundef i64 @_Z3askx(i64 noundef %59)
  %61 = load i32, ptr %7, align 4, !tbaa !11
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [200010 x i64], ptr @a, i64 0, i64 %62
  %64 = load i64, ptr %63, align 8, !tbaa !9
  %65 = call noundef i64 @_Z3askx(i64 noundef %64)
  %66 = sub nsw i64 %60, %65
  %67 = load i32, ptr %7, align 4, !tbaa !11
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [200010 x i64], ptr %4, i64 0, i64 %68
  store i64 %66, ptr %69, align 8, !tbaa !9
  br label %80

70:                                               ; preds = %55
  %71 = load i32, ptr %7, align 4, !tbaa !11
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [200010 x i64], ptr @a, i64 0, i64 %72
  %74 = load i64, ptr %73, align 8, !tbaa !9
  %75 = sub nsw i64 %74, 1
  %76 = call noundef i64 @_Z3askx(i64 noundef %75)
  %77 = load i32, ptr %7, align 4, !tbaa !11
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [200010 x i64], ptr %4, i64 0, i64 %78
  store i64 %76, ptr %79, align 8, !tbaa !9
  br label %80

80:                                               ; preds = %70, %58
  %81 = load i32, ptr %7, align 4, !tbaa !11
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [200010 x i64], ptr @a, i64 0, i64 %82
  %84 = load i64, ptr %83, align 8, !tbaa !9
  call void @_Z3addxx(i64 noundef %84, i64 noundef 1)
  %85 = load i32, ptr %7, align 4, !tbaa !11
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds [200010 x i64], ptr %4, i64 0, i64 %86
  %88 = load i64, ptr %87, align 8, !tbaa !9
  %89 = load i32, ptr %7, align 4, !tbaa !11
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds [200010 x i64], ptr %5, i64 0, i64 %90
  %92 = load i64, ptr %91, align 8, !tbaa !9
  %93 = mul nsw i64 %88, %92
  %94 = load i64, ptr %3, align 8, !tbaa !9
  %95 = add nsw i64 %94, %93
  store i64 %95, ptr %3, align 8, !tbaa !9
  br label %96

96:                                               ; preds = %80
  %97 = load i32, ptr %7, align 4, !tbaa !11
  %98 = add nsw i32 %97, 1
  store i32 %98, ptr %7, align 4, !tbaa !11
  br label %49, !llvm.loop !17

99:                                               ; preds = %54
  %100 = load i64, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 1600080, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 1600080, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i64 %100
}

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

attributes #0 = { mustprogress nounwind 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = !{!"bool", !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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; 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"

@n = external dso_local local_unnamed_addr global i64, align 8
@a = external dso_local local_unnamed_addr global [200010 x i64], align 16
@c = external dso_local local_unnamed_addr global [200010 x i64], align 16
@maxA = external dso_local local_unnamed_addr global i64, align 8

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

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z3addxx(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4calcb(i1 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = alloca [200010 x i64], align 16
  call void @llvm.lifetime.start.p0(i64 1600080, ptr nonnull %2) #3
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1600080) @c, i8 0, i64 1600080, i1 false)
  %3 = load i64, ptr @n, align 8, !tbaa !5
  %4 = and i64 %3, 4294967295
  %.not24 = icmp eq i64 %4, 0
  br i1 %.not24, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %sext45 = shl i64 %3, 32
  %5 = ashr exact i64 %sext45, 32
  br i1 %0, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %.lr.ph.split.us
  %indvars.iv36 = phi i64 [ %indvars.iv.next37, %.lr.ph.split.us ], [ %5, %.lr.ph ]
  %6 = load i64, ptr @maxA, align 8, !tbaa !5
  %7 = tail call noundef i64 @_Z3askx(i64 noundef %6)
  %8 = getelementptr inbounds [200010 x i64], ptr @a, i64 0, i64 %indvars.iv36
  %9 = load i64, ptr %8, align 8, !tbaa !5
  %10 = tail call noundef i64 @_Z3askx(i64 noundef %9)
  %11 = sub nsw i64 %7, %10
  %12 = getelementptr inbounds [200010 x i64], ptr %2, i64 0, i64 %indvars.iv36
  store i64 %11, ptr %12, align 8, !tbaa !5
  %13 = load i64, ptr %8, align 8, !tbaa !5
  tail call void @_Z3addxx(i64 noundef %13, i64 noundef 1)
  %indvars.iv.next37 = add nsw i64 %indvars.iv36, -1
  %.not.us = icmp eq i64 %indvars.iv.next37, 0
  br i1 %.not.us, label %._crit_edgethread-pre-split, label %.lr.ph.split.us, !llvm.loop !9

._crit_edgethread-pre-split:                      ; preds = %.lr.ph.split, %.lr.ph.split.us
  %.pr = load i64, ptr @n, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edgethread-pre-split, %1
  %14 = phi i64 [ %.pr, %._crit_edgethread-pre-split ], [ %3, %1 ]
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1600080) @c, i8 0, i64 1600080, i1 false)
  %.not2326 = icmp slt i64 %14, 1
  br i1 %.not2326, label %._crit_edge31, label %.lr.ph30

.lr.ph30:                                         ; preds = %._crit_edge
  br i1 %0, label %.lr.ph30.split.us, label %.lr.ph30.split

.lr.ph30.split.us:                                ; preds = %.lr.ph30, %.lr.ph30.split.us
  %indvars.iv42 = phi i64 [ %indvars.iv.next43, %.lr.ph30.split.us ], [ 1, %.lr.ph30 ]
  %.02127.us = phi i64 [ %25, %.lr.ph30.split.us ], [ 0, %.lr.ph30 ]
  %15 = load i64, ptr @maxA, align 8, !tbaa !5
  %16 = tail call noundef i64 @_Z3askx(i64 noundef %15)
  %17 = getelementptr inbounds [200010 x i64], ptr @a, i64 0, i64 %indvars.iv42
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = tail call noundef i64 @_Z3askx(i64 noundef %18)
  %20 = sub nsw i64 %16, %19
  %21 = load i64, ptr %17, align 8, !tbaa !5
  tail call void @_Z3addxx(i64 noundef %21, i64 noundef 1)
  %22 = getelementptr inbounds [200010 x i64], ptr %2, i64 0, i64 %indvars.iv42
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = mul nsw i64 %23, %20
  %25 = add nsw i64 %24, %.02127.us
  %indvars.iv.next43 = add nuw nsw i64 %indvars.iv42, 1
  %26 = load i64, ptr @n, align 8, !tbaa !5
  %.not23.us.not = icmp sgt i64 %26, %indvars.iv42
  br i1 %.not23.us.not, label %.lr.ph30.split.us, label %._crit_edge31, !llvm.loop !11

.lr.ph.split:                                     ; preds = %.lr.ph, %.lr.ph.split
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph.split ], [ %5, %.lr.ph ]
  %27 = getelementptr inbounds [200010 x i64], ptr @a, i64 0, i64 %indvars.iv
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = add nsw i64 %28, -1
  %30 = tail call noundef i64 @_Z3askx(i64 noundef %29)
  %31 = getelementptr inbounds [200010 x i64], ptr %2, i64 0, i64 %indvars.iv
  store i64 %30, ptr %31, align 8, !tbaa !5
  %32 = load i64, ptr %27, align 8, !tbaa !5
  tail call void @_Z3addxx(i64 noundef %32, i64 noundef 1)
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %.not = icmp eq i64 %indvars.iv.next, 0
  br i1 %.not, label %._crit_edgethread-pre-split, label %.lr.ph.split, !llvm.loop !9

._crit_edge31:                                    ; preds = %.lr.ph30.split, %.lr.ph30.split.us, %._crit_edge
  %.021.lcssa = phi i64 [ 0, %._crit_edge ], [ %25, %.lr.ph30.split.us ], [ %41, %.lr.ph30.split ]
  call void @llvm.lifetime.end.p0(i64 1600080, ptr nonnull %2) #3
  ret i64 %.021.lcssa

.lr.ph30.split:                                   ; preds = %.lr.ph30, %.lr.ph30.split
  %indvars.iv39 = phi i64 [ %indvars.iv.next40, %.lr.ph30.split ], [ 1, %.lr.ph30 ]
  %.02127 = phi i64 [ %41, %.lr.ph30.split ], [ 0, %.lr.ph30 ]
  %33 = getelementptr inbounds [200010 x i64], ptr @a, i64 0, i64 %indvars.iv39
  %34 = load i64, ptr %33, align 8, !tbaa !5
  %35 = add nsw i64 %34, -1
  %36 = tail call noundef i64 @_Z3askx(i64 noundef %35)
  %37 = load i64, ptr %33, align 8, !tbaa !5
  tail call void @_Z3addxx(i64 noundef %37, i64 noundef 1)
  %38 = getelementptr inbounds [200010 x i64], ptr %2, i64 0, i64 %indvars.iv39
  %39 = load i64, ptr %38, align 8, !tbaa !5
  %40 = mul nsw i64 %39, %36
  %41 = add nsw i64 %40, %.02127
  %indvars.iv.next40 = add nuw nsw i64 %indvars.iv39, 1
  %42 = load i64, ptr @n, align 8, !tbaa !5
  %.not23.not = icmp sgt i64 %42, %indvars.iv39
  br i1 %.not23.not, label %.lr.ph30.split, label %._crit_edge31, !llvm.loop !11
}

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

attributes #0 = { mustprogress nounwind 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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