; LLVM IR from upupming/algorithm :: acwing/日常刷题/kick-start/587. 吃蛋糕.ll (Z5solvev)

; 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 i32, align 4
@f = external dso_local global [10010 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5solvev() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  call void @llvm.memset.p0.i64(ptr align 16 @f, i8 63, i64 40040, i1 false)
  store i32 0, ptr @f, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = call noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %8)
  %10 = fptosi double %9 to i32
  store i32 %10, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %47, %0
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = load i32, ptr %1, align 4, !tbaa !5
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  store i32 2, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %50

16:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = mul nsw i32 %17, %18
  store i32 %19, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 1, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %20 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %20, ptr %6, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %43, %16
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = load i32, ptr @n, align 4, !tbaa !5
  %24 = icmp sle i32 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 5, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %46

26:                                               ; preds = %21
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [10010 x i32], ptr @f, i64 0, i64 %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sub nsw i32 %30, %31
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [10010 x i32], ptr @f, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = add nsw i32 %35, %36
  store i32 %37, ptr %7, align 4, !tbaa !5
  %38 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %29, ptr noundef nonnull align 4 dereferenceable(4) %7)
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [10010 x i32], ptr @f, i64 0, i64 %41
  store i32 %39, ptr %42, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %43

43:                                               ; preds = %26
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %6, align 4, !tbaa !5
  br label %21, !llvm.loop !9

46:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %2, align 4, !tbaa !5
  br label %11, !llvm.loop !11

50:                                               ; preds = %15
  %51 = load i32, ptr @n, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [10010 x i32], ptr @f, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  store i32 1, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret i32 %54
}

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef) #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #3

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

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 nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { inlinehint 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 #4 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; 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 i32, align 4
@f = external dso_local global [10010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5solvev() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(40040) @f, i8 63, i64 40040, i1 false)
  store i32 0, ptr @f, align 16, !tbaa !5
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = tail call noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %2)
  %4 = fptosi double %3 to i32
  %.not17 = icmp slt i32 %4, 1
  %.pre30 = load i32, ptr @n, align 4, !tbaa !5
  br i1 %.not17, label %._crit_edge21, label %.lr.ph20.preheader

.lr.ph20.preheader:                               ; preds = %0
  %5 = add nuw i32 %4, 1
  %wide.trip.count = zext i32 %5 to i64
  br label %.lr.ph20

._crit_edge21:                                    ; preds = %._crit_edge, %0
  %6 = phi i32 [ %.pre30, %0 ], [ %14, %._crit_edge ]
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [10010 x i32], ptr @f, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  ret i32 %9

.lr.ph20:                                         ; preds = %.lr.ph20.preheader, %._crit_edge
  %10 = phi i32 [ %.pre30, %.lr.ph20.preheader ], [ %14, %._crit_edge ]
  %indvars.iv27 = phi i64 [ 1, %.lr.ph20.preheader ], [ %indvars.iv.next28, %._crit_edge ]
  %indvars.iv22 = phi i32 [ 1, %.lr.ph20.preheader ], [ %indvars.iv.next23, %._crit_edge ]
  %indvars.iv = phi i32 [ 3, %.lr.ph20.preheader ], [ %indvars.iv.next, %._crit_edge ]
  %11 = mul nuw nsw i64 %indvars.iv27, %indvars.iv27
  %12 = sext i32 %10 to i64
  %.not1415 = icmp sgt i64 %11, %12
  br i1 %.not1415, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.lr.ph20
  %13 = sext i32 %indvars.iv22 to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph20
  %14 = phi i32 [ %10, %.lr.ph20 ], [ %22, %.lr.ph ]
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %indvars.iv.next = add i32 %indvars.iv, 2
  %indvars.iv.next23 = add i32 %indvars.iv22, %indvars.iv
  %exitcond.not = icmp eq i64 %indvars.iv.next28, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge21, label %.lr.ph20, !llvm.loop !9

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv24 = phi i64 [ %13, %.lr.ph.preheader ], [ %indvars.iv.next25, %.lr.ph ]
  %15 = getelementptr inbounds [10010 x i32], ptr @f, i64 0, i64 %indvars.iv24
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  %16 = sub nuw nsw i64 %indvars.iv24, %11
  %17 = getelementptr inbounds [10010 x i32], ptr @f, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %1, align 4, !tbaa !5
  %20 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %15, ptr noundef nonnull align 4 dereferenceable(4) %1)
  %21 = load i32, ptr %20, align 4, !tbaa !5
  store i32 %21, ptr %15, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  %indvars.iv.next25 = add nuw nsw i64 %indvars.iv24, 1
  %22 = load i32, ptr @n, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %.not14.not = icmp slt i64 %indvars.iv24, %23
  br i1 %.not14.not, label %.lr.ph, label %._crit_edge, !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) #1

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef) local_unnamed_addr #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 nounwind willreturn memory(argmem: write) }
attributes #2 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { inlinehint 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 #4 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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