; LLVM IR from ubaldot/defrecs :: stm32f4x/ADC_Readings/Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang (pvPortMalloc)

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

@pxEnd = external hidden global ptr, align 8
@xBlockAllocatedBit = external hidden global i64, 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

; 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
  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
  %7 = load ptr, ptr @pxEnd, align 8, !tbaa !9
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %11

9:                                                ; preds = %1
  call void @vPortRaiseBASEPRI()
  br label %10

10:                                               ; preds = %10, %9
  br label %10

11:                                               ; preds = %1
  call void @vTaskSuspendAll()
  %12 = load i64, ptr %2, align 8, !tbaa !5
  %13 = load i64, ptr @xBlockAllocatedBit, align 8, !tbaa !5
  %14 = and i64 %12, %13
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %16, label %123

16:                                               ; preds = %11
  %17 = load i64, ptr %2, align 8, !tbaa !5
  %18 = icmp ugt i64 %17, 0
  br i1 %18, label %19, label %33

19:                                               ; preds = %16
  %20 = load i64, ptr %2, align 8, !tbaa !5
  %21 = add i64 %20, 16
  store i64 %21, ptr %2, align 8, !tbaa !5
  %22 = load i64, ptr %2, align 8, !tbaa !5
  %23 = and i64 %22, 7
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %31

25:                                               ; preds = %19
  %26 = load i64, ptr %2, align 8, !tbaa !5
  %27 = and i64 %26, 7
  %28 = sub i64 8, %27
  %29 = load i64, ptr %2, align 8, !tbaa !5
  %30 = add i64 %29, %28
  store i64 %30, ptr %2, align 8, !tbaa !5
  br label %32

31:                                               ; preds = %19
  br label %32

32:                                               ; preds = %31, %25
  br label %34

33:                                               ; preds = %16
  br label %34

34:                                               ; preds = %33, %32
  %35 = load i64, ptr %2, align 8, !tbaa !5
  %36 = icmp ugt i64 %35, 0
  br i1 %36, label %37, label %121

37:                                               ; preds = %34
  %38 = load i64, ptr %2, align 8, !tbaa !5
  %39 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  %40 = icmp ule i64 %38, %39
  br i1 %40, label %41, label %121

41:                                               ; preds = %37
  store ptr @xStart, ptr %4, align 8, !tbaa !9
  %42 = load ptr, ptr @xStart, align 8, !tbaa !11
  store ptr %42, ptr %3, align 8, !tbaa !9
  br label %43

43:                                               ; preds = %56, %41
  %44 = load ptr, ptr %3, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %44, i32 0, i32 1
  %46 = load i64, ptr %45, align 8, !tbaa !13
  %47 = load i64, ptr %2, align 8, !tbaa !5
  %48 = icmp ult i64 %46, %47
  br i1 %48, label %49, label %54

49:                                               ; preds = %43
  %50 = load ptr, ptr %3, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !11
  %53 = icmp ne ptr %52, null
  br label %54

54:                                               ; preds = %49, %43
  %55 = phi i1 [ false, %43 ], [ %53, %49 ]
  br i1 %55, label %56, label %61

56:                                               ; preds = %54
  %57 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %57, ptr %4, align 8, !tbaa !9
  %58 = load ptr, ptr %3, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %58, i32 0, i32 0
  %60 = load ptr, ptr %59, align 8, !tbaa !11
  store ptr %60, ptr %3, align 8, !tbaa !9
  br label %43, !llvm.loop !14

61:                                               ; preds = %54
  %62 = load ptr, ptr %3, align 8, !tbaa !9
  %63 = load ptr, ptr @pxEnd, align 8, !tbaa !9
  %64 = icmp ne ptr %62, %63
  br i1 %64, label %65, label %119

65:                                               ; preds = %61
  %66 = load ptr, ptr %4, align 8, !tbaa !9
  %67 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %66, i32 0, i32 0
  %68 = load ptr, ptr %67, align 8, !tbaa !11
  %69 = getelementptr inbounds i8, ptr %68, i64 16
  store ptr %69, ptr %6, align 8, !tbaa !9
  %70 = load ptr, ptr %3, align 8, !tbaa !9
  %71 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !11
  %73 = load ptr, ptr %4, align 8, !tbaa !9
  %74 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %73, i32 0, i32 0
  store ptr %72, ptr %74, align 8, !tbaa !11
  %75 = load ptr, ptr %3, align 8, !tbaa !9
  %76 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %75, i32 0, i32 1
  %77 = load i64, ptr %76, align 8, !tbaa !13
  %78 = load i64, ptr %2, align 8, !tbaa !5
  %79 = sub i64 %77, %78
  %80 = icmp ugt i64 %79, 32
  br i1 %80, label %81, label %96

81:                                               ; preds = %65
  %82 = load ptr, ptr %3, align 8, !tbaa !9
  %83 = load i64, ptr %2, align 8, !tbaa !5
  %84 = getelementptr inbounds i8, ptr %82, i64 %83
  store ptr %84, ptr %5, align 8, !tbaa !9
  %85 = load ptr, ptr %3, align 8, !tbaa !9
  %86 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %85, i32 0, i32 1
  %87 = load i64, ptr %86, align 8, !tbaa !13
  %88 = load i64, ptr %2, align 8, !tbaa !5
  %89 = sub i64 %87, %88
  %90 = load ptr, ptr %5, align 8, !tbaa !9
  %91 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %90, i32 0, i32 1
  store i64 %89, ptr %91, align 8, !tbaa !13
  %92 = load i64, ptr %2, align 8, !tbaa !5
  %93 = load ptr, ptr %3, align 8, !tbaa !9
  %94 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %93, i32 0, i32 1
  store i64 %92, ptr %94, align 8, !tbaa !13
  %95 = load ptr, ptr %5, align 8, !tbaa !9
  call void @prvInsertBlockIntoFreeList(ptr noundef %95)
  br label %97

96:                                               ; preds = %65
  br label %97

97:                                               ; preds = %96, %81
  %98 = load ptr, ptr %3, align 8, !tbaa !9
  %99 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %98, i32 0, i32 1
  %100 = load i64, ptr %99, align 8, !tbaa !13
  %101 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  %102 = sub i64 %101, %100
  store i64 %102, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  %103 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  %104 = load i64, ptr @xMinimumEverFreeBytesRemaining, align 8, !tbaa !5
  %105 = icmp ult i64 %103, %104
  br i1 %105, label %106, label %108

106:                                              ; preds = %97
  %107 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  store i64 %107, ptr @xMinimumEverFreeBytesRemaining, align 8, !tbaa !5
  br label %109

108:                                              ; preds = %97
  br label %109

109:                                              ; preds = %108, %106
  %110 = load i64, ptr @xBlockAllocatedBit, align 8, !tbaa !5
  %111 = load ptr, ptr %3, align 8, !tbaa !9
  %112 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %111, i32 0, i32 1
  %113 = load i64, ptr %112, align 8, !tbaa !13
  %114 = or i64 %113, %110
  store i64 %114, ptr %112, align 8, !tbaa !13
  %115 = load ptr, ptr %3, align 8, !tbaa !9
  %116 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %115, i32 0, i32 0
  store ptr null, ptr %116, align 8, !tbaa !11
  %117 = load i64, ptr @xNumberOfSuccessfulAllocations, align 8, !tbaa !5
  %118 = add i64 %117, 1
  store i64 %118, ptr @xNumberOfSuccessfulAllocations, align 8, !tbaa !5
  br label %120

119:                                              ; preds = %61
  br label %120

120:                                              ; preds = %119, %109
  br label %122

121:                                              ; preds = %37, %34
  br label %122

122:                                              ; preds = %121, %120
  br label %124

123:                                              ; preds = %11
  br label %124

124:                                              ; preds = %123, %122
  %125 = call i64 @xTaskResumeAll()
  %126 = 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 %126
}

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

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

declare void @vTaskSuspendAll() #3

; Function Attrs: nounwind uwtable
declare hidden void @prvInsertBlockIntoFreeList(ptr noundef) #0

declare i64 @xTaskResumeAll() #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { "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"}


; 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
@xBlockAllocatedBit = 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
@xMinimumEverFreeBytesRemaining = external hidden local_unnamed_addr global i64, align 8
@xNumberOfSuccessfulAllocations = external hidden local_unnamed_addr global i64, align 8

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

4:                                                ; preds = %1
  tail call void @vPortRaiseBASEPRI()
  br label %5

5:                                                ; preds = %5, %4
  br label %5

6:                                                ; preds = %1
  tail call void @vTaskSuspendAll() #3
  %7 = load i64, ptr @xBlockAllocatedBit, align 8, !tbaa !9
  %8 = and i64 %7, %0
  %9 = icmp ne i64 %8, 0
  %.not = icmp eq i64 %0, 0
  %or.cond47 = or i1 %.not, %9
  br i1 %or.cond47, label %.thread, label %10

10:                                               ; preds = %6
  %11 = add i64 %0, 16
  %12 = and i64 %0, 7
  %.not38 = icmp eq i64 %12, 0
  %13 = and i64 %0, -8
  %14 = add i64 %13, 24
  %.030 = select i1 %.not38, i64 %11, i64 %14
  %15 = load i64, ptr @xFreeBytesRemaining, align 8
  %16 = freeze i64 %15
  %17 = add i64 %.030, -1
  %or.cond.not = icmp ult i64 %17, %16
  br i1 %or.cond.not, label %18, label %.thread

18:                                               ; preds = %10
  %19 = load ptr, ptr @xStart, align 8, !tbaa !11
  %20 = getelementptr inbounds i8, ptr %19, i64 8
  %21 = load i64, ptr %20, align 8, !tbaa !13
  %22 = icmp ult i64 %21, %.030
  br i1 %22, label %.lr.ph.preheader, label %.critedge

.lr.ph.preheader:                                 ; preds = %18
  %23 = load ptr, ptr %19, align 8, !tbaa !11
  %.not4160 = icmp eq ptr %23, null
  br i1 %.not4160, label %.critedge, label %.lr.ph62, !llvm.loop !14

.lr.ph62:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %24 = phi ptr [ %28, %.lr.ph ], [ %23, %.lr.ph.preheader ]
  %.0325061 = phi ptr [ %24, %.lr.ph ], [ %19, %.lr.ph.preheader ]
  %25 = getelementptr inbounds i8, ptr %24, i64 8
  %26 = load i64, ptr %25, align 8, !tbaa !13
  %27 = icmp ult i64 %26, %.030
  br i1 %27, label %.lr.ph, label %.critedge, !llvm.loop !14

.lr.ph:                                           ; preds = %.lr.ph62
  %28 = load ptr, ptr %24, align 8, !tbaa !11
  %.not41 = icmp eq ptr %28, null
  br i1 %.not41, label %.critedge, label %.lr.ph62, !llvm.loop !14

.critedge:                                        ; preds = %.lr.ph62, %.lr.ph, %.lr.ph.preheader, %18
  %.032.lcssa49 = phi ptr [ %19, %18 ], [ %19, %.lr.ph.preheader ], [ %24, %.lr.ph ], [ %24, %.lr.ph62 ]
  %.031.lcssa = phi ptr [ @xStart, %18 ], [ @xStart, %.lr.ph.preheader ], [ %.0325061, %.lr.ph ], [ %.0325061, %.lr.ph62 ]
  %.lcssa = phi i64 [ %21, %18 ], [ %21, %.lr.ph.preheader ], [ %26, %.lr.ph ], [ %26, %.lr.ph62 ]
  %29 = getelementptr inbounds i8, ptr %.032.lcssa49, i64 8
  %30 = load ptr, ptr @pxEnd, align 8, !tbaa !5
  %.not42 = icmp eq ptr %.032.lcssa49, %30
  br i1 %.not42, label %.thread, label %31

31:                                               ; preds = %.critedge
  %32 = load ptr, ptr %.031.lcssa, align 8, !tbaa !11
  %33 = getelementptr inbounds i8, ptr %32, i64 16
  %34 = load ptr, ptr %.032.lcssa49, align 8, !tbaa !11
  store ptr %34, ptr %.031.lcssa, align 8, !tbaa !11
  %35 = sub i64 %.lcssa, %.030
  %36 = icmp ugt i64 %35, 32
  br i1 %36, label %37, label %40

37:                                               ; preds = %31
  %38 = getelementptr inbounds i8, ptr %.032.lcssa49, i64 %.030
  %39 = getelementptr inbounds i8, ptr %38, i64 8
  store i64 %35, ptr %39, align 8, !tbaa !13
  store i64 %.030, ptr %29, align 8, !tbaa !13
  tail call void @prvInsertBlockIntoFreeList(ptr noundef nonnull %38)
  br label %40

40:                                               ; preds = %31, %37
  %41 = load i64, ptr %29, align 8, !tbaa !13
  %42 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !9
  %43 = sub i64 %42, %41
  store i64 %43, ptr @xFreeBytesRemaining, align 8, !tbaa !9
  %44 = load i64, ptr @xMinimumEverFreeBytesRemaining, align 8, !tbaa !9
  %45 = icmp ult i64 %43, %44
  br i1 %45, label %46, label %47

46:                                               ; preds = %40
  store i64 %43, ptr @xMinimumEverFreeBytesRemaining, align 8, !tbaa !9
  br label %47

47:                                               ; preds = %40, %46
  %48 = load i64, ptr @xBlockAllocatedBit, align 8, !tbaa !9
  %49 = or i64 %41, %48
  store i64 %49, ptr %29, align 8, !tbaa !13
  store ptr null, ptr %.032.lcssa49, align 8, !tbaa !11
  %50 = load i64, ptr @xNumberOfSuccessfulAllocations, align 8, !tbaa !9
  %51 = add i64 %50, 1
  store i64 %51, ptr @xNumberOfSuccessfulAllocations, align 8, !tbaa !9
  br label %.thread

.thread:                                          ; preds = %6, %.critedge, %47, %10
  %.0 = phi ptr [ %33, %47 ], [ null, %.critedge ], [ null, %10 ], [ null, %6 ]
  %52 = tail call i64 @xTaskResumeAll() #3
  ret ptr %.0
}

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

declare void @vTaskSuspendAll() local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @prvInsertBlockIntoFreeList(ptr noundef) local_unnamed_addr #0

declare i64 @xTaskResumeAll() local_unnamed_addr #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 = { 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 #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 = { 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"}


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