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

; 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 nounwind uwtable
declare dso_local noundef zeroext i1 @_Z8IsItSafePxxxx(ptr noundef, i64 noundef, i64 noundef, 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
define dso_local noundef i64 @_Z4MaxPPxxx(ptr noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !11
  br label %13

13:                                               ; preds = %27, %3
  %14 = load i32, ptr %10, align 4, !tbaa !11
  %15 = sext i32 %14 to i64
  %16 = load i64, ptr %5, align 8, !tbaa !9
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %30

19:                                               ; preds = %13
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load i32, ptr %10, align 4, !tbaa !11
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i64, ptr %21, i64 %23
  %25 = load i64, ptr %24, align 8, !tbaa !9
  %26 = add nsw i64 %20, %25
  store i64 %26, ptr %8, align 8, !tbaa !9
  br label %27

27:                                               ; preds = %19
  %28 = load i32, ptr %10, align 4, !tbaa !11
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %10, align 4, !tbaa !11
  br label %13, !llvm.loop !13

30:                                               ; preds = %18
  %31 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %31, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  store i64 0, ptr %11, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %56, %30
  %33 = load i32, ptr %7, align 4, !tbaa !11
  %34 = sext i32 %33 to i64
  %35 = load i64, ptr %9, align 8, !tbaa !9
  %36 = icmp sle i64 %34, %35
  br i1 %36, label %37, label %57

37:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %38 = load i32, ptr %7, align 4, !tbaa !11
  %39 = sext i32 %38 to i64
  %40 = load i64, ptr %9, align 8, !tbaa !9
  %41 = add nsw i64 %39, %40
  %42 = sdiv i64 %41, 2
  store i64 %42, ptr %12, align 8, !tbaa !9
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load i64, ptr %5, align 8, !tbaa !9
  %45 = load i64, ptr %6, align 8, !tbaa !9
  %46 = load i64, ptr %12, align 8, !tbaa !9
  %47 = call noundef zeroext i1 @_Z8IsItSafePxxxx(ptr noundef %43, i64 noundef %44, i64 noundef %45, i64 noundef %46)
  br i1 %47, label %48, label %52

48:                                               ; preds = %37
  %49 = load i64, ptr %12, align 8, !tbaa !9
  %50 = sub nsw i64 %49, 1
  store i64 %50, ptr %9, align 8, !tbaa !9
  %51 = load i64, ptr %12, align 8, !tbaa !9
  store i64 %51, ptr %11, align 8, !tbaa !9
  br label %56

52:                                               ; preds = %37
  %53 = load i64, ptr %12, align 8, !tbaa !9
  %54 = add nsw i64 %53, 1
  %55 = trunc i64 %54 to i32
  store i32 %55, ptr %7, align 4, !tbaa !11
  br label %56

56:                                               ; preds = %52, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  br label %32, !llvm.loop !15

57:                                               ; preds = %32
  %58 = load i64, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i64 %58
}

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 = { 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 = !{!"any pointer", !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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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
declare dso_local noundef zeroext i1 @_Z8IsItSafePxxxx(ptr noundef, i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4MaxPPxxx(ptr noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i64 %1, 0
  br i1 %4, label %.lr.ph.preheader, label %.lr.ph33.preheader

.lr.ph.preheader:                                 ; preds = %3
  %min.iters.check = icmp ult i64 %1, 4
  br i1 %min.iters.check, label %.lr.ph.preheader41, label %vector.ph

.lr.ph.preheader41:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.02327.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %10, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %1, 9223372036854775804
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %7, %vector.body ]
  %vec.phi39 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %8, %vector.body ]
  %5 = getelementptr inbounds i64, ptr %0, i64 %index
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  %wide.load = load <2 x i64>, ptr %5, align 8, !tbaa !5
  %wide.load40 = load <2 x i64>, ptr %6, align 8, !tbaa !5
  %7 = add <2 x i64> %wide.load, %vec.phi
  %8 = add <2 x i64> %wide.load40, %vec.phi39
  %index.next = add nuw i64 %index, 4
  %9 = icmp eq i64 %index.next, %n.vec
  br i1 %9, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %8, %7
  %10 = tail call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %1
  br i1 %cmp.n, label %.preheader, label %.lr.ph.preheader41

.preheader:                                       ; preds = %.lr.ph, %middle.block
  %.lcssa = phi i64 [ %10, %middle.block ], [ %13, %.lr.ph ]
  %.not29 = icmp slt i64 %.lcssa, 0
  br i1 %.not29, label %._crit_edge, label %.lr.ph33.preheader

.lr.ph33.preheader:                               ; preds = %3, %.preheader
  %.02130.ph = phi i64 [ %.lcssa, %.preheader ], [ 0, %3 ]
  br label %.lr.ph33

.lr.ph:                                           ; preds = %.lr.ph.preheader41, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader41 ]
  %.02327 = phi i64 [ %13, %.lr.ph ], [ %.02327.ph, %.lr.ph.preheader41 ]
  %11 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = add nsw i64 %12, %.02327
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %1
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !13

.lr.ph33:                                         ; preds = %.lr.ph33.preheader, %.lr.ph33
  %14 = phi i64 [ %21, %.lr.ph33 ], [ 0, %.lr.ph33.preheader ]
  %.032 = phi i32 [ %.1, %.lr.ph33 ], [ 0, %.lr.ph33.preheader ]
  %.01831 = phi i64 [ %.119, %.lr.ph33 ], [ 0, %.lr.ph33.preheader ]
  %.02130 = phi i64 [ %.122, %.lr.ph33 ], [ %.02130.ph, %.lr.ph33.preheader ]
  %15 = add nsw i64 %14, %.02130
  %16 = sdiv i64 %15, 2
  %17 = tail call noundef zeroext i1 @_Z8IsItSafePxxxx(ptr noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %16)
  %18 = add nsw i64 %16, -1
  %19 = trunc i64 %16 to i32
  %20 = add i32 %19, 1
  %.122 = select i1 %17, i64 %18, i64 %.02130
  %.119 = select i1 %17, i64 %16, i64 %.01831
  %.1 = select i1 %17, i32 %.032, i32 %20
  %21 = sext i32 %.1 to i64
  %.not = icmp slt i64 %.122, %21
  br i1 %.not, label %._crit_edge, label %.lr.ph33, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph33, %.preheader
  %.018.lcssa = phi i64 [ 0, %.preheader ], [ %.119, %.lr.ph33 ]
  ret i64 %.018.lcssa
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = distinct !{!14, !10}


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