; LLVM IR from ImChong/Practice_LeetCode_C :: F02_Programming_Topics/F01_Data_Structure/02_Linked_List/09_MergeTwoLists/src (MergeTwoLists)

; 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 ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %12, label %10

10:                                               ; preds = %2
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %11, ptr %3, align 8
  br label %69

12:                                               ; preds = %2
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %17, label %15

15:                                               ; preds = %12
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %16, ptr %3, align 8
  br label %69

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %18 = call noalias ptr @malloc(i64 noundef 16) #4
  store ptr %18, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %19, ptr %7, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %50, %17
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %26

23:                                               ; preds = %20
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br label %26

26:                                               ; preds = %23, %20
  %27 = phi i1 [ false, %20 ], [ %25, %23 ]
  br i1 %27, label %28, label %54

28:                                               ; preds = %26
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.ListNode, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %30, align 8, !tbaa !9
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ListNode, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 8, !tbaa !9
  %35 = icmp slt i32 %31, %34
  br i1 %35, label %36, label %43

36:                                               ; preds = %28
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.ListNode, ptr %38, i32 0, i32 1
  store ptr %37, ptr %39, align 8, !tbaa !12
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.ListNode, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !12
  store ptr %42, ptr %4, align 8, !tbaa !5
  br label %50

43:                                               ; preds = %28
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.ListNode, ptr %45, i32 0, i32 1
  store ptr %44, ptr %46, align 8, !tbaa !12
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.ListNode, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !12
  store ptr %49, ptr %5, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %43, %36
  %51 = load ptr, ptr %7, 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 %7, align 8, !tbaa !5
  br label %20, !llvm.loop !13

54:                                               ; preds = %26
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %61

57:                                               ; preds = %54
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.ListNode, ptr %59, i32 0, i32 1
  store ptr %58, ptr %60, align 8, !tbaa !12
  br label %65

61:                                               ; preds = %54
  %62 = load ptr, ptr %5, 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, %57
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.ListNode, ptr %66, i32 0, i32 1
  %68 = load ptr, ptr %67, align 8, !tbaa !12
  store ptr %68, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %69

69:                                               ; preds = %65, %15, %10
  %70 = load ptr, ptr %3, align 8
  ret ptr %70
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; 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 allocsize(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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

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

; Function Attrs: nofree nounwind uwtable
define dso_local ptr @MergeTwoLists(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %26, label %3

3:                                                ; preds = %2
  %.not29 = icmp eq ptr %1, null
  br i1 %.not29, label %26, label %4

4:                                                ; preds = %3
  %5 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  br label %6

6:                                                ; preds = %4, %17
  %.032 = phi ptr [ %5, %4 ], [ %18, %17 ]
  %.02431 = phi ptr [ %1, %4 ], [ %.1, %17 ]
  %.02530 = phi ptr [ %0, %4 ], [ %.126, %17 ]
  %7 = load i32, ptr %.02530, align 8, !tbaa !5
  %8 = load i32, ptr %.02431, align 8, !tbaa !5
  %9 = icmp slt i32 %7, %8
  %10 = getelementptr inbounds i8, ptr %.032, i64 8
  br i1 %9, label %11, label %14

11:                                               ; preds = %6
  store ptr %.02530, ptr %10, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %.02530, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  br label %17

14:                                               ; preds = %6
  store ptr %.02431, ptr %10, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %.02431, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  br label %17

17:                                               ; preds = %14, %11
  %18 = phi ptr [ %.02530, %11 ], [ %.02431, %14 ]
  %.126 = phi ptr [ %13, %11 ], [ %.02530, %14 ]
  %.1 = phi ptr [ %.02431, %11 ], [ %16, %14 ]
  %19 = icmp ne ptr %.126, null
  %20 = icmp ne ptr %.1, null
  %21 = select i1 %19, i1 %20, i1 false
  br i1 %21, label %6, label %22, !llvm.loop !12

22:                                               ; preds = %17
  %spec.select = select i1 %19, ptr %.126, ptr %.1
  %23 = getelementptr inbounds i8, ptr %18, i64 8
  store ptr %spec.select, ptr %23, align 8
  %24 = getelementptr inbounds i8, ptr %5, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  br label %26

26:                                               ; preds = %3, %2, %22
  %.023 = phi ptr [ %25, %22 ], [ %1, %2 ], [ %0, %3 ]
  ret ptr %.023
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

attributes #0 = { nofree 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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }

!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,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass