; LLVM IR from gzc/CLRS :: C16-Greedy-Algorithms/huffman/HUFFMAN.ll (ZN7HUFFMAN9writeTrieEP5Htree)

; 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.HUFFMAN = type { ptr, %"class.std::map", ptr, ptr }
%"class.std::map" = type { %"class.std::_Rb_tree" }
%"class.std::_Rb_tree" = type { %"struct.std::_Rb_tree<char, std::pair<const char, std::vector<bool>>, std::_Select1st<std::pair<const char, std::vector<bool>>>, std::less<char>>::_Rb_tree_impl" }
%"struct.std::_Rb_tree<char, std::pair<const char, std::vector<bool>>, std::_Select1st<std::pair<const char, std::vector<bool>>>, std::less<char>>::_Rb_tree_impl" = type { [8 x i8], %"struct.std::_Rb_tree_header" }
%"struct.std::_Rb_tree_header" = type { %"struct.std::_Rb_tree_node_base", i64 }
%"struct.std::_Rb_tree_node_base" = type { i32, ptr, ptr, ptr }
%struct.Htree = type <{ ptr, ptr, i32, i8, [3 x i8] }>

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef zeroext i1 @_ZL6isLeafP5Htree(ptr noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7HUFFMAN9writeTrieEP5Htree(ptr noundef nonnull align 8 dereferenceable(72) %0, ptr noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %5 = load ptr, ptr %3, align 8
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = call noundef zeroext i1 @_ZL6isLeafP5Htree(ptr noundef %6)
  br i1 %7, label %8, label %16

8:                                                ; preds = %2
  %9 = getelementptr inbounds %class.HUFFMAN, ptr %5, i32 0, i32 2
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  call void @_ZN12BinaryStdOut5writeEb(ptr noundef nonnull align 8 dereferenceable(520) %10, i1 noundef zeroext true)
  %11 = getelementptr inbounds %class.HUFFMAN, ptr %5, i32 0, i32 2
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Htree, ptr %13, i32 0, i32 3
  %15 = load i8, ptr %14, align 4, !tbaa !20
  call void @_ZN12BinaryStdOut5writeEc(ptr noundef nonnull align 8 dereferenceable(520) %12, i8 noundef signext %15)
  br label %25

16:                                               ; preds = %2
  %17 = getelementptr inbounds %class.HUFFMAN, ptr %5, i32 0, i32 2
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  call void @_ZN12BinaryStdOut5writeEb(ptr noundef nonnull align 8 dereferenceable(520) %18, i1 noundef zeroext false)
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.Htree, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !23
  call void @_ZN7HUFFMAN9writeTrieEP5Htree(ptr noundef nonnull align 8 dereferenceable(72) %5, ptr noundef %21)
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.Htree, ptr %22, i32 0, i32 1
  %24 = load ptr, ptr %23, align 8, !tbaa !24
  call void @_ZN7HUFFMAN9writeTrieEP5Htree(ptr noundef nonnull align 8 dereferenceable(72) %5, ptr noundef %24)
  br label %25

25:                                               ; preds = %16, %8
  ret void
}

declare void @_ZN12BinaryStdOut5writeEb(ptr noundef nonnull align 8 dereferenceable(520), i1 noundef zeroext) #2

declare void @_ZN12BinaryStdOut5writeEc(ptr noundef nonnull align 8 dereferenceable(520), i8 noundef signext) #2

attributes #0 = { inlinehint mustprogress 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 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 = { "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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 56}
!10 = !{!"_ZTS7HUFFMAN", !6, i64 0, !11, i64 8, !6, i64 56, !6, i64 64}
!11 = !{!"_ZTSSt3mapIcSt6vectorIbSaIbEESt4lessIcESaISt4pairIKcS2_EEE", !12, i64 0}
!12 = !{!"_ZTSSt8_Rb_treeIcSt4pairIKcSt6vectorIbSaIbEEESt10_Select1stIS5_ESt4lessIcESaIS5_EE", !13, i64 0}
!13 = !{!"_ZTSNSt8_Rb_treeIcSt4pairIKcSt6vectorIbSaIbEEESt10_Select1stIS5_ESt4lessIcESaIS5_EE13_Rb_tree_implIS9_Lb1EEE", !14, i64 0, !16, i64 8}
!14 = !{!"_ZTSSt20_Rb_tree_key_compareISt4lessIcEE", !15, i64 0}
!15 = !{!"_ZTSSt4lessIcE"}
!16 = !{!"_ZTSSt15_Rb_tree_header", !17, i64 0, !19, i64 32}
!17 = !{!"_ZTSSt18_Rb_tree_node_base", !18, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!18 = !{!"_ZTSSt14_Rb_tree_color", !7, i64 0}
!19 = !{!"long", !7, i64 0}
!20 = !{!21, !7, i64 20}
!21 = !{!"_ZTS5Htree", !6, i64 0, !6, i64 8, !22, i64 16, !7, i64 20}
!22 = !{!"int", !7, i64 0}
!23 = !{!21, !6, i64 0}
!24 = !{!21, !6, i64 8}


; 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: inlinehint mustprogress nounwind uwtable
declare hidden noundef zeroext i1 @_ZL6isLeafP5Htree(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7HUFFMAN9writeTrieEP5Htree(ptr nocapture noundef nonnull readonly align 8 dereferenceable(72) %0, ptr noundef %1) local_unnamed_addr #1 align 2 {
  %3 = tail call noundef zeroext i1 @_ZL6isLeafP5Htree(ptr noundef %1)
  br i1 %3, label %tailrecurse._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 56
  br label %tailrecurse

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %2
  %.tr5.lcssa = phi ptr [ %1, %2 ], [ %13, %tailrecurse ]
  %5 = getelementptr inbounds i8, ptr %0, i64 56
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  tail call void @_ZN12BinaryStdOut5writeEb(ptr noundef nonnull align 8 dereferenceable(520) %6, i1 noundef zeroext true)
  %7 = load ptr, ptr %5, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %.tr5.lcssa, i64 20
  %9 = load i8, ptr %8, align 4, !tbaa !19
  tail call void @_ZN12BinaryStdOut5writeEc(ptr noundef nonnull align 8 dereferenceable(520) %7, i8 noundef signext %9)
  ret void

tailrecurse:                                      ; preds = %.lr.ph, %tailrecurse
  %.tr56 = phi ptr [ %1, %.lr.ph ], [ %13, %tailrecurse ]
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  tail call void @_ZN12BinaryStdOut5writeEb(ptr noundef nonnull align 8 dereferenceable(520) %10, i1 noundef zeroext false)
  %11 = load ptr, ptr %.tr56, align 8, !tbaa !22
  tail call void @_ZN7HUFFMAN9writeTrieEP5Htree(ptr noundef nonnull align 8 dereferenceable(72) %0, ptr noundef %11)
  %12 = getelementptr inbounds i8, ptr %.tr56, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !23
  %14 = tail call noundef zeroext i1 @_ZL6isLeafP5Htree(ptr noundef %13)
  br i1 %14, label %tailrecurse._crit_edge, label %tailrecurse
}

declare void @_ZN12BinaryStdOut5writeEb(ptr noundef nonnull align 8 dereferenceable(520), i1 noundef zeroext) local_unnamed_addr #2

declare void @_ZN12BinaryStdOut5writeEc(ptr noundef nonnull align 8 dereferenceable(520), i8 noundef signext) local_unnamed_addr #2

attributes #0 = { inlinehint mustprogress 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 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 = { "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 56}
!6 = !{!"_ZTS7HUFFMAN", !7, i64 0, !10, i64 8, !7, i64 56, !7, i64 64}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSSt3mapIcSt6vectorIbSaIbEESt4lessIcESaISt4pairIKcS2_EEE", !11, i64 0}
!11 = !{!"_ZTSSt8_Rb_treeIcSt4pairIKcSt6vectorIbSaIbEEESt10_Select1stIS5_ESt4lessIcESaIS5_EE", !12, i64 0}
!12 = !{!"_ZTSNSt8_Rb_treeIcSt4pairIKcSt6vectorIbSaIbEEESt10_Select1stIS5_ESt4lessIcESaIS5_EE13_Rb_tree_implIS9_Lb1EEE", !13, i64 0, !15, i64 8}
!13 = !{!"_ZTSSt20_Rb_tree_key_compareISt4lessIcEE", !14, i64 0}
!14 = !{!"_ZTSSt4lessIcE"}
!15 = !{!"_ZTSSt15_Rb_tree_header", !16, i64 0, !18, i64 32}
!16 = !{!"_ZTSSt18_Rb_tree_node_base", !17, i64 0, !7, i64 8, !7, i64 16, !7, i64 24}
!17 = !{!"_ZTSSt14_Rb_tree_color", !8, i64 0}
!18 = !{!"long", !8, i64 0}
!19 = !{!20, !8, i64 20}
!20 = !{!"_ZTS5Htree", !7, i64 0, !7, i64 8, !21, i64 16, !8, i64 20}
!21 = !{!"int", !8, i64 0}
!22 = !{!20, !7, i64 0}
!23 = !{!20, !7, i64 8}


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