; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/ds/worst_fit_memory.ll (Z9worst_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 @_Z9worst_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
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %16

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

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

20:                                               ; preds = %49, %16
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %53

23:                                               ; preds = %20
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.MemoryManager, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds %struct.MemoryBlock, ptr %25, i32 0, i32 1
  %27 = load i32, ptr %26, align 4, !tbaa !14
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = icmp sge i32 %27, %28
  br i1 %29, label %30, label %49

30:                                               ; preds = %23
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.MemoryManager, ptr %31, i32 0, i32 0
  %33 = getelementptr inbounds %struct.MemoryBlock, ptr %32, i32 0, i32 1
  %34 = load i32, ptr %33, align 4, !tbaa !14
  %35 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 1
  %36 = load i32, ptr %35, align 4, !tbaa !13
  %37 = icmp sgt i32 %34, %36
  br i1 %37, label %38, label %49

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

49:                                               ; preds = %38, %30, %23
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.MemoryManager, ptr %50, i32 0, i32 2
  %52 = load ptr, ptr %51, align 8, !tbaa !17
  store ptr %52, ptr %6, align 8, !tbaa !5
  br label %20, !llvm.loop !18

53:                                               ; preds = %20
  %54 = load i32, ptr %5, align 4, !tbaa !9
  %55 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 1
  store i32 %54, ptr %55, 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
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %56, ptr %6, align 8, !tbaa !5
  br label %57

57:                                               ; preds = %77, %53
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.MemoryManager, ptr %58, i32 0, i32 2
  %60 = load ptr, ptr %59, align 8, !tbaa !17
  %61 = icmp ne ptr %60, null
  br i1 %61, label %62, label %81

62:                                               ; preds = %57
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.MemoryManager, ptr %63, i32 0, i32 0
  %65 = getelementptr inbounds %struct.MemoryBlock, ptr %64, i32 0, i32 0
  %66 = load i32, ptr %65, align 8, !tbaa !16
  %67 = getelementptr inbounds %struct.MemoryBlock, ptr %10, i32 0, i32 0
  %68 = load i32, ptr %67, align 4, !tbaa !11
  %69 = icmp eq i32 %66, %68
  br i1 %69, label %70, label %77

70:                                               ; preds = %62
  %71 = load i32, ptr %5, align 4, !tbaa !9
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.MemoryManager, ptr %72, i32 0, i32 0
  %74 = getelementptr inbounds %struct.MemoryBlock, ptr %73, i32 0, i32 1
  %75 = load i32, ptr %74, align 4, !tbaa !14
  %76 = sub nsw i32 %75, %71
  store i32 %76, ptr %74, align 4, !tbaa !14
  br label %81

77:                                               ; preds = %62
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.MemoryManager, ptr %78, i32 0, i32 2
  %80 = load ptr, ptr %79, align 8, !tbaa !17
  store ptr %80, ptr %6, align 8, !tbaa !5
  br label %57, !llvm.loop !21

81:                                               ; preds = %70, %57
  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 %82

82:                                               ; preds = %81, %13
  %83 = load i64, ptr %3, align 4
  ret i64 %83
}

; 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, !10, i64 4}
!15 = !{!"_ZTS13MemoryManager", !12, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!15, !10, i64 0}
!17 = !{!15, !6, i64 16}
!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 @_Z9worst_fitP13MemoryManageri(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %25, label %.preheader21

.preheader21:                                     ; preds = %2, %9
  %.sroa.0.025 = phi i32 [ %.sroa.0.1, %9 ], [ -1, %2 ]
  %.sroa.6.024 = phi i32 [ %.sroa.6.1, %9 ], [ -1, %2 ]
  %.023 = phi ptr [ %11, %9 ], [ %0, %2 ]
  %4 = getelementptr inbounds i8, ptr %.023, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not20 = icmp sge i32 %5, %1
  %6 = icmp sgt i32 %5, %.sroa.6.024
  %or.cond = select i1 %.not20, i1 %6, i1 false
  br i1 %or.cond, label %7, label %9

7:                                                ; preds = %.preheader21
  %8 = load i32, ptr %.023, align 8, !tbaa !12
  br label %9

9:                                                ; preds = %7, %.preheader21
  %.sroa.6.1 = phi i32 [ %5, %7 ], [ %.sroa.6.024, %.preheader21 ]
  %.sroa.0.1 = phi i32 [ %8, %7 ], [ %.sroa.0.025, %.preheader21 ]
  %10 = getelementptr inbounds i8, ptr %.023, i64 16
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %.not = icmp eq ptr %11, null
  br i1 %.not, label %.preheader, label %.preheader21, !llvm.loop !14

.preheader:                                       ; preds = %9, %14
  %.1 = phi ptr [ %13, %14 ], [ %0, %9 ]
  %12 = getelementptr inbounds i8, ptr %.1, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %.not19 = icmp eq ptr %13, null
  br i1 %.not19, label %.loopexit, label %14

14:                                               ; preds = %.preheader
  %15 = load i32, ptr %.1, align 8, !tbaa !12
  %16 = icmp eq i32 %15, %.sroa.0.1
  br i1 %16, label %17, label %.preheader, !llvm.loop !16

17:                                               ; preds = %14
  %18 = getelementptr inbounds i8, ptr %.1, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = sub nsw i32 %19, %1
  store i32 %20, ptr %18, align 4, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader, %17
  %21 = zext i32 %1 to i64
  %22 = shl nuw i64 %21, 32
  %23 = zext i32 %.sroa.0.1 to i64
  %24 = or disjoint i64 %22, %23
  br label %25

25:                                               ; preds = %2, %.loopexit
  %.sroa.015.0.insert.insert = phi i64 [ %24, %.loopexit ], [ -1, %2 ]
  ret i64 %.sroa.015.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, !8, i64 4}
!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 0}
!13 = !{!6, !11, i64 16}
!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