; LLVM IR from intel/qpl :: sources/middle-layer/common/bit_buffer.ll (ZN3qpl2ml10bit_reader9peek_bitsEh)

; 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.qpl::ml::bit_reader" = type <{ ptr, ptr, ptr, i8, [7 x i8], i64, i8, i8, i8, [5 x i8] }>

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3qpl2ml10bit_reader11load_bufferEh(ptr noundef nonnull align 8 dereferenceable(43), i8 noundef zeroext) #0 align 2

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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(ptr noundef) #2

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i16 @_ZN3qpl2ml10bit_reader9peek_bitsEh(ptr noundef nonnull align 8 dereferenceable(43) %0, i8 noundef zeroext %1) #0 align 2 personality ptr @__gxx_personality_v0 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i8 %1, ptr %4, align 1, !tbaa !9
  %10 = load ptr, ptr %3, align 8
  %11 = load i8, ptr %4, align 1, !tbaa !9
  %12 = zext i8 %11 to i32
  %13 = getelementptr inbounds %"class.qpl::ml::bit_reader", ptr %10, i32 0, i32 6
  %14 = load i8, ptr %13, align 8, !tbaa !10
  %15 = zext i8 %14 to i32
  %16 = icmp sgt i32 %12, %15
  br i1 %16, label %17, label %32

17:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 8, ptr %6, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %18 = load i8, ptr %4, align 1, !tbaa !9
  %19 = zext i8 %18 to i32
  %20 = getelementptr inbounds %"class.qpl::ml::bit_reader", ptr %10, i32 0, i32 6
  %21 = load i8, ptr %20, align 8, !tbaa !10
  %22 = zext i8 %21 to i32
  %23 = sub nsw i32 %19, %22
  store i32 %23, ptr %7, align 4, !tbaa !14
  %24 = invoke noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %6, ptr noundef nonnull align 4 dereferenceable(4) %7)
          to label %25 unwind label %46

25:                                               ; preds = %17
  %26 = load i32, ptr %24, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  store i32 %26, ptr %5, align 4, !tbaa !14
  %27 = load i32, ptr %5, align 4, !tbaa !14
  %28 = call noundef i32 @_ZN3qpl2ml4util11bit_to_byteEj(i32 noundef %27) #4
  %29 = mul i32 %28, 8
  store i32 %29, ptr %5, align 4, !tbaa !14
  %30 = load i32, ptr %5, align 4, !tbaa !14
  %31 = trunc i32 %30 to i8
  call void @_ZN3qpl2ml10bit_reader11load_bufferEh(ptr noundef nonnull align 8 dereferenceable(43) %10, i8 noundef zeroext %31) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %32

32:                                               ; preds = %25, %2
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #4
  store i16 0, ptr %8, align 2, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #4
  %33 = load i8, ptr %4, align 1, !tbaa !9
  %34 = zext i8 %33 to i32
  %35 = invoke noundef zeroext i16 @_ZN3qpl2ml4util10build_maskItEET_j(i32 noundef %34)
          to label %36 unwind label %46

36:                                               ; preds = %32
  store i16 %35, ptr %9, align 2, !tbaa !16
  %37 = getelementptr inbounds %"class.qpl::ml::bit_reader", ptr %10, i32 0, i32 5
  %38 = load i64, ptr %37, align 8, !tbaa !18
  %39 = trunc i64 %38 to i16
  %40 = zext i16 %39 to i32
  %41 = load i16, ptr %9, align 2, !tbaa !16
  %42 = zext i16 %41 to i32
  %43 = and i32 %40, %42
  %44 = trunc i32 %43 to i16
  store i16 %44, ptr %8, align 2, !tbaa !16
  %45 = load i16, ptr %8, align 2, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #4
  ret i16 %45

46:                                               ; preds = %32, %17
  %47 = landingpad { ptr, i32 }
          catch ptr null
  %48 = extractvalue { ptr, i32 } %47, 0
  call void @__clang_call_terminate(ptr %48) #5
  unreachable
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN3qpl2ml4util11bit_to_byteEj(i32 noundef) #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i16 @_ZN3qpl2ml4util10build_maskItEET_j(i32 noundef) #0

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { noinline noreturn nounwind uwtable "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 = { 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 #4 = { nounwind }
attributes #5 = { noreturn 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 = !{!7, !7, i64 0}
!10 = !{!11, !7, i64 40}
!11 = !{!"_ZTSN3qpl2ml10bit_readerE", !6, i64 0, !6, i64 8, !6, i64 16, !7, i64 24, !12, i64 32, !7, i64 40, !13, i64 41, !13, i64 42}
!12 = !{!"long", !7, i64 0}
!13 = !{!"bool", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"short", !7, i64 0}
!18 = !{!11, !12, i64 32}


; 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 nounwind uwtable
declare dso_local void @_ZN3qpl2ml10bit_reader11load_bufferEh(ptr noundef nonnull align 8 dereferenceable(43), i8 noundef zeroext) local_unnamed_addr #0 align 2

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

declare i32 @__gxx_personality_v0(...)

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i16 @_ZN3qpl2ml10bit_reader9peek_bitsEh(ptr noundef nonnull align 8 dereferenceable(43) %0, i8 noundef zeroext %1) local_unnamed_addr #0 align 2 personality ptr @__gxx_personality_v0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = zext i8 %1 to i32
  %6 = getelementptr inbounds i8, ptr %0, i64 40
  %7 = load i8, ptr %6, align 8, !tbaa !5
  %8 = icmp ult i8 %7, %1
  br i1 %8, label %9, label %16

9:                                                ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  store i32 8, ptr %3, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %10 = zext i8 %7 to i32
  %11 = sub nsw i32 %5, %10
  store i32 %11, ptr %4, align 4, !tbaa !12
  %12 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %3, ptr noundef nonnull align 4 dereferenceable(4) %4)
  %13 = load i32, ptr %12, align 4, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  %14 = call noundef i32 @_ZN3qpl2ml4util11bit_to_byteEj(i32 noundef %13) #3
  %.tr = trunc i32 %14 to i8
  %15 = shl i8 %.tr, 3
  call void @_ZN3qpl2ml10bit_reader11load_bufferEh(ptr noundef nonnull align 8 dereferenceable(43) %0, i8 noundef zeroext %15) #3
  br label %16

16:                                               ; preds = %9, %2
  %17 = call noundef zeroext i16 @_ZN3qpl2ml4util10build_maskItEET_j(i32 noundef %5)
  %18 = getelementptr inbounds i8, ptr %0, i64 32
  %19 = load i64, ptr %18, align 8, !tbaa !14
  %20 = trunc i64 %19 to i16
  %21 = and i16 %17, %20
  ret i16 %21
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN3qpl2ml4util11bit_to_byteEj(i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i16 @_ZN3qpl2ml4util10build_maskItEET_j(i32 noundef) local_unnamed_addr #0

attributes #0 = { 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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, !8, i64 40}
!6 = !{!"_ZTSN3qpl2ml10bit_readerE", !7, i64 0, !7, i64 8, !7, i64 16, !8, i64 24, !10, i64 32, !8, i64 40, !11, i64 41, !11, i64 42}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"bool", !8, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!6, !10, i64 32}


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