; LLVM IR from KoishiDB/Koishi-DB :: src/disk/filterblock_reader.ll (ZN8koishidb17FilterBlockReader11KeyMayMatchEmRKNS_5SliceE)

; 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.koishidb::FilterBlockReader" = type { %"class.koishidb::BloomFilter", ptr, ptr, i64, i64 }
%"class.koishidb::BloomFilter" = type { i32, i32 }

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

declare noundef i32 @_ZN8koishidb13DecodeFixed32EPKc(ptr noundef) #1

; 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 zeroext i1 @_ZN8koishidb17FilterBlockReader11KeyMayMatchEmRKNS_5SliceE(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1, ptr noundef nonnull align 8 dereferenceable(16) %2) #2 align 2 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca %"class.koishidb::Slice", align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %13 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = getelementptr inbounds %"class.koishidb::FilterBlockReader", ptr %13, i32 0, i32 4
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = lshr i64 %14, %16
  store i64 %17, ptr %8, align 8, !tbaa !9
  %18 = load i64, ptr %8, align 8, !tbaa !9
  %19 = getelementptr inbounds %"class.koishidb::FilterBlockReader", ptr %13, i32 0, i32 3
  %20 = load i64, ptr %19, align 8, !tbaa !15
  %21 = icmp ult i64 %18, %20
  br i1 %21, label %22, label %73

22:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %23 = getelementptr inbounds %"class.koishidb::FilterBlockReader", ptr %13, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = load i64, ptr %8, align 8, !tbaa !9
  %26 = mul i64 %25, 4
  %27 = getelementptr inbounds i8, ptr %24, i64 %26
  %28 = call noundef i32 @_ZN8koishidb13DecodeFixed32EPKc(ptr noundef %27)
  store i32 %28, ptr %9, align 4, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %29 = getelementptr inbounds %"class.koishidb::FilterBlockReader", ptr %13, i32 0, i32 2
  %30 = load ptr, ptr %29, align 8, !tbaa !16
  %31 = load i64, ptr %8, align 8, !tbaa !9
  %32 = mul i64 %31, 4
  %33 = getelementptr inbounds i8, ptr %30, i64 %32
  %34 = getelementptr inbounds i8, ptr %33, i64 4
  %35 = call noundef i32 @_ZN8koishidb13DecodeFixed32EPKc(ptr noundef %34)
  store i32 %35, ptr %10, align 4, !tbaa !17
  %36 = load i32, ptr %9, align 4, !tbaa !17
  %37 = load i32, ptr %10, align 4, !tbaa !17
  %38 = icmp ule i32 %36, %37
  br i1 %38, label %39, label %63

39:                                               ; preds = %22
  %40 = load i32, ptr %10, align 4, !tbaa !17
  %41 = zext i32 %40 to i64
  %42 = getelementptr inbounds %"class.koishidb::FilterBlockReader", ptr %13, i32 0, i32 2
  %43 = load ptr, ptr %42, align 8, !tbaa !16
  %44 = getelementptr inbounds %"class.koishidb::FilterBlockReader", ptr %13, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !18
  %46 = ptrtoint ptr %43 to i64
  %47 = ptrtoint ptr %45 to i64
  %48 = sub i64 %46, %47
  %49 = icmp ule i64 %41, %48
  br i1 %49, label %50, label %63

50:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #3
  %51 = getelementptr inbounds %"class.koishidb::FilterBlockReader", ptr %13, i32 0, i32 1
  %52 = load ptr, ptr %51, align 8, !tbaa !18
  %53 = load i32, ptr %9, align 4, !tbaa !17
  %54 = zext i32 %53 to i64
  %55 = getelementptr inbounds i8, ptr %52, i64 %54
  %56 = load i32, ptr %10, align 4, !tbaa !17
  %57 = load i32, ptr %9, align 4, !tbaa !17
  %58 = sub i32 %56, %57
  %59 = zext i32 %58 to i64
  call void @_ZN8koishidb5SliceC1EPKcm(ptr noundef nonnull align 8 dereferenceable(16) %11, ptr noundef %55, i64 noundef %59)
  %60 = getelementptr inbounds %"class.koishidb::FilterBlockReader", ptr %13, i32 0, i32 0
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = call noundef zeroext i1 @_ZNK8koishidb11BloomFilter11KeyMayMatchERKNS_5SliceES3_(ptr noundef nonnull align 4 dereferenceable(8) %60, ptr noundef nonnull align 8 dereferenceable(16) %61, ptr noundef nonnull align 8 dereferenceable(16) %11)
  store i1 %62, ptr %4, align 1
  store i32 1, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #3
  br label %70

63:                                               ; preds = %39, %22
  %64 = load i32, ptr %9, align 4, !tbaa !17
  %65 = load i32, ptr %10, align 4, !tbaa !17
  %66 = icmp eq i32 %64, %65
  br i1 %66, label %67, label %68

67:                                               ; preds = %63
  store i1 false, ptr %4, align 1
  store i32 1, ptr %12, align 4
  br label %70

68:                                               ; preds = %63
  br label %69

69:                                               ; preds = %68
  store i32 0, ptr %12, align 4
  br label %70

70:                                               ; preds = %69, %67, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %71 = load i32, ptr %12, align 4
  switch i32 %71, label %74 [
    i32 0, label %72
  ]

72:                                               ; preds = %70
  br label %73

73:                                               ; preds = %72, %3
  store i1 true, ptr %4, align 1
  store i32 1, ptr %12, align 4
  br label %74

74:                                               ; preds = %73, %70
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %75 = load i1, ptr %4, align 1
  ret i1 %75
}

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

declare noundef zeroext i1 @_ZNK8koishidb11BloomFilter11KeyMayMatchERKNS_5SliceES3_(ptr noundef nonnull align 4 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) #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 = { 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 #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 32}
!12 = !{!"_ZTSN8koishidb17FilterBlockReaderE", !13, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !10, i64 32}
!13 = !{!"_ZTSN8koishidb11BloomFilterE", !14, i64 0, !14, i64 4}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !10, i64 24}
!16 = !{!12, !6, i64 16}
!17 = !{!14, !14, i64 0}
!18 = !{!12, !6, i64 8}


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

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

declare noundef i32 @_ZN8koishidb13DecodeFixed32EPKc(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress 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 zeroext i1 @_ZN8koishidb17FilterBlockReader11KeyMayMatchEmRKNS_5SliceE(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1, ptr noundef nonnull align 8 dereferenceable(16) %2) local_unnamed_addr #2 align 2 {
  %4 = alloca %"class.koishidb::Slice", align 8
  %5 = getelementptr inbounds i8, ptr %0, i64 32
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = lshr i64 %1, %6
  %8 = getelementptr inbounds i8, ptr %0, i64 24
  %9 = load i64, ptr %8, align 8, !tbaa !13
  %10 = icmp ult i64 %7, %9
  br i1 %10, label %11, label %35

11:                                               ; preds = %3
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %14 = shl i64 %7, 2
  %15 = getelementptr inbounds i8, ptr %13, i64 %14
  %16 = tail call noundef i32 @_ZN8koishidb13DecodeFixed32EPKc(ptr noundef %15)
  %17 = load ptr, ptr %12, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %17, i64 %14
  %19 = getelementptr inbounds i8, ptr %18, i64 4
  %20 = tail call noundef i32 @_ZN8koishidb13DecodeFixed32EPKc(ptr noundef nonnull %19)
  %.not = icmp ugt i32 %16, %20
  br i1 %.not, label %34, label %21

21:                                               ; preds = %11
  %22 = zext i32 %20 to i64
  %23 = load ptr, ptr %12, align 8, !tbaa !14
  %24 = getelementptr inbounds i8, ptr %0, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = ptrtoint ptr %23 to i64
  %27 = ptrtoint ptr %25 to i64
  %28 = sub i64 %26, %27
  %.not19 = icmp ult i64 %28, %22
  br i1 %.not19, label %34, label %.thread

.thread:                                          ; preds = %21
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #3
  %29 = zext i32 %16 to i64
  %30 = getelementptr inbounds i8, ptr %25, i64 %29
  %31 = sub i32 %20, %16
  %32 = zext i32 %31 to i64
  call void @_ZN8koishidb5SliceC1EPKcm(ptr noundef nonnull align 8 dereferenceable(16) %4, ptr noundef %30, i64 noundef %32)
  %33 = call noundef zeroext i1 @_ZNK8koishidb11BloomFilter11KeyMayMatchERKNS_5SliceES3_(ptr noundef nonnull align 4 dereferenceable(8) %0, ptr noundef nonnull align 8 dereferenceable(16) %2, ptr noundef nonnull align 8 dereferenceable(16) %4)
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #3
  br label %36

34:                                               ; preds = %11, %21
  %.not23 = icmp eq i32 %16, %20
  br i1 %.not23, label %36, label %35

35:                                               ; preds = %34, %3
  br label %36

36:                                               ; preds = %.thread, %34, %35
  %.1 = phi i1 [ true, %35 ], [ false, %34 ], [ %33, %.thread ]
  ret i1 %.1
}

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

declare noundef zeroext i1 @_ZNK8koishidb11BloomFilter11KeyMayMatchERKNS_5SliceES3_(ptr noundef nonnull align 4 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { 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 #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, !12, i64 32}
!6 = !{!"_ZTSN8koishidb17FilterBlockReaderE", !7, i64 0, !11, i64 8, !11, i64 16, !12, i64 24, !12, i64 32}
!7 = !{!"_ZTSN8koishidb11BloomFilterE", !8, i64 0, !8, i64 4}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!"long", !9, i64 0}
!13 = !{!6, !12, i64 24}
!14 = !{!6, !11, i64 16}
!15 = !{!6, !11, i64 8}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass