; LLVM IR from KoishiDB/Koishi-DB :: src/util/util.ll (ZN8koishidb17CreateMemtableKeyERKNS_5SliceEmS2_NS_7KeyTypeE)

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

; 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

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

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

declare void @_ZN8koishidb5SliceC1EPKcm(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef, i64 noundef) unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #3

; Function Attrs: mustprogress uwtable
define dso_local { ptr, i64 } @_ZN8koishidb17CreateMemtableKeyERKNS_5SliceEmS2_NS_7KeyTypeE(ptr noundef nonnull align 8 dereferenceable(16) %0, i64 noundef %1, ptr noundef nonnull align 8 dereferenceable(16) %2, i8 noundef zeroext %3) #4 {
  %5 = alloca %"class.koishidb::Slice", align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca i64, align 8
  %16 = alloca ptr, align 8
  %17 = alloca i32, align 4
  %18 = alloca ptr, align 8
  %19 = alloca i64, align 8
  %20 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i8 %3, ptr %9, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = call noundef i64 @_ZNK8koishidb5Slice4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %21)
  store i64 %22, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = call noundef i64 @_ZNK8koishidb5Slice4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %23)
  store i64 %24, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %25 = load i64, ptr %10, align 8, !tbaa !9
  %26 = add i64 %25, 8
  %27 = load i64, ptr %11, align 8, !tbaa !9
  %28 = add i64 %26, %27
  %29 = add i64 %28, 10
  store i64 %29, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %30 = load i64, ptr %12, align 8, !tbaa !9
  %31 = call noalias noundef nonnull ptr @_Znam(i64 noundef %30) #6
  store ptr %31, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %32 = load i64, ptr %10, align 8, !tbaa !9
  %33 = add i64 %32, 8
  %34 = trunc i64 %33 to i32
  %35 = load ptr, ptr %13, align 8, !tbaa !5
  %36 = call noundef i64 @_ZN8koishidb14EncodeVarint32EjPc(i32 noundef %34, ptr noundef %35)
  store i64 %36, ptr %14, align 8, !tbaa !9
  %37 = load ptr, ptr %13, align 8, !tbaa !5
  %38 = load i64, ptr %14, align 8, !tbaa !9
  %39 = getelementptr inbounds i8, ptr %37, i64 %38
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = call noundef ptr @_ZNK8koishidb5Slice4dataEv(ptr noundef nonnull align 8 dereferenceable(16) %40)
  %42 = load i64, ptr %10, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %39, ptr align 1 %41, i64 %42, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = shl i64 %43, 8
  %45 = load i8, ptr %9, align 1, !tbaa !11
  %46 = sext i8 %45 to i64
  %47 = or i64 %44, %46
  store i64 %47, ptr %15, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #5
  store ptr %15, ptr %16, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #5
  store i32 0, ptr %17, align 4, !tbaa !13
  br label %48

48:                                               ; preds = %66, %4
  %49 = load i32, ptr %17, align 4, !tbaa !13
  %50 = icmp slt i32 %49, 8
  br i1 %50, label %52, label %51

51:                                               ; preds = %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #5
  br label %69

52:                                               ; preds = %48
  %53 = load ptr, ptr %16, align 8, !tbaa !5
  %54 = load i32, ptr %17, align 4, !tbaa !13
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i8, ptr %53, i64 %55
  %57 = load i8, ptr %56, align 1, !tbaa !15
  %58 = load ptr, ptr %13, align 8, !tbaa !5
  %59 = load i64, ptr %14, align 8, !tbaa !9
  %60 = load i64, ptr %10, align 8, !tbaa !9
  %61 = add i64 %59, %60
  %62 = load i32, ptr %17, align 4, !tbaa !13
  %63 = sext i32 %62 to i64
  %64 = add i64 %61, %63
  %65 = getelementptr inbounds i8, ptr %58, i64 %64
  store i8 %57, ptr %65, align 1, !tbaa !15
  br label %66

66:                                               ; preds = %52
  %67 = load i32, ptr %17, align 4, !tbaa !13
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %17, align 4, !tbaa !13
  br label %48, !llvm.loop !16

69:                                               ; preds = %51
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #5
  %70 = load ptr, ptr %13, align 8, !tbaa !5
  %71 = load i64, ptr %14, align 8, !tbaa !9
  %72 = getelementptr inbounds i8, ptr %70, i64 %71
  %73 = load i64, ptr %10, align 8, !tbaa !9
  %74 = getelementptr inbounds i8, ptr %72, i64 %73
  %75 = getelementptr inbounds i8, ptr %74, i64 8
  store ptr %75, ptr %18, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #5
  %76 = load i64, ptr %11, align 8, !tbaa !9
  %77 = trunc i64 %76 to i32
  %78 = load ptr, ptr %18, align 8, !tbaa !5
  %79 = call noundef i64 @_ZN8koishidb14EncodeVarint32EjPc(i32 noundef %77, ptr noundef %78)
  store i64 %79, ptr %19, align 8, !tbaa !9
  %80 = load ptr, ptr %18, align 8, !tbaa !5
  %81 = load i64, ptr %19, align 8, !tbaa !9
  %82 = getelementptr inbounds i8, ptr %80, i64 %81
  %83 = load ptr, ptr %8, align 8, !tbaa !5
  %84 = call noundef ptr @_ZNK8koishidb5Slice4dataEv(ptr noundef nonnull align 8 dereferenceable(16) %83)
  %85 = load i64, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %82, ptr align 1 %84, i64 %85, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %20) #5
  %86 = load i64, ptr %10, align 8, !tbaa !9
  %87 = add i64 %86, 8
  %88 = load i64, ptr %14, align 8, !tbaa !9
  %89 = add i64 %87, %88
  %90 = load i64, ptr %19, align 8, !tbaa !9
  %91 = add i64 %89, %90
  %92 = load i64, ptr %11, align 8, !tbaa !9
  %93 = add i64 %91, %92
  store i64 %93, ptr %20, align 8, !tbaa !9
  %94 = load ptr, ptr %13, align 8, !tbaa !5
  %95 = load i64, ptr %20, align 8, !tbaa !9
  call void @_ZN8koishidb5SliceC1EPKcm(ptr noundef nonnull align 8 dereferenceable(16) %5, ptr noundef %94, i64 noundef %95)
  call void @llvm.lifetime.end.p0(i64 8, ptr %20) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  %96 = load { ptr, i64 }, ptr %5, align 8
  ret { ptr, i64 } %96
}

declare noundef i64 @_ZN8koishidb14EncodeVarint32EjPc(i32 noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nobuiltin allocsize(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 = { 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 #5 = { nounwind }
attributes #6 = { builtin allocsize(0) }

!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, !12, i64 0}
!12 = !{!"_ZTSN8koishidb7KeyTypeE", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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 }

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

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

declare void @_ZN8koishidb5SliceC1EPKcm(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef, i64 noundef) unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local { ptr, i64 } @_ZN8koishidb17CreateMemtableKeyERKNS_5SliceEmS2_NS_7KeyTypeE(ptr noundef nonnull align 8 dereferenceable(16) %0, i64 noundef %1, ptr noundef nonnull align 8 dereferenceable(16) %2, i8 noundef zeroext %3) local_unnamed_addr #3 {
  %5 = alloca %"class.koishidb::Slice", align 8
  %6 = tail call noundef i64 @_ZNK8koishidb5Slice4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %0)
  %7 = tail call noundef i64 @_ZNK8koishidb5Slice4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  %8 = add i64 %6, 8
  %9 = add i64 %8, %7
  %10 = add i64 %9, 10
  %11 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %10) #4
  %12 = trunc i64 %8 to i32
  %13 = tail call noundef i64 @_ZN8koishidb14EncodeVarint32EjPc(i32 noundef %12, ptr noundef nonnull %11)
  %14 = getelementptr i8, ptr %11, i64 %13
  %15 = tail call noundef ptr @_ZNK8koishidb5Slice4dataEv(ptr noundef nonnull align 8 dereferenceable(16) %0)
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %14, ptr align 1 %15, i64 %6, i1 false)
  %16 = shl i64 %1, 8
  %17 = sext i8 %3 to i64
  %18 = or i64 %16, %17
  %19 = getelementptr i8, ptr %14, i64 %6
  store i64 %18, ptr %19, align 1, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i64 8
  %21 = trunc i64 %7 to i32
  %22 = tail call noundef i64 @_ZN8koishidb14EncodeVarint32EjPc(i32 noundef %21, ptr noundef nonnull %20)
  %23 = getelementptr inbounds i8, ptr %20, i64 %22
  %24 = tail call noundef ptr @_ZNK8koishidb5Slice4dataEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %23, ptr align 1 %24, i64 %7, i1 false)
  %25 = add i64 %9, %13
  %26 = add i64 %25, %22
  call void @_ZN8koishidb5SliceC1EPKcm(ptr noundef nonnull align 8 dereferenceable(16) %5, ptr noundef nonnull %11, i64 noundef %26)
  %.fca.0.load = load ptr, ptr %5, align 8
  %.fca.0.insert = insertvalue { ptr, i64 } poison, ptr %.fca.0.load, 0
  %.fca.1.gep = getelementptr inbounds i8, ptr %5, i64 8
  %.fca.1.load = load i64, ptr %.fca.1.gep, align 8
  %.fca.1.insert = insertvalue { ptr, i64 } %.fca.0.insert, i64 %.fca.1.load, 1
  ret { ptr, i64 } %.fca.1.insert
}

declare noundef i64 @_ZN8koishidb14EncodeVarint32EjPc(i32 noundef, ptr noundef) local_unnamed_addr #0

attributes #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 nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nobuiltin allocsize(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 #3 = { 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 = { builtin allocsize(0) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass