; LLVM IR from pmwkaa/monotone :: monotone/engine/io (memtable)

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

%struct.Memtable = type { ptr, %struct.Rbtree, i32, i32, i32, i64, i64, i64, %struct.List, i32, %struct.Heap }
%struct.Rbtree = type { ptr }
%struct.List = type { ptr, ptr }
%struct.Heap = type { ptr, ptr, i32, ptr }
%struct.MemtablePage = type { %struct.RbtreeNode, i32, [0 x ptr] }
%struct.RbtreeNode = type { ptr, ptr, ptr, i8 }

@.str.1 = external hidden unnamed_addr constant [79 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [22 x i8], align 1
@__PRETTY_FUNCTION__.memtable_unset = external hidden unnamed_addr constant [41 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #0

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

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

; Function Attrs: hot inlinehint nounwind uwtable
declare hidden ptr @memtable_search_page(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @memtable_unset(ptr noundef %0, ptr noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.Memtable, ptr %10, i32 0, i32 4
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  br label %16

15:                                               ; preds = %2
  call void @__assert_fail(ptr noundef @.str.2, ptr noundef @.str.1, i32 noundef 284, ptr noundef @__PRETTY_FUNCTION__.memtable_unset) #7
  unreachable

16:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #8
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = call ptr @memtable_search_page(ptr noundef %17, ptr noundef %18) #9
  store ptr %19, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #8
  store i8 0, ptr %6, align 1, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #8
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = call i32 @memtable_search(ptr noundef %20, ptr noundef %21, ptr noundef %22, ptr noundef %6) #9
  store i32 %23, ptr %7, align 4, !tbaa !18
  %24 = load i8, ptr %6, align 1, !tbaa !16, !range !19, !noundef !20
  %25 = trunc i8 %24 to i1
  br i1 %25, label %27, label %26

26:                                               ; preds = %16
  store i32 1, ptr %8, align 4
  br label %80

27:                                               ; preds = %16
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.MemtablePage, ptr %28, i32 0, i32 2
  %30 = load i32, ptr %7, align 4, !tbaa !18
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [0 x ptr], ptr %29, i64 0, i64 %31
  store ptr null, ptr %32, align 8, !tbaa !5
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.MemtablePage, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 8, !tbaa !18
  %36 = add nsw i32 %35, -1
  store i32 %36, ptr %34, align 8, !tbaa !18
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.Memtable, ptr %37, i32 0, i32 5
  %39 = call i64 @atomic_u64_dec(ptr noundef %38)
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.MemtablePage, ptr %40, i32 0, i32 1
  %42 = load i32, ptr %41, align 8, !tbaa !18
  %43 = icmp eq i32 %42, 0
  br i1 %43, label %44, label %53

44:                                               ; preds = %27
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.Memtable, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.MemtablePage, ptr %47, i32 0, i32 0
  call void @rbtree_remove(ptr noundef %46, ptr noundef %48)
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.Memtable, ptr %49, i32 0, i32 4
  %51 = load i32, ptr %50, align 8, !tbaa !9
  %52 = add nsw i32 %51, -1
  store i32 %52, ptr %50, align 8, !tbaa !9
  br label %79

53:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #8
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.MemtablePage, ptr %54, i32 0, i32 1
  %56 = load i32, ptr %55, align 8, !tbaa !18
  %57 = load i32, ptr %7, align 4, !tbaa !18
  %58 = sub nsw i32 %56, %57
  %59 = sext i32 %58 to i64
  %60 = mul i64 %59, 8
  %61 = trunc i64 %60 to i32
  store i32 %61, ptr %9, align 4, !tbaa !18
  %62 = load i32, ptr %9, align 4, !tbaa !18
  %63 = icmp sgt i32 %62, 0
  br i1 %63, label %64, label %78

64:                                               ; preds = %53
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.MemtablePage, ptr %65, i32 0, i32 2
  %67 = load i32, ptr %7, align 4, !tbaa !18
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [0 x ptr], ptr %66, i64 0, i64 %68
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.MemtablePage, ptr %70, i32 0, i32 2
  %72 = load i32, ptr %7, align 4, !tbaa !18
  %73 = add nsw i32 %72, 1
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [0 x ptr], ptr %71, i64 0, i64 %74
  %76 = load i32, ptr %9, align 4, !tbaa !18
  %77 = sext i32 %76 to i64
  call void @llvm.memmove.p0.p0.i64(ptr align 8 %69, ptr align 8 %75, i64 %77, i1 false)
  br label %78

78:                                               ; preds = %64, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #8
  br label %79

79:                                               ; preds = %78, %44
  store i32 0, ptr %8, align 4
  br label %80

80:                                               ; preds = %79, %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #8
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #8
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #8
  %81 = load i32, ptr %8, align 4
  switch i32 %81, label %83 [
    i32 0, label %82
    i32 1, label %82
  ]

82:                                               ; preds = %80, %80
  ret void

83:                                               ; preds = %80
  unreachable
}

; Function Attrs: hot inlinehint nounwind uwtable
declare hidden i32 @memtable_search(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @atomic_u64_dec(ptr noundef) #4

declare void @rbtree_remove(ptr noundef, ptr noundef) #5

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

attributes #0 = { noreturn nounwind "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 = { hot inlinehint nounwind 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 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 = { inlinehint nounwind 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 = { "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 #6 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #7 = { noreturn nounwind }
attributes #8 = { nounwind }
attributes #9 = { hot }

!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/C++ TBAA"}
!9 = !{!10, !12, i64 24}
!10 = !{!"Memtable", !6, i64 0, !11, i64 8, !12, i64 16, !12, i64 20, !12, i64 24, !13, i64 32, !13, i64 40, !13, i64 48, !14, i64 56, !12, i64 72, !15, i64 80}
!11 = !{!"Rbtree", !6, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!"List", !6, i64 0, !6, i64 8}
!15 = !{!"Heap", !6, i64 0, !6, i64 8, !12, i64 16, !6, i64 24}
!16 = !{!17, !17, i64 0}
!17 = !{!"_Bool", !7, i64 0}
!18 = !{!12, !12, i64 0}
!19 = !{i8 0, i8 2}
!20 = !{}


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

@.str.1 = external hidden unnamed_addr constant [79 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [22 x i8], align 1
@__PRETTY_FUNCTION__.memtable_unset = external hidden unnamed_addr constant [41 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: hot inlinehint nounwind uwtable
declare hidden ptr @memtable_search_page(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @memtable_unset(ptr noundef %0, ptr noundef %1) local_unnamed_addr #3 {
  %3 = alloca i8, align 1
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %8, label %7

7:                                                ; preds = %2
  tail call void @__assert_fail(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, i32 noundef 284, ptr noundef nonnull @__PRETTY_FUNCTION__.memtable_unset) #7
  unreachable

8:                                                ; preds = %2
  %9 = tail call ptr @memtable_search_page(ptr noundef nonnull %0, ptr noundef %1) #8
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %3) #9
  store i8 0, ptr %3, align 1, !tbaa !15
  %10 = call i32 @memtable_search(ptr noundef nonnull %0, ptr noundef %9, ptr noundef %1, ptr noundef nonnull %3) #8
  %11 = load i8, ptr %3, align 1, !tbaa !15, !range !17, !noundef !18
  %12 = trunc nuw i8 %11 to i1
  br i1 %12, label %13, label %37

13:                                               ; preds = %8
  %14 = getelementptr inbounds i8, ptr %9, i64 40
  %15 = sext i32 %10 to i64
  %16 = getelementptr inbounds [0 x ptr], ptr %14, i64 0, i64 %15
  store ptr null, ptr %16, align 8, !tbaa !19
  %17 = getelementptr inbounds i8, ptr %9, i64 32
  %18 = load i32, ptr %17, align 8, !tbaa !20
  %19 = add nsw i32 %18, -1
  store i32 %19, ptr %17, align 8, !tbaa !20
  %20 = getelementptr inbounds i8, ptr %0, i64 32
  %21 = call i64 @atomic_u64_dec(ptr noundef nonnull %20)
  %22 = load i32, ptr %17, align 8, !tbaa !20
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %28

24:                                               ; preds = %13
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  call void @rbtree_remove(ptr noundef nonnull %25, ptr noundef nonnull %9) #9
  %26 = load i32, ptr %4, align 8, !tbaa !5
  %27 = add nsw i32 %26, -1
  store i32 %27, ptr %4, align 8, !tbaa !5
  br label %37

28:                                               ; preds = %13
  %29 = sub nsw i32 %22, %10
  %30 = shl i32 %29, 3
  %31 = icmp sgt i32 %30, 0
  br i1 %31, label %32, label %37

32:                                               ; preds = %28
  %33 = add nsw i32 %10, 1
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [0 x ptr], ptr %14, i64 0, i64 %34
  %36 = zext nneg i32 %30 to i64
  call void @llvm.memmove.p0.p0.i64(ptr nonnull align 8 %16, ptr nonnull align 8 %35, i64 %36, i1 false)
  br label %37

37:                                               ; preds = %24, %32, %28, %8
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %3) #9
  ret void
}

; Function Attrs: hot inlinehint nounwind uwtable
declare hidden i32 @memtable_search(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @atomic_u64_dec(ptr noundef) local_unnamed_addr #4

declare void @rbtree_remove(ptr noundef, ptr noundef) local_unnamed_addr #5

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

attributes #0 = { noreturn nounwind "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 = { hot inlinehint nounwind 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 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 = { inlinehint nounwind 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 = { "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 #6 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #7 = { noreturn nounwind }
attributes #8 = { hot }
attributes #9 = { 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, !11, i64 24}
!6 = !{!"Memtable", !7, i64 0, !10, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !13, i64 56, !11, i64 72, !14, i64 80}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"Rbtree", !7, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!"List", !7, i64 0, !7, i64 8}
!14 = !{!"Heap", !7, i64 0, !7, i64 8, !11, i64 16, !7, i64 24}
!15 = !{!16, !16, i64 0}
!16 = !{!"_Bool", !8, i64 0}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = !{!7, !7, i64 0}
!20 = !{!11, !11, i64 0}


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