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

@pvPortMalloc.pucAlignedHeap = external hidden global ptr, align 8
@ucHeap = external hidden global [15360 x i8], align 16
@xNextFreeByte = 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
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store ptr null, ptr %3, align 8, !tbaa !9
  %4 = load i64, ptr %2, align 8, !tbaa !5
  %5 = and i64 %4, 7
  %6 = icmp ne i64 %5, 0
  br i1 %6, label %7, label %13

7:                                                ; preds = %1
  %8 = load i64, ptr %2, align 8, !tbaa !5
  %9 = and i64 %8, 7
  %10 = sub i64 8, %9
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = add i64 %11, %10
  store i64 %12, ptr %2, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %7, %1
  call void @vTaskSuspendAll()
  %14 = load ptr, ptr @pvPortMalloc.pucAlignedHeap, align 8, !tbaa !9
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %20

16:                                               ; preds = %13
  %17 = and i32 ptrtoint (ptr getelementptr inbounds ([15360 x i8], ptr @ucHeap, i64 0, i64 8) to i32), -8
  %18 = zext i32 %17 to i64
  %19 = inttoptr i64 %18 to ptr
  store ptr %19, ptr @pvPortMalloc.pucAlignedHeap, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %16, %13
  %21 = load i64, ptr @xNextFreeByte, align 8, !tbaa !5
  %22 = load i64, ptr %2, align 8, !tbaa !5
  %23 = add i64 %21, %22
  %24 = icmp ult i64 %23, 15352
  br i1 %24, label %25, label %38

25:                                               ; preds = %20
  %26 = load i64, ptr @xNextFreeByte, align 8, !tbaa !5
  %27 = load i64, ptr %2, align 8, !tbaa !5
  %28 = add i64 %26, %27
  %29 = load i64, ptr @xNextFreeByte, align 8, !tbaa !5
  %30 = icmp ugt i64 %28, %29
  br i1 %30, label %31, label %38

31:                                               ; preds = %25
  %32 = load ptr, ptr @pvPortMalloc.pucAlignedHeap, align 8, !tbaa !9
  %33 = load i64, ptr @xNextFreeByte, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %32, i64 %33
  store ptr %34, ptr %3, align 8, !tbaa !9
  %35 = load i64, ptr %2, align 8, !tbaa !5
  %36 = load i64, ptr @xNextFreeByte, align 8, !tbaa !5
  %37 = add i64 %36, %35
  store i64 %37, ptr @xNextFreeByte, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %31, %25, %20
  %39 = call i64 @xTaskResumeAll()
  %40 = load ptr, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret ptr %40
}

; 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

declare i64 @xTaskResumeAll() #2

; 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 = { "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}


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

@pvPortMalloc.pucAlignedHeap = external hidden local_unnamed_addr global ptr, align 8
@ucHeap = external hidden global [15360 x i8], align 16
@xNextFreeByte = 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 {
  %.biased = add i64 %0, 7
  %.06 = and i64 %.biased, -8
  tail call void @vTaskSuspendAll() #2
  %2 = load ptr, ptr @pvPortMalloc.pucAlignedHeap, align 8, !tbaa !5
  %3 = icmp eq ptr %2, null
  br i1 %3, label %4, label %7

4:                                                ; preds = %1
  %5 = zext i32 ptrtoint (ptr getelementptr inbounds (i8, ptr @ucHeap, i64 8) to i32) to i64
  %6 = inttoptr i64 %5 to ptr
  store ptr %6, ptr @pvPortMalloc.pucAlignedHeap, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %4, %1
  %8 = phi ptr [ %6, %4 ], [ %2, %1 ]
  %9 = load i64, ptr @xNextFreeByte, align 8, !tbaa !9
  %10 = add i64 %9, %.06
  %11 = icmp ult i64 %10, 15352
  %12 = icmp ugt i64 %10, %9
  %or.cond = and i1 %11, %12
  br i1 %or.cond, label %13, label %15

13:                                               ; preds = %7
  %14 = getelementptr inbounds i8, ptr %8, i64 %9
  store i64 %10, ptr @xNextFreeByte, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %13, %7
  %.0 = phi ptr [ %14, %13 ], [ null, %7 ]
  %16 = tail call i64 @xTaskResumeAll() #2
  ret ptr %.0
}

declare void @vTaskSuspendAll() local_unnamed_addr #1

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 = !{!"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}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass