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

; 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 [300000 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 %23

7:                                                ; preds = %1
  %8 = load i64, ptr %2, align 8, !tbaa !5
  %9 = load i64, ptr %2, align 8, !tbaa !5
  %10 = and i64 %9, 7
  %11 = sub i64 8, %10
  %12 = add i64 %8, %11
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = icmp ugt i64 %12, %13
  br i1 %14, label %15, label %21

15:                                               ; preds = %7
  %16 = load i64, ptr %2, align 8, !tbaa !5
  %17 = and i64 %16, 7
  %18 = sub i64 8, %17
  %19 = load i64, ptr %2, align 8, !tbaa !5
  %20 = add i64 %19, %18
  store i64 %20, ptr %2, align 8, !tbaa !5
  br label %22

21:                                               ; preds = %7
  store i64 0, ptr %2, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %21, %15
  br label %23

23:                                               ; preds = %22, %1
  call void @vTaskSuspendAll()
  %24 = load ptr, ptr @pvPortMalloc.pucAlignedHeap, align 8, !tbaa !9
  %25 = icmp eq ptr %24, null
  br i1 %25, label %26, label %30

26:                                               ; preds = %23
  %27 = and i32 ptrtoint (ptr getelementptr inbounds ([300000 x i8], ptr @ucHeap, i64 0, i64 8) to i32), -8
  %28 = zext i32 %27 to i64
  %29 = inttoptr i64 %28 to ptr
  store ptr %29, ptr @pvPortMalloc.pucAlignedHeap, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %26, %23
  %31 = load i64, ptr %2, align 8, !tbaa !5
  %32 = icmp ugt i64 %31, 0
  br i1 %32, label %33, label %51

33:                                               ; preds = %30
  %34 = load i64, ptr @xNextFreeByte, align 8, !tbaa !5
  %35 = load i64, ptr %2, align 8, !tbaa !5
  %36 = add i64 %34, %35
  %37 = icmp ult i64 %36, 299992
  br i1 %37, label %38, label %51

38:                                               ; preds = %33
  %39 = load i64, ptr @xNextFreeByte, align 8, !tbaa !5
  %40 = load i64, ptr %2, align 8, !tbaa !5
  %41 = add i64 %39, %40
  %42 = load i64, ptr @xNextFreeByte, align 8, !tbaa !5
  %43 = icmp ugt i64 %41, %42
  br i1 %43, label %44, label %51

44:                                               ; preds = %38
  %45 = load ptr, ptr @pvPortMalloc.pucAlignedHeap, align 8, !tbaa !9
  %46 = load i64, ptr @xNextFreeByte, align 8, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %45, i64 %46
  store ptr %47, ptr %3, align 8, !tbaa !9
  %48 = load i64, ptr %2, align 8, !tbaa !5
  %49 = load i64, ptr @xNextFreeByte, align 8, !tbaa !5
  %50 = add i64 %49, %48
  store i64 %50, ptr @xNextFreeByte, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %44, %38, %33, %30
  %52 = call i64 @xTaskResumeAll()
  %53 = load ptr, ptr %3, align 8, !tbaa !9
  %54 = icmp eq ptr %53, null
  br i1 %54, label %55, label %56

55:                                               ; preds = %51
  call void @vApplicationMallocFailedHook()
  br label %56

56:                                               ; preds = %55, %51
  %57 = load ptr, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret ptr %57
}

; 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

declare void @vApplicationMallocFailedHook() #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 [300000 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 {
  %2 = and i64 %0, 7
  %.not = icmp eq i64 %2, 0
  %reass.sub = and i64 %0, -8
  %3 = add i64 %reass.sub, 8
  %4 = icmp ugt i64 %3, %0
  %. = select i1 %4, i64 %3, i64 0
  %.011 = select i1 %.not, i64 %0, i64 %.
  tail call void @vTaskSuspendAll() #2
  %5 = load ptr, ptr @pvPortMalloc.pucAlignedHeap, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %10

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

10:                                               ; preds = %7, %1
  %11 = phi ptr [ %9, %7 ], [ %5, %1 ]
  %.not16 = icmp eq i64 %.011, 0
  br i1 %.not16, label %20, label %12

12:                                               ; preds = %10
  %13 = load i64, ptr @xNextFreeByte, align 8, !tbaa !9
  %14 = add i64 %13, %.011
  %15 = icmp ult i64 %14, 299992
  %16 = icmp ugt i64 %14, %13
  %or.cond = and i1 %15, %16
  br i1 %or.cond, label %17, label %20

17:                                               ; preds = %12
  %18 = getelementptr inbounds i8, ptr %11, i64 %13
  store i64 %14, ptr @xNextFreeByte, align 8, !tbaa !9
  %19 = tail call i64 @xTaskResumeAll() #2
  br label %22

20:                                               ; preds = %10, %12
  %21 = tail call i64 @xTaskResumeAll() #2
  tail call void @vApplicationMallocFailedHook() #2
  br label %22

22:                                               ; preds = %17, %20
  %.018 = phi ptr [ null, %20 ], [ %18, %17 ]
  ret ptr %.018
}

declare void @vTaskSuspendAll() local_unnamed_addr #1

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 = !{!"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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass