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

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

14:                                               ; preds = %57, %4
  %15 = load i64, ptr %12, align 8, !tbaa !9
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 2, ptr %13, align 4
  br label %60

19:                                               ; preds = %14
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = load i64, ptr %12, align 8, !tbaa !9
  %22 = getelementptr inbounds i64, ptr %20, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !9
  %24 = load i64, ptr %9, align 8, !tbaa !9
  %25 = icmp sgt i64 %23, %24
  br i1 %25, label %26, label %27

26:                                               ; preds = %19
  store i1 false, ptr %5, align 1
  store i32 1, ptr %13, align 4
  br label %60

27:                                               ; preds = %19
  %28 = load i64, ptr %11, align 8, !tbaa !9
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = load i64, ptr %12, align 8, !tbaa !9
  %31 = getelementptr inbounds i64, ptr %29, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !9
  %33 = add nsw i64 %28, %32
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = icmp sgt i64 %33, %34
  br i1 %35, label %36, label %49

36:                                               ; preds = %27
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load i64, ptr %12, align 8, !tbaa !9
  %39 = getelementptr inbounds i64, ptr %37, i64 %38
  %40 = load i64, ptr %39, align 8, !tbaa !9
  store i64 %40, ptr %11, align 8, !tbaa !9
  %41 = load i32, ptr %10, align 4, !tbaa !11
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %10, align 4, !tbaa !11
  %43 = load i32, ptr %10, align 4, !tbaa !11
  %44 = sext i32 %43 to i64
  %45 = load i64, ptr %8, align 8, !tbaa !9
  %46 = icmp sgt i64 %44, %45
  br i1 %46, label %47, label %48

47:                                               ; preds = %36
  store i1 false, ptr %5, align 1
  store i32 1, ptr %13, align 4
  br label %60

48:                                               ; preds = %36
  br label %56

49:                                               ; preds = %27
  %50 = load i64, ptr %11, align 8, !tbaa !9
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = load i64, ptr %12, align 8, !tbaa !9
  %53 = getelementptr inbounds i64, ptr %51, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !9
  %55 = add nsw i64 %50, %54
  store i64 %55, ptr %11, align 8, !tbaa !9
  br label %56

56:                                               ; preds = %49, %48
  br label %57

57:                                               ; preds = %56
  %58 = load i64, ptr %12, align 8, !tbaa !9
  %59 = add nsw i64 %58, 1
  store i64 %59, ptr %12, align 8, !tbaa !9
  br label %14, !llvm.loop !13

60:                                               ; preds = %47, %26, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  %61 = load i32, ptr %13, align 4
  switch i32 %61, label %63 [
    i32 2, label %62
  ]

62:                                               ; preds = %60
  store i1 true, ptr %5, align 1
  store i32 1, ptr %13, align 4
  br label %63

63:                                               ; preds = %62, %60
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %64 = load i1, ptr %5, align 1
  ret i1 %64
}

; 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

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


; 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 nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef zeroext i1 @_Z8IsItSafePxxxx(ptr nocapture noundef readonly %0, i64 noundef %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %.not28 = icmp slt i64 %1, 1
  br i1 %.not28, label %.critedge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %5 = load i64, ptr %0, align 8, !tbaa !5
  %6 = icmp sgt i64 %5, %3
  br i1 %6, label %.critedge, label %.lr.ph41

.lr.ph:                                           ; preds = %17
  %7 = getelementptr inbounds i64, ptr %0, i64 %18
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = icmp sgt i64 %8, %3
  br i1 %9, label %.critedge, label %.lr.ph41, !llvm.loop !9

.lr.ph41:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %10 = phi i64 [ %8, %.lr.ph ], [ %5, %.lr.ph.preheader ]
  %.0212940 = phi i32 [ %.122, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %.0203039 = phi i64 [ %.1, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %.0193138 = phi i64 [ %18, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %.not3237 = phi i1 [ %.not, %.lr.ph ], [ false, %.lr.ph.preheader ]
  %11 = add nsw i64 %10, %.0203039
  %12 = icmp sgt i64 %11, %3
  br i1 %12, label %13, label %17

13:                                               ; preds = %.lr.ph41
  %14 = add nsw i32 %.0212940, 1
  %15 = sext i32 %14 to i64
  %16 = icmp sgt i64 %15, %2
  br i1 %16, label %.critedge, label %17

17:                                               ; preds = %.lr.ph41, %13
  %.122 = phi i32 [ %14, %13 ], [ %.0212940, %.lr.ph41 ]
  %.1 = phi i64 [ %10, %13 ], [ %11, %.lr.ph41 ]
  %18 = add nuw nsw i64 %.0193138, 1
  %.not = icmp sge i64 %18, %1
  %exitcond = icmp eq i64 %18, %1
  br i1 %exitcond, label %.critedge, label %.lr.ph, !llvm.loop !9

.critedge:                                        ; preds = %17, %.lr.ph, %13, %.lr.ph.preheader, %4
  %.not.lcssa = phi i1 [ true, %4 ], [ false, %.lr.ph.preheader ], [ %.not3237, %13 ], [ %.not, %.lr.ph ], [ %.not, %17 ]
  ret i1 %.not.lcssa
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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" }

!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"}


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