; LLVM IR from upupming/algorithm :: acwing/寒假每日一题（提高组）/week3/1262. 鱼塘钓鱼.ll (Z4workii)

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

@spend = external dso_local global [110 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z3geti(i32 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 uwtable
define dso_local noundef i32 @_Z4workii(i32 noundef %0, i32 noundef %1) #0 {
  %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
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 16 @spend, i8 0, i64 440, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %43, %2
  %11 = load i32, ptr %6, align 4, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %15, label %14

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

15:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 1, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 1, ptr %9, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %30, %15
  %17 = load i32, ptr %9, align 4, !tbaa !5
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = icmp sle i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %33

21:                                               ; preds = %16
  %22 = load i32, ptr %9, align 4, !tbaa !5
  %23 = call noundef i32 @_Z3geti(i32 noundef %22)
  %24 = load i32, ptr %8, align 4, !tbaa !5
  %25 = call noundef i32 @_Z3geti(i32 noundef %24)
  %26 = icmp sgt i32 %23, %25
  br i1 %26, label %27, label %29

27:                                               ; preds = %21
  %28 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %28, ptr %8, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %27, %21
  br label %30

30:                                               ; preds = %29
  %31 = load i32, ptr %9, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %9, align 4, !tbaa !5
  br label %16, !llvm.loop !9

33:                                               ; preds = %20
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = call noundef i32 @_Z3geti(i32 noundef %34)
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = add nsw i32 %36, %35
  store i32 %37, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [110 x i32], ptr @spend, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %40, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %43

43:                                               ; preds = %33
  %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 %10, !llvm.loop !11

46:                                               ; preds = %14
  %47 = load i32, ptr %5, align 4, !tbaa !5
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret i32 %47
}

; 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 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 = !{!"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"

@spend = external dso_local local_unnamed_addr global [110 x i32], align 16

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z4workii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(440) @spend, i8 0, i64 440, i1 false)
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.preheader.lr.ph, label %._crit_edge21

.preheader.lr.ph:                                 ; preds = %2
  %.not16 = icmp slt i32 %0, 1
  br i1 %.not16, label %.preheader.us, label %.preheader

.preheader.us:                                    ; preds = %.preheader.lr.ph, %.preheader.us
  %.01420.us = phi i32 [ %8, %.preheader.us ], [ 0, %.preheader.lr.ph ]
  %.01519.us = phi i32 [ %5, %.preheader.us ], [ 0, %.preheader.lr.ph ]
  %4 = tail call noundef i32 @_Z3geti(i32 noundef 1)
  %5 = add nsw i32 %4, %.01519.us
  %6 = load i32, ptr getelementptr inbounds (i8, ptr @spend, i64 4), align 4, !tbaa !5
  %7 = add nsw i32 %6, 1
  store i32 %7, ptr getelementptr inbounds (i8, ptr @spend, i64 4), align 4, !tbaa !5
  %8 = add nuw nsw i32 %.01420.us, 1
  %exitcond26.not = icmp eq i32 %8, %1
  br i1 %exitcond26.not, label %._crit_edge21, label %.preheader.us, !llvm.loop !9

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %.01420 = phi i32 [ %15, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %.01519 = phi i32 [ %10, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  br label %16

._crit_edge21:                                    ; preds = %._crit_edge, %.preheader.us, %2
  %.015.lcssa = phi i32 [ 0, %2 ], [ %5, %.preheader.us ], [ %10, %._crit_edge ]
  ret i32 %.015.lcssa

._crit_edge:                                      ; preds = %16
  %9 = tail call noundef i32 @_Z3geti(i32 noundef %spec.select)
  %10 = add nsw i32 %9, %.01519
  %11 = sext i32 %spec.select to i64
  %12 = getelementptr inbounds [110 x i32], ptr @spend, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr %12, align 4, !tbaa !5
  %15 = add nuw nsw i32 %.01420, 1
  %exitcond25.not = icmp eq i32 %15, %1
  br i1 %exitcond25.not, label %._crit_edge21, label %.preheader, !llvm.loop !9

16:                                               ; preds = %.preheader, %16
  %.018 = phi i32 [ 1, %.preheader ], [ %20, %16 ]
  %.01317 = phi i32 [ 1, %.preheader ], [ %spec.select, %16 ]
  %17 = tail call noundef i32 @_Z3geti(i32 noundef %.018)
  %18 = tail call noundef i32 @_Z3geti(i32 noundef %.01317)
  %19 = icmp sgt i32 %17, %18
  %spec.select = select i1 %19, i32 %.018, i32 %.01317
  %20 = add nuw i32 %.018, 1
  %exitcond.not = icmp eq i32 %.018, %0
  br i1 %exitcond.not, label %._crit_edge, label %16, !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

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass