; 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 }
%"struct.tflite::GreedyMemoryPlanner::BufferRequirements" = type { i32, 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 void @_ZN6tflite19GreedyMemoryPlanner24CalculateOffsetsIfNeededEv(ptr noundef nonnull align 8 dereferenceable(65)) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_ZN6tflite19GreedyMemoryPlanner20GetMaximumMemorySizeEv(ptr noundef nonnull align 8 dereferenceable(65) %0) unnamed_addr #1 align 2 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8
  call void @_ZN6tflite19GreedyMemoryPlanner24CalculateOffsetsIfNeededEv(ptr noundef nonnull align 8 dereferenceable(65) %9)
  %10 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %9, i32 0, i32 2
  %11 = load i32, ptr %10, align 4, !tbaa !9
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %1
  store i64 0, ptr %2, align 8
  br label %64

14:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %15 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %9, i32 0, i32 6
  %16 = load ptr, ptr %15, align 8, !tbaa !14
  %17 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %9, i32 0, i32 8
  %18 = load i32, ptr %17, align 4, !tbaa !15
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %16, i64 %19
  store ptr %20, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !16
  br label %21

21:                                               ; preds = %61, %14
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %62

24:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %25 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %9, i32 0, i32 3
  %26 = load ptr, ptr %25, align 8, !tbaa !18
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %27, i32 0, i32 1
  %29 = load i32, ptr %28, align 4, !tbaa !19
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::BufferRequirements", ptr %26, i64 %30
  store ptr %31, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 4, !tbaa !21
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::BufferRequirements", ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 4, !tbaa !22
  %38 = add nsw i32 %34, %37
  %39 = sext i32 %38 to i64
  store i64 %39, ptr %7, align 8, !tbaa !16
  %40 = load i64, ptr %7, align 8, !tbaa !16
  %41 = load i64, ptr %5, align 8, !tbaa !16
  %42 = icmp ugt i64 %40, %41
  br i1 %42, label %43, label %45

43:                                               ; preds = %24
  %44 = load i64, ptr %7, align 8, !tbaa !16
  store i64 %44, ptr %5, align 8, !tbaa !16
  br label %45

45:                                               ; preds = %43, %24
  %46 = load ptr, ptr %4, 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 !24
  %49 = icmp eq i32 %48, -1
  br i1 %49, label %50, label %51

50:                                               ; preds = %45
  store i32 3, ptr %8, align 4
  br label %59

51:                                               ; preds = %45
  %52 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %9, i32 0, i32 6
  %53 = load ptr, ptr %52, align 8, !tbaa !14
  %54 = load ptr, ptr %4, 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 !24
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %53, i64 %57
  store ptr %58, ptr %4, align 8, !tbaa !5
  store i32 0, ptr %8, align 4
  br label %59

59:                                               ; preds = %51, %50
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %60 = load i32, ptr %8, align 4
  switch i32 %60, label %66 [
    i32 0, label %61
    i32 3, label %62
  ]

61:                                               ; preds = %59
  br label %21, !llvm.loop !25

62:                                               ; preds = %59, %21
  %63 = load i64, ptr %5, align 8, !tbaa !16
  store i64 %63, ptr %2, align 8
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %64

64:                                               ; preds = %62, %13
  %65 = load i64, ptr %2, align 8
  ret i64 %65

66:                                               ; preds = %59
  unreachable
}

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, !12, i64 12}
!10 = !{!"_ZTSN6tflite19GreedyMemoryPlannerE", !11, i64 0, !12, i64 8, !12, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !12, i64 48, !12, i64 52, !6, i64 56, !13, i64 64}
!11 = !{!"_ZTSN6tflite18MicroMemoryPlannerE"}
!12 = !{!"int", !7, i64 0}
!13 = !{!"bool", !7, i64 0}
!14 = !{!10, !6, i64 40}
!15 = !{!10, !12, i64 52}
!16 = !{!17, !17, i64 0}
!17 = !{!"long", !7, i64 0}
!18 = !{!10, !6, i64 16}
!19 = !{!20, !12, i64 4}
!20 = !{!"_ZTSN6tflite19GreedyMemoryPlanner9ListEntryE", !12, i64 0, !12, i64 4, !12, i64 8}
!21 = !{!20, !12, i64 0}
!22 = !{!23, !12, i64 0}
!23 = !{!"_ZTSN6tflite19GreedyMemoryPlanner18BufferRequirementsE", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12}
!24 = !{!20, !12, i64 8}
!25 = distinct !{!25, !26}
!26 = !{!"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 }
%"struct.tflite::GreedyMemoryPlanner::BufferRequirements" = type { i32, i32, i32, i32 }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6tflite19GreedyMemoryPlanner24CalculateOffsetsIfNeededEv(ptr noundef nonnull align 8 dereferenceable(65)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef range(i64 -2147483648, 2147483648) i64 @_ZN6tflite19GreedyMemoryPlanner20GetMaximumMemorySizeEv(ptr noundef nonnull align 8 dereferenceable(65) %0) unnamed_addr #0 align 2 {
  tail call void @_ZN6tflite19GreedyMemoryPlanner24CalculateOffsetsIfNeededEv(ptr noundef nonnull align 8 dereferenceable(65) %0)
  %2 = getelementptr inbounds i8, ptr %0, i64 12
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %.loopexit, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 40
  %7 = load ptr, ptr %6, align 8
  %.not = icmp eq ptr %7, null
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load ptr, ptr %8, align 8
  br i1 %.not, label %.loopexit, label %.split.preheader

.split.preheader:                                 ; preds = %5
  %10 = getelementptr inbounds i8, ptr %0, i64 52
  %11 = load i32, ptr %10, align 4, !tbaa !13
  br label %.split

.split:                                           ; preds = %.split.preheader, %.split
  %.014 = phi i64 [ %spec.select, %.split ], [ 0, %.split.preheader ]
  %.pn.in = phi i32 [ %21, %.split ], [ %11, %.split.preheader ]
  %.pn = sext i32 %.pn.in to i64
  %.013 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::ListEntry", ptr %7, i64 %.pn
  %12 = getelementptr inbounds i8, ptr %.013, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !14
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::BufferRequirements", ptr %9, i64 %14
  %16 = load i32, ptr %.013, align 4, !tbaa !16
  %17 = load i32, ptr %15, align 4, !tbaa !17
  %18 = add nsw i32 %17, %16
  %19 = sext i32 %18 to i64
  %spec.select = tail call i64 @llvm.umax.i64(i64 %.014, i64 %19)
  %20 = getelementptr inbounds i8, ptr %.013, i64 8
  %21 = load i32, ptr %20, align 4, !tbaa !19
  %22 = icmp eq i32 %21, -1
  br i1 %22, label %.loopexit, label %.split

.loopexit:                                        ; preds = %.split, %5, %1
  %.012 = phi i64 [ 0, %1 ], [ 0, %5 ], [ %spec.select, %.split ]
  ret i64 %.012
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 12}
!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 = !{!6, !8, i64 52}
!14 = !{!15, !8, i64 4}
!15 = !{!"_ZTSN6tflite19GreedyMemoryPlanner9ListEntryE", !8, i64 0, !8, i64 4, !8, i64 8}
!16 = !{!15, !8, i64 0}
!17 = !{!18, !8, i64 0}
!18 = !{!"_ZTSN6tflite19GreedyMemoryPlanner18BufferRequirementsE", !8, i64 0, !8, i64 4, !8, i64 8, !8, i64 12}
!19 = !{!15, !8, i64 8}


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