; LLVM IR from gargarchit/Data_Structure_n_Algorithms :: Linked_List/Implementation.ll (Z12deletemiddleRP4Nodei)

; 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
declare dso_local noundef i32 @_Z11length_listP4Node(ptr noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z11delete_headRP4Node(ptr noundef nonnull align 8 dereferenceable(8)) #2

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z11delete_tailRP4Node(ptr noundef nonnull align 8 dereferenceable(8)) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z12deletemiddleRP4Nodei(ptr noundef nonnull align 8 dereferenceable(8) %0, i32 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %15

11:                                               ; preds = %2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  call void @_Z11delete_headRP4Node(ptr noundef nonnull align 8 dereferenceable(8) %12)
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %14, ptr %3, align 8
  br label %50

15:                                               ; preds = %2
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  %19 = call noundef i32 @_Z11length_listP4Node(ptr noundef %18)
  %20 = icmp sge i32 %16, %19
  br i1 %20, label %21, label %27

21:                                               ; preds = %15
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = call noundef ptr @_Z11delete_tailRP4Node(ptr noundef nonnull align 8 dereferenceable(8) %22)
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %23, ptr %24, align 8, !tbaa !5
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load ptr, ptr %25, align 8, !tbaa !5
  store ptr %26, ptr %3, align 8
  br label %50

27:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store ptr null, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  store ptr %29, ptr %8, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %35, %27
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = load i32, ptr %5, align 4, !tbaa !9
  %33 = sub nsw i32 %32, 1
  %34 = icmp sle i32 %31, %33
  br i1 %34, label %35, label %42

35:                                               ; preds = %30
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %36, ptr %7, align 8, !tbaa !5
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = getelementptr inbounds %class.Node, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  store ptr %39, ptr %8, align 8, !tbaa !5
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %6, align 4, !tbaa !9
  br label %30, !llvm.loop !13

42:                                               ; preds = %30
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = getelementptr inbounds %class.Node, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = getelementptr inbounds %class.Node, ptr %46, i32 0, i32 1
  store ptr %45, ptr %47, align 8, !tbaa !11
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load ptr, ptr %48, align 8, !tbaa !5
  store ptr %49, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %50

50:                                               ; preds = %42, %21, %11
  %51 = load ptr, ptr %3, align 8
  ret ptr %51
}

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 = { mustprogress 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 #3 = { 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"}


; 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 nounwind uwtable
declare dso_local noundef i32 @_Z11length_listP4Node(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z11delete_headRP4Node(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z11delete_tailRP4Node(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z12deletemiddleRP4Nodei(ptr noundef nonnull align 8 dereferenceable(8) %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %4, label %6

4:                                                ; preds = %2
  tail call void @_Z11delete_headRP4Node(ptr noundef nonnull align 8 dereferenceable(8) %0)
  %5 = load ptr, ptr %0, align 8, !tbaa !5
  br label %25

6:                                                ; preds = %2
  %7 = load ptr, ptr %0, align 8, !tbaa !5
  %8 = tail call noundef i32 @_Z11length_listP4Node(ptr noundef %7)
  %.not = icmp sgt i32 %8, %1
  br i1 %.not, label %.preheader, label %10

.preheader:                                       ; preds = %6
  %.022 = load ptr, ptr %0, align 8, !tbaa !5
  %.not21.not23 = icmp sgt i32 %1, 0
  tail call void @llvm.assume(i1 %.not21.not23)
  %xtraiter = and i32 %1, 7
  %9 = icmp ult i32 %1, 8
  br i1 %9, label %._crit_edge.unr-lcssa, label %.preheader.new

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

10:                                               ; preds = %6
  %11 = tail call noundef ptr @_Z11delete_tailRP4Node(ptr noundef nonnull align 8 dereferenceable(8) %0)
  store ptr %11, ptr %0, align 8, !tbaa !5
  br label %25

.lr.ph:                                           ; preds = %.lr.ph, %.preheader.new
  %.025 = phi ptr [ %.022, %.preheader.new ], [ %.0.7, %.lr.ph ]
  %niter = phi i32 [ 0, %.preheader.new ], [ %niter.next.7, %.lr.ph ]
  %12 = getelementptr inbounds i8, ptr %.025, i64 8
  %.0 = load ptr, ptr %12, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %.0, i64 8
  %.0.1 = load ptr, ptr %13, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %.0.1, i64 8
  %.0.2 = load ptr, ptr %14, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %.0.2, i64 8
  %.0.3 = load ptr, ptr %15, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %.0.3, i64 8
  %.0.4 = load ptr, ptr %16, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %.0.4, i64 8
  %.0.5 = load ptr, ptr %17, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %.0.5, i64 8
  %.0.6 = load ptr, ptr %18, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %.0.6, i64 8
  %.0.7 = load ptr, ptr %19, 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.unr-lcssa, label %.lr.ph, !llvm.loop !9

._crit_edge.unr-lcssa:                            ; preds = %.lr.ph, %.preheader
  %.025.lcssa.ph = phi ptr [ poison, %.preheader ], [ %.0.6, %.lr.ph ]
  %.0.lcssa.ph = phi ptr [ poison, %.preheader ], [ %.0.7, %.lr.ph ]
  %.025.unr = phi ptr [ %.022, %.preheader ], [ %.0.7, %.lr.ph ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.unr-lcssa, %.lr.ph.epil
  %.025.epil = phi ptr [ %.0.epil, %.lr.ph.epil ], [ %.025.unr, %._crit_edge.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.unr-lcssa ]
  %20 = getelementptr inbounds i8, ptr %.025.epil, i64 8
  %.0.epil = load ptr, ptr %20, 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.ph.epil, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.unr-lcssa
  %.025.lcssa = phi ptr [ %.025.lcssa.ph, %._crit_edge.unr-lcssa ], [ %.025.epil, %.lr.ph.epil ]
  %.0.lcssa = phi ptr [ %.0.lcssa.ph, %._crit_edge.unr-lcssa ], [ %.0.epil, %.lr.ph.epil ]
  %21 = getelementptr inbounds i8, ptr %.025.lcssa, i64 8
  %22 = getelementptr inbounds i8, ptr %.0.lcssa, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  store ptr %23, ptr %21, align 8, !tbaa !13
  %24 = load ptr, ptr %0, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %._crit_edge, %10, %4
  %.019 = phi ptr [ %5, %4 ], [ %11, %10 ], [ %24, %._crit_edge ]
  ret ptr %.019
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #2

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" }
attributes #1 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }

!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 = !{!14, !6, i64 8}
!14 = !{!"_ZTS4Node", !15, i64 0, !6, i64 8}
!15 = !{!"int", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass