; LLVM IR from Dev-XYS/Algorithms :: Binary-Search-Tree.ll (Z6insertii)

; 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.node = type { i32, i32, i32, i32 }

@T = external dso_local global [100000 x %struct.node], align 16
@tc = external dso_local global i32, align 4
@_satellite = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6insertii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %5 = load i32, ptr %3, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %6
  %8 = getelementptr inbounds %struct.node, ptr %7, i32 0, i32 0
  %9 = load i32, ptr %8, align 16, !tbaa !9
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %41

11:                                               ; preds = %2
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %13
  %15 = getelementptr inbounds %struct.node, ptr %14, i32 0, i32 1
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %41

18:                                               ; preds = %11
  %19 = load i32, ptr @tc, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr @tc, align 4, !tbaa !5
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.node, ptr %23, i32 0, i32 0
  store i32 %19, ptr %24, align 16, !tbaa !9
  %25 = load i32, ptr @tc, align 4, !tbaa !5
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr @tc, align 4, !tbaa !5
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %28
  %30 = getelementptr inbounds %struct.node, ptr %29, i32 0, i32 1
  store i32 %25, ptr %30, align 4, !tbaa !11
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.node, ptr %34, i32 0, i32 2
  store i32 %31, ptr %35, align 8, !tbaa !12
  %36 = load i32, ptr @_satellite, align 4, !tbaa !5
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.node, ptr %39, i32 0, i32 3
  store i32 %36, ptr %40, align 4, !tbaa !13
  br label %64

41:                                               ; preds = %11, %2
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %44
  %46 = getelementptr inbounds %struct.node, ptr %45, i32 0, i32 2
  %47 = load i32, ptr %46, align 8, !tbaa !12
  %48 = icmp slt i32 %42, %47
  br i1 %48, label %49, label %56

49:                                               ; preds = %41
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %51
  %53 = getelementptr inbounds %struct.node, ptr %52, i32 0, i32 0
  %54 = load i32, ptr %53, align 16, !tbaa !9
  %55 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z6insertii(i32 noundef %54, i32 noundef %55)
  br label %63

56:                                               ; preds = %41
  %57 = load i32, ptr %3, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %58
  %60 = getelementptr inbounds %struct.node, ptr %59, i32 0, i32 1
  %61 = load i32, ptr %60, align 4, !tbaa !11
  %62 = load i32, ptr %4, align 4, !tbaa !5
  call void @_Z6insertii(i32 noundef %61, i32 noundef %62)
  br label %63

63:                                               ; preds = %56, %49
  br label %64

64:                                               ; preds = %63, %18
  ret void
}

attributes #0 = { mustprogress 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++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTS4node", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !6, i64 12}


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

%struct.node = type { i32, i32, i32, i32 }

@T = external dso_local local_unnamed_addr global [100000 x %struct.node], align 16
@tc = external dso_local local_unnamed_addr global i32, align 4
@_satellite = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z6insertii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.backedge, %2
  %.tr = phi i32 [ %0, %2 ], [ %.tr.be, %tailrecurse.backedge ]
  %3 = sext i32 %.tr to i64
  %4 = getelementptr inbounds [100000 x %struct.node], ptr @T, i64 0, i64 %3
  %5 = load i32, ptr %4, align 16, !tbaa !5
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %7, label %19

7:                                                ; preds = %tailrecurse
  %8 = getelementptr inbounds i8, ptr %4, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !10
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %19

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, ptr %4, i64 4
  %13 = load i32, ptr @tc, align 4, !tbaa !11
  %14 = add nsw i32 %13, 1
  store i32 %13, ptr %4, align 16, !tbaa !5
  %15 = add nsw i32 %13, 2
  store i32 %15, ptr @tc, align 4, !tbaa !11
  store i32 %14, ptr %12, align 4, !tbaa !10
  %16 = getelementptr inbounds i8, ptr %4, i64 8
  store i32 %1, ptr %16, align 8, !tbaa !12
  %17 = load i32, ptr @_satellite, align 4, !tbaa !11
  %18 = getelementptr inbounds i8, ptr %4, i64 12
  store i32 %17, ptr %18, align 4, !tbaa !13
  ret void

19:                                               ; preds = %7, %tailrecurse
  %20 = getelementptr inbounds i8, ptr %4, i64 8
  %21 = load i32, ptr %20, align 8, !tbaa !12
  %22 = icmp sgt i32 %21, %1
  br i1 %22, label %tailrecurse.backedge, label %23

23:                                               ; preds = %19
  %24 = getelementptr inbounds i8, ptr %4, i64 4
  %25 = load i32, ptr %24, align 4, !tbaa !10
  br label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %23, %19
  %.tr.be = phi i32 [ %25, %23 ], [ %5, %19 ]
  br label %tailrecurse
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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, !7, i64 0}
!6 = !{!"_ZTS4node", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!7, !7, i64 0}
!12 = !{!6, !7, i64 8}
!13 = !{!6, !7, i64 12}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass