; LLVM IR from begeekmyfriend/leetcode :: 0117_populating_next_right_pointers_in_each_node_ii (connect)

; 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.list_head = type { ptr, ptr }
%struct.queue_node = type { ptr, %struct.list_head }
%struct.Node = type { i32, ptr, ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local ptr @connect(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca %struct.list_head, align 8
  %5 = alloca %struct.list_head, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %16

14:                                               ; preds = %1
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %15, ptr %2, align 8
  br label %90

16:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 16, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #3
  call void @INIT_LIST_HEAD(ptr noundef %4)
  call void @INIT_LIST_HEAD(ptr noundef %5)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = call ptr @node_new(ptr noundef %17, ptr noundef %4)
  store ptr %18, ptr %8, align 8, !tbaa !5
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.queue_node, ptr %19, i32 0, i32 1
  call void @list_add_tail(ptr noundef %20, ptr noundef %5)
  br label %21

21:                                               ; preds = %87, %16
  %22 = call i32 @list_empty(ptr noundef %5)
  %23 = icmp ne i32 %22, 0
  %24 = xor i1 %23, true
  br i1 %24, label %25, label %88

25:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store ptr null, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %26 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %26, ptr %10, align 4, !tbaa !9
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %84, %25
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %31, label %87

31:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %32 = getelementptr inbounds %struct.list_head, ptr %5, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !11
  %34 = getelementptr inbounds i8, ptr %33, i64 sub (i64 0, i64 ptrtoint (ptr getelementptr inbounds (%struct.queue_node, ptr null, i32 0, i32 1) to i64))
  store ptr %34, ptr %11, align 8, !tbaa !5
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %43

37:                                               ; preds = %31
  %38 = load ptr, ptr %11, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.queue_node, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !13
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.Node, ptr %41, i32 0, i32 3
  store ptr %40, ptr %42, align 8, !tbaa !15
  br label %43

43:                                               ; preds = %37, %31
  %44 = load ptr, ptr %11, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.queue_node, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !13
  store ptr %46, ptr %9, align 8, !tbaa !5
  %47 = load ptr, ptr %11, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.queue_node, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !13
  %50 = getelementptr inbounds %struct.Node, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !17
  %52 = icmp ne ptr %51, null
  br i1 %52, label %53, label %64

53:                                               ; preds = %43
  %54 = load ptr, ptr %11, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.queue_node, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !13
  %57 = getelementptr inbounds %struct.Node, ptr %56, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !17
  %59 = call ptr @node_new(ptr noundef %58, ptr noundef %4)
  store ptr %59, ptr %8, align 8, !tbaa !5
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.queue_node, ptr %60, i32 0, i32 1
  call void @list_add_tail(ptr noundef %61, ptr noundef %5)
  %62 = load i32, ptr %7, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %7, align 4, !tbaa !9
  br label %64

64:                                               ; preds = %53, %43
  %65 = load ptr, ptr %11, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.queue_node, ptr %65, i32 0, i32 0
  %67 = load ptr, ptr %66, align 8, !tbaa !13
  %68 = getelementptr inbounds %struct.Node, ptr %67, i32 0, i32 2
  %69 = load ptr, ptr %68, align 8, !tbaa !18
  %70 = icmp ne ptr %69, null
  br i1 %70, label %71, label %82

71:                                               ; preds = %64
  %72 = load ptr, ptr %11, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.queue_node, ptr %72, i32 0, i32 0
  %74 = load ptr, ptr %73, align 8, !tbaa !13
  %75 = getelementptr inbounds %struct.Node, ptr %74, i32 0, i32 2
  %76 = load ptr, ptr %75, align 8, !tbaa !18
  %77 = call ptr @node_new(ptr noundef %76, ptr noundef %4)
  store ptr %77, ptr %8, align 8, !tbaa !5
  %78 = load ptr, ptr %8, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.queue_node, ptr %78, i32 0, i32 1
  call void @list_add_tail(ptr noundef %79, ptr noundef %5)
  %80 = load i32, ptr %7, align 4, !tbaa !9
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %7, align 4, !tbaa !9
  br label %82

82:                                               ; preds = %71, %64
  %83 = load ptr, ptr %11, align 8, !tbaa !5
  call void @node_free(ptr noundef %83, ptr noundef %4)
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %84

84:                                               ; preds = %82
  %85 = load i32, ptr %6, align 4, !tbaa !9
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %6, align 4, !tbaa !9
  br label %27, !llvm.loop !19

87:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %21, !llvm.loop !21

88:                                               ; preds = %21
  %89 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %89, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %4) #3
  br label %90

90:                                               ; preds = %88, %14
  %91 = load ptr, ptr %2, align 8
  ret ptr %91
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @INIT_LIST_HEAD(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @node_new(ptr noundef, ptr noundef) #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_add_tail(ptr noundef, ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @list_empty(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @node_free(ptr noundef, ptr noundef) #0

; 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 = { inlinehint 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 #3 = { 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 0}
!12 = !{!"list_head", !6, i64 0, !6, i64 8}
!13 = !{!14, !6, i64 0}
!14 = !{!"queue_node", !6, i64 0, !12, i64 8}
!15 = !{!16, !6, i64 24}
!16 = !{!"Node", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!17 = !{!16, !6, i64 8}
!18 = !{!16, !6, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}


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

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @connect(ptr noundef returned %0) local_unnamed_addr #0 {
  %2 = alloca %struct.list_head, align 8
  %3 = alloca %struct.list_head, align 8
  %4 = icmp eq ptr %0, null
  br i1 %4, label %32, label %5

5:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #3
  call void @INIT_LIST_HEAD(ptr noundef nonnull %2)
  call void @INIT_LIST_HEAD(ptr noundef nonnull %3)
  %6 = call ptr @node_new(ptr noundef nonnull %0, ptr noundef nonnull %2)
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  call void @list_add_tail(ptr noundef nonnull %7, ptr noundef nonnull %3)
  %8 = call i32 @list_empty(ptr noundef nonnull %3)
  %.not33 = icmp eq i32 %8, 0
  br i1 %.not33, label %.preheader, label %._crit_edge

.loopexit:                                        ; preds = %30, %.preheader
  %.1.lcssa = phi i32 [ %.02434, %.preheader ], [ %.3, %30 ]
  %9 = call i32 @list_empty(ptr noundef nonnull %3)
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %.preheader, label %._crit_edge, !llvm.loop !5

.preheader:                                       ; preds = %5, %.loopexit
  %.02434 = phi i32 [ %.1.lcssa, %.loopexit ], [ 1, %5 ]
  %10 = icmp sgt i32 %.02434, 0
  br i1 %10, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %30
  %.02232 = phi i32 [ %31, %30 ], [ 0, %.preheader ]
  %.02331 = phi ptr [ %.pre, %30 ], [ null, %.preheader ]
  %.130 = phi i32 [ %.3, %30 ], [ %.02434, %.preheader ]
  %11 = load ptr, ptr %3, align 8, !tbaa !7
  %12 = getelementptr inbounds i8, ptr %11, i64 -8
  %.not27 = icmp eq ptr %.02331, null
  %.pre = load ptr, ptr %12, align 8, !tbaa !12
  br i1 %.not27, label %15, label %13

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.02331, i64 24
  store ptr %.pre, ptr %14, align 8, !tbaa !14
  br label %15

15:                                               ; preds = %13, %.lr.ph
  %16 = getelementptr inbounds i8, ptr %.pre, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !17
  %.not28 = icmp eq ptr %17, null
  br i1 %.not28, label %22, label %18

18:                                               ; preds = %15
  %19 = call ptr @node_new(ptr noundef nonnull %17, ptr noundef nonnull %2)
  %20 = getelementptr inbounds i8, ptr %19, i64 8
  call void @list_add_tail(ptr noundef nonnull %20, ptr noundef nonnull %3)
  %21 = add nsw i32 %.130, 1
  %.pre35 = load ptr, ptr %12, align 8, !tbaa !12
  br label %22

22:                                               ; preds = %18, %15
  %23 = phi ptr [ %.pre35, %18 ], [ %.pre, %15 ]
  %.2 = phi i32 [ %21, %18 ], [ %.130, %15 ]
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %25 = load ptr, ptr %24, align 8, !tbaa !18
  %.not29 = icmp eq ptr %25, null
  br i1 %.not29, label %30, label %26

26:                                               ; preds = %22
  %27 = call ptr @node_new(ptr noundef nonnull %25, ptr noundef nonnull %2)
  %28 = getelementptr inbounds i8, ptr %27, i64 8
  call void @list_add_tail(ptr noundef nonnull %28, ptr noundef nonnull %3)
  %29 = add nsw i32 %.2, 1
  br label %30

30:                                               ; preds = %26, %22
  %.3 = phi i32 [ %29, %26 ], [ %.2, %22 ]
  call void @node_free(ptr noundef nonnull %12, ptr noundef nonnull %2)
  %31 = add nuw nsw i32 %.02232, 1
  %exitcond.not = icmp eq i32 %31, %.02434
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !19

._crit_edge:                                      ; preds = %.loopexit, %5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #3
  br label %32

32:                                               ; preds = %1, %._crit_edge
  ret ptr %0
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @INIT_LIST_HEAD(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden ptr @node_new(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_add_tail(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @list_empty(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @node_free(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !9, i64 0}
!8 = !{!"list_head", !9, i64 0, !9, i64 8}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!13, !9, i64 0}
!13 = !{!"queue_node", !9, i64 0, !8, i64 8}
!14 = !{!15, !9, i64 24}
!15 = !{!"Node", !16, i64 0, !9, i64 8, !9, i64 16, !9, i64 24}
!16 = !{!"int", !10, i64 0}
!17 = !{!15, !9, i64 8}
!18 = !{!15, !9, i64 16}
!19 = distinct !{!19, !6}


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