; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 10. Linked List/LinkedListMenu.ll (Z13sorted_insertP4node)

; 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, ptr }

@head = external dso_local global ptr, align 8
@current = external dso_local global ptr, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z13sorted_insertP4node(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr @head, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %7

5:                                                ; preds = %1
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %6, ptr @head, align 8, !tbaa !5
  br label %52

7:                                                ; preds = %1
  %8 = load ptr, ptr @head, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.node, ptr %8, i32 0, i32 0
  %10 = load i32, ptr %9, align 8, !tbaa !9
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.node, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !9
  %14 = icmp sge i32 %10, %13
  br i1 %14, label %15, label %20

15:                                               ; preds = %7
  %16 = load ptr, ptr @head, align 8, !tbaa !5
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.node, ptr %17, i32 0, i32 1
  store ptr %16, ptr %18, align 8, !tbaa !12
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %19, ptr @head, align 8, !tbaa !5
  br label %51

20:                                               ; preds = %7
  %21 = load ptr, ptr @head, align 8, !tbaa !5
  store ptr %21, ptr @current, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %38, %20
  %23 = load ptr, ptr @current, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.node, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !12
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %42

27:                                               ; preds = %22
  %28 = load ptr, ptr @current, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.node, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !12
  %31 = getelementptr inbounds %struct.node, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 8, !tbaa !9
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.node, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 8, !tbaa !9
  %36 = icmp sgt i32 %32, %35
  br i1 %36, label %37, label %38

37:                                               ; preds = %27
  br label %42

38:                                               ; preds = %27
  %39 = load ptr, ptr @current, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.node, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !12
  store ptr %41, ptr @current, align 8, !tbaa !5
  br label %22, !llvm.loop !13

42:                                               ; preds = %37, %22
  %43 = load ptr, ptr @current, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.node, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !12
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.node, ptr %46, i32 0, i32 1
  store ptr %45, ptr %47, align 8, !tbaa !12
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = load ptr, ptr @current, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.node, ptr %49, i32 0, i32 1
  store ptr %48, ptr %50, align 8, !tbaa !12
  br label %51

51:                                               ; preds = %42, %15
  br label %52

52:                                               ; preds = %51, %5
  ret void
}

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


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

@head = external dso_local local_unnamed_addr global ptr, align 8
@current = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z13sorted_insertP4node(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @head, align 8, !tbaa !5
  %3 = icmp eq ptr %2, null
  br i1 %3, label %15, label %4

4:                                                ; preds = %1
  %5 = load i32, ptr %2, align 8, !tbaa !9
  %6 = load i32, ptr %0, align 8, !tbaa !9
  %.not = icmp slt i32 %5, %6
  br i1 %.not, label %.preheader, label %.sink.split

.preheader:                                       ; preds = %4, %9
  %storemerge = phi ptr [ %8, %9 ], [ %2, %4 ]
  %7 = getelementptr inbounds i8, ptr %storemerge, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  %.not8 = icmp eq ptr %8, null
  br i1 %.not8, label %12, label %9

9:                                                ; preds = %.preheader
  %10 = load i32, ptr %8, align 8, !tbaa !9
  %11 = icmp sgt i32 %10, %6
  br i1 %11, label %12, label %.preheader, !llvm.loop !13

12:                                               ; preds = %9, %.preheader
  %13 = getelementptr inbounds i8, ptr %storemerge, i64 8
  store ptr %storemerge, ptr @current, align 8, !tbaa !5
  br label %.sink.split

.sink.split:                                      ; preds = %4, %12
  %.sink = phi ptr [ %8, %12 ], [ %2, %4 ]
  %head.sink.ph = phi ptr [ %13, %12 ], [ @head, %4 ]
  %14 = getelementptr inbounds i8, ptr %0, i64 8
  store ptr %.sink, ptr %14, align 8, !tbaa !12
  br label %15

15:                                               ; preds = %.sink.split, %1
  %head.sink = phi ptr [ @head, %1 ], [ %head.sink.ph, %.sink.split ]
  store ptr %0, ptr %head.sink, align 8, !tbaa !5
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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, !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,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass