; LLVM IR from whoiswelliton/Computer_Engineering_UTFPR-PB :: Embedded_Systems/TIVA Connected TM4C1294XL Projects/00 - freeRTOS10_on_TIVA (vPortGetHeapStats)

; 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 %42

10:                                               ; preds = %1
  br label %11

11:                                               ; preds = %37, %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 = load i64, ptr %6, align 8, !tbaa !9
  %28 = icmp ult i64 %26, %27
  br i1 %28, label %29, label %33

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

33:                                               ; preds = %29, %23
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  store ptr %36, ptr %3, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %33
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = load ptr, ptr @pxEnd, align 8, !tbaa !5
  %40 = icmp ne ptr %38, %39
  br i1 %40, label %11, label %41, !llvm.loop !14

41:                                               ; preds = %37
  br label %42

42:                                               ; preds = %41, %1
  %43 = call i64 @xTaskResumeAll()
  %44 = load i64, ptr %5, align 8, !tbaa !9
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.xHeapStats, ptr %45, i32 0, i32 1
  store i64 %44, ptr %46, align 8, !tbaa !16
  %47 = load i64, ptr %6, align 8, !tbaa !9
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.xHeapStats, ptr %48, i32 0, i32 2
  store i64 %47, ptr %49, align 8, !tbaa !18
  %50 = load i64, ptr %4, align 8, !tbaa !9
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.xHeapStats, ptr %51, i32 0, i32 3
  store i64 %50, ptr %52, align 8, !tbaa !19
  call void @vPortEnterCritical()
  %53 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !9
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.xHeapStats, ptr %54, i32 0, i32 0
  store i64 %53, ptr %55, align 8, !tbaa !20
  %56 = load i64, ptr @xNumberOfSuccessfulAllocations, align 8, !tbaa !9
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.xHeapStats, ptr %57, i32 0, i32 5
  store i64 %56, ptr %58, align 8, !tbaa !21
  %59 = load i64, ptr @xNumberOfSuccessfulFrees, align 8, !tbaa !9
  %60 = load ptr, ptr %2, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.xHeapStats, ptr %60, i32 0, i32 6
  store i64 %59, ptr %61, align 8, !tbaa !22
  %62 = load i64, ptr @xMinimumEverFreeBytesRemaining, align 8, !tbaa !9
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.xHeapStats, ptr %63, i32 0, i32 4
  store i64 %62, ptr %64, 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
  %.024 = phi ptr [ %8, %4 ], [ %2, %.preheader ]
  %.022 = phi i64 [ %5, %4 ], [ 0, %.preheader ]
  %.019 = phi i64 [ %spec.select, %4 ], [ 0, %.preheader ]
  %.0 = phi i64 [ %.1, %4 ], [ 4294967295, %.preheader ]
  %5 = add i64 %.022, 1
  %6 = getelementptr inbounds i8, ptr %.024, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !12
  %spec.select = tail call i64 @llvm.umax.i64(i64 %7, i64 %.019)
  %.1 = tail call i64 @llvm.umin.i64(i64 %7, i64 %.0)
  %8 = load ptr, ptr %.024, align 8, !tbaa !5
  %.not29 = icmp eq ptr %8, %3
  br i1 %.not29, label %.loopexit, label %4, !llvm.loop !13

.loopexit:                                        ; preds = %4, %1
  %.123 = phi i64 [ 0, %1 ], [ %5, %4 ]
  %.221 = phi i64 [ 0, %1 ], [ %spec.select, %4 ]
  %.2 = phi i64 [ 4294967295, %1 ], [ %.1, %4 ]
  %9 = tail call i64 @xTaskResumeAll() #3
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  store i64 %.221, ptr %10, align 8, !tbaa !15
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 %.2, ptr %11, align 8, !tbaa !17
  %12 = getelementptr inbounds i8, ptr %0, i64 24
  store i64 %.123, ptr %12, align 8, !tbaa !18
  tail call void @vPortEnterCritical() #3
  %13 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !19
  store i64 %13, ptr %0, align 8, !tbaa !20
  %14 = load i64, ptr @xNumberOfSuccessfulAllocations, align 8, !tbaa !19
  %15 = getelementptr inbounds i8, ptr %0, i64 40
  store i64 %14, ptr %15, align 8, !tbaa !21
  %16 = load i64, ptr @xNumberOfSuccessfulFrees, align 8, !tbaa !19
  %17 = getelementptr inbounds i8, ptr %0, i64 48
  store i64 %16, ptr %17, align 8, !tbaa !22
  %18 = load i64, ptr @xMinimumEverFreeBytesRemaining, align 8, !tbaa !19
  %19 = getelementptr inbounds i8, ptr %0, i64 32
  store i64 %18, ptr %19, 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