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

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

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %16, 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
  br label %17

17:                                               ; preds = %31, %15
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.MemoryManager, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %29

22:                                               ; preds = %17
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.MemoryManager, ptr %23, i32 0, i32 0
  %25 = getelementptr inbounds %struct.MemoryBlock, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !16
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = icmp slt i32 %26, %27
  br label %29

29:                                               ; preds = %22, %17
  %30 = phi i1 [ false, %17 ], [ %28, %22 ]
  br i1 %30, label %31, label %35

31:                                               ; preds = %29
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.MemoryManager, ptr %32, i32 0, i32 2
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  store ptr %34, ptr %6, align 8, !tbaa !5
  br label %17, !llvm.loop !17

35:                                               ; preds = %29
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.MemoryManager, ptr %36, i32 0, i32 0
  %38 = getelementptr inbounds %struct.MemoryBlock, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %38, align 4, !tbaa !16
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = icmp sge i32 %39, %40
  br i1 %41, label %42, label %56

42:                                               ; preds = %35
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.MemoryManager, ptr %43, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %3, ptr align 8 %44, i64 8, i1 false), !tbaa.struct !19
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.MemoryManager, ptr %45, i32 0, i32 0
  %47 = getelementptr inbounds %struct.MemoryBlock, ptr %46, i32 0, i32 1
  %48 = load i32, ptr %47, align 4, !tbaa !16
  %49 = getelementptr inbounds %struct.MemoryBlock, ptr %3, i32 0, i32 1
  store i32 %48, ptr %49, align 4, !tbaa !13
  %50 = load i32, ptr %5, align 4, !tbaa !9
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.MemoryManager, ptr %51, i32 0, i32 0
  %53 = getelementptr inbounds %struct.MemoryBlock, ptr %52, i32 0, i32 1
  %54 = load i32, ptr %53, align 4, !tbaa !16
  %55 = sub nsw i32 %54, %50
  store i32 %55, ptr %53, align 4, !tbaa !16
  br label %59

56:                                               ; preds = %35
  %57 = getelementptr inbounds %struct.MemoryBlock, ptr %3, i32 0, i32 0
  store i32 -1, ptr %57, align 4, !tbaa !11
  %58 = getelementptr inbounds %struct.MemoryBlock, ptr %3, i32 0, i32 1
  store i32 -1, ptr %58, align 4, !tbaa !13
  br label %59

59:                                               ; preds = %56, %42
  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 %60

60:                                               ; preds = %59, %12
  %61 = load i64, ptr %3, align 4
  ret i64 %61
}

; 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 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{i64 0, i64 4, !9, i64 4, i64 4, !9}


; 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 @_Z9first_fitP13MemoryManageri(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %14, label %.preheader

.preheader:                                       ; preds = %2, %.preheader
  %.0 = phi ptr [ %5, %.preheader ], [ %0, %2 ]
  %4 = getelementptr inbounds i8, ptr %.0, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %.not = icmp ne ptr %5, null
  %.phi.trans.insert = getelementptr inbounds i8, ptr %.0, i64 4
  %.pre = load i32, ptr %.phi.trans.insert, align 4, !tbaa !12
  %6 = icmp slt i32 %.pre, %1
  %or.cond = select i1 %.not, i1 %6, i1 false
  br i1 %or.cond, label %.preheader, label %.critedge, !llvm.loop !13

.critedge:                                        ; preds = %.preheader
  %.not15 = icmp slt i32 %.pre, %1
  br i1 %.not15, label %14, label %7

7:                                                ; preds = %.critedge
  %8 = getelementptr inbounds i8, ptr %.0, i64 4
  %.sroa.0.0.copyload = load i32, ptr %.0, align 8, !tbaa !15
  %9 = sub nsw i32 %.pre, %1
  store i32 %9, ptr %8, align 4, !tbaa !12
  %10 = zext i32 %.pre to i64
  %11 = shl nuw i64 %10, 32
  %12 = zext i32 %.sroa.0.0.copyload to i64
  %13 = or disjoint i64 %11, %12
  br label %14

14:                                               ; preds = %7, %.critedge, %2
  %.sroa.0.0.insert.insert = phi i64 [ -1, %2 ], [ %13, %7 ], [ -1, %.critedge ]
  ret i64 %.sroa.0.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 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!8, !8, i64 0}


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