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

; 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.BinaryStdIn = type { %"class.std::basic_ifstream", i8, i32 }
%"class.std::basic_ifstream" = type { %"class.std::basic_istream.base", %"class.std::basic_filebuf", %"class.std::basic_ios" }
%"class.std::basic_istream.base" = type { ptr, i64 }
%"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 noundef zeroext i1 @_ZN11BinaryStdIn10fillBufferEv(ptr noundef nonnull align 8 dereferenceable(528)) #0 align 2

; 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

; Function Attrs: mustprogress uwtable
define dso_local noundef signext i8 @_ZN11BinaryStdIn8readCharEv(ptr noundef nonnull align 8 dereferenceable(528) %0) #0 align 2 {
  %2 = alloca i8, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8
  %9 = getelementptr inbounds %class.BinaryStdIn, ptr %8, i32 0, i32 2
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = icmp eq i32 %10, 8
  br i1 %11, label %12, label %17

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #2
  %13 = getelementptr inbounds %class.BinaryStdIn, ptr %8, i32 0, i32 1
  %14 = load i8, ptr %13, align 8, !tbaa !22
  store i8 %14, ptr %4, align 1, !tbaa !23
  %15 = call noundef zeroext i1 @_ZN11BinaryStdIn10fillBufferEv(ptr noundef nonnull align 8 dereferenceable(528) %8)
  %16 = load i8, ptr %4, align 1, !tbaa !23
  store i8 %16, ptr %2, align 1
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #2
  br label %44

17:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #2
  %18 = getelementptr inbounds %class.BinaryStdIn, ptr %8, i32 0, i32 1
  %19 = load i8, ptr %18, align 8, !tbaa !22
  store i8 %19, ptr %5, align 1, !tbaa !23
  %20 = getelementptr inbounds %class.BinaryStdIn, ptr %8, i32 0, i32 2
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = sub nsw i32 8, %21
  %23 = load i8, ptr %5, align 1, !tbaa !23
  %24 = sext i8 %23 to i32
  %25 = shl i32 %24, %22
  %26 = trunc i32 %25 to i8
  store i8 %26, ptr %5, align 1, !tbaa !23
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %27 = getelementptr inbounds %class.BinaryStdIn, ptr %8, i32 0, i32 2
  %28 = load i32, ptr %27, align 4, !tbaa !9
  store i32 %28, ptr %6, align 4, !tbaa !24
  %29 = call noundef zeroext i1 @_ZN11BinaryStdIn10fillBufferEv(ptr noundef nonnull align 8 dereferenceable(528) %8)
  %30 = load i32, ptr %6, align 4, !tbaa !24
  %31 = getelementptr inbounds %class.BinaryStdIn, ptr %8, i32 0, i32 2
  store i32 %30, ptr %31, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  %32 = getelementptr inbounds %class.BinaryStdIn, ptr %8, i32 0, i32 1
  %33 = load i8, ptr %32, align 8, !tbaa !22
  store i8 %33, ptr %7, align 1, !tbaa !23
  %34 = load i8, ptr %7, align 1, !tbaa !23
  %35 = zext i8 %34 to i32
  %36 = getelementptr inbounds %class.BinaryStdIn, ptr %8, i32 0, i32 2
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = ashr i32 %35, %37
  %39 = load i8, ptr %5, align 1, !tbaa !23
  %40 = sext i8 %39 to i32
  %41 = or i32 %40, %38
  %42 = trunc i32 %41 to i8
  store i8 %42, ptr %5, align 1, !tbaa !23
  %43 = load i8, ptr %5, align 1, !tbaa !23
  store i8 %43, ptr %2, align 1
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #2
  br label %44

44:                                               ; preds = %17, %12
  %45 = load i8, ptr %2, align 1
  ret i8 %45
}

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


; 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 noundef zeroext i1 @_ZN11BinaryStdIn10fillBufferEv(ptr noundef nonnull align 8 dereferenceable(528)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef signext i8 @_ZN11BinaryStdIn8readCharEv(ptr noundef nonnull align 8 dereferenceable(528) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 524
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp eq i32 %3, 8
  %5 = getelementptr inbounds i8, ptr %0, i64 520
  %6 = load i8, ptr %5, align 8, !tbaa !21
  br i1 %4, label %7, label %9

7:                                                ; preds = %1
  %8 = tail call noundef zeroext i1 @_ZN11BinaryStdIn10fillBufferEv(ptr noundef nonnull align 8 dereferenceable(528) %0)
  br label %19

9:                                                ; preds = %1
  %10 = sub nsw i32 8, %3
  %11 = zext i8 %6 to i32
  %12 = shl i32 %11, %10
  %13 = tail call noundef zeroext i1 @_ZN11BinaryStdIn10fillBufferEv(ptr noundef nonnull align 8 dereferenceable(528) %0)
  store i32 %3, ptr %2, align 4, !tbaa !5
  %14 = load i8, ptr %5, align 8, !tbaa !21
  %15 = zext i8 %14 to i32
  %16 = lshr i32 %15, %3
  %17 = or i32 %16, %12
  %18 = trunc i32 %17 to i8
  br label %19

19:                                               ; preds = %9, %7
  %.0 = phi i8 [ %6, %7 ], [ %18, %9 ]
  ret i8 %.0
}

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, !20, i64 524}
!6 = !{!"_ZTS11BinaryStdIn", !7, i64 0, !10, i64 520, !20, i64 524}
!7 = !{!"_ZTSSt14basic_ifstreamIcSt11char_traitsIcEE", !8, i64 0, !12, i64 16}
!8 = !{!"_ZTSSi", !9, i64 8}
!9 = !{!"long", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"_ZTSSt13basic_filebufIcSt11char_traitsIcEE", !13, i64 0, !10, i64 64, !16, i64 104, !18, i64 120, !19, i64 124, !19, i64 132, !19, i64 140, !14, i64 152, !9, i64 160, !17, i64 168, !17, i64 169, !17, i64 170, !10, i64 171, !14, i64 176, !14, i64 184, !17, i64 192, !14, i64 200, !14, i64 208, !9, 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", !10, i64 0}
!15 = !{!"_ZTSSt6locale", !14, i64 0}
!16 = !{!"_ZTSSt12__basic_fileIcE", !14, i64 0, !17, i64 8}
!17 = !{!"bool", !10, i64 0}
!18 = !{!"_ZTSSt13_Ios_Openmode", !10, i64 0}
!19 = !{!"_ZTS11__mbstate_t", !20, i64 0, !10, i64 4}
!20 = !{!"int", !10, i64 0}
!21 = !{!6, !10, i64 520}


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