; LLVM IR from mahendramahara/DSA-in-C-Programing :: TREE (B-Tree-Implementation)

; 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.BTreeNode = type { [4 x i32], i32, [4 x 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

; Function Attrs: nounwind uwtable
define dso_local void @insertNode(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %10 = load ptr, ptr %7, align 8, !tbaa !9
  %11 = getelementptr inbounds %struct.BTreeNode, ptr %10, i32 0, i32 1
  %12 = load i32, ptr %11, align 8, !tbaa !11
  store i32 %12, ptr %9, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %17, %4
  %14 = load i32, ptr %9, align 4, !tbaa !5
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = icmp sgt i32 %14, %15
  br i1 %16, label %17, label %44

17:                                               ; preds = %13
  %18 = load ptr, ptr %7, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.BTreeNode, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %9, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [4 x i32], ptr %19, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = load ptr, ptr %7, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.BTreeNode, ptr %24, i32 0, i32 0
  %26 = load i32, ptr %9, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [4 x i32], ptr %25, i64 0, i64 %28
  store i32 %23, ptr %29, align 4, !tbaa !5
  %30 = load ptr, ptr %7, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.BTreeNode, ptr %30, i32 0, i32 2
  %32 = load i32, ptr %9, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [4 x ptr], ptr %31, i64 0, i64 %33
  %35 = load ptr, ptr %34, align 8, !tbaa !9
  %36 = load ptr, ptr %7, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.BTreeNode, ptr %36, i32 0, i32 2
  %38 = load i32, ptr %9, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [4 x ptr], ptr %37, i64 0, i64 %40
  store ptr %35, ptr %41, align 8, !tbaa !9
  %42 = load i32, ptr %9, align 4, !tbaa !5
  %43 = add nsw i32 %42, -1
  store i32 %43, ptr %9, align 4, !tbaa !5
  br label %13, !llvm.loop !13

44:                                               ; preds = %13
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.BTreeNode, ptr %46, i32 0, i32 0
  %48 = load i32, ptr %9, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [4 x i32], ptr %47, i64 0, i64 %50
  store i32 %45, ptr %51, align 4, !tbaa !5
  %52 = load ptr, ptr %8, align 8, !tbaa !9
  %53 = load ptr, ptr %7, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.BTreeNode, ptr %53, i32 0, i32 2
  %55 = load i32, ptr %9, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [4 x ptr], ptr %54, i64 0, i64 %57
  store ptr %52, ptr %58, align 8, !tbaa !9
  %59 = load ptr, ptr %7, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.BTreeNode, ptr %59, i32 0, i32 1
  %61 = load i32, ptr %60, align 8, !tbaa !11
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %60, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"BTreeNode", !7, i64 0, !6, i64 16, !7, i64 24}
!13 = distinct !{!13, !14}
!14 = !{!"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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @insertNode(i32 noundef %0, i32 noundef %1, ptr nocapture noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %2, i64 16
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = icmp sgt i32 %6, %1
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4
  %8 = getelementptr inbounds i8, ptr %2, i64 24
  %9 = sext i32 %6 to i64
  %10 = sext i32 %1 to i64
  %11 = sub nsw i64 %9, %10
  %min.iters.check = icmp ult i64 %11, 2
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %11, -2
  %ind.end = sub nsw i64 %9, %n.vec
  %invariant.gep = getelementptr i8, ptr %2, i64 -4
  %invariant.gep26 = getelementptr i8, ptr %2, i64 -4
  %invariant.gep28 = getelementptr i8, ptr %8, i64 -8
  %invariant.gep30 = getelementptr i8, ptr %8, i64 -8
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = sub i64 %9, %index
  %gep = getelementptr [4 x i32], ptr %invariant.gep, i64 0, i64 %offset.idx
  %wide.load = load <2 x i32>, ptr %gep, align 4, !tbaa !10
  %12 = add nsw i64 %offset.idx, 1
  %gep27 = getelementptr [4 x i32], ptr %invariant.gep26, i64 0, i64 %12
  store <2 x i32> %wide.load, ptr %gep27, align 4, !tbaa !10
  %gep29 = getelementptr [4 x ptr], ptr %invariant.gep28, i64 0, i64 %offset.idx
  %wide.load21 = load <2 x ptr>, ptr %gep29, align 8, !tbaa !11
  %gep31 = getelementptr [4 x ptr], ptr %invariant.gep30, i64 0, i64 %12
  store <2 x ptr> %wide.load21, ptr %gep31, align 8, !tbaa !11
  %index.next = add nuw i64 %index, 2
  %13 = icmp eq i64 %index.next, %n.vec
  br i1 %13, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %11, %n.vec
  br i1 %cmp.n, label %._crit_edge.loopexit, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv.ph = phi i64 [ %9, %.lr.ph ], [ %ind.end, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %14 = getelementptr inbounds [4 x i32], ptr %2, i64 0, i64 %indvars.iv
  %15 = load i32, ptr %14, align 4, !tbaa !10
  %16 = add nsw i64 %indvars.iv, 1
  %17 = getelementptr inbounds [4 x i32], ptr %2, i64 0, i64 %16
  store i32 %15, ptr %17, align 4, !tbaa !10
  %18 = getelementptr inbounds [4 x ptr], ptr %8, i64 0, i64 %indvars.iv
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  %20 = getelementptr inbounds [4 x ptr], ptr %8, i64 0, i64 %16
  store ptr %19, ptr %20, align 8, !tbaa !11
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %21 = icmp sgt i64 %indvars.iv.next, %10
  br i1 %21, label %scalar.ph, label %._crit_edge.loopexit, !llvm.loop !17

._crit_edge.loopexit:                             ; preds = %scalar.ph, %middle.block
  %indvars.iv.next.lcssa = phi i64 [ %ind.end, %middle.block ], [ %indvars.iv.next, %scalar.ph ]
  %22 = trunc nsw i64 %indvars.iv.next.lcssa to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %4
  %.0.lcssa = phi i32 [ %6, %4 ], [ %22, %._crit_edge.loopexit ]
  %23 = add nsw i32 %.0.lcssa, 1
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [4 x i32], ptr %2, i64 0, i64 %24
  store i32 %0, ptr %25, align 4, !tbaa !10
  %26 = getelementptr inbounds i8, ptr %2, i64 24
  %27 = getelementptr inbounds [4 x ptr], ptr %26, i64 0, i64 %24
  store ptr %3, ptr %27, align 8, !tbaa !11
  %28 = load i32, ptr %5, align 8, !tbaa !5
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %5, align 8, !tbaa !5
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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" }

!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, !9, i64 16}
!6 = !{!"BTreeNode", !7, i64 0, !9, i64 16, !7, i64 24}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!9, !9, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = distinct !{!13, !14, !15, !16}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = distinct !{!17, !14, !16, !15}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass