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

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

@.str.2 = external hidden unnamed_addr constant [34 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [43 x i8], align 1

; 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

declare i32 @printf(ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
declare dso_local void @freeNodeMemory(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @removedFromSLL(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @removeFromLinkedList(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  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) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.LinkedList, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  store ptr %10, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.Node, ptr %11, i32 0, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  store ptr %13, ptr %6, align 8, !tbaa !5
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.Node, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !15
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %25

19:                                               ; preds = %2
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.LinkedList, ptr %21, i32 0, i32 0
  store ptr %20, ptr %22, align 8, !tbaa !11
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  call void @removedFromSLL(ptr noundef %23)
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  call void @freeNodeMemory(ptr noundef %24)
  store i32 1, ptr %7, align 4
  br label %61

25:                                               ; preds = %2
  br label %26

26:                                               ; preds = %37, %25
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %35

29:                                               ; preds = %26
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Node, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 8, !tbaa !15
  %33 = load i32, ptr %4, align 4, !tbaa !9
  %34 = icmp ne i32 %32, %33
  br label %35

35:                                               ; preds = %29, %26
  %36 = phi i1 [ false, %26 ], [ %34, %29 ]
  br i1 %36, label %37, label %42

37:                                               ; preds = %35
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %38, ptr %5, align 8, !tbaa !5
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.Node, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !13
  store ptr %41, ptr %6, align 8, !tbaa !5
  br label %26, !llvm.loop !16

42:                                               ; preds = %35
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = icmp ne ptr %43, null
  br i1 %44, label %45, label %57

45:                                               ; preds = %42
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.Node, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !13
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.Node, ptr %49, i32 0, i32 1
  store ptr %48, ptr %50, align 8, !tbaa !13
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.Node, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !15
  %54 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %53)
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  call void @removedFromSLL(ptr noundef %55)
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  call void @freeNodeMemory(ptr noundef %56)
  br label %60

57:                                               ; preds = %42
  %58 = load i32, ptr %4, align 4, !tbaa !9
  %59 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %58)
  br label %60

60:                                               ; preds = %57, %45
  store i32 0, ptr %7, align 4
  br label %61

61:                                               ; preds = %60, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %62 = load i32, ptr %7, align 4
  switch i32 %62, label %64 [
    i32 0, label %63
    i32 1, label %63
  ]

63:                                               ; preds = %61, %61
  ret void

64:                                               ; preds = %61
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"LinkedList", !6, i64 0, !10, i64 8}
!13 = !{!14, !6, i64 8}
!14 = !{!"Node", !10, i64 0, !6, i64 8}
!15 = !{!14, !10, i64 0}
!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"

@.str.2 = external hidden unnamed_addr constant [34 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [43 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @removeFromLinkedList(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !11
  %6 = load i32, ptr %3, align 8, !tbaa !13
  %7 = icmp eq i32 %6, %1
  br i1 %7, label %9, label %.preheader

.preheader:                                       ; preds = %2
  %.not24 = icmp eq ptr %5, null
  br i1 %.not24, label %.critedge21, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %8 = load i32, ptr %5, align 8, !tbaa !13
  %.not2034 = icmp eq i32 %8, %1
  br i1 %.not2034, label %.critedge, label %.lr.ph36

9:                                                ; preds = %2
  store ptr %5, ptr %0, align 8, !tbaa !5
  tail call void @removedFromSLL(ptr noundef nonnull %0)
  tail call void @freeNodeMemory(ptr noundef nonnull %3)
  br label %18

.lr.ph:                                           ; preds = %.lr.ph36
  %10 = load i32, ptr %12, align 8, !tbaa !13
  %.not20 = icmp eq i32 %10, %1
  br i1 %.not20, label %.critedge, label %.lr.ph36, !llvm.loop !14

.lr.ph36:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %.02635 = phi ptr [ %12, %.lr.ph ], [ %5, %.lr.ph.preheader ]
  %11 = getelementptr inbounds i8, ptr %.02635, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %.critedge21, label %.lr.ph, !llvm.loop !14

.critedge:                                        ; preds = %.lr.ph, %.lr.ph.preheader
  %.026.lcssa = phi ptr [ %5, %.lr.ph.preheader ], [ %12, %.lr.ph ]
  %.01925.lcssa = phi ptr [ %3, %.lr.ph.preheader ], [ %.02635, %.lr.ph ]
  %13 = getelementptr inbounds i8, ptr %.026.lcssa, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %.01925.lcssa, i64 8
  store ptr %14, ptr %15, align 8, !tbaa !11
  %16 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %1)
  tail call void @removedFromSLL(ptr noundef nonnull %0)
  tail call void @freeNodeMemory(ptr noundef nonnull %.026.lcssa)
  br label %18

.critedge21:                                      ; preds = %.lr.ph36, %.preheader
  %17 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %1)
  br label %18

18:                                               ; preds = %.critedge, %.critedge21, %9
  ret void
}

attributes #0 = { nofree nounwind "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 = { 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, !7, i64 0}
!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 = !{!12, !7, i64 8}
!12 = !{!"Node", !10, i64 0, !7, i64 8}
!13 = !{!12, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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