; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 10. Linked List/cpp/Linked List Complete.ll (Z19getIntersectionNodeP4nodeS0_)

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

%class.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: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6lengthP4node(ptr noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_Z19getIntersectionNodeP4nodeS0_(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = call noundef i32 @_Z6lengthP4node(ptr noundef %12)
  store i32 %13, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = call noundef i32 @_Z6lengthP4node(ptr noundef %14)
  store i32 %15, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = icmp sgt i32 %16, %17
  br i1 %18, label %19, label %25

19:                                               ; preds = %2
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = sub nsw i32 %20, %21
  store i32 %22, ptr %8, align 4, !tbaa !9
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %23, ptr %9, align 8, !tbaa !5
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %24, ptr %10, align 8, !tbaa !5
  br label %31

25:                                               ; preds = %2
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = sub nsw i32 %26, %27
  store i32 %28, ptr %8, align 4, !tbaa !9
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %29, ptr %9, align 8, !tbaa !5
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %30, ptr %10, align 8, !tbaa !5
  br label %31

31:                                               ; preds = %25, %19
  br label %32

32:                                               ; preds = %43, %31
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %46

35:                                               ; preds = %32
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = getelementptr inbounds %class.node, ptr %36, i32 0, i32 1
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  store ptr %38, ptr %9, align 8, !tbaa !5
  %39 = load ptr, ptr %9, align 8, !tbaa !5
  %40 = icmp eq ptr %39, null
  br i1 %40, label %41, label %43

41:                                               ; preds = %35
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %42, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %70

43:                                               ; preds = %35
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = add nsw i32 %44, -1
  store i32 %45, ptr %8, align 4, !tbaa !9
  br label %32, !llvm.loop !13

46:                                               ; preds = %32
  br label %47

47:                                               ; preds = %61, %46
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = icmp ne ptr %48, null
  br i1 %49, label %50, label %53

50:                                               ; preds = %47
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = icmp ne ptr %51, null
  br label %53

53:                                               ; preds = %50, %47
  %54 = phi i1 [ false, %47 ], [ %52, %50 ]
  br i1 %54, label %55, label %68

55:                                               ; preds = %53
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = load ptr, ptr %10, align 8, !tbaa !5
  %58 = icmp eq ptr %56, %57
  br i1 %58, label %59, label %61

59:                                               ; preds = %55
  %60 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %60, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %70

61:                                               ; preds = %55
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = getelementptr inbounds %class.node, ptr %62, i32 0, i32 1
  %64 = load ptr, ptr %63, align 8, !tbaa !11
  store ptr %64, ptr %9, align 8, !tbaa !5
  %65 = load ptr, ptr %10, align 8, !tbaa !5
  %66 = getelementptr inbounds %class.node, ptr %65, i32 0, i32 1
  %67 = load ptr, ptr %66, align 8, !tbaa !11
  store ptr %67, ptr %10, align 8, !tbaa !5
  br label %47, !llvm.loop !15

68:                                               ; preds = %53
  %69 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %69, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %70

70:                                               ; preds = %68, %59, %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %71 = load ptr, ptr %3, align 8
  ret ptr %71
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS4node", !10, i64 0, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !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: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6lengthP4node(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_Z19getIntersectionNodeP4nodeS0_(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = tail call noundef i32 @_Z6lengthP4node(ptr noundef %0)
  %4 = tail call noundef i32 @_Z6lengthP4node(ptr noundef %1)
  %5 = icmp sgt i32 %3, %4
  %.025 = select i1 %5, ptr %0, ptr %1
  %.0 = select i1 %5, ptr %1, ptr %0
  %.not42 = icmp eq i32 %3, %4
  br i1 %.not42, label %.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %6 = sub nsw i32 %3, %4
  %.027 = tail call i32 @llvm.abs.i32(i32 %6, i1 true)
  br label %.lr.ph

7:                                                ; preds = %.lr.ph
  %8 = add nsw i32 %.12843, -1
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %.preheader, label %.lr.ph, !llvm.loop !5

.preheader:                                       ; preds = %7, %2
  %.126.lcssa = phi ptr [ %.025, %2 ], [ %14, %7 ]
  %9 = icmp eq ptr %.126.lcssa, null
  %10 = icmp eq ptr %.0, null
  %11 = icmp eq ptr %.126.lcssa, %.0
  %12 = or i1 %10, %11
  %or.cond45 = or i1 %9, %12
  br i1 %or.cond45, label %.loopexit, label %.lr.ph48

.lr.ph:                                           ; preds = %.lr.ph.preheader, %7
  %.12644 = phi ptr [ %14, %7 ], [ %.025, %.lr.ph.preheader ]
  %.12843 = phi i32 [ %8, %7 ], [ %.027, %.lr.ph.preheader ]
  %13 = getelementptr inbounds i8, ptr %.12644, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !7
  %15 = icmp eq ptr %14, null
  br i1 %15, label %.loopexit, label %7

.lr.ph48:                                         ; preds = %.preheader, %.lr.ph48
  %.147 = phi ptr [ %19, %.lr.ph48 ], [ %.0, %.preheader ]
  %.246 = phi ptr [ %17, %.lr.ph48 ], [ %.126.lcssa, %.preheader ]
  %16 = getelementptr inbounds i8, ptr %.246, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !7
  %18 = getelementptr inbounds i8, ptr %.147, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !7
  %20 = icmp eq ptr %17, null
  %21 = icmp eq ptr %19, null
  %22 = icmp eq ptr %17, %19
  %23 = or i1 %21, %22
  %or.cond = select i1 %20, i1 true, i1 %23
  br i1 %or.cond, label %.loopexit, label %.lr.ph48, !llvm.loop !13

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph48, %.preheader
  %.029 = phi ptr [ %.126.lcssa, %.preheader ], [ %17, %.lr.ph48 ], [ null, %.lr.ph ]
  ret ptr %.029
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

attributes #0 = { mustprogress 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 speculatable willreturn memory(none) }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !12, i64 8}
!8 = !{!"_ZTS4node", !9, i64 0, !12, i64 8}
!9 = !{!"int", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"any pointer", !10, i64 0}
!13 = distinct !{!13, !6}


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