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

@.str.1 = external hidden unnamed_addr constant [12 x i8], align 1

declare i32 @printf(ptr noundef, ...) #0

; Function Attrs: nounwind uwtable
define dso_local void @search(i32 noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !9
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = icmp ne ptr %7, null
  br i1 %8, label %10, label %9

9:                                                ; preds = %3
  br label %69

10:                                               ; preds = %3
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = load ptr, ptr %6, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct.BTreeNode, ptr %12, i32 0, i32 0
  %14 = getelementptr inbounds [4 x i32], ptr %13, i64 0, i64 1
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp slt i32 %11, %15
  br i1 %16, label %17, label %19

17:                                               ; preds = %10
  %18 = load ptr, ptr %5, align 8, !tbaa !9
  store i32 0, ptr %18, align 4, !tbaa !5
  br label %59

19:                                               ; preds = %10
  %20 = load ptr, ptr %6, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.BTreeNode, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = load ptr, ptr %5, align 8, !tbaa !9
  store i32 %22, ptr %23, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %41, %19
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = load ptr, ptr %6, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.BTreeNode, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %5, align 8, !tbaa !9
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [4 x i32], ptr %27, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp slt i32 %25, %32
  br i1 %33, label %34, label %38

34:                                               ; preds = %24
  %35 = load ptr, ptr %5, align 8, !tbaa !9
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = icmp sgt i32 %36, 1
  br label %38

38:                                               ; preds = %34, %24
  %39 = phi i1 [ false, %24 ], [ %37, %34 ]
  br i1 %39, label %40, label %45

40:                                               ; preds = %38
  br label %41

41:                                               ; preds = %40
  %42 = load ptr, ptr %5, align 8, !tbaa !9
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = add nsw i32 %43, -1
  store i32 %44, ptr %42, align 4, !tbaa !5
  br label %24, !llvm.loop !13

45:                                               ; preds = %38
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = load ptr, ptr %6, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.BTreeNode, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %5, align 8, !tbaa !9
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [4 x i32], ptr %48, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = icmp eq i32 %46, %53
  br i1 %54, label %55, label %58

55:                                               ; preds = %45
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %56)
  br label %69

58:                                               ; preds = %45
  br label %59

59:                                               ; preds = %58, %17
  %60 = load i32, ptr %4, align 4, !tbaa !5
  %61 = load ptr, ptr %5, align 8, !tbaa !9
  %62 = load ptr, ptr %6, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.BTreeNode, ptr %62, i32 0, i32 2
  %64 = load ptr, ptr %5, align 8, !tbaa !9
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [4 x ptr], ptr %63, i64 0, i64 %66
  %68 = load ptr, ptr %67, align 8, !tbaa !9
  call void @search(i32 noundef %60, ptr noundef %61, ptr noundef %68)
  br label %69

69:                                               ; preds = %59, %55, %9
  ret void
}

attributes #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 = { 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" }

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

@.str.1 = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nofree nounwind uwtable
define dso_local void @search(i32 noundef %0, ptr nocapture noundef writeonly %1, ptr noundef readonly %2) local_unnamed_addr #1 {
  %.not22 = icmp eq ptr %2, null
  br i1 %.not22, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %3, %tailrecurse
  %.tr2123 = phi ptr [ %25, %tailrecurse ], [ %2, %3 ]
  %4 = getelementptr inbounds i8, ptr %.tr2123, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, %0
  br i1 %6, label %7, label %8

7:                                                ; preds = %.lr.ph
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %tailrecurse

8:                                                ; preds = %.lr.ph
  %9 = getelementptr inbounds i8, ptr %.tr2123, i64 16
  %10 = load i32, ptr %9, align 8, !tbaa !9
  %11 = sext i32 %10 to i64
  br label %12

12:                                               ; preds = %12, %8
  %indvars.iv = phi i64 [ %indvars.iv.next, %12 ], [ %11, %8 ]
  %13 = trunc nsw i64 %indvars.iv to i32
  store i32 %13, ptr %1, align 4, !tbaa !5
  %14 = getelementptr inbounds [4 x i32], ptr %.tr2123, i64 0, i64 %indvars.iv
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp sgt i32 %15, %0
  %17 = icmp sgt i64 %indvars.iv, 1
  %or.cond = and i1 %17, %16
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  br i1 %or.cond, label %12, label %.critedge, !llvm.loop !11

.critedge:                                        ; preds = %12
  %18 = icmp eq i32 %15, %0
  br i1 %18, label %19, label %tailrecurse

19:                                               ; preds = %.critedge
  %20 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %0)
  br label %.loopexit

tailrecurse:                                      ; preds = %.critedge, %7
  %21 = phi i64 [ %indvars.iv, %.critedge ], [ 0, %7 ]
  %22 = getelementptr inbounds i8, ptr %.tr2123, i64 24
  %sext = shl i64 %21, 32
  %23 = ashr exact i64 %sext, 32
  %24 = getelementptr inbounds [4 x ptr], ptr %22, i64 0, i64 %23
  %25 = load ptr, ptr %24, align 8, !tbaa !13
  %.not = icmp eq ptr %25, null
  br i1 %.not, label %.loopexit, label %.lr.ph

.loopexit:                                        ; preds = %tailrecurse, %3, %19
  ret void
}

attributes #0 = { nofree 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 = { nofree 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" }

!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, !6, i64 16}
!10 = !{!"BTreeNode", !7, i64 0, !6, i64 16, !7, i64 24}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass