; LLVM IR from KishanBGajera/2024-366-Days-Of-DSA :: Algorithms/Two Pointers - Hare and Tortoise Algorithm/Cycle or Loop Detection (insertAt)

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

%struct.LinkedList = type { ptr, i32 }
%struct.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: nounwind uwtable
declare dso_local void @addedToSLL(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @addToLinkedList(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @insertAt(ptr noundef %0, i32 noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.LinkedList, ptr %9, i32 0, i32 1
  %11 = load i32, ptr %10, align 8, !tbaa !11
  %12 = sub nsw i32 %11, 1
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %15, label %18

15:                                               ; preds = %3
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  call void @addToLinkedList(ptr noundef %16, ptr noundef %17)
  br label %42

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.LinkedList, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  store ptr %21, ptr %8, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %26, %18
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = icmp ne i32 %23, %24
  br i1 %25, label %26, label %32

26:                                               ; preds = %22
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %7, align 4, !tbaa !9
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.Node, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  store ptr %31, ptr %8, align 8, !tbaa !5
  br label %22, !llvm.loop !16

32:                                               ; preds = %22
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Node, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !14
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.Node, ptr %36, i32 0, i32 1
  store ptr %35, ptr %37, align 8, !tbaa !14
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.Node, ptr %39, i32 0, i32 1
  store ptr %38, ptr %40, align 8, !tbaa !14
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  call void @addedToSLL(ptr noundef %41)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %42

42:                                               ; preds = %32, %15
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"LinkedList", !6, i64 0, !10, i64 8}
!13 = !{!12, !6, i64 0}
!14 = !{!15, !6, i64 8}
!15 = !{!"Node", !10, i64 0, !6, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"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: nounwind uwtable
declare dso_local void @addedToSLL(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @addToLinkedList(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @insertAt(ptr noundef %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %.not = icmp sgt i32 %5, %1
  br i1 %.not, label %.preheader, label %7

.preheader:                                       ; preds = %3
  %.016 = load ptr, ptr %0, align 8, !tbaa !11
  %.not1517 = icmp eq i32 %1, 0
  br i1 %.not1517, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %xtraiter = and i32 %1, 7
  %6 = icmp ult i32 %1, 8
  br i1 %6, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

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

7:                                                ; preds = %3
  tail call void @addToLinkedList(ptr noundef nonnull %0, ptr noundef %2)
  br label %20

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.019 = phi ptr [ %.016, %.lr.ph.preheader.new ], [ %.0.7, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.7, %.lr.ph ]
  %8 = getelementptr inbounds i8, ptr %.019, i64 8
  %.0 = load ptr, ptr %8, align 8, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %.0, i64 8
  %.0.1 = load ptr, ptr %9, align 8, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %.0.1, i64 8
  %.0.2 = load ptr, ptr %10, align 8, !tbaa !11
  %11 = getelementptr inbounds i8, ptr %.0.2, i64 8
  %.0.3 = load ptr, ptr %11, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %.0.3, i64 8
  %.0.4 = load ptr, ptr %12, align 8, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %.0.4, i64 8
  %.0.5 = load ptr, ptr %13, align 8, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %.0.5, i64 8
  %.0.6 = load ptr, ptr %14, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %.0.6, i64 8
  %.0.7 = load ptr, ptr %15, align 8, !tbaa !11
  %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.ph, !llvm.loop !12

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.0.lcssa23.ph = phi ptr [ poison, %.lr.ph.preheader ], [ %.0.7, %.lr.ph ]
  %.019.unr = phi ptr [ %.016, %.lr.ph.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.loopexit.unr-lcssa, %.lr.ph.epil
  %.019.epil = phi ptr [ %.0.epil, %.lr.ph.epil ], [ %.019.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %16 = getelementptr inbounds i8, ptr %.019.epil, i64 8
  %.0.epil = load ptr, ptr %16, align 8, !tbaa !11
  %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 !14

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %.preheader
  %.0.lcssa = phi ptr [ %.016, %.preheader ], [ %.0.lcssa23.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.0.epil, %.lr.ph.epil ]
  %17 = getelementptr inbounds i8, ptr %.0.lcssa, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !16
  %19 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr %18, ptr %19, align 8, !tbaa !16
  store ptr %2, ptr %17, align 8, !tbaa !16
  tail call void @addedToSLL(ptr noundef nonnull %0)
  br label %20

20:                                               ; preds = %._crit_edge, %7
  ret void
}

attributes #0 = { 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" }

!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 = !{!"LinkedList", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.unroll.disable"}
!16 = !{!17, !7, i64 8}
!17 = !{!"Node", !10, i64 0, !7, i64 8}


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