; LLVM IR from Algo-Phantoms/Algo-Tree :: Code/C++/Splitting_Linked_lists.ll (Z5splitv)

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

@head = external dso_local global ptr, align 8
@list1 = external dso_local global ptr, align 8
@list2 = external dso_local global ptr, align 8

; 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 void @_Z5splitv() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  %4 = load ptr, ptr @head, align 8, !tbaa !5
  store ptr %4, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %8, %0
  %6 = load ptr, ptr %1, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %8, label %14

8:                                                ; preds = %5
  %9 = load ptr, ptr %1, align 8, !tbaa !5
  %10 = getelementptr inbounds %class.Node, ptr %9, i32 0, i32 1
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  store ptr %11, ptr %1, align 8, !tbaa !5
  %12 = load i32, ptr %2, align 4, !tbaa !9
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr %2, align 4, !tbaa !9
  br label %5, !llvm.loop !13

14:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  %15 = load ptr, ptr @head, align 8, !tbaa !5
  store ptr %15, ptr %1, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %22, %14
  %17 = load i32, ptr %3, align 4, !tbaa !9
  %18 = load i32, ptr %2, align 4, !tbaa !9
  %19 = sub nsw i32 %18, 1
  %20 = sdiv i32 %19, 2
  %21 = icmp ne i32 %17, %20
  br i1 %21, label %22, label %28

22:                                               ; preds = %16
  %23 = load ptr, ptr %1, align 8, !tbaa !5
  %24 = getelementptr inbounds %class.Node, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  store ptr %25, ptr %1, align 8, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %3, align 4, !tbaa !9
  br label %16, !llvm.loop !15

28:                                               ; preds = %16
  %29 = load ptr, ptr @head, align 8, !tbaa !5
  store ptr %29, ptr @list1, align 8, !tbaa !5
  %30 = load ptr, ptr %1, align 8, !tbaa !5
  %31 = getelementptr inbounds %class.Node, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  store ptr %32, ptr @list2, align 8, !tbaa !5
  %33 = load ptr, ptr %1, align 8, !tbaa !5
  %34 = getelementptr inbounds %class.Node, ptr %33, i32 0, i32 1
  store ptr null, ptr %34, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret void
}

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}


; 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
@list1 = external dso_local local_unnamed_addr global ptr, align 8
@list2 = 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 @_Z5splitv() local_unnamed_addr #0 {
  %.0911 = load ptr, ptr @head, align 8, !tbaa !5, !nonnull !9, !noundef !9
  br label %.lr.ph

.preheader:                                       ; preds = %.lr.ph
  %1 = lshr i32 %.0813, 1
  %.not1016 = icmp ult i32 %.0813, 2
  br i1 %.not1016, label %._crit_edge, label %.lr.ph19.preheader

.lr.ph19.preheader:                               ; preds = %.preheader
  %2 = add nsw i32 %1, -1
  %xtraiter = and i32 %1, 7
  %3 = icmp ult i32 %2, 7
  br i1 %3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph19.preheader.new

.lr.ph19.preheader.new:                           ; preds = %.lr.ph19.preheader
  %unroll_iter = and i32 %1, 1073741816
  br label %.lr.ph19

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.0914 = phi ptr [ %.09, %.lr.ph ], [ %.0911, %0 ]
  %.0813 = phi i32 [ %5, %.lr.ph ], [ 0, %0 ]
  %4 = getelementptr inbounds i8, ptr %.0914, i64 8
  %5 = add nuw nsw i32 %.0813, 1
  %.09 = load ptr, ptr %4, align 8, !tbaa !5
  %.not = icmp eq ptr %.09, null
  br i1 %.not, label %.preheader, label %.lr.ph, !llvm.loop !10

.lr.ph19:                                         ; preds = %.lr.ph19, %.lr.ph19.preheader.new
  %.118 = phi ptr [ %.0911, %.lr.ph19.preheader.new ], [ %.1.7, %.lr.ph19 ]
  %niter = phi i32 [ 0, %.lr.ph19.preheader.new ], [ %niter.next.7, %.lr.ph19 ]
  %6 = getelementptr inbounds i8, ptr %.118, i64 8
  %.1 = load ptr, ptr %6, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %.1, i64 8
  %.1.1 = load ptr, ptr %7, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %.1.1, i64 8
  %.1.2 = load ptr, ptr %8, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %.1.2, i64 8
  %.1.3 = load ptr, ptr %9, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %.1.3, i64 8
  %.1.4 = load ptr, ptr %10, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %.1.4, i64 8
  %.1.5 = load ptr, ptr %11, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %.1.5, i64 8
  %.1.6 = load ptr, ptr %12, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %.1.6, i64 8
  %.1.7 = load ptr, ptr %13, align 8, !tbaa !5
  %niter.next.7 = add i32 %niter, 8
  %niter.ncmp.7 = icmp eq i32 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph19, !llvm.loop !12

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph19, %.lr.ph19.preheader
  %.1.lcssa27.ph = phi ptr [ poison, %.lr.ph19.preheader ], [ %.1.7, %.lr.ph19 ]
  %.118.unr = phi ptr [ %.0911, %.lr.ph19.preheader ], [ %.1.7, %.lr.ph19 ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph19.epil

.lr.ph19.epil:                                    ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph19.epil
  %.118.epil = phi ptr [ %.1.epil, %.lr.ph19.epil ], [ %.118.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph19.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %14 = getelementptr inbounds i8, ptr %.118.epil, i64 8
  %.1.epil = load ptr, ptr %14, align 8, !tbaa !5
  %epil.iter.next = add i32 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i32 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph19.epil, !llvm.loop !13

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph19.epil, %.preheader
  %.1.lcssa = phi ptr [ %.0911, %.preheader ], [ %.1.lcssa27.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.1.epil, %.lr.ph19.epil ]
  store ptr %.0911, ptr @list1, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %.1.lcssa, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  store ptr %16, ptr @list2, align 8, !tbaa !5
  store ptr null, ptr %15, align 8, !tbaa !15
  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 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.unroll.disable"}
!15 = !{!16, !6, i64 8}
!16 = !{!"_ZTS4Node", !17, i64 0, !6, i64 8}
!17 = !{!"int", !7, i64 0}


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