; LLVM IR from tanakamasayuki/Arduino_TensorFlowLite_ESP32 :: src/tensorflow/lite/micro/memory_planner (greedy_memory_planner)

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

%"class.tflite::GreedyMemoryPlanner" = type <{ %"class.tflite::MicroMemoryPlanner", i32, i32, ptr, ptr, ptr, ptr, i32, i32, ptr, i8, [7 x i8] }>
%"class.tflite::MicroMemoryPlanner" = type { ptr }
%"struct.tflite::GreedyMemoryPlanner::ListEntry" = type { i32, i32, i32 }

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK6tflite19GreedyMemoryPlanner22DoesEntryOverlapInTimeEPKNS0_9ListEntryEii(ptr noundef nonnull align 8 dereferenceable(65), ptr noundef, i32 noundef, i32 noundef) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN6tflite19GreedyMemoryPlanner30NextSimultaneouslyActiveBufferEPKNS0_9ListEntryEii(ptr noundef nonnull align 8 dereferenceable(65) %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #1 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  %13 = load ptr, ptr %6, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store ptr null, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %23

16:                                               ; preds = %4
  %17 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %13, i32 0, i32 6
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %13, i32 0, i32 8
  %20 = load i32, ptr %19, align 4, !tbaa !15
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %18, i64 %21
  store ptr %22, ptr %11, align 8, !tbaa !5
  br label %37

23:                                               ; preds = %4
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %24, i32 0, i32 2
  %26 = load i32, ptr %25, align 4, !tbaa !16
  %27 = icmp eq i32 %26, -1
  br i1 %27, label %28, label %29

28:                                               ; preds = %23
  store ptr null, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %62

29:                                               ; preds = %23
  %30 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %13, i32 0, i32 6
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %32, i32 0, i32 2
  %34 = load i32, ptr %33, align 4, !tbaa !16
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %31, i64 %35
  store ptr %36, ptr %11, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %29, %16
  br label %38

38:                                               ; preds = %59, %37
  %39 = load ptr, ptr %11, align 8, !tbaa !5
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = call noundef zeroext i1 @_ZNK6tflite19GreedyMemoryPlanner22DoesEntryOverlapInTimeEPKNS0_9ListEntryEii(ptr noundef nonnull align 8 dereferenceable(65) %13, ptr noundef %39, i32 noundef %40, i32 noundef %41)
  br i1 %42, label %43, label %45

43:                                               ; preds = %38
  %44 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %44, ptr %10, align 8, !tbaa !5
  br label %60

45:                                               ; preds = %38
  %46 = load ptr, ptr %11, align 8, !tbaa !5
  %47 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %46, i32 0, i32 2
  %48 = load i32, ptr %47, align 4, !tbaa !16
  %49 = icmp eq i32 %48, -1
  br i1 %49, label %50, label %51

50:                                               ; preds = %45
  br label %60

51:                                               ; preds = %45
  %52 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %13, i32 0, i32 6
  %53 = load ptr, ptr %52, align 8, !tbaa !11
  %54 = load ptr, ptr %11, align 8, !tbaa !5
  %55 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %54, i32 0, i32 2
  %56 = load i32, ptr %55, align 4, !tbaa !16
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %53, i64 %57
  store ptr %58, ptr %11, align 8, !tbaa !5
  br label %59

59:                                               ; preds = %51
  br i1 true, label %38, label %60, !llvm.loop !18

60:                                               ; preds = %59, %50, %43
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %61, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %62

62:                                               ; preds = %60, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %63 = load ptr, ptr %5, align 8
  ret ptr %63
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 40}
!12 = !{!"_ZTSN6tflite19GreedyMemoryPlannerE", !13, i64 0, !10, i64 8, !10, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !10, i64 48, !10, i64 52, !6, i64 56, !14, i64 64}
!13 = !{!"_ZTSN6tflite18MicroMemoryPlannerE"}
!14 = !{!"bool", !7, i64 0}
!15 = !{!12, !10, i64 52}
!16 = !{!17, !10, i64 8}
!17 = !{!"_ZTSN6tflite19GreedyMemoryPlanner9ListEntryE", !10, i64 0, !10, i64 4, !10, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"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.tflite::GreedyMemoryPlanner::ListEntry" = type { i32, i32, i32 }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK6tflite19GreedyMemoryPlanner22DoesEntryOverlapInTimeEPKNS0_9ListEntryEii(ptr noundef nonnull align 8 dereferenceable(65), ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN6tflite19GreedyMemoryPlanner30NextSimultaneouslyActiveBufferEPKNS0_9ListEntryEii(ptr noundef nonnull align 8 dereferenceable(65) %0, ptr noundef readonly %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 align 2 {
  %5 = icmp eq ptr %1, null
  br i1 %5, label %6, label %9

6:                                                ; preds = %4
  %7 = getelementptr inbounds i8, ptr %0, i64 52
  %8 = load i32, ptr %7, align 4, !tbaa !5
  br label %13

9:                                                ; preds = %4
  %10 = getelementptr inbounds i8, ptr %1, i64 8
  %11 = load i32, ptr %10, align 4, !tbaa !13
  %12 = icmp eq i32 %11, -1
  br i1 %12, label %.loopexit, label %13

13:                                               ; preds = %9, %6
  %.sink20 = phi i32 [ %8, %6 ], [ %11, %9 ]
  %.sink.in = getelementptr inbounds i8, ptr %0, i64 40
  %.sink = load ptr, ptr %.sink.in, align 8, !tbaa !15
  %14 = sext i32 %.sink20 to i64
  %15 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %.sink, i64 %14
  %16 = tail call noundef zeroext i1 @_ZNK6tflite19GreedyMemoryPlanner22DoesEntryOverlapInTimeEPKNS0_9ListEntryEii(ptr noundef nonnull align 8 dereferenceable(65) %0, ptr noundef %15, i32 noundef %2, i32 noundef %3)
  br i1 %16, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %13
  %17 = getelementptr inbounds i8, ptr %0, i64 40
  br label %18

18:                                               ; preds = %.lr.ph, %22
  %.116 = phi ptr [ %15, %.lr.ph ], [ %25, %22 ]
  %19 = getelementptr inbounds i8, ptr %.116, i64 8
  %20 = load i32, ptr %19, align 4, !tbaa !13
  %21 = icmp eq i32 %20, -1
  br i1 %21, label %.loopexit, label %22

22:                                               ; preds = %18
  %23 = load ptr, ptr %17, align 8, !tbaa !15
  %24 = sext i32 %20 to i64
  %25 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %23, i64 %24
  %26 = tail call noundef zeroext i1 @_ZNK6tflite19GreedyMemoryPlanner22DoesEntryOverlapInTimeEPKNS0_9ListEntryEii(ptr noundef nonnull align 8 dereferenceable(65) %0, ptr noundef %25, i32 noundef %2, i32 noundef %3)
  br i1 %26, label %.loopexit, label %18, !llvm.loop !16

.loopexit:                                        ; preds = %22, %18, %13, %9
  %.012 = phi ptr [ null, %9 ], [ %15, %13 ], [ %25, %22 ], [ null, %18 ]
  ret ptr %.012
}

attributes #0 = { mustprogress 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" }

!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, !8, i64 52}
!6 = !{!"_ZTSN6tflite19GreedyMemoryPlannerE", !7, i64 0, !8, i64 8, !8, i64 12, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !8, i64 48, !8, i64 52, !11, i64 56, !12, i64 64}
!7 = !{!"_ZTSN6tflite18MicroMemoryPlannerE"}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!"bool", !9, i64 0}
!13 = !{!14, !8, i64 8}
!14 = !{!"_ZTSN6tflite19GreedyMemoryPlanner9ListEntryE", !8, i64 0, !8, i64 4, !8, i64 8}
!15 = !{!6, !11, i64 40}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass