; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 10. Linked List/cpp/AppendLastNToFirst.ll (Z17append_LinkedListP4Nodei)

; 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 @_Z17append_LinkedListP4Nodei(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %8, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %9, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %class.Node, ptr %10, i32 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  store ptr %12, ptr %7, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %23, %2
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = icmp sgt i32 %14, -1
  br i1 %15, label %16, label %27

16:                                               ; preds = %13
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = add nsw i32 %17, -1
  store i32 %18, ptr %4, align 4, !tbaa !9
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %23

21:                                               ; preds = %16
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %22, ptr %6, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %21, %16
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %class.Node, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  store ptr %26, ptr %6, align 8, !tbaa !5
  br label %13, !llvm.loop !13

27:                                               ; preds = %13
  br label %28

28:                                               ; preds = %31, %27
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = icmp ne ptr %29, null
  br i1 %30, label %31, label %39

31:                                               ; preds = %28
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %32, ptr %7, align 8, !tbaa !5
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %class.Node, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  store ptr %35, ptr %6, align 8, !tbaa !5
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %class.Node, ptr %36, i32 0, i32 1
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  store ptr %38, ptr %5, align 8, !tbaa !5
  br label %28, !llvm.loop !15

39:                                               ; preds = %28
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %class.Node, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  store ptr %42, ptr %6, align 8, !tbaa !5
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %class.Node, ptr %43, i32 0, i32 1
  store ptr null, ptr %44, align 8, !tbaa !11
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = icmp ne ptr %45, null
  br i1 %46, label %47, label %62

47:                                               ; preds = %39
  br label %48

48:                                               ; preds = %53, %47
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %class.Node, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !11
  %52 = icmp ne ptr %51, null
  br i1 %52, label %53, label %57

53:                                               ; preds = %48
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = getelementptr inbounds %class.Node, ptr %54, i32 0, i32 1
  %56 = load ptr, ptr %55, align 8, !tbaa !11
  store ptr %56, ptr %7, align 8, !tbaa !5
  br label %48, !llvm.loop !16

57:                                               ; preds = %48
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = getelementptr inbounds %class.Node, ptr %59, i32 0, i32 1
  store ptr %58, ptr %60, align 8, !tbaa !11
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %61, ptr %3, align 8, !tbaa !5
  br label %62

62:                                               ; preds = %57, %39
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret ptr %63
}

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


; 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 @_Z17append_LinkedListP4Nodei(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = icmp sgt i32 %1, -1
  br i1 %5, label %.lr.ph.preheader, label %.lr.ph34.preheader

.lr.ph.preheader:                                 ; preds = %2
  %6 = add nuw i32 %1, 1
  %xtraiter = and i32 %6, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %.02030.prol = phi ptr [ %10, %.lr.ph.prol ], [ %0, %.lr.ph.preheader ]
  %.02329.prol = phi i32 [ %7, %.lr.ph.prol ], [ %1, %.lr.ph.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %7 = add nsw i32 %.02329.prol, -1
  %8 = icmp eq ptr %.02030.prol, null
  %spec.select.prol = select i1 %8, ptr %0, ptr %.02030.prol
  %9 = getelementptr inbounds i8, ptr %spec.select.prol, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !11

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.lcssa45.unr = phi ptr [ poison, %.lr.ph.preheader ], [ %10, %.lr.ph.prol ]
  %.02030.unr = phi ptr [ %0, %.lr.ph.preheader ], [ %10, %.lr.ph.prol ]
  %.02329.unr = phi i32 [ %1, %.lr.ph.preheader ], [ %7, %.lr.ph.prol ]
  %11 = icmp ult i32 %1, 3
  br i1 %11, label %.preheader28, label %.lr.ph

.preheader28:                                     ; preds = %.lr.ph, %.lr.ph.prol.loopexit
  %.lcssa45 = phi ptr [ %.lcssa45.unr, %.lr.ph.prol.loopexit ], [ %24, %.lr.ph ]
  %.not31 = icmp eq ptr %.lcssa45, null
  br i1 %.not31, label %._crit_edge, label %.lr.ph34.preheader

.lr.ph34.preheader:                               ; preds = %2, %.preheader28
  %.233.ph = phi ptr [ %0, %2 ], [ %.lcssa45, %.preheader28 ]
  br label %.lr.ph34

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.02030 = phi ptr [ %24, %.lr.ph ], [ %.02030.unr, %.lr.ph.prol.loopexit ]
  %.02329 = phi i32 [ %21, %.lr.ph ], [ %.02329.unr, %.lr.ph.prol.loopexit ]
  %12 = icmp eq ptr %.02030, null
  %spec.select = select i1 %12, ptr %0, ptr %.02030
  %13 = getelementptr inbounds i8, ptr %spec.select, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  %spec.select.1 = select i1 %15, ptr %0, ptr %14
  %16 = getelementptr inbounds i8, ptr %spec.select.1, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = icmp eq ptr %17, null
  %spec.select.2 = select i1 %18, ptr %0, ptr %17
  %19 = getelementptr inbounds i8, ptr %spec.select.2, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  %21 = add nsw i32 %.02329, -4
  %22 = icmp eq ptr %20, null
  %spec.select.3 = select i1 %22, ptr %0, ptr %20
  %23 = getelementptr inbounds i8, ptr %spec.select.3, i64 8
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  %.not43.3 = icmp eq i32 %.02329, 3
  br i1 %.not43.3, label %.preheader28, label %.lr.ph, !llvm.loop !13

.lr.ph34:                                         ; preds = %.lr.ph34.preheader, %.lr.ph34
  %.233 = phi ptr [ %26, %.lr.ph34 ], [ %.233.ph, %.lr.ph34.preheader ]
  %.02232 = phi ptr [ %28, %.lr.ph34 ], [ %0, %.lr.ph34.preheader ]
  %25 = getelementptr inbounds i8, ptr %.233, i64 8
  %26 = load ptr, ptr %25, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %.02232, i64 8
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  %.not = icmp eq ptr %26, null
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph34, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph34
  %.phi.trans.insert = getelementptr inbounds i8, ptr %28, i64 8
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader28
  %29 = phi ptr [ %4, %.preheader28 ], [ %.pre, %._crit_edge.loopexit ]
  %.022.lcssa = phi ptr [ %0, %.preheader28 ], [ %28, %._crit_edge.loopexit ]
  %.0.lcssa = phi ptr [ %4, %.preheader28 ], [ %.233, %._crit_edge.loopexit ]
  %30 = getelementptr inbounds i8, ptr %.022.lcssa, i64 8
  store ptr null, ptr %30, align 8, !tbaa !5
  %.not26 = icmp eq ptr %29, null
  br i1 %.not26, label %35, label %.preheader

.preheader:                                       ; preds = %._crit_edge, %.preheader
  %.1 = phi ptr [ %32, %.preheader ], [ %.0.lcssa, %._crit_edge ]
  %31 = getelementptr inbounds i8, ptr %.1, i64 8
  %32 = load ptr, ptr %31, align 8, !tbaa !5
  %.not27 = icmp eq ptr %32, null
  br i1 %.not27, label %33, label %.preheader, !llvm.loop !16

33:                                               ; preds = %.preheader
  %34 = getelementptr inbounds i8, ptr %.1, i64 8
  store ptr %0, ptr %34, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %33, %._crit_edge
  %.024 = phi ptr [ %29, %33 ], [ %0, %._crit_edge ]
  ret ptr %.024
}

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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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