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

; Function Attrs: hot nounwind uwtable
define dso_local ptr @memtable_set(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %4, 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 eq i32 %12, 0
  br i1 %13, label %14, label %36

14:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = call ptr @memtable_page_allocate(ptr noundef %15)
  store ptr %16, ptr %6, align 8, !tbaa !5
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.MemtablePage, ptr %18, i32 0, i32 2
  %20 = getelementptr inbounds [0 x ptr], ptr %19, i64 0, i64 0
  store ptr %17, ptr %20, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.MemtablePage, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 8, !tbaa !16
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %22, align 8, !tbaa !16
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.Memtable, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.MemtablePage, ptr %27, i32 0, i32 0
  call void @rbtree_set(ptr noundef %26, ptr noundef null, i32 noundef 0, ptr noundef %28)
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.Memtable, ptr %29, i32 0, i32 4
  %31 = load i32, ptr %30, align 8, !tbaa !9
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %30, align 8, !tbaa !9
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Memtable, ptr %33, i32 0, i32 5
  %35 = call i64 @atomic_u64_inc(ptr noundef %34)
  store ptr null, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  br label %59

36:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = call ptr @memtable_search_page(ptr noundef %37, ptr noundef %38) #7
  store ptr %39, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  store ptr null, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = call ptr @memtable_insert(ptr noundef %40, ptr noundef %41, ptr noundef %8, ptr noundef %42) #7
  store ptr %43, ptr %9, align 8, !tbaa !5
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = icmp ne ptr %44, null
  br i1 %45, label %46, label %57

46:                                               ; preds = %36
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.Memtable, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.MemtablePage, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.MemtablePage, ptr %51, i32 0, i32 0
  call void @rbtree_set(ptr noundef %48, ptr noundef %50, i32 noundef -1, ptr noundef %52)
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.Memtable, ptr %53, i32 0, i32 4
  %55 = load i32, ptr %54, align 8, !tbaa !9
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %54, align 8, !tbaa !9
  br label %57

57:                                               ; preds = %46, %36
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %58, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  br label %59

59:                                               ; preds = %57, %14
  %60 = load ptr, ptr %3, align 8
  ret ptr %60
}

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

; Function Attrs: nounwind uwtable
declare hidden ptr @memtable_page_allocate(ptr noundef) #2

declare void @rbtree_set(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #3

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

; 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) #5

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

attributes #0 = { hot 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { "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 = { 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 #6 = { nounwind }
attributes #7 = { 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 = !{!12, !12, i64 0}


; 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: hot nounwind uwtable
define dso_local ptr @memtable_set(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca ptr, align 8
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %7, label %18

7:                                                ; preds = %2
  %8 = tail call ptr @memtable_page_allocate(ptr noundef nonnull %0)
  %9 = getelementptr inbounds i8, ptr %8, i64 40
  store ptr %1, ptr %9, align 8, !tbaa !15
  %10 = getelementptr inbounds i8, ptr %8, i64 32
  %11 = load i32, ptr %10, align 8, !tbaa !16
  %12 = add nsw i32 %11, 1
  store i32 %12, ptr %10, align 8, !tbaa !16
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  tail call void @rbtree_set(ptr noundef nonnull %13, ptr noundef null, i32 noundef 0, ptr noundef %8) #6
  %14 = load i32, ptr %4, align 8, !tbaa !5
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %0, i64 32
  %17 = tail call i64 @atomic_u64_inc(ptr noundef nonnull %16)
  br label %27

18:                                               ; preds = %2
  %19 = tail call ptr @memtable_search_page(ptr noundef nonnull %0, ptr noundef %1) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #6
  store ptr null, ptr %3, align 8, !tbaa !15
  %20 = call ptr @memtable_insert(ptr noundef nonnull %0, ptr noundef %19, ptr noundef nonnull %3, ptr noundef %1) #7
  %21 = load ptr, ptr %3, align 8, !tbaa !15
  %.not = icmp eq ptr %21, null
  br i1 %.not, label %26, label %22

22:                                               ; preds = %18
  %23 = getelementptr inbounds i8, ptr %0, i64 8
  call void @rbtree_set(ptr noundef nonnull %23, ptr noundef %19, i32 noundef -1, ptr noundef nonnull %21) #6
  %24 = load i32, ptr %4, align 8, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %4, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %22, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #6
  br label %27

27:                                               ; preds = %26, %7
  %.0 = phi ptr [ null, %7 ], [ %20, %26 ]
  ret ptr %.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: nounwind uwtable
declare hidden ptr @memtable_page_allocate(ptr noundef) local_unnamed_addr #2

declare void @rbtree_set(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #3

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

; 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 #5

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

attributes #0 = { hot 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = { "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 = { 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 #6 = { nounwind }
attributes #7 = { 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, !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 = !{!7, !7, i64 0}
!16 = !{!11, !11, i64 0}


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