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

@pvPortMalloc.xHeapHasBeenInitialised = 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
@xFreeBytesRemaining = 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
  %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) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  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 %28

15:                                               ; preds = %12
  %16 = load i64, ptr %2, align 8, !tbaa !5
  %17 = add i64 %16, 16
  store i64 %17, ptr %2, align 8, !tbaa !5
  %18 = load i64, ptr %2, align 8, !tbaa !5
  %19 = and i64 %18, 7
  %20 = icmp ne i64 %19, 0
  br i1 %20, label %21, label %27

21:                                               ; preds = %15
  %22 = load i64, ptr %2, align 8, !tbaa !5
  %23 = and i64 %22, 7
  %24 = sub i64 8, %23
  %25 = load i64, ptr %2, align 8, !tbaa !5
  %26 = add i64 %25, %24
  store i64 %26, ptr %2, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %21, %15
  br label %28

28:                                               ; preds = %27, %12
  %29 = load i64, ptr %2, align 8, !tbaa !5
  %30 = icmp ugt i64 %29, 0
  br i1 %30, label %31, label %119

31:                                               ; preds = %28
  %32 = load i64, ptr %2, align 8, !tbaa !5
  %33 = icmp ult i64 %32, 15352
  br i1 %33, label %34, label %119

34:                                               ; preds = %31
  store ptr @xStart, ptr %4, align 8, !tbaa !9
  %35 = load ptr, ptr @xStart, align 8, !tbaa !11
  store ptr %35, ptr %3, align 8, !tbaa !9
  br label %36

36:                                               ; preds = %49, %34
  %37 = load ptr, ptr %3, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %37, i32 0, i32 1
  %39 = load i64, ptr %38, align 8, !tbaa !13
  %40 = load i64, ptr %2, align 8, !tbaa !5
  %41 = icmp ult i64 %39, %40
  br i1 %41, label %42, label %47

42:                                               ; preds = %36
  %43 = load ptr, ptr %3, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  %46 = icmp ne ptr %45, null
  br label %47

47:                                               ; preds = %42, %36
  %48 = phi i1 [ false, %36 ], [ %46, %42 ]
  br i1 %48, label %49, label %54

49:                                               ; preds = %47
  %50 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %50, ptr %4, align 8, !tbaa !9
  %51 = load ptr, ptr %3, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !11
  store ptr %53, ptr %3, align 8, !tbaa !9
  br label %36, !llvm.loop !14

54:                                               ; preds = %47
  %55 = load ptr, ptr %3, align 8, !tbaa !9
  %56 = icmp ne ptr %55, @xEnd
  br i1 %56, label %57, label %118

57:                                               ; preds = %54
  %58 = load ptr, ptr %4, 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
  %61 = getelementptr inbounds i8, ptr %60, i64 16
  store ptr %61, ptr %6, align 8, !tbaa !9
  %62 = load ptr, ptr %3, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !11
  %65 = load ptr, ptr %4, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %65, i32 0, i32 0
  store ptr %64, ptr %66, align 8, !tbaa !11
  %67 = load ptr, ptr %3, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %67, i32 0, i32 1
  %69 = load i64, ptr %68, align 8, !tbaa !13
  %70 = load i64, ptr %2, align 8, !tbaa !5
  %71 = sub i64 %69, %70
  %72 = icmp ugt i64 %71, 32
  br i1 %72, label %73, label %112

73:                                               ; preds = %57
  %74 = load ptr, ptr %3, align 8, !tbaa !9
  %75 = load i64, ptr %2, align 8, !tbaa !5
  %76 = getelementptr inbounds i8, ptr %74, i64 %75
  store ptr %76, ptr %5, align 8, !tbaa !9
  %77 = load ptr, ptr %3, align 8, !tbaa !9
  %78 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %77, i32 0, i32 1
  %79 = load i64, ptr %78, align 8, !tbaa !13
  %80 = load i64, ptr %2, align 8, !tbaa !5
  %81 = sub i64 %79, %80
  %82 = load ptr, ptr %5, align 8, !tbaa !9
  %83 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %82, i32 0, i32 1
  store i64 %81, ptr %83, align 8, !tbaa !13
  %84 = load i64, ptr %2, align 8, !tbaa !5
  %85 = load ptr, ptr %3, align 8, !tbaa !9
  %86 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %85, i32 0, i32 1
  store i64 %84, ptr %86, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %87 = load ptr, ptr %5, 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
  store i64 %89, ptr %8, align 8, !tbaa !5
  store ptr @xStart, ptr %7, align 8, !tbaa !9
  br label %90

90:                                               ; preds = %99, %73
  %91 = load ptr, ptr %7, align 8, !tbaa !9
  %92 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %91, i32 0, i32 0
  %93 = load ptr, ptr %92, align 8, !tbaa !11
  %94 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %93, i32 0, i32 1
  %95 = load i64, ptr %94, align 8, !tbaa !13
  %96 = load i64, ptr %8, align 8, !tbaa !5
  %97 = icmp ult i64 %95, %96
  br i1 %97, label %98, label %103

98:                                               ; preds = %90
  br label %99

99:                                               ; preds = %98
  %100 = load ptr, ptr %7, align 8, !tbaa !9
  %101 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %100, i32 0, i32 0
  %102 = load ptr, ptr %101, align 8, !tbaa !11
  store ptr %102, ptr %7, align 8, !tbaa !9
  br label %90, !llvm.loop !16

103:                                              ; preds = %90
  %104 = load ptr, ptr %7, align 8, !tbaa !9
  %105 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %104, i32 0, i32 0
  %106 = load ptr, ptr %105, align 8, !tbaa !11
  %107 = load ptr, ptr %5, align 8, !tbaa !9
  %108 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %107, i32 0, i32 0
  store ptr %106, ptr %108, align 8, !tbaa !11
  %109 = load ptr, ptr %5, align 8, !tbaa !9
  %110 = load ptr, ptr %7, align 8, !tbaa !9
  %111 = getelementptr inbounds %struct.A_BLOCK_LINK, ptr %110, i32 0, i32 0
  store ptr %109, ptr %111, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %112

112:                                              ; preds = %103, %57
  %113 = load ptr, ptr %3, align 8, !tbaa !9
  %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 @xFreeBytesRemaining, align 8, !tbaa !5
  %117 = sub i64 %116, %115
  store i64 %117, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  br label %118

118:                                              ; preds = %112, %54
  br label %119

119:                                              ; preds = %118, %31, %28
  %120 = call i64 @xTaskResumeAll()
  %121 = load ptr, ptr %6, align 8, !tbaa !9
  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 ptr %121
}

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare i64 @xTaskResumeAll() #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 = { 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
@xStart = external hidden local_unnamed_addr global %struct.A_BLOCK_LINK, align 8
@xEnd = external hidden global %struct.A_BLOCK_LINK, align 8
@xFreeBytesRemaining = 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 {
  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
  %.not = icmp eq i64 %0, 0
  br i1 %.not, label %.thread, label %6

6:                                                ; preds = %5
  %7 = add i64 %0, 16
  %8 = and i64 %0, 7
  %.not43 = icmp eq i64 %8, 0
  %9 = and i64 %0, -8
  %10 = add i64 %9, 24
  %.0 = select i1 %.not43, i64 %7, i64 %10
  %11 = add i64 %.0, -1
  %or.cond = icmp ult i64 %11, 15351
  br i1 %or.cond, label %12, label %.thread

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

.lr.ph.preheader:                                 ; preds = %12
  %17 = load ptr, ptr %13, align 8, !tbaa !9
  %.not4461 = icmp eq ptr %17, null
  br i1 %.not4461, label %.critedge, label %.lr.ph63, !llvm.loop !13

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

.lr.ph:                                           ; preds = %.lr.ph63
  %22 = load ptr, ptr %18, align 8, !tbaa !9
  %.not44 = icmp eq ptr %22, null
  br i1 %.not44, label %.critedge, label %.lr.ph63, !llvm.loop !13

.critedge:                                        ; preds = %.lr.ph63, %.lr.ph, %.lr.ph.preheader, %12
  %.037.lcssa50 = phi ptr [ %13, %12 ], [ %13, %.lr.ph.preheader ], [ %18, %.lr.ph ], [ %18, %.lr.ph63 ]
  %.038.lcssa = phi ptr [ @xStart, %12 ], [ @xStart, %.lr.ph.preheader ], [ %.0375262, %.lr.ph ], [ %.0375262, %.lr.ph63 ]
  %.lcssa48 = phi i64 [ %15, %12 ], [ %15, %.lr.ph.preheader ], [ %20, %.lr.ph ], [ %20, %.lr.ph63 ]
  %23 = getelementptr inbounds i8, ptr %.037.lcssa50, i64 8
  %.not45 = icmp eq ptr %.037.lcssa50, @xEnd
  br i1 %.not45, label %.thread, label %24

24:                                               ; preds = %.critedge
  %25 = load ptr, ptr %.038.lcssa, align 8, !tbaa !9
  %26 = getelementptr inbounds i8, ptr %25, i64 16
  %27 = load ptr, ptr %.037.lcssa50, align 8, !tbaa !9
  store ptr %27, ptr %.038.lcssa, align 8, !tbaa !9
  %28 = sub i64 %.lcssa48, %.0
  %29 = icmp ugt i64 %28, 32
  br i1 %29, label %30, label %._crit_edge

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

30:                                               ; preds = %24
  %31 = getelementptr inbounds i8, ptr %.037.lcssa50, i64 %.0
  %32 = getelementptr inbounds i8, ptr %31, i64 8
  store i64 %28, ptr %32, align 8, !tbaa !12
  store i64 %.0, ptr %23, align 8, !tbaa !12
  %33 = load i64, ptr %32, align 8, !tbaa !12
  br label %34

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

39:                                               ; preds = %34
  store ptr %35, ptr %31, align 8, !tbaa !9
  store ptr %31, ptr %.035, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %._crit_edge, %39
  %41 = phi i64 [ %.pre, %._crit_edge ], [ %.0, %39 ]
  %42 = load i64, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  %43 = sub i64 %42, %41
  store i64 %43, ptr @xFreeBytesRemaining, align 8, !tbaa !5
  br label %.thread

.thread:                                          ; preds = %5, %.critedge, %40, %6
  %.036 = phi ptr [ %26, %40 ], [ null, %.critedge ], [ null, %6 ], [ null, %5 ]
  %44 = tail call i64 @xTaskResumeAll() #2
  ret ptr %.036
}

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

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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass