; LLVM IR from kaynewbie/algorithm :: algorithm/CodingInterviews (question-23)

; 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.ListNode = type { i32, ptr }

; Function Attrs: nounwind uwtable
define dso_local ptr @entryNodeOfLoop(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %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
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = call ptr @meetingNode(ptr noundef %10)
  store ptr %11, ptr %4, align 8, !tbaa !5
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %58

15:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.ListNode, ptr %16, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  store ptr %18, ptr %7, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %23, %15
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = icmp ne ptr %20, %21
  br i1 %22, label %23, label %29

23:                                               ; preds = %19
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.ListNode, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  store ptr %26, ptr %7, align 8, !tbaa !5
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %6, align 4, !tbaa !9
  br label %19, !llvm.loop !13

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

32:                                               ; preds = %41, %29
  %33 = load i32, ptr %9, align 4, !tbaa !9
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  store i32 4, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %44

37:                                               ; preds = %32
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.ListNode, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !11
  store ptr %40, ptr %7, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %37
  %42 = load i32, ptr %9, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %9, align 4, !tbaa !9
  br label %32, !llvm.loop !15

44:                                               ; preds = %36
  br label %45

45:                                               ; preds = %49, %44
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = icmp ne ptr %46, %47
  br i1 %48, label %49, label %56

49:                                               ; preds = %45
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.ListNode, ptr %50, i32 0, i32 1
  %52 = load ptr, ptr %51, align 8, !tbaa !11
  store ptr %52, ptr %7, align 8, !tbaa !5
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.ListNode, ptr %53, i32 0, i32 1
  %55 = load ptr, ptr %54, align 8, !tbaa !11
  store ptr %55, ptr %8, align 8, !tbaa !5
  br label %45, !llvm.loop !16

56:                                               ; preds = %45
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %57, ptr %2, align 8
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %58

58:                                               ; preds = %56, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %59 = load ptr, ptr %2, align 8
  ret ptr %59
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @meetingNode(ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 8}
!12 = !{!"ListNode", !10, i64 0, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !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: nounwind uwtable
define dso_local ptr @entryNodeOfLoop(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call ptr @meetingNode(ptr noundef %0)
  %3 = icmp eq ptr %2, null
  br i1 %3, label %.loopexit, label %.preheader25

.preheader25:                                     ; preds = %1, %.preheader25
  %indvar = phi i32 [ %indvar.next, %.preheader25 ], [ 0, %1 ]
  %.020 = phi i32 [ %4, %.preheader25 ], [ 1, %1 ]
  %.pn = phi ptr [ %.019, %.preheader25 ], [ %2, %1 ]
  %.019.in = getelementptr inbounds i8, ptr %.pn, i64 8
  %.019 = load ptr, ptr %.019.in, align 8, !tbaa !5
  %.not = icmp eq ptr %.019, %2
  %4 = add nuw nsw i32 %.020, 1
  %indvar.next = add i32 %indvar, 1
  br i1 %.not, label %.preheader24.preheader, label %.preheader25, !llvm.loop !11

.preheader24.preheader:                           ; preds = %.preheader25
  %xtraiter = and i32 %.020, 7
  %5 = icmp ult i32 %indvar, 7
  br i1 %5, label %.preheader.unr-lcssa, label %.preheader24.preheader.new

.preheader24.preheader.new:                       ; preds = %.preheader24.preheader
  %unroll_iter = and i32 %.020, 2147483640
  br label %.preheader24

.preheader.unr-lcssa:                             ; preds = %.preheader24, %.preheader24.preheader
  %.lcssa35.ph = phi ptr [ poison, %.preheader24.preheader ], [ %23, %.preheader24 ]
  %.126.unr = phi ptr [ %0, %.preheader24.preheader ], [ %23, %.preheader24 ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader, label %.preheader24.epil

.preheader24.epil:                                ; preds = %.preheader.unr-lcssa, %.preheader24.epil
  %.126.epil = phi ptr [ %7, %.preheader24.epil ], [ %.126.unr, %.preheader.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.preheader24.epil ], [ 0, %.preheader.unr-lcssa ]
  %6 = getelementptr inbounds i8, ptr %.126.epil, i64 8
  %7 = load ptr, ptr %6, 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 %.preheader, label %.preheader24.epil, !llvm.loop !13

.preheader:                                       ; preds = %.preheader24.epil, %.preheader.unr-lcssa
  %.lcssa35 = phi ptr [ %.lcssa35.ph, %.preheader.unr-lcssa ], [ %7, %.preheader24.epil ]
  %.not2328 = icmp eq ptr %.lcssa35, %0
  br i1 %.not2328, label %.loopexit, label %.lr.ph

.preheader24:                                     ; preds = %.preheader24, %.preheader24.preheader.new
  %.126 = phi ptr [ %0, %.preheader24.preheader.new ], [ %23, %.preheader24 ]
  %niter = phi i32 [ 0, %.preheader24.preheader.new ], [ %niter.next.7, %.preheader24 ]
  %8 = getelementptr inbounds i8, ptr %.126, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %21, i64 8
  %23 = load ptr, ptr %22, 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 %.preheader.unr-lcssa, label %.preheader24, !llvm.loop !15

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.01830 = phi ptr [ %27, %.lr.ph ], [ %0, %.preheader ]
  %.229 = phi ptr [ %25, %.lr.ph ], [ %.lcssa35, %.preheader ]
  %24 = getelementptr inbounds i8, ptr %.229, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %.01830, i64 8
  %27 = load ptr, ptr %26, align 8, !tbaa !5
  %.not23 = icmp eq ptr %25, %27
  br i1 %.not23, label %.loopexit, label %.lr.ph, !llvm.loop !16

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %1
  %.021 = phi ptr [ null, %1 ], [ %.lcssa35, %.preheader ], [ %25, %.lr.ph ]
  ret ptr %.021
}

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

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


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