; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 10. Linked List/c lang (merge_common_LL)

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

@head3 = external dso_local global ptr, align 8
@head4 = external dso_local global ptr, align 8

; 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: nounwind uwtable
declare dso_local void @insert_r(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @merge(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %11, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  br label %12

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

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

20:                                               ; preds = %32, %15
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %36

23:                                               ; preds = %20
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.node, ptr %24, i32 0, i32 0
  %26 = load i32, ptr %25, align 8, !tbaa !9
  %27 = load i32, ptr %7, align 4, !tbaa !12
  %28 = icmp eq i32 %26, %27
  br i1 %28, label %29, label %31

29:                                               ; preds = %23
  %30 = load i32, ptr %7, align 4, !tbaa !12
  call void @insert_r(ptr noundef @head3, i32 noundef %30)
  store i32 1, ptr %8, align 4, !tbaa !12
  br label %36

31:                                               ; preds = %23
  br label %32

32:                                               ; preds = %31
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.node, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  store ptr %35, ptr %6, align 8, !tbaa !5
  br label %20, !llvm.loop !14

36:                                               ; preds = %29, %20
  %37 = load i32, ptr %8, align 4, !tbaa !12
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %39, label %43

39:                                               ; preds = %36
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.node, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !9
  call void @insert_r(ptr noundef @head4, i32 noundef %42)
  br label %43

43:                                               ; preds = %39, %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %44

44:                                               ; preds = %43
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.node, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  store ptr %47, ptr %5, align 8, !tbaa !5
  br label %12, !llvm.loop !16

48:                                               ; preds = %12
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %49, ptr %5, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %81, %48
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = icmp ne ptr %51, null
  br i1 %52, label %53, label %85

53:                                               ; preds = %50
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.node, ptr %54, i32 0, i32 0
  %56 = load i32, ptr %55, align 8, !tbaa !9
  store i32 %56, ptr %9, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !12
  %57 = load ptr, ptr @head3, align 8, !tbaa !5
  store ptr %57, ptr %6, align 8, !tbaa !5
  br label %58

58:                                               ; preds = %69, %53
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = icmp ne ptr %59, null
  br i1 %60, label %61, label %73

61:                                               ; preds = %58
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.node, ptr %62, i32 0, i32 0
  %64 = load i32, ptr %63, align 8, !tbaa !9
  %65 = load i32, ptr %9, align 4, !tbaa !12
  %66 = icmp eq i32 %64, %65
  br i1 %66, label %67, label %68

67:                                               ; preds = %61
  store i32 1, ptr %10, align 4, !tbaa !12
  br label %73

68:                                               ; preds = %61
  br label %69

69:                                               ; preds = %68
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.node, ptr %70, i32 0, i32 1
  %72 = load ptr, ptr %71, align 8, !tbaa !13
  store ptr %72, ptr %6, align 8, !tbaa !5
  br label %58, !llvm.loop !17

73:                                               ; preds = %67, %58
  %74 = load i32, ptr %10, align 4, !tbaa !12
  %75 = icmp eq i32 %74, 0
  br i1 %75, label %76, label %80

76:                                               ; preds = %73
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.node, ptr %77, i32 0, i32 0
  %79 = load i32, ptr %78, align 8, !tbaa !9
  call void @insert_r(ptr noundef @head4, i32 noundef %79)
  br label %80

80:                                               ; preds = %76, %73
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %81

81:                                               ; preds = %80
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.node, ptr %82, i32 0, i32 1
  %84 = load ptr, ptr %83, align 8, !tbaa !13
  store ptr %84, ptr %5, align 8, !tbaa !5
  br label %50, !llvm.loop !18

85:                                               ; preds = %50
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"node", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = distinct !{!17, !15}
!18 = distinct !{!18, !15}


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

@head3 = external dso_local global ptr, align 8
@head4 = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @insert_r(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @merge(ptr noundef readonly %0, ptr noundef readonly %1) local_unnamed_addr #0 {
  %.not33 = icmp eq ptr %0, null
  br i1 %.not33, label %.preheader, label %.lr.ph35

.lr.ph35:                                         ; preds = %2
  %.not2831 = icmp eq ptr %1, null
  br i1 %.not2831, label %.critedge.us, label %.lr.ph

.critedge.us:                                     ; preds = %.lr.ph35, %.critedge.us
  %.02134.us = phi ptr [ %5, %.critedge.us ], [ %0, %.lr.ph35 ]
  %3 = load i32, ptr %.02134.us, align 8, !tbaa !5
  tail call void @insert_r(ptr noundef nonnull @head4, i32 noundef %3)
  %4 = getelementptr inbounds i8, ptr %.02134.us, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !11
  %.not.us = icmp eq ptr %5, null
  br i1 %.not.us, label %.preheader, label %.critedge.us, !llvm.loop !12

.preheader:                                       ; preds = %..critedge_crit_edge, %.critedge.us, %2
  %.not2640 = icmp eq ptr %1, null
  br i1 %.not2640, label %._crit_edge, label %.lr.ph42.preheader

.lr.ph42.preheader:                               ; preds = %.preheader
  %.12436.pre44 = load ptr, ptr @head3, align 8, !tbaa !14
  br label %.lr.ph42

.lr.ph:                                           ; preds = %.lr.ph35, %..critedge_crit_edge
  %.02134 = phi ptr [ %14, %..critedge_crit_edge ], [ %0, %.lr.ph35 ]
  %6 = load i32, ptr %.02134, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %.lr.ph, %10
  %.02332 = phi ptr [ %1, %.lr.ph ], [ %12, %10 ]
  %8 = load i32, ptr %.02332, align 8, !tbaa !5
  %9 = icmp eq i32 %8, %6
  br i1 %9, label %..critedge_crit_edge, label %10

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %.02332, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %.not28 = icmp eq ptr %12, null
  br i1 %.not28, label %..critedge_crit_edge, label %7, !llvm.loop !15

..critedge_crit_edge:                             ; preds = %10, %7
  %head3.sink = phi ptr [ @head3, %7 ], [ @head4, %10 ]
  tail call void @insert_r(ptr noundef nonnull %head3.sink, i32 noundef %6)
  %13 = getelementptr inbounds i8, ptr %.02134, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  %.not = icmp eq ptr %14, null
  br i1 %.not, label %.preheader, label %.lr.ph, !llvm.loop !12

.lr.ph42:                                         ; preds = %.lr.ph42.preheader, %.loopexit
  %.12436 = phi ptr [ %.1243645, %.loopexit ], [ %.12436.pre44, %.lr.ph42.preheader ]
  %.141 = phi ptr [ %21, %.loopexit ], [ %1, %.lr.ph42.preheader ]
  %15 = load i32, ptr %.141, align 8, !tbaa !5
  %.not2737 = icmp eq ptr %.12436, null
  br i1 %.not2737, label %.critedge30, label %.lr.ph39

16:                                               ; preds = %.lr.ph39
  %17 = getelementptr inbounds i8, ptr %.12438, i64 8
  %.124 = load ptr, ptr %17, align 8, !tbaa !14
  %.not27 = icmp eq ptr %.124, null
  br i1 %.not27, label %.critedge30, label %.lr.ph39, !llvm.loop !16

.lr.ph39:                                         ; preds = %.lr.ph42, %16
  %.12438 = phi ptr [ %.124, %16 ], [ %.12436, %.lr.ph42 ]
  %18 = load i32, ptr %.12438, align 8, !tbaa !5
  %19 = icmp eq i32 %18, %15
  br i1 %19, label %.loopexit, label %16

.critedge30:                                      ; preds = %16, %.lr.ph42
  tail call void @insert_r(ptr noundef nonnull @head4, i32 noundef %15)
  %.12436.pre = load ptr, ptr @head3, align 8, !tbaa !14
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph39, %.critedge30
  %.1243645 = phi ptr [ %.12436.pre, %.critedge30 ], [ %.12436, %.lr.ph39 ]
  %20 = getelementptr inbounds i8, ptr %.141, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %.not26 = icmp eq ptr %21, null
  br i1 %.not26, label %._crit_edge, label %.lr.ph42, !llvm.loop !17

._crit_edge:                                      ; preds = %.loopexit, %.preheader
  ret void
}

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, !7, i64 0}
!6 = !{!"node", !7, i64 0, !10, i64 8, !10, i64 16}
!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"}
!14 = !{!10, !10, i64 0}
!15 = distinct !{!15, !13}
!16 = distinct !{!16, !13}
!17 = distinct !{!17, !13}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass