; LLVM IR from narke/Aragveli :: src/kernel/memory (heap)

; 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.anon = type { ptr }
%struct.anon.0 = type { ptr }
%struct.memory_range = type { i64, i64, %struct.anon.1 }
%struct.anon.1 = type { ptr }

@free_ranges = external dso_local global %struct.anon, align 8
@used_ranges = external dso_local global %struct.anon.0, align 8

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local void @heap_free(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %9, label %8

8:                                                ; preds = %1
  store i32 1, ptr %4, align 4
  br label %77

9:                                                ; preds = %1
  %10 = load ptr, ptr @used_ranges, align 8, !tbaa !9
  store ptr %10, ptr %3, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %71, %9
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %76

14:                                               ; preds = %11
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.memory_range, ptr %15, i32 0, i32 0
  %17 = load i64, ptr %16, align 8, !tbaa !11
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = ptrtoint ptr %18 to i64
  %20 = icmp eq i64 %17, %19
  br i1 %20, label %21, label %70

21:                                               ; preds = %14
  br label %22

22:                                               ; preds = %21
  %23 = load ptr, ptr @used_ranges, align 8, !tbaa !9
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = icmp eq ptr %23, %24
  br i1 %25, label %26, label %34

26:                                               ; preds = %22
  br label %27

27:                                               ; preds = %26
  %28 = load ptr, ptr @used_ranges, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.memory_range, ptr %28, i32 0, i32 2
  %30 = getelementptr inbounds %struct.anon.1, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  store ptr %31, ptr @used_ranges, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %27
  br label %33

33:                                               ; preds = %32
  br label %59

34:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %35 = load ptr, ptr @used_ranges, align 8, !tbaa !9
  store ptr %35, ptr %5, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %43, %34
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.memory_range, ptr %37, i32 0, i32 2
  %39 = getelementptr inbounds %struct.anon.1, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = icmp ne ptr %40, %41
  br i1 %42, label %43, label %48

43:                                               ; preds = %36
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.memory_range, ptr %44, i32 0, i32 2
  %46 = getelementptr inbounds %struct.anon.1, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !14
  store ptr %47, ptr %5, align 8, !tbaa !5
  br label %36, !llvm.loop !15

48:                                               ; preds = %36
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.memory_range, ptr %49, i32 0, i32 2
  %51 = getelementptr inbounds %struct.anon.1, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !14
  %53 = getelementptr inbounds %struct.memory_range, ptr %52, i32 0, i32 2
  %54 = getelementptr inbounds %struct.anon.1, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !14
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.memory_range, ptr %56, i32 0, i32 2
  %58 = getelementptr inbounds %struct.anon.1, ptr %57, i32 0, i32 0
  store ptr %55, ptr %58, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %59

59:                                               ; preds = %48, %33
  br label %60

60:                                               ; preds = %59
  br label %61

61:                                               ; preds = %60
  br label %62

62:                                               ; preds = %61
  %63 = load ptr, ptr @free_ranges, align 8, !tbaa !9
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.memory_range, ptr %64, i32 0, i32 2
  %66 = getelementptr inbounds %struct.anon.1, ptr %65, i32 0, i32 0
  store ptr %63, ptr %66, align 8, !tbaa !14
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %67, ptr @free_ranges, align 8, !tbaa !9
  br label %68

68:                                               ; preds = %62
  br label %69

69:                                               ; preds = %68
  br label %70

70:                                               ; preds = %69, %14
  br label %71

71:                                               ; preds = %70
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.memory_range, ptr %72, i32 0, i32 2
  %74 = getelementptr inbounds %struct.anon.1, ptr %73, i32 0, i32 0
  %75 = load ptr, ptr %74, align 8, !tbaa !14
  store ptr %75, ptr %3, align 8, !tbaa !5
  br label %11, !llvm.loop !17

76:                                               ; preds = %11
  store i32 0, ptr %4, align 4
  br label %77

77:                                               ; preds = %76, %8
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  %78 = load i32, ptr %4, align 4
  switch i32 %78, label %80 [
    i32 0, label %79
    i32 1, label %79
  ]

79:                                               ; preds = %77, %77
  ret void

80:                                               ; preds = %77
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"", !6, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"memory_range", !13, i64 0, !13, i64 8, !10, i64 16}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !6, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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

%struct.anon = type { ptr }
%struct.anon.0 = type { ptr }

@free_ranges = external dso_local local_unnamed_addr global %struct.anon, align 8
@used_ranges = external dso_local local_unnamed_addr global %struct.anon.0, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @heap_free(ptr noundef %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %.preheader20

.preheader20:                                     ; preds = %1
  %.01324 = load ptr, ptr @used_ranges, align 8, !tbaa !5
  %.not1825 = icmp eq ptr %.01324, null
  br i1 %.not1825, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader20
  %free_ranges.promoted = load ptr, ptr @free_ranges, align 8, !tbaa !5
  %2 = ptrtoint ptr %0 to i64
  br label %3

3:                                                ; preds = %.lr.ph, %21
  %.01327 = phi ptr [ %.01324, %.lr.ph ], [ %.013, %21 ]
  %4 = phi ptr [ %.01324, %.lr.ph ], [ %22, %21 ]
  %.0132326 = phi ptr [ %free_ranges.promoted, %.lr.ph ], [ %.01322, %21 ]
  %5 = load i64, ptr %.01327, align 8, !tbaa !10
  %6 = icmp eq i64 %5, %2
  br i1 %6, label %7, label %._crit_edge

._crit_edge:                                      ; preds = %3
  %.phi.trans.insert = getelementptr inbounds i8, ptr %.01327, i64 16
  %.013.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !5
  br label %21

7:                                                ; preds = %3
  %8 = icmp eq ptr %4, %.01327
  br i1 %8, label %9, label %.preheader

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %4, i64 16
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  store ptr %11, ptr @used_ranges, align 8, !tbaa !5
  br label %18

.preheader:                                       ; preds = %7, %.preheader
  %.0 = phi ptr [ %13, %.preheader ], [ %4, %7 ]
  %12 = getelementptr inbounds i8, ptr %.0, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %.not19 = icmp eq ptr %13, %.01327
  br i1 %.not19, label %14, label %.preheader, !llvm.loop !14

14:                                               ; preds = %.preheader
  %15 = getelementptr inbounds i8, ptr %.0, i64 16
  %16 = getelementptr inbounds i8, ptr %13, i64 16
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  store ptr %17, ptr %15, align 8, !tbaa !13
  br label %18

18:                                               ; preds = %9, %14
  %19 = phi ptr [ %11, %9 ], [ %4, %14 ]
  %20 = getelementptr inbounds i8, ptr %.01327, i64 16
  store ptr %.0132326, ptr %20, align 8, !tbaa !13
  store ptr %.01327, ptr @free_ranges, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %._crit_edge, %18
  %.013 = phi ptr [ %.013.pre, %._crit_edge ], [ %.0132326, %18 ]
  %.01322 = phi ptr [ %.0132326, %._crit_edge ], [ %.01327, %18 ]
  %22 = phi ptr [ %4, %._crit_edge ], [ %19, %18 ]
  %.not18 = icmp eq ptr %.013, null
  br i1 %.not18, label %.loopexit, label %3, !llvm.loop !16

.loopexit:                                        ; preds = %21, %.preheader20, %1
  ret void
}

attributes #0 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !12, i64 0}
!11 = !{!"memory_range", !12, i64 0, !12, i64 8, !6, i64 16}
!12 = !{!"long", !8, i64 0}
!13 = !{!11, !7, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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