; LLVM IR from ctlst-tech/catpilot :: os/freertos/memmang (heap_5)

; 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.A_BLOCK_LINK = type { ptr, i64 }
%struct.xHeapStats = type { i64, i64, i64, i64, i64, i64, i64 }

@pxEnd = external hidden global ptr, align 8
@xFreeBytesRemaining = external hidden global i64, align 8
@xStart = external hidden global %struct.A_BLOCK_LINK, align 8
@xMinimumEverFreeBytesRemaining = external hidden global i64, align 8
@xNumberOfSuccessfulAllocations = external hidden global i64, align 8
@xNumberOfSuccessfulFrees = external hidden global i64, align 8

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

declare void @vTaskSuspendAll() #1

declare i64 @xTaskResumeAll() #1

; 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 @vPortGetHeapStats(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 0, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 0, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 4294967295, ptr %6, align 8, !tbaa !9
  call void @vTaskSuspendAll()
  %7 = load ptr, ptr @xStart, align 8, !tbaa !11
  store ptr %7, ptr %3, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %10, label %48

10:                                               ; preds = %1
  br label %11

11:                                               ; preds = %43, %10
  %12 = load i64, ptr %4, align 8, !tbaa !9
  %13 = add i64 %12, 1
  store i64 %13, ptr %4, align 8, !tbaa !9
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %14, i32 0, i32 1
  %16 = load i64, ptr %15, align 8, !tbaa !13
  %17 = load i64, ptr %5, align 8, !tbaa !9
  %18 = icmp ugt i64 %16, %17
  br i1 %18, label %19, label %23

19:                                               ; preds = %11
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %20, i32 0, i32 1
  %22 = load i64, ptr %21, align 8, !tbaa !13
  store i64 %22, ptr %5, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %19, %11
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %24, i32 0, i32 1
  %26 = load i64, ptr %25, align 8, !tbaa !13
  %27 = icmp ne i64 %26, 0
  br i1 %27, label %28, label %39

28:                                               ; preds = %23
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %29, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !13
  %32 = load i64, ptr %6, align 8, !tbaa !9
  %33 = icmp ult i64 %31, %32
  br i1 %33, label %34, label %38

34:                                               ; preds = %28
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %35, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !13
  store i64 %37, ptr %6, align 8, !tbaa !9
  br label %38

38:                                               ; preds = %34, %28
  br label %39

39:                                               ; preds = %38, %23
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  store ptr %42, ptr %3, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %39
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = load ptr, ptr @pxEnd, align 8, !tbaa !5
  %46 = icmp ne ptr %44, %45
  br i1 %46, label %11, label %47, !llvm.loop !14

47:                                               ; preds = %43
  br label %48

48:                                               ; preds = %47, %1
  %49 = call i64 @xTaskResumeAll()
  %50 = load i64, ptr %5, align 8, !tbaa !9
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.xHeapStats, ptr %51, i32 0, i32 1
  store i64 %50, ptr %52, align 8, !tbaa !16
  %53 = load i64, ptr %6, align 8, !tbaa !9
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.xHeapStats, ptr %54, i32 0, i32 2
  store i64 %53, ptr %55, align 8, !tbaa !18
  %56 = load i64, ptr %4, align 8, !tbaa !9
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.xHeapStats, ptr %57, i32 0, i32 3
  store i64 %56, ptr %58, align 8, !tbaa !19
  call void @vPortEnterCritical()
  %59 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !9
  %60 = load ptr, ptr %2, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.xHeapStats, ptr %60, i32 0, i32 0
  store i64 %59, ptr %61, align 8, !tbaa !20
  %62 = load i64, ptr @xNumberOfSuccessfulAllocations, align 8, !tbaa !9
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.xHeapStats, ptr %63, i32 0, i32 5
  store i64 %62, ptr %64, align 8, !tbaa !21
  %65 = load i64, ptr @xNumberOfSuccessfulFrees, align 8, !tbaa !9
  %66 = load ptr, ptr %2, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.xHeapStats, ptr %66, i32 0, i32 6
  store i64 %65, ptr %67, align 8, !tbaa !22
  %68 = load i64, ptr @xMinimumEverFreeBytesRemaining, align 8, !tbaa !9
  %69 = load ptr, ptr %2, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.xHeapStats, ptr %69, i32 0, i32 4
  store i64 %68, ptr %70, align 8, !tbaa !23
  call void @vPortExitCritical()
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

declare void @vPortEnterCritical() #1

declare void @vPortExitCritical() #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"A_BLOCK_LINK", !6, i64 0, !10, i64 8}
!13 = !{!12, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !10, i64 8}
!17 = !{!"xHeapStats", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!18 = !{!17, !10, i64 16}
!19 = !{!17, !10, i64 24}
!20 = !{!17, !10, i64 0}
!21 = !{!17, !10, i64 40}
!22 = !{!17, !10, i64 48}
!23 = !{!17, !10, i64 32}


; 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.A_BLOCK_LINK = type { ptr, i64 }

@pxEnd = external hidden local_unnamed_addr global ptr, align 8
@xFreeBytesRemaining = external hidden local_unnamed_addr global i64, align 8
@xStart = external hidden local_unnamed_addr global %struct.A_BLOCK_LINK, align 8
@xMinimumEverFreeBytesRemaining = external hidden local_unnamed_addr global i64, align 8
@xNumberOfSuccessfulAllocations = external hidden local_unnamed_addr global i64, align 8
@xNumberOfSuccessfulFrees = external hidden local_unnamed_addr global i64, align 8

declare void @vTaskSuspendAll() local_unnamed_addr #0

declare i64 @xTaskResumeAll() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @vPortGetHeapStats(ptr nocapture noundef writeonly %0) local_unnamed_addr #1 {
  tail call void @vTaskSuspendAll() #3
  %2 = load ptr, ptr @xStart, align 8, !tbaa !5
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %1
  %3 = load ptr, ptr @pxEnd, align 8, !tbaa !11
  br label %4

4:                                                ; preds = %.preheader, %4
  %.025 = phi ptr [ %9, %4 ], [ %2, %.preheader ]
  %.023 = phi i64 [ %5, %4 ], [ 0, %.preheader ]
  %.020 = phi i64 [ %spec.select, %4 ], [ 0, %.preheader ]
  %.0 = phi i64 [ %.1, %4 ], [ 4294967295, %.preheader ]
  %5 = add i64 %.023, 1
  %6 = getelementptr inbounds i8, ptr %.025, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !12
  %spec.select = tail call i64 @llvm.umax.i64(i64 %7, i64 %.020)
  %.not30.not = icmp eq i64 %7, 0
  %8 = tail call i64 @llvm.umin.i64(i64 %7, i64 %.0)
  %.1 = select i1 %.not30.not, i64 %.0, i64 %8
  %9 = load ptr, ptr %.025, align 8, !tbaa !5
  %.not31 = icmp eq ptr %9, %3
  br i1 %.not31, label %.loopexit, label %4, !llvm.loop !13

.loopexit:                                        ; preds = %4, %1
  %.124 = phi i64 [ 0, %1 ], [ %5, %4 ]
  %.222 = phi i64 [ 0, %1 ], [ %spec.select, %4 ]
  %.2 = phi i64 [ 4294967295, %1 ], [ %.1, %4 ]
  %10 = tail call i64 @xTaskResumeAll() #3
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  store i64 %.222, ptr %11, align 8, !tbaa !15
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 %.2, ptr %12, align 8, !tbaa !17
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  store i64 %.124, ptr %13, align 8, !tbaa !18
  tail call void @vPortEnterCritical() #3
  %14 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !19
  store i64 %14, ptr %0, align 8, !tbaa !20
  %15 = load i64, ptr @xNumberOfSuccessfulAllocations, align 8, !tbaa !19
  %16 = getelementptr inbounds i8, ptr %0, i64 40
  store i64 %15, ptr %16, align 8, !tbaa !21
  %17 = load i64, ptr @xNumberOfSuccessfulFrees, align 8, !tbaa !19
  %18 = getelementptr inbounds i8, ptr %0, i64 48
  store i64 %17, ptr %18, align 8, !tbaa !22
  %19 = load i64, ptr @xMinimumEverFreeBytesRemaining, align 8, !tbaa !19
  %20 = getelementptr inbounds i8, ptr %0, i64 32
  store i64 %19, ptr %20, align 8, !tbaa !23
  tail call void @vPortExitCritical() #3
  ret void
}

declare void @vPortEnterCritical() local_unnamed_addr #0

declare void @vPortExitCritical() local_unnamed_addr #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #2

attributes #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 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !7, i64 0}
!6 = !{!"A_BLOCK_LINK", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!6, !10, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !10, i64 8}
!16 = !{!"xHeapStats", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!17 = !{!16, !10, i64 16}
!18 = !{!16, !10, i64 24}
!19 = !{!10, !10, i64 0}
!20 = !{!16, !10, i64 0}
!21 = !{!16, !10, i64 40}
!22 = !{!16, !10, i64 48}
!23 = !{!16, !10, i64 32}


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