; LLVM IR from tanakamasayuki/Arduino_TensorFlowLite_ESP32 :: src/tensorflow/lite/micro/arena_allocator (recording_simple_memory_allocator)

; 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::RecordingSimpleMemoryAllocator" = type { %"class.tflite::SimpleMemoryAllocator.base", i64, i64, i64, i64 }
%"class.tflite::SimpleMemoryAllocator.base" = type <{ %"class.tflite::INonPersistentBufferAllocator", %"class.tflite::IPersistentBufferAllocator", ptr, ptr, ptr, ptr, ptr, ptr, i64, i32 }>
%"class.tflite::INonPersistentBufferAllocator" = type { ptr }
%"class.tflite::IPersistentBufferAllocator" = type { ptr }

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

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

16:                                               ; preds = %3
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = call noundef ptr @_ZNK6tflite21SimpleMemoryAllocator4tailEv(ptr noundef nonnull align 8 dereferenceable(76) %9)
  %19 = ptrtoint ptr %17 to i64
  %20 = ptrtoint ptr %18 to i64
  %21 = sub i64 %19, %20
  %22 = getelementptr inbounds %"class.tflite::RecordingSimpleMemoryAllocator", ptr %9, i32 0, i32 3
  %23 = load i64, ptr %22, align 8, !tbaa !11
  %24 = add i64 %23, %21
  store i64 %24, ptr %22, align 8, !tbaa !11
  %25 = load i64, ptr %5, align 8, !tbaa !9
  %26 = getelementptr inbounds %"class.tflite::RecordingSimpleMemoryAllocator", ptr %9, i32 0, i32 2
  %27 = load i64, ptr %26, align 8, !tbaa !17
  %28 = add i64 %27, %25
  store i64 %28, ptr %26, align 8, !tbaa !17
  %29 = getelementptr inbounds %"class.tflite::RecordingSimpleMemoryAllocator", ptr %9, i32 0, i32 4
  %30 = load i64, ptr %29, align 8, !tbaa !18
  %31 = add i64 %30, 1
  store i64 %31, ptr %29, align 8, !tbaa !18
  br label %32

32:                                               ; preds = %16, %3
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret ptr %33
}

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

declare noundef ptr @_ZNK6tflite21SimpleMemoryAllocator4tailEv(ptr noundef nonnull align 8 dereferenceable(76)) #2

declare noundef ptr @_ZN6tflite21SimpleMemoryAllocator24AllocatePersistentBufferEmm(ptr noundef nonnull align 8 dereferenceable(76), i64 noundef, i64 noundef) unnamed_addr #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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 96}
!12 = !{!"_ZTSN6tflite30RecordingSimpleMemoryAllocatorE", !13, i64 0, !10, i64 80, !10, i64 88, !10, i64 96, !10, i64 104}
!13 = !{!"_ZTSN6tflite21SimpleMemoryAllocatorE", !14, i64 0, !15, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !10, i64 64, !16, i64 72}
!14 = !{!"_ZTSN6tflite29INonPersistentBufferAllocatorE"}
!15 = !{!"_ZTSN6tflite26IPersistentBufferAllocatorE"}
!16 = !{!"int", !7, i64 0}
!17 = !{!12, !10, i64 88}
!18 = !{!12, !10, i64 104}


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

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN6tflite30RecordingSimpleMemoryAllocator24AllocatePersistentBufferEmm(ptr noundef nonnull align 8 dereferenceable(112) %0, i64 noundef %1, i64 noundef %2) unnamed_addr #0 align 2 {
  %4 = tail call noundef ptr @_ZNK6tflite21SimpleMemoryAllocator4tailEv(ptr noundef nonnull align 8 dereferenceable(76) %0)
  %5 = tail call noundef ptr @_ZN6tflite21SimpleMemoryAllocator24AllocatePersistentBufferEmm(ptr noundef nonnull align 8 dereferenceable(76) %0, i64 noundef %1, i64 noundef %2)
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %18, label %6

6:                                                ; preds = %3
  %7 = tail call noundef ptr @_ZNK6tflite21SimpleMemoryAllocator4tailEv(ptr noundef nonnull align 8 dereferenceable(76) %0)
  %8 = ptrtoint ptr %4 to i64
  %9 = ptrtoint ptr %7 to i64
  %10 = sub i64 %8, %9
  %11 = getelementptr inbounds i8, ptr %0, i64 96
  %12 = getelementptr inbounds i8, ptr %0, i64 88
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = add i64 %13, %1
  store i64 %14, ptr %12, align 8, !tbaa !5
  %15 = load <2 x i64>, ptr %11, align 8, !tbaa !15
  %16 = insertelement <2 x i64> <i64 poison, i64 1>, i64 %10, i64 0
  %17 = add <2 x i64> %16, %15
  store <2 x i64> %17, ptr %11, align 8, !tbaa !15
  br label %18

18:                                               ; preds = %6, %3
  ret ptr %5
}

declare noundef ptr @_ZNK6tflite21SimpleMemoryAllocator4tailEv(ptr noundef nonnull align 8 dereferenceable(76)) local_unnamed_addr #1

declare noundef ptr @_ZN6tflite21SimpleMemoryAllocator24AllocatePersistentBufferEmm(ptr noundef nonnull align 8 dereferenceable(76), i64 noundef, i64 noundef) 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, !13, i64 88}
!6 = !{!"_ZTSN6tflite30RecordingSimpleMemoryAllocatorE", !7, i64 0, !13, i64 80, !13, i64 88, !13, i64 96, !13, i64 104}
!7 = !{!"_ZTSN6tflite21SimpleMemoryAllocatorE", !8, i64 0, !9, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !13, i64 64, !14, i64 72}
!8 = !{!"_ZTSN6tflite29INonPersistentBufferAllocatorE"}
!9 = !{!"_ZTSN6tflite26IPersistentBufferAllocatorE"}
!10 = !{!"any pointer", !11, i64 0}
!11 = !{!"omnipotent char", !12, i64 0}
!12 = !{!"Simple C++ TBAA"}
!13 = !{!"long", !11, i64 0}
!14 = !{!"int", !11, i64 0}
!15 = !{!13, !13, i64 0}


; Effective O3 passes: GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SLPVectorizerPass,SROAPass,TailCallElimPass