; LLVM IR from tensorflow/tflite-micro :: 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::BufferRequirements" = type { i32, i32, i32, i32 }

@.str = external hidden unnamed_addr constant [29 x i8], align 1

; 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 uwtable
define dso_local noundef i32 @_ZN6tflite19GreedyMemoryPlanner9AddBufferEiii(ptr noundef nonnull align 8 dereferenceable(65) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) unnamed_addr #1 align 2 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  %11 = load ptr, ptr %6, align 8
  %12 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %11, i32 0, i32 2
  %13 = load i32, ptr %12, align 4, !tbaa !11
  %14 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %11, i32 0, i32 1
  %15 = load i32, ptr %14, align 8, !tbaa !15
  %16 = icmp sge i32 %13, %15
  br i1 %16, label %17, label %20

17:                                               ; preds = %4
  %18 = getelementptr inbounds %"class.tflite::GreedyMemoryPlanner", ptr %11, i32 0, i32 1
  %19 = load i32, ptr %18, align 8, !tbaa !15
  call void (ptr, ...) @_Z11MicroPrintfPKcz(ptr noundef @.str, i32 noundef %19)
  store i32 1, ptr %5, align 4
  br label %42

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

42:                                               ; preds = %20, %17
  %43 = load i32, ptr %5, align 4
  ret i32 %43
}

declare void @_Z11MicroPrintfPKcz(ptr noundef, ...) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { "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 = !{!"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, !10, i64 12}
!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 8}
!16 = !{!12, !6, i64 16}
!17 = !{!18, !10, i64 0}
!18 = !{!"_ZTSN6tflite19GreedyMemoryPlanner18BufferRequirementsE", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12}
!19 = !{!18, !10, i64 8}
!20 = !{!18, !10, i64 12}
!21 = !{!18, !10, i64 4}
!22 = !{!12, !14, i64 64}


; 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::BufferRequirements" = type { i32, i32, i32, i32 }

@.str = external hidden unnamed_addr constant [29 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 0, 2) i32 @_ZN6tflite19GreedyMemoryPlanner9AddBufferEiii(ptr nocapture noundef nonnull align 8 dereferenceable(65) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) unnamed_addr #0 align 2 {
  %5 = getelementptr inbounds i8, ptr %0, i64 12
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load i32, ptr %7, align 8, !tbaa !13
  %.not = icmp slt i32 %6, %8
  br i1 %.not, label %10, label %9

9:                                                ; preds = %4
  tail call void (ptr, ...) @_Z11MicroPrintfPKcz(ptr noundef nonnull @.str, i32 noundef %8)
  br label %20

10:                                               ; preds = %4
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !14
  %13 = sext i32 %6 to i64
  %14 = getelementptr inbounds %"struct.tflite::GreedyMemoryPlanner::BufferRequirements", ptr %12, i64 %13
  store i32 %1, ptr %14, align 4, !tbaa !15
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  store i32 %2, ptr %15, align 4, !tbaa !17
  %16 = getelementptr inbounds i8, ptr %14, i64 12
  store i32 %3, ptr %16, align 4, !tbaa !18
  %17 = getelementptr inbounds i8, ptr %14, i64 4
  store i32 -1, ptr %17, align 4, !tbaa !19
  %18 = add nsw i32 %6, 1
  store i32 %18, ptr %5, align 4, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %0, i64 64
  store i8 1, ptr %19, align 8, !tbaa !20
  br label %20

20:                                               ; preds = %10, %9
  %.0 = phi i32 [ 1, %9 ], [ 0, %10 ]
  ret i32 %.0
}

declare void @_Z11MicroPrintfPKcz(ptr noundef, ...) local_unnamed_addr #1

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

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


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