; LLVM IR from Seeed-Studio/Seeed_Arduino_edgeimpulse :: src/edge-impulse-sdk/tensorflow/lite/micro (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", i64, i64, i64 }
%"class.tflite::SimpleMemoryAllocator" = type { ptr, ptr, ptr, ptr, ptr, ptr }

; 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 ptr @_ZN6tflite30RecordingSimpleMemoryAllocator16AllocateFromHeadEmm(ptr noundef nonnull align 8 dereferenceable(72) %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 @_ZNK6tflite21SimpleMemoryAllocator7GetHeadEv(ptr noundef nonnull align 8 dereferenceable(48) %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 @_ZN6tflite21SimpleMemoryAllocator16AllocateFromHeadEmm(ptr noundef nonnull align 8 dereferenceable(48) %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 = call noundef ptr @_ZNK6tflite21SimpleMemoryAllocator7GetHeadEv(ptr noundef nonnull align 8 dereferenceable(48) %9)
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %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 2
  %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 1
  %27 = load i64, ptr %26, align 8, !tbaa !14
  %28 = add i64 %27, %25
  store i64 %28, ptr %26, align 8, !tbaa !14
  %29 = getelementptr inbounds %"class.tflite::RecordingSimpleMemoryAllocator", ptr %9, i32 0, i32 3
  %30 = load i64, ptr %29, align 8, !tbaa !15
  %31 = add i64 %30, 1
  store i64 %31, ptr %29, align 8, !tbaa !15
  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
}

declare noundef ptr @_ZNK6tflite21SimpleMemoryAllocator7GetHeadEv(ptr noundef nonnull align 8 dereferenceable(48)) #2

declare noundef ptr @_ZN6tflite21SimpleMemoryAllocator16AllocateFromHeadEmm(ptr noundef nonnull align 8 dereferenceable(48), 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 56}
!12 = !{!"_ZTSN6tflite30RecordingSimpleMemoryAllocatorE", !13, i64 0, !10, i64 48, !10, i64 56, !10, i64 64}
!13 = !{!"_ZTSN6tflite21SimpleMemoryAllocatorE", !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!14 = !{!12, !10, i64 48}
!15 = !{!12, !10, 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"

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN6tflite30RecordingSimpleMemoryAllocator16AllocateFromHeadEmm(ptr noundef nonnull align 8 dereferenceable(72) %0, i64 noundef %1, i64 noundef %2) unnamed_addr #0 align 2 {
  %4 = tail call noundef ptr @_ZNK6tflite21SimpleMemoryAllocator7GetHeadEv(ptr noundef nonnull align 8 dereferenceable(48) %0)
  %5 = tail call noundef ptr @_ZN6tflite21SimpleMemoryAllocator16AllocateFromHeadEmm(ptr noundef nonnull align 8 dereferenceable(48) %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 @_ZNK6tflite21SimpleMemoryAllocator7GetHeadEv(ptr noundef nonnull align 8 dereferenceable(48) %0)
  %8 = ptrtoint ptr %7 to i64
  %9 = ptrtoint ptr %4 to i64
  %10 = sub i64 %8, %9
  %11 = getelementptr inbounds i8, ptr %0, i64 56
  %12 = getelementptr inbounds i8, ptr %0, i64 48
  %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 !12
  %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 !12
  br label %18

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

declare noundef ptr @_ZNK6tflite21SimpleMemoryAllocator7GetHeadEv(ptr noundef nonnull align 8 dereferenceable(48)) local_unnamed_addr #1

declare noundef ptr @_ZN6tflite21SimpleMemoryAllocator16AllocateFromHeadEmm(ptr noundef nonnull align 8 dereferenceable(48), 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, !11, i64 48}
!6 = !{!"_ZTSN6tflite30RecordingSimpleMemoryAllocatorE", !7, i64 0, !11, i64 48, !11, i64 56, !11, i64 64}
!7 = !{!"_ZTSN6tflite21SimpleMemoryAllocatorE", !8, i64 8, !8, i64 16, !8, i64 24, !8, i64 32, !8, i64 40}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!11, !11, i64 0}


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