; LLVM IR from ravireddy07/cpp-book :: src/Coding-Blocks/RodCutting.ll (Z6rodCutxPx)

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z6rodCutxPx(i64 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  %11 = load i64, ptr %3, align 8, !tbaa !5
  %12 = add nsw i64 %11, 1
  %13 = call ptr @llvm.stacksave.p0()
  store ptr %13, ptr %5, align 8
  %14 = alloca i64, i64 %12, align 16
  store i64 %12, ptr %6, align 8
  %15 = getelementptr inbounds i64, ptr %14, i64 0
  store i64 0, ptr %15, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store i64 1, ptr %7, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %51, %2
  %17 = load i64, ptr %7, align 8, !tbaa !5
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = icmp sle i64 %17, %18
  br i1 %19, label %21, label %20

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

21:                                               ; preds = %16
  %22 = load i64, ptr %7, align 8, !tbaa !5
  %23 = getelementptr inbounds i64, ptr %14, i64 %22
  store i64 -2147483648, ptr %23, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  store i64 0, ptr %9, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %47, %21
  %25 = load i64, ptr %9, align 8, !tbaa !5
  %26 = load i64, ptr %7, align 8, !tbaa !5
  %27 = icmp slt i64 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 5, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %50

29:                                               ; preds = %24
  %30 = load i64, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds i64, ptr %14, i64 %30
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %32 = load ptr, ptr %4, align 8, !tbaa !9
  %33 = load i64, ptr %9, align 8, !tbaa !5
  %34 = getelementptr inbounds i64, ptr %32, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = load i64, ptr %7, align 8, !tbaa !5
  %37 = load i64, ptr %9, align 8, !tbaa !5
  %38 = add nsw i64 %37, 1
  %39 = sub nsw i64 %36, %38
  %40 = getelementptr inbounds i64, ptr %14, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = add nsw i64 %35, %41
  store i64 %42, ptr %10, align 8, !tbaa !5
  %43 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %31, ptr noundef nonnull align 8 dereferenceable(8) %10)
  %44 = load i64, ptr %43, align 8, !tbaa !5
  %45 = load i64, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds i64, ptr %14, i64 %45
  store i64 %44, ptr %46, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %47

47:                                               ; preds = %29
  %48 = load i64, ptr %9, align 8, !tbaa !5
  %49 = add nsw i64 %48, 1
  store i64 %49, ptr %9, align 8, !tbaa !5
  br label %24, !llvm.loop !11

50:                                               ; preds = %28
  br label %51

51:                                               ; preds = %50
  %52 = load i64, ptr %7, align 8, !tbaa !5
  %53 = add nsw i64 %52, 1
  store i64 %53, ptr %7, align 8, !tbaa !5
  br label %16, !llvm.loop !13

54:                                               ; preds = %20
  %55 = load i64, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds i64, ptr %14, i64 %55
  %57 = load i64, ptr %56, align 8, !tbaa !5
  store i32 1, ptr %8, align 4
  %58 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %58)
  ret i64 %57
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #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 nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #3

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 }
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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z6rodCutxPx(i64 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = alloca i64, align 8
  %4 = add i64 %0, 1
  %5 = alloca i64, i64 %4, align 16
  store i64 0, ptr %5, align 16, !tbaa !5
  %.not18 = icmp slt i64 %0, 1
  br i1 %.not18, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %9, %2
  %6 = getelementptr inbounds i64, ptr %5, i64 %0
  %7 = load i64, ptr %6, align 8, !tbaa !5
  ret i64 %7

.lr.ph:                                           ; preds = %2, %9
  %.01519 = phi i64 [ %10, %9 ], [ 1, %2 ]
  %8 = getelementptr inbounds i64, ptr %5, i64 %.01519
  store i64 -2147483648, ptr %8, align 8, !tbaa !5
  br label %11

9:                                                ; preds = %11
  %10 = add nuw i64 %.01519, 1
  %exitcond20.not = icmp eq i64 %.01519, %0
  br i1 %exitcond20.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

11:                                               ; preds = %.lr.ph, %11
  %.017 = phi i64 [ 0, %.lr.ph ], [ %14, %11 ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %12 = getelementptr inbounds i64, ptr %1, i64 %.017
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = add nuw nsw i64 %.017, 1
  %15 = sub nsw i64 %.01519, %14
  %16 = getelementptr inbounds i64, ptr %5, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = add nsw i64 %17, %13
  store i64 %18, ptr %3, align 8, !tbaa !5
  %19 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %8, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %20 = load i64, ptr %19, align 8, !tbaa !5
  store i64 %20, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  %exitcond.not = icmp eq i64 %14, %.01519
  br i1 %exitcond.not, label %9, label %11, !llvm.loop !11
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2

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

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 = { 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 #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,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass