; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/ds/best_fit_memory.ll (Z8best_fitP13MemoryManageri)

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

%struct.MemoryBlock = type { i32, i32 }
%struct.MemoryManager = type { %struct.MemoryBlock, ptr, ptr }

; Function Attrs: mustprogress nounwind uwtable
define dso_local i64 @_Z8best_fitP13MemoryManageri(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca %struct.MemoryBlock, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca %struct.MemoryBlock, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %17

14:                                               ; preds = %2
  %15 = getelementptr inbounds %struct.MemoryBlock, ptr %3, i32 0, i32 0
  store i32 -1, ptr %15, align 4, !tbaa !11
  %16 = getelementptr inbounds %struct.MemoryBlock, ptr %3, i32 0, i32 1
  store i32 -1, ptr %16, align 4, !tbaa !13
  br label %108

17:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %18, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %19 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 0
  store i32 -1, ptr %19, align 4, !tbaa !11
  %20 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 1
  store i32 -1, ptr %20, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %75, %17
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.MemoryManager, ptr %22, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %79

26:                                               ; preds = %21
  %27 = load i32, ptr %11, align 4, !tbaa !9
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %48

29:                                               ; preds = %26
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.MemoryManager, ptr %30, i32 0, i32 0
  %32 = getelementptr inbounds %struct.MemoryBlock, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 4, !tbaa !16
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = icmp sge i32 %33, %34
  br i1 %35, label %36, label %47

36:                                               ; preds = %29
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.MemoryManager, ptr %37, i32 0, i32 0
  %39 = getelementptr inbounds %struct.MemoryBlock, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 8, !tbaa !17
  %41 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 0
  store i32 %40, ptr %41, align 4, !tbaa !11
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.MemoryManager, ptr %42, i32 0, i32 0
  %44 = getelementptr inbounds %struct.MemoryBlock, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 4, !tbaa !16
  %46 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 1
  store i32 %45, ptr %46, align 4, !tbaa !13
  store i32 1, ptr %11, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %36, %29
  br label %75

48:                                               ; preds = %26
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.MemoryManager, ptr %49, i32 0, i32 0
  %51 = getelementptr inbounds %struct.MemoryBlock, ptr %50, i32 0, i32 1
  %52 = load i32, ptr %51, align 4, !tbaa !16
  %53 = load i32, ptr %5, align 4, !tbaa !9
  %54 = icmp sge i32 %52, %53
  br i1 %54, label %55, label %74

55:                                               ; preds = %48
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.MemoryManager, ptr %56, i32 0, i32 0
  %58 = getelementptr inbounds %struct.MemoryBlock, ptr %57, i32 0, i32 1
  %59 = load i32, ptr %58, align 4, !tbaa !16
  %60 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 1
  %61 = load i32, ptr %60, align 4, !tbaa !13
  %62 = icmp slt i32 %59, %61
  br i1 %62, label %63, label %74

63:                                               ; preds = %55
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.MemoryManager, ptr %64, i32 0, i32 0
  %66 = getelementptr inbounds %struct.MemoryBlock, ptr %65, i32 0, i32 0
  %67 = load i32, ptr %66, align 8, !tbaa !17
  %68 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 0
  store i32 %67, ptr %68, align 4, !tbaa !11
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.MemoryManager, ptr %69, i32 0, i32 0
  %71 = getelementptr inbounds %struct.MemoryBlock, ptr %70, i32 0, i32 1
  %72 = load i32, ptr %71, align 4, !tbaa !16
  %73 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 1
  store i32 %72, ptr %73, align 4, !tbaa !13
  br label %74

74:                                               ; preds = %63, %55, %48
  br label %75

75:                                               ; preds = %74, %47
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.MemoryManager, ptr %76, i32 0, i32 2
  %78 = load ptr, ptr %77, align 8, !tbaa !14
  store ptr %78, ptr %6, align 8, !tbaa !5
  br label %21, !llvm.loop !18

79:                                               ; preds = %21
  %80 = load i32, ptr %5, align 4, !tbaa !9
  %81 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 1
  store i32 %80, ptr %81, align 4, !tbaa !13
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %3, ptr align 4 %10, i64 8, i1 false), !tbaa.struct !20
  %82 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %82, ptr %6, align 8, !tbaa !5
  br label %83

83:                                               ; preds = %103, %79
  %84 = load ptr, ptr %6, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.MemoryManager, ptr %84, i32 0, i32 2
  %86 = load ptr, ptr %85, align 8, !tbaa !14
  %87 = icmp ne ptr %86, null
  br i1 %87, label %88, label %107

88:                                               ; preds = %83
  %89 = load ptr, ptr %6, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.MemoryManager, ptr %89, i32 0, i32 0
  %91 = getelementptr inbounds %struct.MemoryBlock, ptr %90, i32 0, i32 0
  %92 = load i32, ptr %91, align 8, !tbaa !17
  %93 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 0
  %94 = load i32, ptr %93, align 4, !tbaa !11
  %95 = icmp eq i32 %92, %94
  br i1 %95, label %96, label %103

96:                                               ; preds = %88
  %97 = load i32, ptr %5, align 4, !tbaa !9
  %98 = load ptr, ptr %6, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.MemoryManager, ptr %98, i32 0, i32 0
  %100 = getelementptr inbounds %struct.MemoryBlock, ptr %99, i32 0, i32 1
  %101 = load i32, ptr %100, align 4, !tbaa !16
  %102 = sub nsw i32 %101, %97
  store i32 %102, ptr %100, align 4, !tbaa !16
  br label %107

103:                                              ; preds = %88
  %104 = load ptr, ptr %6, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.MemoryManager, ptr %104, i32 0, i32 2
  %106 = load ptr, ptr %105, align 8, !tbaa !14
  store ptr %106, ptr %6, align 8, !tbaa !5
  br label %83, !llvm.loop !21

107:                                              ; preds = %96, %83
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %108

108:                                              ; preds = %107, %14
  %109 = load i64, ptr %3, align 4
  ret i64 %109
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS11MemoryBlock", !10, i64 0, !10, i64 4}
!13 = !{!12, !10, i64 4}
!14 = !{!15, !6, i64 16}
!15 = !{!"_ZTS13MemoryManager", !12, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!15, !10, i64 4}
!17 = !{!15, !10, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{i64 0, i64 4, !9, i64 4, i64 4, !9}
!21 = distinct !{!21, !19}


; 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 memory(readwrite, inaccessiblemem: none) uwtable
define dso_local i64 @_Z8best_fitP13MemoryManageri(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %31, label %.preheader30

.preheader30:                                     ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %.not32 = icmp eq ptr %5, null
  br i1 %.not32, label %.preheader, label %.lr.ph

.preheader:                                       ; preds = %14, %.preheader30
  %.sroa.0.0.lcssa = phi i32 [ -1, %.preheader30 ], [ %.sroa.0.1, %14 ]
  br label %17

.lr.ph:                                           ; preds = %.preheader30, %14
  %6 = phi ptr [ %16, %14 ], [ %5, %.preheader30 ]
  %.036 = phi i32 [ %.1, %14 ], [ 0, %.preheader30 ]
  %.sroa.0.035 = phi i32 [ %.sroa.0.1, %14 ], [ -1, %.preheader30 ]
  %.sroa.7.034 = phi i32 [ %.sroa.7.1, %14 ], [ -1, %.preheader30 ]
  %.02133 = phi ptr [ %6, %14 ], [ %0, %.preheader30 ]
  %7 = icmp eq i32 %.036, 0
  %8 = getelementptr inbounds i8, ptr %.02133, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !12
  br i1 %7, label %10, label %11

10:                                               ; preds = %.lr.ph
  %.not29 = icmp slt i32 %9, %1
  br i1 %.not29, label %14, label %.sink.split

11:                                               ; preds = %.lr.ph
  %.not28 = icmp sge i32 %9, %1
  %12 = icmp slt i32 %9, %.sroa.7.034
  %or.cond = select i1 %.not28, i1 %12, i1 false
  br i1 %or.cond, label %.sink.split, label %14

.sink.split:                                      ; preds = %11, %10
  %13 = load i32, ptr %.02133, align 8, !tbaa !13
  br label %14

14:                                               ; preds = %.sink.split, %11, %10
  %.sroa.7.1 = phi i32 [ %.sroa.7.034, %10 ], [ %.sroa.7.034, %11 ], [ %9, %.sink.split ]
  %.sroa.0.1 = phi i32 [ %.sroa.0.035, %10 ], [ %.sroa.0.035, %11 ], [ %13, %.sink.split ]
  %.1 = phi i32 [ 0, %10 ], [ 1, %11 ], [ 1, %.sink.split ]
  %15 = getelementptr inbounds i8, ptr %6, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !5
  %.not = icmp eq ptr %16, null
  br i1 %.not, label %.preheader, label %.lr.ph, !llvm.loop !14

17:                                               ; preds = %.preheader, %20
  %.122 = phi ptr [ %19, %20 ], [ %0, %.preheader ]
  %18 = getelementptr inbounds i8, ptr %.122, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  %.not27 = icmp eq ptr %19, null
  br i1 %.not27, label %.loopexit, label %20

20:                                               ; preds = %17
  %21 = load i32, ptr %.122, align 8, !tbaa !13
  %22 = icmp eq i32 %21, %.sroa.0.0.lcssa
  br i1 %22, label %23, label %17, !llvm.loop !16

23:                                               ; preds = %20
  %24 = getelementptr inbounds i8, ptr %.122, i64 4
  %25 = load i32, ptr %24, align 4, !tbaa !12
  %26 = sub nsw i32 %25, %1
  store i32 %26, ptr %24, align 4, !tbaa !12
  br label %.loopexit

.loopexit:                                        ; preds = %17, %23
  %27 = zext i32 %1 to i64
  %28 = shl nuw i64 %27, 32
  %29 = zext i32 %.sroa.0.0.lcssa to i64
  %30 = or disjoint i64 %28, %29
  br label %31

31:                                               ; preds = %2, %.loopexit
  %.sroa.020.0.insert.insert = phi i64 [ %30, %.loopexit ], [ -1, %2 ]
  ret i64 %.sroa.020.0.insert.insert
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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, !11, i64 16}
!6 = !{!"_ZTS13MemoryManager", !7, i64 0, !11, i64 8, !11, i64 16}
!7 = !{!"_ZTS11MemoryBlock", !8, i64 0, !8, i64 4}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!6, !8, i64 4}
!13 = !{!6, !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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