; LLVM IR from seppo0010/rlite :: src (page_btree)

; 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.rl_btree_node = type { ptr, ptr, ptr, i64 }
%struct.rl_btree = type { ptr, i64, i64, ptr, i64, i64 }
%struct.rl_btree_type = type { ptr, ptr, i32, i32, ptr }

; 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 i32 @rl_read(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @rl_flatten_btree_node(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #2 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %15 = load ptr, ptr %8, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.rl_btree_node, ptr %15, i32 0, i32 1
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %48

19:                                               ; preds = %5
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.rl_btree, ptr %21, i32 0, i32 3
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %24 = getelementptr inbounds %struct.rl_btree_type, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !16
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.rl_btree_node, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %29 = getelementptr inbounds i64, ptr %28, i64 0
  %30 = load i64, ptr %29, align 8, !tbaa !18
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = call i32 @rl_read(ptr noundef %20, ptr noundef %25, i64 noundef %30, ptr noundef %31, ptr noundef %13, i32 noundef 1)
  store i32 %32, ptr %11, align 4, !tbaa !9
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = icmp ne i32 2, %33
  br i1 %34, label %35, label %36

35:                                               ; preds = %19
  br label %110

36:                                               ; preds = %19
  %37 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %37, ptr %14, align 8, !tbaa !5
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = load ptr, ptr %14, align 8, !tbaa !5
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = call i32 @rl_flatten_btree_node(ptr noundef %38, ptr noundef %39, ptr noundef %40, ptr noundef %41, ptr noundef %42)
  store i32 %43, ptr %11, align 4, !tbaa !9
  %44 = load i32, ptr %11, align 4, !tbaa !9
  %45 = icmp ne i32 0, %44
  br i1 %45, label %46, label %47

46:                                               ; preds = %36
  br label %110

47:                                               ; preds = %36
  br label %48

48:                                               ; preds = %47, %5
  store i64 0, ptr %12, align 8, !tbaa !18
  br label %49

49:                                               ; preds = %106, %48
  %50 = load i64, ptr %12, align 8, !tbaa !18
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.rl_btree_node, ptr %51, i32 0, i32 3
  %53 = load i64, ptr %52, align 8, !tbaa !19
  %54 = icmp slt i64 %50, %53
  br i1 %54, label %55, label %109

55:                                               ; preds = %49
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.rl_btree_node, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !20
  %59 = load i64, ptr %12, align 8, !tbaa !18
  %60 = getelementptr inbounds ptr, ptr %58, i64 %59
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = load ptr, ptr %62, align 8, !tbaa !5
  %64 = load ptr, ptr %10, align 8, !tbaa !5
  %65 = load i64, ptr %64, align 8, !tbaa !18
  %66 = getelementptr inbounds ptr, ptr %63, i64 %65
  store ptr %61, ptr %66, align 8, !tbaa !5
  %67 = load ptr, ptr %10, align 8, !tbaa !5
  %68 = load i64, ptr %67, align 8, !tbaa !18
  %69 = add nsw i64 %68, 1
  store i64 %69, ptr %67, align 8, !tbaa !18
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.rl_btree_node, ptr %70, i32 0, i32 1
  %72 = load ptr, ptr %71, align 8, !tbaa !11
  %73 = icmp ne ptr %72, null
  br i1 %73, label %74, label %105

74:                                               ; preds = %55
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.rl_btree, ptr %76, i32 0, i32 3
  %78 = load ptr, ptr %77, align 8, !tbaa !14
  %79 = getelementptr inbounds %struct.rl_btree_type, ptr %78, i32 0, i32 1
  %80 = load ptr, ptr %79, align 8, !tbaa !16
  %81 = load ptr, ptr %8, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.rl_btree_node, ptr %81, i32 0, i32 1
  %83 = load ptr, ptr %82, align 8, !tbaa !11
  %84 = load i64, ptr %12, align 8, !tbaa !18
  %85 = add nsw i64 %84, 1
  %86 = getelementptr inbounds i64, ptr %83, i64 %85
  %87 = load i64, ptr %86, align 8, !tbaa !18
  %88 = load ptr, ptr %7, align 8, !tbaa !5
  %89 = call i32 @rl_read(ptr noundef %75, ptr noundef %80, i64 noundef %87, ptr noundef %88, ptr noundef %13, i32 noundef 1)
  store i32 %89, ptr %11, align 4, !tbaa !9
  %90 = load i32, ptr %11, align 4, !tbaa !9
  %91 = icmp ne i32 2, %90
  br i1 %91, label %92, label %93

92:                                               ; preds = %74
  br label %110

93:                                               ; preds = %74
  %94 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %94, ptr %14, align 8, !tbaa !5
  %95 = load ptr, ptr %6, align 8, !tbaa !5
  %96 = load ptr, ptr %7, align 8, !tbaa !5
  %97 = load ptr, ptr %14, align 8, !tbaa !5
  %98 = load ptr, ptr %9, align 8, !tbaa !5
  %99 = load ptr, ptr %10, align 8, !tbaa !5
  %100 = call i32 @rl_flatten_btree_node(ptr noundef %95, ptr noundef %96, ptr noundef %97, ptr noundef %98, ptr noundef %99)
  store i32 %100, ptr %11, align 4, !tbaa !9
  %101 = load i32, ptr %11, align 4, !tbaa !9
  %102 = icmp ne i32 0, %101
  br i1 %102, label %103, label %104

103:                                              ; preds = %93
  br label %110

104:                                              ; preds = %93
  br label %105

105:                                              ; preds = %104, %55
  br label %106

106:                                              ; preds = %105
  %107 = load i64, ptr %12, align 8, !tbaa !18
  %108 = add nsw i64 %107, 1
  store i64 %108, ptr %12, align 8, !tbaa !18
  br label %49, !llvm.loop !21

109:                                              ; preds = %49
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %110

110:                                              ; preds = %109, %103, %92, %46, %35
  %111 = load i32, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  ret i32 %111
}

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 = { 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 }

!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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"rl_btree_node", !6, i64 0, !6, i64 8, !6, i64 16, !13, i64 24}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !6, i64 24}
!15 = !{!"rl_btree", !6, i64 0, !13, i64 8, !13, i64 16, !6, i64 24, !13, i64 32, !13, i64 40}
!16 = !{!17, !6, i64 8}
!17 = !{!"", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !6, i64 24}
!18 = !{!13, !13, i64 0}
!19 = !{!12, !13, i64 24}
!20 = !{!12, !6, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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"

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

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

declare i32 @rl_read(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 3, 2) i32 @rl_flatten_btree_node(ptr noundef %0, ptr noundef %1, ptr nocapture noundef readonly %2, ptr noundef %3, ptr noundef %4) local_unnamed_addr #2 {
  %6 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #3
  %7 = getelementptr inbounds i8, ptr %2, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %.not = icmp eq ptr %8, null
  br i1 %.not, label %19, label %9

9:                                                ; preds = %5
  %10 = getelementptr inbounds i8, ptr %1, i64 24
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %11, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = load i64, ptr %8, align 8, !tbaa !16
  %15 = call i32 @rl_read(ptr noundef %0, ptr noundef %13, i64 noundef %14, ptr noundef %1, ptr noundef nonnull %6, i32 noundef 1) #3
  %.not40 = icmp eq i32 %15, 2
  br i1 %.not40, label %16, label %.loopexit

16:                                               ; preds = %9
  %17 = load ptr, ptr %6, align 8, !tbaa !17
  %18 = call i32 @rl_flatten_btree_node(ptr noundef %0, ptr noundef nonnull %1, ptr noundef %17, ptr noundef %3, ptr noundef %4)
  %.not41 = icmp eq i32 %18, 0
  br i1 %.not41, label %19, label %.loopexit

19:                                               ; preds = %16, %5
  %20 = getelementptr inbounds i8, ptr %1, i64 24
  %21 = getelementptr inbounds i8, ptr %2, i64 24
  %22 = load i64, ptr %21, align 8, !tbaa !18
  %23 = icmp sgt i64 %22, 0
  br i1 %23, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %19, %43
  %.045 = phi i64 [ %44, %43 ], [ 0, %19 ]
  %24 = load ptr, ptr %2, align 8, !tbaa !19
  %25 = getelementptr inbounds ptr, ptr %24, i64 %.045
  %26 = load ptr, ptr %25, align 8, !tbaa !17
  %27 = load ptr, ptr %3, align 8, !tbaa !17
  %28 = load i64, ptr %4, align 8, !tbaa !16
  %29 = getelementptr inbounds ptr, ptr %27, i64 %28
  store ptr %26, ptr %29, align 8, !tbaa !17
  %30 = add nsw i64 %28, 1
  store i64 %30, ptr %4, align 8, !tbaa !16
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %.not42 = icmp eq ptr %31, null
  br i1 %.not42, label %43, label %32

32:                                               ; preds = %.lr.ph
  %33 = load ptr, ptr %20, align 8, !tbaa !11
  %34 = getelementptr inbounds i8, ptr %33, i64 8
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = getelementptr inbounds i64, ptr %31, i64 %.045
  %37 = getelementptr inbounds i8, ptr %36, i64 8
  %38 = load i64, ptr %37, align 8, !tbaa !16
  %39 = call i32 @rl_read(ptr noundef %0, ptr noundef %35, i64 noundef %38, ptr noundef %1, ptr noundef nonnull %6, i32 noundef 1) #3
  %.not43 = icmp eq i32 %39, 2
  br i1 %.not43, label %40, label %.loopexit

40:                                               ; preds = %32
  %41 = load ptr, ptr %6, align 8, !tbaa !17
  %42 = call i32 @rl_flatten_btree_node(ptr noundef %0, ptr noundef nonnull %1, ptr noundef %41, ptr noundef nonnull %3, ptr noundef nonnull %4)
  %.not44 = icmp eq i32 %42, 0
  br i1 %.not44, label %43, label %.loopexit

43:                                               ; preds = %.lr.ph, %40
  %44 = add nuw nsw i64 %.045, 1
  %45 = load i64, ptr %21, align 8, !tbaa !18
  %46 = icmp slt i64 %44, %45
  br i1 %46, label %.lr.ph, label %.loopexit, !llvm.loop !20

.loopexit:                                        ; preds = %32, %40, %43, %19, %16, %9
  %.033 = phi i32 [ %15, %9 ], [ %18, %16 ], [ 0, %19 ], [ %39, %32 ], [ %42, %40 ], [ 0, %43 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #3
  ret i32 %.033
}

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 = { 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 }

!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, !7, i64 8}
!6 = !{!"rl_btree_node", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!12, !7, i64 24}
!12 = !{!"rl_btree", !7, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !10, i64 32, !10, i64 40}
!13 = !{!14, !7, i64 8}
!14 = !{!"", !7, i64 0, !7, i64 8, !15, i64 16, !15, i64 20, !7, i64 24}
!15 = !{!"int", !8, i64 0}
!16 = !{!10, !10, i64 0}
!17 = !{!7, !7, i64 0}
!18 = !{!6, !10, i64 24}
!19 = !{!6, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass