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

; 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: 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 nounwind uwtable
define dso_local noundef ptr @_Z10bubbleSortP4Node(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %8, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store ptr null, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %13

11:                                               ; preds = %1
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %12, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %54

13:                                               ; preds = %1
  br label %14

14:                                               ; preds = %48, %13
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %52

17:                                               ; preds = %14
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %class.Node, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  store ptr %20, ptr %5, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %44, %17
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %48

24:                                               ; preds = %21
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %class.Node, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 8, !tbaa !12
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %class.Node, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 8, !tbaa !12
  %31 = icmp sgt i32 %27, %30
  br i1 %31, label %32, label %44

32:                                               ; preds = %24
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %class.Node, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 8, !tbaa !12
  store i32 %35, ptr %6, align 4, !tbaa !13
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %class.Node, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !12
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %class.Node, ptr %39, i32 0, i32 0
  store i32 %38, ptr %40, align 8, !tbaa !12
  %41 = load i32, ptr %6, align 4, !tbaa !13
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %class.Node, ptr %42, i32 0, i32 0
  store i32 %41, ptr %43, align 8, !tbaa !12
  br label %44

44:                                               ; preds = %32, %24
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %class.Node, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !9
  store ptr %47, ptr %5, align 8, !tbaa !5
  br label %21, !llvm.loop !14

48:                                               ; preds = %21
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %class.Node, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !9
  store ptr %51, ptr %4, align 8, !tbaa !5
  br label %14, !llvm.loop !16

52:                                               ; preds = %14
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %53, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %54

54:                                               ; preds = %52, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %55 = load ptr, ptr %2, align 8
  ret ptr %55
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #2 = { 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, !6, i64 8}
!10 = !{!"_ZTS4Node", !11, i64 0, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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 nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @_Z10bubbleSortP4Node(ptr noundef returned %0) local_unnamed_addr #0 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %1, %._crit_edge
  %.01724 = phi ptr [ %.01621, %._crit_edge ], [ %0, %1 ]
  %3 = getelementptr inbounds i8, ptr %.01724, i64 8
  %.01621 = load ptr, ptr %3, align 8, !tbaa !5
  %.not2022 = icmp eq ptr %.01621, null
  br i1 %.not2022, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %8
  %.01623 = phi ptr [ %.016, %8 ], [ %.01621, %.preheader ]
  %4 = load i32, ptr %.01724, align 8, !tbaa !11
  %5 = load i32, ptr %.01623, align 8, !tbaa !11
  %6 = icmp sgt i32 %4, %5
  br i1 %6, label %7, label %8

7:                                                ; preds = %.lr.ph
  store i32 %5, ptr %.01724, align 8, !tbaa !11
  store i32 %4, ptr %.01623, align 8, !tbaa !11
  br label %8

8:                                                ; preds = %7, %.lr.ph
  %9 = getelementptr inbounds i8, ptr %.01623, i64 8
  %.016 = load ptr, ptr %9, align 8, !tbaa !5
  %.not20 = icmp eq ptr %.016, null
  br i1 %.not20, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %8
  br i1 %.not2022, label %.loopexit, label %.preheader, !llvm.loop !14

.loopexit:                                        ; preds = %.preheader, %._crit_edge, %1
  ret ptr %0
}

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass