; LLVM IR from KoishiDB/Koishi-DB :: src/memory/write_batch.ll (ZN8koishidb10WriteBatch9InsertAllEPNS_8MemtableE)

; 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.koishidb::Slice" = type { ptr, i64 }
%class.anon = type { ptr, ptr }
%"class.koishidb::WriteBatch" = type { %"class.std::__cxx11::basic_string" }
%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN8koishidb18WriteBatchInternal5CountEPKNS_10WriteBatchE(ptr noundef) #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN8koishidb10WriteBatch9InsertAllEPNS_8MemtableE(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef %1) #0 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca %"class.koishidb::Slice", align 8
  %8 = alloca %class.anon, align 8
  %9 = alloca i32, align 4
  %10 = alloca i8, align 1
  %11 = alloca %"class.koishidb::Slice", align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %12 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %13 = call noundef i32 @_ZN8koishidb18WriteBatchInternal5CountEPKNS_10WriteBatchE(ptr noundef %12)
  store i32 %13, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %14 = call noundef i64 @_ZN8koishidb18WriteBatchInternal8SequenceEPNS_10WriteBatchE(ptr noundef %12)
  store i64 %14, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #4
  %15 = getelementptr inbounds %"class.koishidb::WriteBatch", ptr %12, i32 0, i32 0
  call void @_ZN8koishidb5SliceC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(ptr noundef nonnull align 8 dereferenceable(16) %7, ptr noundef nonnull align 8 dereferenceable(32) %15)
  call void @_ZN8koishidb5Slice7AdvanceEm(ptr noundef nonnull align 8 dereferenceable(16) %7, i64 noundef 12)
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #4
  %16 = getelementptr inbounds %class.anon, ptr %8, i32 0, i32 0
  store ptr %6, ptr %16, align 8, !tbaa !5
  %17 = getelementptr inbounds %class.anon, ptr %8, i32 0, i32 1
  store ptr %7, ptr %17, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %38, %2
  %19 = load i32, ptr %9, align 4, !tbaa !9
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %41

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #4
  %24 = call noundef ptr @_ZNK8koishidb5Slice4dataEv(ptr noundef nonnull align 8 dereferenceable(16) %7)
  %25 = getelementptr inbounds i8, ptr %24, i64 0
  %26 = load i8, ptr %25, align 1, !tbaa !13
  store i8 %26, ptr %10, align 1, !tbaa !13
  call void @_ZN8koishidb5Slice7AdvanceEm(ptr noundef nonnull align 8 dereferenceable(16) %7, i64 noundef 1)
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #4
  %27 = load i8, ptr %10, align 1, !tbaa !13
  %28 = call { ptr, i64 } @"_ZZN8koishidb10WriteBatch9InsertAllEPNS_8MemtableEENK3$_0clEc"(ptr noundef nonnull align 8 dereferenceable(16) %8, i8 noundef signext %27)
  %29 = getelementptr inbounds { ptr, i64 }, ptr %11, i32 0, i32 0
  %30 = extractvalue { ptr, i64 } %28, 0
  store ptr %30, ptr %29, align 8
  %31 = getelementptr inbounds { ptr, i64 }, ptr %11, i32 0, i32 1
  %32 = extractvalue { ptr, i64 } %28, 1
  store i64 %32, ptr %31, align 8
  %33 = call noundef i64 @_ZNK8koishidb5Slice4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %11)
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %37

35:                                               ; preds = %23
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  call void @_ZN8koishidb8Memtable6InsertERKNS_5SliceE(ptr noundef nonnull align 8 dereferenceable(80) %36, ptr noundef nonnull align 8 dereferenceable(16) %11)
  br label %37

37:                                               ; preds = %35, %23
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #4
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %9, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %9, align 4, !tbaa !9
  br label %18, !llvm.loop !14

41:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZN8koishidb18WriteBatchInternal8SequenceEPNS_10WriteBatchE(ptr noundef) #0 align 2

declare void @_ZN8koishidb5SliceC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(32)) unnamed_addr #2

declare void @_ZN8koishidb5Slice7AdvanceEm(ptr noundef nonnull align 8 dereferenceable(16), i64 noundef) #2

declare noundef ptr @_ZNK8koishidb5Slice4dataEv(ptr noundef nonnull align 8 dereferenceable(16)) #2

; Function Attrs: inlinehint mustprogress uwtable
declare hidden { ptr, i64 } @"_ZZN8koishidb10WriteBatch9InsertAllEPNS_8MemtableEENK3$_0clEc"(ptr noundef nonnull align 8 dereferenceable(16), i8 noundef signext) #3 align 2

declare noundef i64 @_ZNK8koishidb5Slice4sizeEv(ptr noundef nonnull align 8 dereferenceable(16)) #2

declare void @_ZN8koishidb8Memtable6InsertERKNS_5SliceE(ptr noundef nonnull align 8 dereferenceable(80), ptr noundef nonnull align 8 dereferenceable(16)) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { inlinehint 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 #4 = { 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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

%"class.koishidb::Slice" = type { ptr, i64 }
%class.anon = type { ptr, ptr }

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN8koishidb18WriteBatchInternal5CountEPKNS_10WriteBatchE(ptr noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN8koishidb10WriteBatch9InsertAllEPNS_8MemtableE(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef %1) local_unnamed_addr #0 align 2 {
  %3 = alloca i64, align 8
  %4 = alloca %"class.koishidb::Slice", align 8
  %5 = alloca %class.anon, align 8
  %6 = alloca %"class.koishidb::Slice", align 8
  %7 = tail call noundef i32 @_ZN8koishidb18WriteBatchInternal5CountEPKNS_10WriteBatchE(ptr noundef nonnull %0)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  %8 = tail call noundef i64 @_ZN8koishidb18WriteBatchInternal8SequenceEPNS_10WriteBatchE(ptr noundef nonnull %0)
  store i64 %8, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #4
  call void @_ZN8koishidb5SliceC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(ptr noundef nonnull align 8 dereferenceable(16) %4, ptr noundef nonnull align 8 dereferenceable(32) %0)
  call void @_ZN8koishidb5Slice7AdvanceEm(ptr noundef nonnull align 8 dereferenceable(16) %4, i64 noundef 12)
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #4
  store ptr %3, ptr %5, align 8, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %5, i64 8
  store ptr %4, ptr %9, align 8, !tbaa !9
  %10 = icmp sgt i32 %7, 0
  br i1 %10, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2
  %11 = getelementptr inbounds i8, ptr %6, i64 8
  br label %12

._crit_edge:                                      ; preds = %20, %2
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #4
  ret void

12:                                               ; preds = %.lr.ph, %20
  %.05 = phi i32 [ 0, %.lr.ph ], [ %21, %20 ]
  %13 = call noundef ptr @_ZNK8koishidb5Slice4dataEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %14 = load i8, ptr %13, align 1, !tbaa !11
  call void @_ZN8koishidb5Slice7AdvanceEm(ptr noundef nonnull align 8 dereferenceable(16) %4, i64 noundef 1)
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %6) #4
  %15 = call { ptr, i64 } @"_ZZN8koishidb10WriteBatch9InsertAllEPNS_8MemtableEENK3$_0clEc"(ptr noundef nonnull align 8 dereferenceable(16) %5, i8 noundef signext %14)
  %16 = extractvalue { ptr, i64 } %15, 0
  store ptr %16, ptr %6, align 8
  %17 = extractvalue { ptr, i64 } %15, 1
  store i64 %17, ptr %11, align 8
  %18 = call noundef i64 @_ZNK8koishidb5Slice4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %6)
  %.not = icmp eq i64 %18, 0
  br i1 %.not, label %20, label %19

19:                                               ; preds = %12
  call void @_ZN8koishidb8Memtable6InsertERKNS_5SliceE(ptr noundef nonnull align 8 dereferenceable(80) %1, ptr noundef nonnull align 8 dereferenceable(16) %6)
  br label %20

20:                                               ; preds = %19, %12
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %6) #4
  %21 = add nuw nsw i32 %.05, 1
  %exitcond.not = icmp eq i32 %21, %7
  br i1 %exitcond.not, label %._crit_edge, label %12, !llvm.loop !12
}

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZN8koishidb18WriteBatchInternal8SequenceEPNS_10WriteBatchE(ptr noundef) local_unnamed_addr #0 align 2

declare void @_ZN8koishidb5SliceC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(32)) unnamed_addr #2

declare void @_ZN8koishidb5Slice7AdvanceEm(ptr noundef nonnull align 8 dereferenceable(16), i64 noundef) local_unnamed_addr #2

declare noundef ptr @_ZNK8koishidb5Slice4dataEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress uwtable
declare hidden { ptr, i64 } @"_ZZN8koishidb10WriteBatch9InsertAllEPNS_8MemtableEENK3$_0clEc"(ptr noundef nonnull align 8 dereferenceable(16), i8 noundef signext) local_unnamed_addr #3 align 2

declare noundef i64 @_ZNK8koishidb5Slice4sizeEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #2

declare void @_ZN8koishidb8Memtable6InsertERKNS_5SliceE(ptr noundef nonnull align 8 dereferenceable(80), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { inlinehint 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 #4 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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