; LLVM IR from Dev-XYS/Algorithms :: Splay-with-Parent(Array).ll (Z6inserti)

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

@ch = external dso_local global [100000 x [2 x i32]], align 16
@fa = external dso_local global [100000 x i32], align 16
@key = external dso_local global [100000 x i32], align 16
@use = external dso_local global i32, align 4
@root = external dso_local global i32, align 4

; 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: mustprogress uwtable
declare dso_local void @_Z5splayii(i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z3cmpii(i32 noundef, i32 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6inserti(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %6 = load i32, ptr @root, align 4, !tbaa !5
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %8, label %26

8:                                                ; preds = %1
  %9 = load i32, ptr @use, align 4, !tbaa !5
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr @use, align 4, !tbaa !5
  store i32 %10, ptr @root, align 4, !tbaa !5
  %11 = load i32, ptr @use, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100000 x i32], ptr @fa, i64 0, i64 %12
  store i32 0, ptr %13, align 4, !tbaa !5
  %14 = load i32, ptr @use, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [100000 x [2 x i32]], ptr @ch, i64 0, i64 %15
  %17 = getelementptr inbounds [2 x i32], ptr %16, i64 0, i64 1
  store i32 0, ptr %17, align 4, !tbaa !5
  %18 = load i32, ptr @use, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [100000 x [2 x i32]], ptr @ch, i64 0, i64 %19
  %21 = getelementptr inbounds [2 x i32], ptr %20, i64 0, i64 0
  store i32 0, ptr %21, align 8, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = load i32, ptr @use, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [100000 x i32], ptr @key, i64 0, i64 %24
  store i32 %22, ptr %25, align 4, !tbaa !5
  br label %84

26:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %27 = load i32, ptr @root, align 4, !tbaa !5
  store i32 %27, ptr %3, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %81, %26
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [100000 x i32], ptr @key, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = icmp ne i32 %32, %33
  br i1 %34, label %35, label %82

35:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %36 = load i32, ptr %2, align 4, !tbaa !5
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [100000 x i32], ptr @key, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = call noundef i32 @_Z3cmpii(i32 noundef %36, i32 noundef %40)
  store i32 %41, ptr %4, align 4, !tbaa !5
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = icmp eq i32 %42, -1
  br i1 %43, label %44, label %45

44:                                               ; preds = %35
  store i32 3, ptr %5, align 4
  br label %79

45:                                               ; preds = %35
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [100000 x [2 x i32]], ptr @ch, i64 0, i64 %47
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [2 x i32], ptr %48, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %71

54:                                               ; preds = %45
  %55 = load i32, ptr @use, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr @use, align 4, !tbaa !5
  %57 = load i32, ptr %3, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [100000 x [2 x i32]], ptr @ch, i64 0, i64 %58
  %60 = load i32, ptr %4, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [2 x i32], ptr %59, i64 0, i64 %61
  store i32 %56, ptr %62, align 4, !tbaa !5
  %63 = load i32, ptr %3, align 4, !tbaa !5
  %64 = load i32, ptr @use, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [100000 x i32], ptr @fa, i64 0, i64 %65
  store i32 %63, ptr %66, align 4, !tbaa !5
  %67 = load i32, ptr %2, align 4, !tbaa !5
  %68 = load i32, ptr @use, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [100000 x i32], ptr @key, i64 0, i64 %69
  store i32 %67, ptr %70, align 4, !tbaa !5
  br label %71

71:                                               ; preds = %54, %45
  %72 = load i32, ptr %3, align 4, !tbaa !5
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [100000 x [2 x i32]], ptr @ch, i64 0, i64 %73
  %75 = load i32, ptr %4, align 4, !tbaa !5
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [2 x i32], ptr %74, i64 0, i64 %76
  %78 = load i32, ptr %77, align 4, !tbaa !5
  store i32 %78, ptr %3, align 4, !tbaa !5
  store i32 0, ptr %5, align 4
  br label %79

79:                                               ; preds = %71, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %80 = load i32, ptr %5, align 4
  switch i32 %80, label %85 [
    i32 0, label %81
    i32 3, label %82
  ]

81:                                               ; preds = %79
  br label %28, !llvm.loop !9

82:                                               ; preds = %79, %28
  %83 = load i32, ptr %3, align 4, !tbaa !5
  call void @_Z5splayii(i32 noundef %83, i32 noundef 0)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %84

84:                                               ; preds = %82, %8
  ret void

85:                                               ; preds = %79
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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" }
attributes #2 = { mustprogress 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@ch = external dso_local local_unnamed_addr global [100000 x [2 x i32]], align 16
@fa = external dso_local local_unnamed_addr global [100000 x i32], align 16
@key = external dso_local local_unnamed_addr global [100000 x i32], align 16
@use = external dso_local local_unnamed_addr global i32, align 4
@root = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z5splayii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z3cmpii(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6inserti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @root, align 4, !tbaa !5
  %3 = icmp eq i32 %2, 0
  br i1 %3, label %7, label %.preheader

.preheader:                                       ; preds = %1
  %4 = sext i32 %2 to i64
  %5 = getelementptr inbounds [100000 x i32], ptr @key, i64 0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not24 = icmp eq i32 %6, %0
  br i1 %.not24, label %.thread, label %.lr.ph

7:                                                ; preds = %1
  %8 = load i32, ptr @use, align 4, !tbaa !5
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr @use, align 4, !tbaa !5
  store i32 %9, ptr @root, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [100000 x i32], ptr @fa, i64 0, i64 %10
  store i32 0, ptr %11, align 4, !tbaa !5
  %12 = getelementptr inbounds [100000 x [2 x i32]], ptr @ch, i64 0, i64 %10, i64 1
  store i32 0, ptr %12, align 4, !tbaa !5
  %13 = getelementptr inbounds [100000 x [2 x i32]], ptr @ch, i64 0, i64 %10
  store i32 0, ptr %13, align 8, !tbaa !5
  %14 = getelementptr inbounds [100000 x i32], ptr @key, i64 0, i64 %10
  store i32 %0, ptr %14, align 4, !tbaa !5
  br label %30

.lr.ph:                                           ; preds = %.preheader, %29
  %15 = phi i32 [ %.pre, %29 ], [ %6, %.preheader ]
  %16 = phi i64 [ %.phi.trans.insert, %29 ], [ %4, %.preheader ]
  %.01625 = phi i32 [ %22, %29 ], [ %2, %.preheader ]
  %17 = tail call noundef i32 @_Z3cmpii(i32 noundef %0, i32 noundef %15)
  %18 = icmp eq i32 %17, -1
  br i1 %18, label %.thread, label %19

19:                                               ; preds = %.lr.ph
  %20 = sext i32 %17 to i64
  %21 = getelementptr inbounds [100000 x [2 x i32]], ptr @ch, i64 0, i64 %16, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %.thread29, label %29

.thread29:                                        ; preds = %19
  %24 = load i32, ptr @use, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr @use, align 4, !tbaa !5
  store i32 %25, ptr %21, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100000 x i32], ptr @fa, i64 0, i64 %26
  store i32 %.01625, ptr %27, align 4, !tbaa !5
  %28 = getelementptr inbounds [100000 x i32], ptr @key, i64 0, i64 %26
  store i32 %0, ptr %28, align 4, !tbaa !5
  br label %.thread

29:                                               ; preds = %19
  %.phi.trans.insert = sext i32 %22 to i64
  %.phi.trans.insert28 = getelementptr inbounds [100000 x i32], ptr @key, i64 0, i64 %.phi.trans.insert
  %.pre = load i32, ptr %.phi.trans.insert28, align 4, !tbaa !5
  %.not = icmp eq i32 %.pre, %0
  br i1 %.not, label %.thread, label %.lr.ph

.thread:                                          ; preds = %29, %.lr.ph, %.thread29, %.preheader
  %.016.lcssa = phi i32 [ %2, %.preheader ], [ %25, %.thread29 ], [ %.01625, %.lr.ph ], [ %22, %29 ]
  tail call void @_Z5splayii(i32 noundef %.016.lcssa, i32 noundef 0)
  br label %30

30:                                               ; preds = %.thread, %7
  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" }
attributes #1 = { mustprogress 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++ TBAA"}


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