; LLVM IR from devparthgarg/Data-Structures-and-Algorithms :: 11. Linked List/24 Sorted Insert in LL/24 Sorted Insert in LL.ll (Z12sortedInsertRP4Nodei)

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

%class.Node = type { i32, ptr }

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z12sortedInsertRP4Nodei(ptr noundef nonnull align 8 dereferenceable(8) %0, i32 noundef %1) #0 personality ptr @__gxx_personality_v0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %12 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 16) #6
  %13 = load i32, ptr %5, align 4, !tbaa !9
  invoke void @_ZN4NodeC2Ei(ptr noundef nonnull align 8 dereferenceable(16) %12, i32 noundef %13)
          to label %14 unwind label %20

14:                                               ; preds = %2
  store ptr %12, ptr %6, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load ptr, ptr %15, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %24

18:                                               ; preds = %14
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %19, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %71

20:                                               ; preds = %2
  %21 = landingpad { ptr, i32 }
          cleanup
  %22 = extractvalue { ptr, i32 } %21, 0
  store ptr %22, ptr %7, align 8
  %23 = extractvalue { ptr, i32 } %21, 1
  store i32 %23, ptr %8, align 4
  call void @_ZdlPvm(ptr noundef %12, i64 noundef 16) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %73

24:                                               ; preds = %14
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load ptr, ptr %26, align 8, !tbaa !5
  %28 = getelementptr inbounds %class.Node, ptr %27, i32 0, i32 0
  %29 = load i32, ptr %28, align 8, !tbaa !11
  %30 = icmp slt i32 %25, %29
  br i1 %30, label %31, label %37

31:                                               ; preds = %24
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load ptr, ptr %32, align 8, !tbaa !5
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %class.Node, ptr %34, i32 0, i32 1
  store ptr %33, ptr %35, align 8, !tbaa !13
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %36, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %71

37:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load ptr, ptr %38, align 8, !tbaa !5
  store ptr %39, ptr %10, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %55, %37
  %41 = load ptr, ptr %10, align 8, !tbaa !5
  %42 = getelementptr inbounds %class.Node, ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 8, !tbaa !13
  %44 = icmp ne ptr %43, null
  br i1 %44, label %45, label %53

45:                                               ; preds = %40
  %46 = load ptr, ptr %10, align 8, !tbaa !5
  %47 = getelementptr inbounds %class.Node, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !13
  %49 = getelementptr inbounds %class.Node, ptr %48, i32 0, i32 0
  %50 = load i32, ptr %49, align 8, !tbaa !11
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = icmp slt i32 %50, %51
  br label %53

53:                                               ; preds = %45, %40
  %54 = phi i1 [ false, %40 ], [ %52, %45 ]
  br i1 %54, label %55, label %59

55:                                               ; preds = %53
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = getelementptr inbounds %class.Node, ptr %56, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !13
  store ptr %58, ptr %10, align 8, !tbaa !5
  br label %40, !llvm.loop !14

59:                                               ; preds = %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %60 = load ptr, ptr %10, align 8, !tbaa !5
  %61 = getelementptr inbounds %class.Node, ptr %60, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !13
  store ptr %62, ptr %11, align 8, !tbaa !5
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = load ptr, ptr %10, align 8, !tbaa !5
  %65 = getelementptr inbounds %class.Node, ptr %64, i32 0, i32 1
  store ptr %63, ptr %65, align 8, !tbaa !13
  %66 = load ptr, ptr %11, align 8, !tbaa !5
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %class.Node, ptr %67, i32 0, i32 1
  store ptr %66, ptr %68, align 8, !tbaa !13
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = load ptr, ptr %69, align 8, !tbaa !5
  store ptr %70, ptr %3, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %71

71:                                               ; preds = %59, %31, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %72 = load ptr, ptr %3, align 8
  ret ptr %72

73:                                               ; preds = %20
  %74 = load ptr, ptr %7, align 8
  %75 = load i32, ptr %8, align 4
  %76 = insertvalue { ptr, i32 } poison, ptr %74, 0
  %77 = insertvalue { ptr, i32 } %76, i32 %75, 1
  resume { ptr, i32 } %77
}

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

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN4NodeC2Ei(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) unnamed_addr #3 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) #4

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nobuiltin allocsize(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 = { 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 #4 = { nobuiltin 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 #5 = { nounwind }
attributes #6 = { builtin allocsize(0) }
attributes #7 = { builtin 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS4Node", !10, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"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 uwtable
define dso_local noundef ptr @_Z12sortedInsertRP4Nodei(ptr nocapture noundef nonnull readonly align 8 dereferenceable(8) %0, i32 noundef %1) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %3 = tail call noalias noundef nonnull dereferenceable(16) ptr @_Znwm(i64 noundef 16) #3
  tail call void @_ZN4NodeC2Ei(ptr noundef nonnull align 8 dereferenceable(16) %3, i32 noundef %1)
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %19, label %6

6:                                                ; preds = %2
  %7 = load i32, ptr %4, align 8, !tbaa !9
  %8 = icmp sgt i32 %7, %1
  br i1 %8, label %9, label %.preheader

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %3, i64 8
  store ptr %4, ptr %10, align 8, !tbaa !12
  br label %19

.preheader:                                       ; preds = %6, %13
  %.019 = phi ptr [ %12, %13 ], [ %4, %6 ]
  %11 = getelementptr inbounds i8, ptr %.019, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %.critedge, label %13

13:                                               ; preds = %.preheader
  %14 = load i32, ptr %12, align 8, !tbaa !9
  %15 = icmp slt i32 %14, %1
  br i1 %15, label %.preheader, label %.critedge, !llvm.loop !13

.critedge:                                        ; preds = %.preheader, %13
  %16 = getelementptr inbounds i8, ptr %.019, i64 8
  store ptr %3, ptr %16, align 8, !tbaa !12
  %17 = getelementptr inbounds i8, ptr %3, i64 8
  store ptr %12, ptr %17, align 8, !tbaa !12
  %18 = load ptr, ptr %0, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %2, %.critedge, %9
  %.0 = phi ptr [ %3, %9 ], [ %18, %.critedge ], [ %3, %2 ]
  ret ptr %.0
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN4NodeC2Ei(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) unnamed_addr #2 align 2

declare i32 @__gxx_personality_v0(...)

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 = { nobuiltin allocsize(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 = { builtin allocsize(0) }

!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++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTS4Node", !11, i64 0, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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