; LLVM IR from devparthgarg/Data-Structures-and-Algorithms :: 15. Binary Tree/17 Convert Binary Tree to Doubly Linked List/17.ll (Z7BTtoDLLP4Node)

; 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, ptr }

@pre = external dso_local global ptr, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z7BTtoDLLP4Node(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %5 = load ptr, ptr %3, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %9

7:                                                ; preds = %1
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %8, ptr %2, align 8
  br label %32

9:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %class.Node, ptr %10, i32 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = call noundef ptr @_Z7BTtoDLLP4Node(ptr noundef %12)
  store ptr %13, ptr %4, align 8, !tbaa !5
  %14 = load ptr, ptr @pre, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %18

16:                                               ; preds = %9
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %17, ptr %4, align 8, !tbaa !5
  br label %25

18:                                               ; preds = %9
  %19 = load ptr, ptr @pre, align 8, !tbaa !5
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %class.Node, ptr %20, i32 0, i32 1
  store ptr %19, ptr %21, align 8, !tbaa !9
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = load ptr, ptr @pre, align 8, !tbaa !5
  %24 = getelementptr inbounds %class.Node, ptr %23, i32 0, i32 2
  store ptr %22, ptr %24, align 8, !tbaa !12
  br label %25

25:                                               ; preds = %18, %16
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %26, ptr @pre, align 8, !tbaa !5
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %class.Node, ptr %27, i32 0, i32 2
  %29 = load ptr, ptr %28, align 8, !tbaa !12
  %30 = call noundef ptr @_Z7BTtoDLLP4Node(ptr noundef %29)
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %31, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %32

32:                                               ; preds = %25, %7
  %33 = load ptr, ptr %2, align 8
  ret ptr %33
}

; 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 = { mustprogress 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++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"_ZTS4Node", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 16}


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

@pre = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @_Z7BTtoDLLP4Node(ptr noundef %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %tailrecurse._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = tail call noundef ptr @_Z7BTtoDLLP4Node(ptr noundef %3)
  %5 = load ptr, ptr @pre, align 8, !tbaa !11
  %6 = icmp eq ptr %5, null
  br i1 %6, label %tailrecurse.peel, label %7

7:                                                ; preds = %.lr.ph.preheader
  store ptr %5, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %5, i64 16
  store ptr %0, ptr %8, align 8, !tbaa !12
  br label %tailrecurse.peel

tailrecurse.peel:                                 ; preds = %7, %.lr.ph.preheader
  %.0.peel = phi ptr [ %4, %7 ], [ %0, %.lr.ph.preheader ]
  store ptr %0, ptr @pre, align 8, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = icmp eq ptr %10, null
  br i1 %11, label %tailrecurse._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %tailrecurse.peel, %tailrecurse
  %.tr14 = phi ptr [ %20, %tailrecurse ], [ %10, %tailrecurse.peel ]
  %12 = getelementptr inbounds i8, ptr %.tr14, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %14 = tail call noundef ptr @_Z7BTtoDLLP4Node(ptr noundef %13)
  %15 = load ptr, ptr @pre, align 8, !tbaa !11
  %16 = icmp eq ptr %15, null
  br i1 %16, label %tailrecurse, label %17

17:                                               ; preds = %.lr.ph
  store ptr %15, ptr %12, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %15, i64 16
  store ptr %.tr14, ptr %18, align 8, !tbaa !12
  br label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph, %17
  store ptr %.tr14, ptr @pre, align 8, !tbaa !11
  %19 = getelementptr inbounds i8, ptr %.tr14, i64 16
  %20 = load ptr, ptr %19, align 8, !tbaa !12
  %21 = icmp eq ptr %20, null
  br i1 %21, label %tailrecurse._crit_edge, label %.lr.ph, !llvm.loop !13

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %tailrecurse.peel, %1
  %current.ret.tr13 = phi ptr [ null, %1 ], [ %.0.peel, %tailrecurse.peel ], [ %.0.peel, %tailrecurse ]
  ret ptr %current.ret.tr13
}

attributes #0 = { mustprogress nofree 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" }

!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, !10, i64 8}
!6 = !{!"_ZTS4Node", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!6, !10, i64 16}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.peeled.count", i32 1}


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