; LLVM IR from JessonYue/LeetCodeLearning :: Array/wuchao/linkedlist (MergeTwoLinkedList)

; 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 @mergeTwoLists(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.ListNode, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %15

13:                                               ; preds = %2
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %14, ptr %3, align 8
  br label %76

15:                                               ; preds = %2
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %20

18:                                               ; preds = %15
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %19, ptr %3, align 8
  br label %76

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store ptr %6, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %21, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %22, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %23, ptr %10, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %54, %20
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %30

27:                                               ; preds = %24
  %28 = load ptr, ptr %10, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br label %30

30:                                               ; preds = %27, %24
  %31 = phi i1 [ false, %24 ], [ %29, %27 ]
  br i1 %31, label %32, label %58

32:                                               ; preds = %30
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.ListNode, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 8, !tbaa !9
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.ListNode, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !9
  %39 = icmp slt i32 %35, %38
  br i1 %39, label %40, label %47

40:                                               ; preds = %32
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.ListNode, ptr %42, i32 0, i32 1
  store ptr %41, ptr %43, align 8, !tbaa !12
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.ListNode, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !12
  store ptr %46, ptr %9, align 8, !tbaa !5
  br label %54

47:                                               ; preds = %32
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.ListNode, ptr %49, i32 0, i32 1
  store ptr %48, ptr %50, align 8, !tbaa !12
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.ListNode, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !12
  store ptr %53, ptr %10, align 8, !tbaa !5
  br label %54

54:                                               ; preds = %47, %40
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.ListNode, ptr %55, i32 0, i32 1
  %57 = load ptr, ptr %56, align 8, !tbaa !12
  store ptr %57, ptr %7, align 8, !tbaa !5
  br label %24, !llvm.loop !13

58:                                               ; preds = %30
  %59 = load ptr, ptr %9, align 8, !tbaa !5
  %60 = icmp ne ptr %59, null
  br i1 %60, label %61, label %65

61:                                               ; preds = %58
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.ListNode, ptr %63, i32 0, i32 1
  store ptr %62, ptr %64, align 8, !tbaa !12
  br label %65

65:                                               ; preds = %61, %58
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  %67 = icmp ne ptr %66, null
  br i1 %67, label %68, label %72

68:                                               ; preds = %65
  %69 = load ptr, ptr %10, align 8, !tbaa !5
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.ListNode, ptr %70, i32 0, i32 1
  store ptr %69, ptr %71, align 8, !tbaa !12
  br label %72

72:                                               ; preds = %68, %65
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.ListNode, ptr %73, i32 0, i32 1
  %75 = load ptr, ptr %74, align 8, !tbaa !12
  store ptr %75, ptr %3, align 8
  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 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #2
  br label %76

76:                                               ; preds = %72, %18, %13
  %77 = load ptr, ptr %3, align 8
  ret ptr %77
}

; 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, !11, i64 0}
!10 = !{!"ListNode", !11, i64 0, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"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"

%struct.ListNode = type { i32, ptr }

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local ptr @mergeTwoLists(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca %struct.ListNode, align 8
  %4 = icmp eq ptr %0, null
  br i1 %4, label %33, label %5

5:                                                ; preds = %2
  %6 = icmp eq ptr %1, null
  br i1 %6, label %33, label %7

7:                                                ; preds = %5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #2
  br label %8

8:                                                ; preds = %7, %19
  %.035 = phi ptr [ %1, %7 ], [ %.1, %19 ]
  %.02634 = phi ptr [ %0, %7 ], [ %.127, %19 ]
  %.02933 = phi ptr [ %3, %7 ], [ %20, %19 ]
  %9 = load i32, ptr %.02634, align 8, !tbaa !5
  %10 = load i32, ptr %.035, align 8, !tbaa !5
  %11 = icmp slt i32 %9, %10
  %12 = getelementptr inbounds i8, ptr %.02933, i64 8
  br i1 %11, label %13, label %16

13:                                               ; preds = %8
  store ptr %.02634, ptr %12, align 8, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %.02634, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  br label %19

16:                                               ; preds = %8
  store ptr %.035, ptr %12, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %.035, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  br label %19

19:                                               ; preds = %16, %13
  %20 = phi ptr [ %.02634, %13 ], [ %.035, %16 ]
  %.127 = phi ptr [ %15, %13 ], [ %.02634, %16 ]
  %.1 = phi ptr [ %.035, %13 ], [ %18, %16 ]
  %21 = icmp ne ptr %.127, null
  %22 = icmp ne ptr %.1, null
  %23 = select i1 %21, i1 %22, i1 false
  br i1 %23, label %8, label %24, !llvm.loop !12

24:                                               ; preds = %19
  br i1 %21, label %25, label %27

25:                                               ; preds = %24
  %26 = getelementptr inbounds i8, ptr %20, i64 8
  store ptr %.127, ptr %26, align 8, !tbaa !11
  br label %27

27:                                               ; preds = %25, %24
  br i1 %22, label %28, label %30

28:                                               ; preds = %27
  %29 = getelementptr inbounds i8, ptr %20, i64 8
  store ptr %.1, ptr %29, align 8, !tbaa !11
  br label %30

30:                                               ; preds = %28, %27
  %31 = getelementptr inbounds i8, ptr %3, i64 8
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #2
  br label %33

33:                                               ; preds = %5, %2, %30
  %.028 = phi ptr [ %32, %30 ], [ %1, %2 ], [ %0, %5 ]
  ret ptr %.028
}

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

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

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, 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" }
attributes #1 = { mustprogress 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, !7, i64 0}
!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 = !{!6, !10, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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