; LLVM IR from KishanBGajera/2024-366-Days-Of-DSA :: Data Structures/Linear/Linked List/Doubly Linked List/C (main)

; 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.DoublyLinkedList = type { ptr, i32 }
%struct.Node = type { i32, ptr, 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 @nodeAdded(ptr noundef, ptr noundef) #1

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

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

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

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

18:                                               ; preds = %3
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %24

21:                                               ; preds = %18
  %22 = load ptr, ptr %5, align 8, !tbaa !9
  %23 = load ptr, ptr %6, align 8, !tbaa !9
  call void @insertAtBeginning(ptr noundef %22, ptr noundef %23)
  br label %54

24:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %25 = load ptr, ptr %5, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.DoublyLinkedList, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  store ptr %27, ptr %8, align 8, !tbaa !9
  br label %28

28:                                               ; preds = %33, %24
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sub nsw i32 %30, 1
  %32 = icmp ne i32 %29, %31
  br i1 %32, label %33, label %39

33:                                               ; preds = %28
  %34 = load ptr, ptr %8, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.Node, ptr %34, i32 0, i32 2
  %36 = load ptr, ptr %35, align 8, !tbaa !14
  store ptr %36, ptr %8, align 8, !tbaa !9
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %7, align 4, !tbaa !5
  br label %28, !llvm.loop !16

39:                                               ; preds = %28
  %40 = load ptr, ptr %8, align 8, !tbaa !9
  %41 = load ptr, ptr %6, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.Node, ptr %41, i32 0, i32 1
  store ptr %40, ptr %42, align 8, !tbaa !18
  %43 = load ptr, ptr %8, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.Node, ptr %43, i32 0, i32 2
  %45 = load ptr, ptr %44, align 8, !tbaa !14
  %46 = load ptr, ptr %6, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.Node, ptr %46, i32 0, i32 2
  store ptr %45, ptr %47, align 8, !tbaa !14
  %48 = load ptr, ptr %6, align 8, !tbaa !9
  %49 = load ptr, ptr %8, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.Node, ptr %49, i32 0, i32 2
  store ptr %48, ptr %50, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %51

51:                                               ; preds = %39
  %52 = load ptr, ptr %5, align 8, !tbaa !9
  %53 = load ptr, ptr %6, align 8, !tbaa !9
  call void @nodeAdded(ptr noundef %52, ptr noundef %53)
  br label %54

54:                                               ; preds = %51, %21, %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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"DoublyLinkedList", !10, i64 0, !6, i64 8}
!13 = !{!12, !10, i64 0}
!14 = !{!15, !10, i64 16}
!15 = !{!"Node", !6, i64 0, !10, i64 8, !10, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!15, !10, i64 8}


; 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 @nodeAdded(ptr noundef, ptr noundef) local_unnamed_addr #0

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

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

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

6:                                                ; preds = %3
  tail call void @appendToList(ptr noundef nonnull %1, ptr noundef %2)
  br label %26

7:                                                ; preds = %3
  %8 = icmp eq i32 %0, 0
  br i1 %8, label %12, label %.preheader

.preheader:                                       ; preds = %7
  %9 = add nsw i32 %0, -1
  %.022 = load ptr, ptr %1, align 8, !tbaa !11
  %.not2123 = icmp eq i32 %9, 0
  br i1 %.not2123, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %10 = add i32 %0, -2
  %xtraiter = and i32 %9, 7
  %11 = icmp ult i32 %10, 7
  br i1 %11, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

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

12:                                               ; preds = %7
  tail call void @insertAtBeginning(ptr noundef nonnull %1, ptr noundef %2)
  br label %26

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.025 = phi ptr [ %.022, %.lr.ph.preheader.new ], [ %.0.7, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.7, %.lr.ph ]
  %13 = getelementptr inbounds i8, ptr %.025, i64 16
  %.0 = load ptr, ptr %13, align 8, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %.0, i64 16
  %.0.1 = load ptr, ptr %14, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %.0.1, i64 16
  %.0.2 = load ptr, ptr %15, align 8, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %.0.2, i64 16
  %.0.3 = load ptr, ptr %16, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %.0.3, i64 16
  %.0.4 = load ptr, ptr %17, align 8, !tbaa !11
  %18 = getelementptr inbounds i8, ptr %.0.4, i64 16
  %.0.5 = load ptr, ptr %18, align 8, !tbaa !11
  %19 = getelementptr inbounds i8, ptr %.0.5, i64 16
  %.0.6 = load ptr, ptr %19, align 8, !tbaa !11
  %20 = getelementptr inbounds i8, ptr %.0.6, i64 16
  %.0.7 = load ptr, ptr %20, 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.lcssa29.ph = phi ptr [ poison, %.lr.ph.preheader ], [ %.0.7, %.lr.ph ]
  %.025.unr = phi ptr [ %.022, %.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
  %.025.epil = phi ptr [ %.0.epil, %.lr.ph.epil ], [ %.025.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %21 = getelementptr inbounds i8, ptr %.025.epil, i64 16
  %.0.epil = load ptr, ptr %21, 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 [ %.022, %.preheader ], [ %.0.lcssa29.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.0.epil, %.lr.ph.epil ]
  %22 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr %.0.lcssa, ptr %22, align 8, !tbaa !16
  %23 = getelementptr inbounds i8, ptr %.0.lcssa, i64 16
  %24 = load ptr, ptr %23, align 8, !tbaa !18
  %25 = getelementptr inbounds i8, ptr %2, i64 16
  store ptr %24, ptr %25, align 8, !tbaa !18
  store ptr %2, ptr %23, align 8, !tbaa !18
  tail call void @nodeAdded(ptr noundef nonnull %1, ptr noundef %2)
  br label %26

26:                                               ; preds = %._crit_edge, %12, %6
  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 = !{!"DoublyLinkedList", !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, !7, i64 16}
!18 = !{!17, !7, i64 16}


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