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

; 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.BinaryStdOut = type { i8, i32, %"class.std::basic_ofstream" }
%"class.std::basic_ofstream" = type { %"class.std::basic_ostream.base", %"class.std::basic_filebuf", %"class.std::basic_ios" }
%"class.std::basic_ostream.base" = type { ptr }
%"class.std::basic_filebuf" = type { %"class.std::basic_streambuf", %union.pthread_mutex_t, %"class.std::__basic_file", i32, %struct.__mbstate_t, %struct.__mbstate_t, %struct.__mbstate_t, ptr, i64, i8, i8, i8, i8, ptr, ptr, i8, ptr, ptr, i64, ptr, ptr }
%"class.std::basic_streambuf" = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, %"class.std::locale" }
%"class.std::locale" = type { ptr }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%"class.std::__basic_file" = type <{ ptr, i8, [7 x i8] }>
%struct.__mbstate_t = type { i32, %union.anon.0 }
%union.anon.0 = type { i32 }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN12BinaryStdOut11clearBufferEv(ptr noundef nonnull align 8 dereferenceable(520)) #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN12BinaryStdOut8writeBitEb(ptr noundef nonnull align 8 dereferenceable(520) %0, i1 noundef zeroext %1) #0 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  %5 = zext i1 %1 to i8
  store i8 %5, ptr %4, align 1, !tbaa !9
  %6 = load ptr, ptr %3, align 8
  %7 = getelementptr inbounds %class.BinaryStdOut, ptr %6, i32 0, i32 0
  %8 = load i8, ptr %7, align 8, !tbaa !11
  %9 = sext i8 %8 to i32
  %10 = shl i32 %9, 1
  %11 = trunc i32 %10 to i8
  store i8 %11, ptr %7, align 8, !tbaa !11
  %12 = load i8, ptr %4, align 1, !tbaa !9, !range !23, !noundef !24
  %13 = trunc i8 %12 to i1
  br i1 %13, label %14, label %20

14:                                               ; preds = %2
  %15 = getelementptr inbounds %class.BinaryStdOut, ptr %6, i32 0, i32 0
  %16 = load i8, ptr %15, align 8, !tbaa !11
  %17 = sext i8 %16 to i32
  %18 = or i32 %17, 1
  %19 = trunc i32 %18 to i8
  store i8 %19, ptr %15, align 8, !tbaa !11
  br label %20

20:                                               ; preds = %14, %2
  %21 = getelementptr inbounds %class.BinaryStdOut, ptr %6, i32 0, i32 1
  %22 = load i32, ptr %21, align 4, !tbaa !25
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %21, align 4, !tbaa !25
  %24 = getelementptr inbounds %class.BinaryStdOut, ptr %6, i32 0, i32 1
  %25 = load i32, ptr %24, align 4, !tbaa !25
  %26 = icmp eq i32 %25, 8
  br i1 %26, label %27, label %28

27:                                               ; preds = %20
  call void @_ZN12BinaryStdOut11clearBufferEv(ptr noundef nonnull align 8 dereferenceable(520) %6)
  br label %28

28:                                               ; preds = %27, %20
  ret void
}

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

!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, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"_ZTS12BinaryStdOut", !7, i64 0, !13, i64 4, !14, i64 8}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_ZTSSt14basic_ofstreamIcSt11char_traitsIcEE", !15, i64 0, !16, i64 8}
!15 = !{!"_ZTSSo"}
!16 = !{!"_ZTSSt13basic_filebufIcSt11char_traitsIcEE", !17, i64 0, !7, i64 64, !19, i64 104, !20, i64 120, !21, i64 124, !21, i64 132, !21, i64 140, !6, i64 152, !22, i64 160, !10, i64 168, !10, i64 169, !10, i64 170, !7, i64 171, !6, i64 176, !6, i64 184, !10, i64 192, !6, i64 200, !6, i64 208, !22, i64 216, !6, i64 224, !6, i64 232}
!17 = !{!"_ZTSSt15basic_streambufIcSt11char_traitsIcEE", !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !18, i64 56}
!18 = !{!"_ZTSSt6locale", !6, i64 0}
!19 = !{!"_ZTSSt12__basic_fileIcE", !6, i64 0, !10, i64 8}
!20 = !{!"_ZTSSt13_Ios_Openmode", !7, i64 0}
!21 = !{!"_ZTS11__mbstate_t", !13, i64 0, !7, i64 4}
!22 = !{!"long", !7, i64 0}
!23 = !{i8 0, i8 2}
!24 = !{}
!25 = !{!12, !13, i64 4}


; 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: mustprogress uwtable
declare dso_local void @_ZN12BinaryStdOut11clearBufferEv(ptr noundef nonnull align 8 dereferenceable(520)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN12BinaryStdOut8writeBitEb(ptr noundef nonnull align 8 dereferenceable(520) %0, i1 noundef zeroext %1) local_unnamed_addr #0 align 2 {
  %3 = load i8, ptr %0, align 8, !tbaa !5
  %4 = shl i8 %3, 1
  %5 = zext i1 %1 to i8
  %spec.select = or disjoint i8 %4, %5
  store i8 %spec.select, ptr %0, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !21
  %8 = add nsw i32 %7, 1
  store i32 %8, ptr %6, align 4, !tbaa !21
  %9 = icmp eq i32 %8, 8
  br i1 %9, label %10, label %11

10:                                               ; preds = %2
  tail call void @_ZN12BinaryStdOut11clearBufferEv(ptr noundef nonnull align 8 dereferenceable(520) %0)
  br label %11

11:                                               ; preds = %10, %2
  ret void
}

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

!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 = !{!"_ZTS12BinaryStdOut", !7, i64 0, !9, i64 4, !10, i64 8}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"_ZTSSt14basic_ofstreamIcSt11char_traitsIcEE", !11, i64 0, !12, i64 8}
!11 = !{!"_ZTSSo"}
!12 = !{!"_ZTSSt13basic_filebufIcSt11char_traitsIcEE", !13, i64 0, !7, i64 64, !16, i64 104, !18, i64 120, !19, i64 124, !19, i64 132, !19, i64 140, !14, i64 152, !20, i64 160, !17, i64 168, !17, i64 169, !17, i64 170, !7, i64 171, !14, i64 176, !14, i64 184, !17, i64 192, !14, i64 200, !14, i64 208, !20, i64 216, !14, i64 224, !14, i64 232}
!13 = !{!"_ZTSSt15basic_streambufIcSt11char_traitsIcEE", !14, i64 8, !14, i64 16, !14, i64 24, !14, i64 32, !14, i64 40, !14, i64 48, !15, i64 56}
!14 = !{!"any pointer", !7, i64 0}
!15 = !{!"_ZTSSt6locale", !14, i64 0}
!16 = !{!"_ZTSSt12__basic_fileIcE", !14, i64 0, !17, i64 8}
!17 = !{!"bool", !7, i64 0}
!18 = !{!"_ZTSSt13_Ios_Openmode", !7, i64 0}
!19 = !{!"_ZTS11__mbstate_t", !9, i64 0, !7, i64 4}
!20 = !{!"long", !7, i64 0}
!21 = !{!6, !9, i64 4}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass