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

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

@pvPortMalloc.xHeapHasBeenInitialised = external hidden global i64, align 8
@xFreeBytesRemaining = external hidden global i64, align 8
@xStart = external hidden global %struct.A_BLOCK_LINK, align 8
@xEnd = external hidden global %struct.A_BLOCK_LINK, align 8

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

11:                                               ; preds = %1
  call void @prvHeapInit()
  store i64 1, ptr @pvPortMalloc.xHeapHasBeenInitialised, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %11, %1
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = icmp ugt i64 %13, 0
  br i1 %14, label %15, label %46

15:                                               ; preds = %12
  %16 = load i64, ptr %2, align 8, !tbaa !5
  %17 = add i64 %16, 16
  %18 = load i64, ptr %2, align 8, !tbaa !5
  %19 = icmp ugt i64 %17, %18
  br i1 %19, label %20, label %46

20:                                               ; preds = %15
  %21 = load i64, ptr %2, align 8, !tbaa !5
  %22 = add i64 %21, 16
  store i64 %22, ptr %2, align 8, !tbaa !5
  %23 = load i64, ptr %2, align 8, !tbaa !5
  %24 = load i64, ptr %2, align 8, !tbaa !5
  %25 = and i64 %24, 7
  %26 = sub i64 8, %25
  %27 = add i64 %23, %26
  %28 = load i64, ptr %2, align 8, !tbaa !5
  %29 = icmp ugt i64 %27, %28
  br i1 %29, label %30, label %44

30:                                               ; preds = %20
  %31 = load i64, ptr %2, align 8, !tbaa !5
  %32 = and i64 %31, 7
  %33 = sub i64 8, %32
  %34 = load i64, ptr %2, align 8, !tbaa !5
  %35 = add i64 %34, %33
  store i64 %35, ptr %2, align 8, !tbaa !5
  %36 = load i64, ptr %2, align 8, !tbaa !5
  %37 = and i64 %36, 7
  %38 = icmp eq i64 %37, 0
  %39 = zext i1 %38 to i32
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %41, label %43

41:                                               ; preds = %30
  call void @vPortRaiseBASEPRI()
  br label %42

42:                                               ; preds = %42, %41
  br label %42

43:                                               ; preds = %30
  br label %45

44:                                               ; preds = %20
  store i64 0, ptr %2, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %44, %43
  br label %47

46:                                               ; preds = %15, %12
  store i64 0, ptr %2, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %46, %45
  %48 = load i64, ptr %2, align 8, !tbaa !5
  %49 = icmp ugt i64 %48, 0
  br i1 %49, label %50, label %139

50:                                               ; preds = %47
  %51 = load i64, ptr %2, align 8, !tbaa !5
  %52 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  %53 = icmp ule i64 %51, %52
  br i1 %53, label %54, label %139

54:                                               ; preds = %50
  store ptr @xStart, ptr %4, align 8, !tbaa !9
  %55 = load ptr, ptr @xStart, align 8, !tbaa !11
  store ptr %55, ptr %3, align 8, !tbaa !9
  br label %56

56:                                               ; preds = %69, %54
  %57 = load ptr, ptr %3, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %57, i32 0, i32 1
  %59 = load i64, ptr %58, align 8, !tbaa !13
  %60 = load i64, ptr %2, align 8, !tbaa !5
  %61 = icmp ult i64 %59, %60
  br i1 %61, label %62, label %67

62:                                               ; preds = %56
  %63 = load ptr, ptr %3, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !11
  %66 = icmp ne ptr %65, null
  br label %67

67:                                               ; preds = %62, %56
  %68 = phi i1 [ false, %56 ], [ %66, %62 ]
  br i1 %68, label %69, label %74

69:                                               ; preds = %67
  %70 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %70, ptr %4, align 8, !tbaa !9
  %71 = load ptr, ptr %3, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %71, i32 0, i32 0
  %73 = load ptr, ptr %72, align 8, !tbaa !11
  store ptr %73, ptr %3, align 8, !tbaa !9
  br label %56, !llvm.loop !14

74:                                               ; preds = %67
  %75 = load ptr, ptr %3, align 8, !tbaa !9
  %76 = icmp ne ptr %75, @xEnd
  br i1 %76, label %77, label %138

77:                                               ; preds = %74
  %78 = load ptr, ptr %4, align 8, !tbaa !9
  %79 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %78, i32 0, i32 0
  %80 = load ptr, ptr %79, align 8, !tbaa !11
  %81 = getelementptr inbounds i8, ptr %80, i64 16
  store ptr %81, ptr %6, align 8, !tbaa !9
  %82 = load ptr, ptr %3, align 8, !tbaa !9
  %83 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %82, i32 0, i32 0
  %84 = load ptr, ptr %83, align 8, !tbaa !11
  %85 = load ptr, ptr %4, align 8, !tbaa !9
  %86 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %85, i32 0, i32 0
  store ptr %84, ptr %86, align 8, !tbaa !11
  %87 = load ptr, ptr %3, align 8, !tbaa !9
  %88 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %87, i32 0, i32 1
  %89 = load i64, ptr %88, align 8, !tbaa !13
  %90 = load i64, ptr %2, align 8, !tbaa !5
  %91 = sub i64 %89, %90
  %92 = icmp ugt i64 %91, 32
  br i1 %92, label %93, label %132

93:                                               ; preds = %77
  %94 = load ptr, ptr %3, align 8, !tbaa !9
  %95 = load i64, ptr %2, align 8, !tbaa !5
  %96 = getelementptr inbounds i8, ptr %94, i64 %95
  store ptr %96, ptr %5, align 8, !tbaa !9
  %97 = load ptr, ptr %3, align 8, !tbaa !9
  %98 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %97, i32 0, i32 1
  %99 = load i64, ptr %98, align 8, !tbaa !13
  %100 = load i64, ptr %2, align 8, !tbaa !5
  %101 = sub i64 %99, %100
  %102 = load ptr, ptr %5, align 8, !tbaa !9
  %103 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %102, i32 0, i32 1
  store i64 %101, ptr %103, align 8, !tbaa !13
  %104 = load i64, ptr %2, align 8, !tbaa !5
  %105 = load ptr, ptr %3, align 8, !tbaa !9
  %106 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %105, i32 0, i32 1
  store i64 %104, ptr %106, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %107 = load ptr, ptr %5, align 8, !tbaa !9
  %108 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %107, i32 0, i32 1
  %109 = load i64, ptr %108, align 8, !tbaa !13
  store i64 %109, ptr %8, align 8, !tbaa !5
  store ptr @xStart, ptr %7, align 8, !tbaa !9
  br label %110

110:                                              ; preds = %119, %93
  %111 = load ptr, ptr %7, align 8, !tbaa !9
  %112 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %111, i32 0, i32 0
  %113 = load ptr, ptr %112, align 8, !tbaa !11
  %114 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %113, i32 0, i32 1
  %115 = load i64, ptr %114, align 8, !tbaa !13
  %116 = load i64, ptr %8, align 8, !tbaa !5
  %117 = icmp ult i64 %115, %116
  br i1 %117, label %118, label %123

118:                                              ; preds = %110
  br label %119

119:                                              ; preds = %118
  %120 = load ptr, ptr %7, align 8, !tbaa !9
  %121 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %120, i32 0, i32 0
  %122 = load ptr, ptr %121, align 8, !tbaa !11
  store ptr %122, ptr %7, align 8, !tbaa !9
  br label %110, !llvm.loop !16

123:                                              ; preds = %110
  %124 = load ptr, ptr %7, align 8, !tbaa !9
  %125 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %124, i32 0, i32 0
  %126 = load ptr, ptr %125, align 8, !tbaa !11
  %127 = load ptr, ptr %5, align 8, !tbaa !9
  %128 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %127, i32 0, i32 0
  store ptr %126, ptr %128, align 8, !tbaa !11
  %129 = load ptr, ptr %5, align 8, !tbaa !9
  %130 = load ptr, ptr %7, align 8, !tbaa !9
  %131 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %130, i32 0, i32 0
  store ptr %129, ptr %131, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %132

132:                                              ; preds = %123, %77
  %133 = load ptr, ptr %3, align 8, !tbaa !9
  %134 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %133, i32 0, i32 1
  %135 = load i64, ptr %134, align 8, !tbaa !13
  %136 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  %137 = sub i64 %136, %135
  store i64 %137, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  br label %138

138:                                              ; preds = %132, %74
  br label %139

139:                                              ; preds = %138, %50, %47
  %140 = call i64 @xTaskResumeAll()
  %141 = load ptr, ptr %6, align 8, !tbaa !9
  %142 = icmp eq ptr %141, null
  br i1 %142, label %143, label %144

143:                                              ; preds = %139
  call void @vApplicationMallocFailedHook()
  br label %144

144:                                              ; preds = %143, %139
  %145 = load ptr, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret ptr %145
}

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

declare void @vTaskSuspendAll() #2

; Function Attrs: nounwind uwtable
declare hidden void @prvHeapInit() #0

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @vPortRaiseBASEPRI() #3

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

declare i64 @xTaskResumeAll() #2

declare void @vApplicationMallocFailedHook() #2

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


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

@pvPortMalloc.xHeapHasBeenInitialised = external hidden local_unnamed_addr global i64, 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
@xEnd = external hidden global %struct.A_BLOCK_LINK, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @pvPortMalloc(i64 noundef %0) local_unnamed_addr #0 {
  tail call void @vTaskSuspendAll() #2
  %2 = load i64, ptr @pvPortMalloc.xHeapHasBeenInitialised, align 8, !tbaa !5
  %3 = icmp eq i64 %2, 0
  br i1 %3, label %4, label %5

4:                                                ; preds = %1
  tail call void @prvHeapInit()
  store i64 1, ptr @pvPortMalloc.xHeapHasBeenInitialised, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %4, %1
  %6 = add i64 %0, -1
  %or.cond = icmp ult i64 %6, -17
  br i1 %or.cond, label %7, label %.thread

7:                                                ; preds = %5
  %8 = add nuw i64 %0, 16
  %9 = and i64 %0, -8
  %10 = add i64 %9, 24
  %11 = icmp ule i64 %10, %8
  %. = select i1 %11, i64 0, i64 %10
  %12 = load i64, ptr @xFreeBytesRemaining, align 8
  %.not50 = icmp ugt i64 %., %12
  %or.cond53 = select i1 %11, i1 true, i1 %.not50
  br i1 %or.cond53, label %.thread, label %13

13:                                               ; preds = %7
  %14 = load ptr, ptr @xStart, align 8, !tbaa !9
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  %16 = load i64, ptr %15, align 8, !tbaa !12
  %17 = icmp ult i64 %16, %.
  br i1 %17, label %.lr.ph.preheader, label %.critedge

.lr.ph.preheader:                                 ; preds = %13
  %18 = load ptr, ptr %14, align 8, !tbaa !9
  %.not5175 = icmp eq ptr %18, null
  br i1 %.not5175, label %.critedge, label %.lr.ph77, !llvm.loop !13

.lr.ph77:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %19 = phi ptr [ %23, %.lr.ph ], [ %18, %.lr.ph.preheader ]
  %.0426676 = phi ptr [ %19, %.lr.ph ], [ %14, %.lr.ph.preheader ]
  %20 = getelementptr inbounds i8, ptr %19, i64 8
  %21 = load i64, ptr %20, align 8, !tbaa !12
  %22 = icmp ult i64 %21, %.
  br i1 %22, label %.lr.ph, label %.critedge, !llvm.loop !13

.lr.ph:                                           ; preds = %.lr.ph77
  %23 = load ptr, ptr %19, align 8, !tbaa !9
  %.not51 = icmp eq ptr %23, null
  br i1 %.not51, label %.critedge, label %.lr.ph77, !llvm.loop !13

.critedge:                                        ; preds = %.lr.ph77, %.lr.ph, %.lr.ph.preheader, %13
  %.042.lcssa64 = phi ptr [ %14, %13 ], [ %14, %.lr.ph.preheader ], [ %19, %.lr.ph ], [ %19, %.lr.ph77 ]
  %.043.lcssa = phi ptr [ @xStart, %13 ], [ @xStart, %.lr.ph.preheader ], [ %.0426676, %.lr.ph ], [ %.0426676, %.lr.ph77 ]
  %.lcssa62 = phi i64 [ %16, %13 ], [ %16, %.lr.ph.preheader ], [ %21, %.lr.ph ], [ %21, %.lr.ph77 ]
  %24 = getelementptr inbounds i8, ptr %.042.lcssa64, i64 8
  %.not52 = icmp eq ptr %.042.lcssa64, @xEnd
  br i1 %.not52, label %.thread, label %25

25:                                               ; preds = %.critedge
  %26 = load ptr, ptr %.043.lcssa, align 8, !tbaa !9
  %27 = getelementptr inbounds i8, ptr %26, i64 16
  %28 = load ptr, ptr %.042.lcssa64, align 8, !tbaa !9
  store ptr %28, ptr %.043.lcssa, align 8, !tbaa !9
  %29 = sub i64 %.lcssa62, %.
  %30 = icmp ugt i64 %29, 32
  br i1 %30, label %31, label %._crit_edge

._crit_edge:                                      ; preds = %25
  %.pre = load i64, ptr %24, align 8, !tbaa !12
  br label %41

31:                                               ; preds = %25
  %32 = getelementptr inbounds i8, ptr %.042.lcssa64, i64 %.
  %33 = getelementptr inbounds i8, ptr %32, i64 8
  store i64 %29, ptr %33, align 8, !tbaa !12
  store i64 %., ptr %24, align 8, !tbaa !12
  %34 = load i64, ptr %33, align 8, !tbaa !12
  br label %35

35:                                               ; preds = %35, %31
  %.040 = phi ptr [ @xStart, %31 ], [ %36, %35 ]
  %36 = load ptr, ptr %.040, align 8, !tbaa !9
  %37 = getelementptr inbounds i8, ptr %36, i64 8
  %38 = load i64, ptr %37, align 8, !tbaa !12
  %39 = icmp ult i64 %38, %34
  br i1 %39, label %35, label %40, !llvm.loop !15

40:                                               ; preds = %35
  store ptr %36, ptr %32, align 8, !tbaa !9
  store ptr %32, ptr %.040, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %._crit_edge, %40
  %42 = phi i64 [ %.pre, %._crit_edge ], [ %., %40 ]
  %43 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  %44 = sub i64 %43, %42
  store i64 %44, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  %45 = tail call i64 @xTaskResumeAll() #2
  br label %47

.thread:                                          ; preds = %5, %.critedge, %7
  %46 = tail call i64 @xTaskResumeAll() #2
  tail call void @vApplicationMallocFailedHook() #2
  br label %47

47:                                               ; preds = %41, %.thread
  %.04160 = phi ptr [ null, %.thread ], [ %27, %41 ]
  ret ptr %.04160
}

declare void @vTaskSuspendAll() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @prvHeapInit() local_unnamed_addr #0

declare i64 @xTaskResumeAll() local_unnamed_addr #1

declare void @vApplicationMallocFailedHook() local_unnamed_addr #1

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


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