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

; 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 i32 @findKthNodeFormTail(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %10, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 1, ptr %8, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %25, %2
  %12 = load i32, ptr %8, align 4, !tbaa !9
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = sub nsw i32 %13, 1
  %15 = icmp sle i32 %12, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %11
  store i32 2, ptr %9, align 4
  br label %28

17:                                               ; preds = %11
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %28

21:                                               ; preds = %17
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.ListNode, ptr %22, i32 0, i32 1
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  store ptr %24, ptr %6, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %21
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %8, align 4, !tbaa !9
  br label %11, !llvm.loop !13

28:                                               ; preds = %20, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %29 = load i32, ptr %9, align 4
  switch i32 %29, label %48 [
    i32 2, label %30
  ]

30:                                               ; preds = %28
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %31, ptr %7, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %37, %30
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.ListNode, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %44

37:                                               ; preds = %32
  %38 = load ptr, ptr %6, 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 %6, align 8, !tbaa !5
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.ListNode, ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 8, !tbaa !11
  store ptr %43, ptr %7, align 8, !tbaa !5
  br label %32, !llvm.loop !15

44:                                               ; preds = %32
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.ListNode, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %46, align 8, !tbaa !16
  store i32 %47, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %48

48:                                               ; preds = %44, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %49 = load i32, ptr %3, align 4
  ret i32 %49
}

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

; 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 = !{!12, !10, i64 0}


; 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: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @findKthNodeFormTail(ptr noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not.not.not19 = icmp sgt i32 %1, 1
  br i1 %.not.not.not19, label %.lr.ph, label %.critedge.preheader

.critedge.preheader:                              ; preds = %6, %2
  %.014.lcssa = phi ptr [ %0, %2 ], [ %8, %6 ]
  %3 = getelementptr inbounds i8, ptr %.014.lcssa, i64 8
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %.not22 = icmp eq ptr %4, null
  br i1 %.not22, label %.critedge._crit_edge, label %.critedge

.lr.ph:                                           ; preds = %2, %6
  %.01221 = phi i32 [ %9, %6 ], [ 1, %2 ]
  %.01420 = phi ptr [ %8, %6 ], [ %0, %2 ]
  %5 = icmp eq ptr %.01420, null
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %.lr.ph
  %7 = getelementptr inbounds i8, ptr %.01420, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = add nuw nsw i32 %.01221, 1
  %exitcond.not = icmp eq i32 %9, %1
  br i1 %exitcond.not, label %.critedge.preheader, label %.lr.ph, !llvm.loop !11

.critedge:                                        ; preds = %.critedge.preheader, %.critedge
  %10 = phi ptr [ %14, %.critedge ], [ %4, %.critedge.preheader ]
  %.01323 = phi ptr [ %12, %.critedge ], [ %0, %.critedge.preheader ]
  %11 = getelementptr inbounds i8, ptr %.01323, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %10, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  %.not = icmp eq ptr %14, null
  br i1 %.not, label %.critedge._crit_edge, label %.critedge, !llvm.loop !13

.critedge._crit_edge:                             ; preds = %.critedge, %.critedge.preheader
  %.013.lcssa = phi ptr [ %0, %.critedge.preheader ], [ %12, %.critedge ]
  %15 = load i32, ptr %.013.lcssa, align 8, !tbaa !14
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %.critedge._crit_edge
  %.116 = phi i32 [ %15, %.critedge._crit_edge ], [ -1, %.lr.ph ]
  ret i32 %.116
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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, !12}
!14 = !{!6, !7, i64 0}


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