; LLVM IR from codemistic/Data-Structures-and-Algorithms :: CPP/LINKED LIST/CIRCULAR LINKED LIST/reverse_linkedlist.ll (Z6middleRP4node)

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

9:                                                ; preds = %12, %1
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %18

12:                                               ; preds = %9
  %13 = load i32, ptr %3, align 4, !tbaa !9
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr %3, align 4, !tbaa !9
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %class.node, ptr %15, i32 0, i32 1
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  store ptr %17, ptr %4, align 8, !tbaa !5
  br label %9, !llvm.loop !13

18:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = sdiv i32 %19, 2
  %21 = sub nsw i32 %20, 1
  store i32 %21, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  store ptr %23, ptr %6, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %28, %18
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = add nsw i32 %25, -1
  store i32 %26, ptr %5, align 4, !tbaa !9
  %27 = icmp ne i32 %25, 0
  br i1 %27, label %28, label %32

28:                                               ; preds = %24
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %class.node, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  store ptr %31, ptr %6, align 8, !tbaa !5
  br label %24, !llvm.loop !15

32:                                               ; preds = %24
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret ptr %33
}

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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef ptr @_Z6middleRP4node(ptr nocapture noundef nonnull readonly align 8 dereferenceable(8) %0) local_unnamed_addr #0 {
  %.0912 = load ptr, ptr %0, align 8, !tbaa !5
  %.not13 = icmp eq ptr %.0912, null
  br i1 %.not13, label %._crit_edge.preheader, label %.lr.ph

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.0915 = phi ptr [ %.09, %.lr.ph ], [ %.0912, %1 ]
  %.01014 = phi i32 [ %2, %.lr.ph ], [ 0, %1 ]
  %2 = add nuw nsw i32 %.01014, 1
  %3 = getelementptr inbounds i8, ptr %.0915, i64 8
  %.09 = load ptr, ptr %3, align 8, !tbaa !5
  %.not = icmp eq ptr %.09, null
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !9

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %4 = lshr i32 %2, 1
  br label %._crit_edge.preheader

._crit_edge.preheader:                            ; preds = %._crit_edge.loopexit, %1
  %.08.in.ph = phi i32 [ %4, %._crit_edge.loopexit ], [ 0, %1 ]
  %5 = add nsw i32 %.08.in.ph, -1
  %xtraiter = and i32 %.08.in.ph, 7
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge.prol.loopexit, label %._crit_edge.prol

._crit_edge.prol:                                 ; preds = %._crit_edge.preheader, %._crit_edge.prol
  %.08.in.prol = phi i32 [ %.08.prol, %._crit_edge.prol ], [ %.08.in.ph, %._crit_edge.preheader ]
  %.0.in.prol = phi ptr [ %6, %._crit_edge.prol ], [ %0, %._crit_edge.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %._crit_edge.prol ], [ 0, %._crit_edge.preheader ]
  %.0.prol = load ptr, ptr %.0.in.prol, align 8, !tbaa !5
  %.08.prol = add nsw i32 %.08.in.prol, -1
  %6 = getelementptr inbounds i8, ptr %.0.prol, i64 8
  %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 %._crit_edge.prol.loopexit, label %._crit_edge.prol, !llvm.loop !11

._crit_edge.prol.loopexit:                        ; preds = %._crit_edge.prol, %._crit_edge.preheader
  %.0.lcssa.unr = phi ptr [ poison, %._crit_edge.preheader ], [ %.0.prol, %._crit_edge.prol ]
  %.08.in.unr = phi i32 [ %.08.in.ph, %._crit_edge.preheader ], [ %.08.prol, %._crit_edge.prol ]
  %.0.in.unr = phi ptr [ %0, %._crit_edge.preheader ], [ %6, %._crit_edge.prol ]
  %7 = icmp ult i32 %5, 7
  br i1 %7, label %.unr-lcssa, label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.prol.loopexit, %._crit_edge
  %.08.in = phi i32 [ %.08.7, %._crit_edge ], [ %.08.in.unr, %._crit_edge.prol.loopexit ]
  %.0.in = phi ptr [ %15, %._crit_edge ], [ %.0.in.unr, %._crit_edge.prol.loopexit ]
  %.0 = load ptr, ptr %.0.in, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %.0, i64 8
  %.0.1 = load ptr, ptr %8, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %.0.1, i64 8
  %.0.2 = load ptr, ptr %9, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %.0.2, i64 8
  %.0.3 = load ptr, ptr %10, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %.0.3, i64 8
  %.0.4 = load ptr, ptr %11, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %.0.4, i64 8
  %.0.5 = load ptr, ptr %12, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %.0.5, i64 8
  %.0.6 = load ptr, ptr %13, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %.0.6, i64 8
  %.0.7 = load ptr, ptr %14, align 8, !tbaa !5
  %.08.7 = add nsw i32 %.08.in, -8
  %.not11.7 = icmp eq i32 %.08.7, 0
  %15 = getelementptr inbounds i8, ptr %.0.7, i64 8
  br i1 %.not11.7, label %.unr-lcssa, label %._crit_edge, !llvm.loop !13

.unr-lcssa:                                       ; preds = %._crit_edge, %._crit_edge.prol.loopexit
  %.0.lcssa = phi ptr [ %.0.lcssa.unr, %._crit_edge.prol.loopexit ], [ %.0.7, %._crit_edge ]
  ret ptr %.0.lcssa
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!13 = distinct !{!13, !10}


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