; LLVM IR from SSBMTonberry/pocketlzma :: ref_spec/lzma1900/CPP/7zip/Bundles/LzmaSpec (LzmaSpec)

; 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.CRangeDecoder = type <{ i32, i32, ptr, i8, [7 x i8] }>

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN13CRangeDecoder9NormalizeEv(ptr noundef nonnull align 8 dereferenceable(17)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN13CRangeDecoder9DecodeBitEPt(ptr noundef nonnull align 8 dereferenceable(17) %0, ptr noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = load i16, ptr %9, align 2, !tbaa !9
  %11 = zext i16 %10 to i32
  store i32 %11, ptr %5, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %12 = getelementptr inbounds %class.CRangeDecoder, ptr %8, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !13
  %14 = lshr i32 %13, 11
  %15 = load i32, ptr %5, align 4, !tbaa !11
  %16 = mul i32 %14, %15
  store i32 %16, ptr %6, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %17 = getelementptr inbounds %class.CRangeDecoder, ptr %8, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !16
  %19 = load i32, ptr %6, align 4, !tbaa !11
  %20 = icmp ult i32 %18, %19
  br i1 %20, label %21, label %29

21:                                               ; preds = %2
  %22 = load i32, ptr %5, align 4, !tbaa !11
  %23 = sub i32 2048, %22
  %24 = lshr i32 %23, 5
  %25 = load i32, ptr %5, align 4, !tbaa !11
  %26 = add i32 %25, %24
  store i32 %26, ptr %5, align 4, !tbaa !11
  %27 = load i32, ptr %6, align 4, !tbaa !11
  %28 = getelementptr inbounds %class.CRangeDecoder, ptr %8, i32 0, i32 0
  store i32 %27, ptr %28, align 8, !tbaa !13
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %42

29:                                               ; preds = %2
  %30 = load i32, ptr %5, align 4, !tbaa !11
  %31 = lshr i32 %30, 5
  %32 = load i32, ptr %5, align 4, !tbaa !11
  %33 = sub i32 %32, %31
  store i32 %33, ptr %5, align 4, !tbaa !11
  %34 = load i32, ptr %6, align 4, !tbaa !11
  %35 = getelementptr inbounds %class.CRangeDecoder, ptr %8, i32 0, i32 1
  %36 = load i32, ptr %35, align 4, !tbaa !16
  %37 = sub i32 %36, %34
  store i32 %37, ptr %35, align 4, !tbaa !16
  %38 = load i32, ptr %6, align 4, !tbaa !11
  %39 = getelementptr inbounds %class.CRangeDecoder, ptr %8, i32 0, i32 0
  %40 = load i32, ptr %39, align 8, !tbaa !13
  %41 = sub i32 %40, %38
  store i32 %41, ptr %39, align 8, !tbaa !13
  store i32 1, ptr %7, align 4, !tbaa !11
  br label %42

42:                                               ; preds = %29, %21
  %43 = load i32, ptr %5, align 4, !tbaa !11
  %44 = trunc i32 %43 to i16
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  store i16 %44, ptr %45, align 2, !tbaa !9
  call void @_ZN13CRangeDecoder9NormalizeEv(ptr noundef nonnull align 8 dereferenceable(17) %8)
  %46 = load i32, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %46
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"_ZTS13CRangeDecoder", !12, i64 0, !12, i64 4, !6, i64 8, !15, i64 16}
!15 = !{!"bool", !7, i64 0}
!16 = !{!14, !12, 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 @_ZN13CRangeDecoder9NormalizeEv(ptr noundef nonnull align 8 dereferenceable(17)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 0, 2) i32 @_ZN13CRangeDecoder9DecodeBitEPt(ptr noundef nonnull align 8 dereferenceable(17) %0, ptr nocapture noundef %1) local_unnamed_addr #0 align 2 {
  %3 = load i16, ptr %1, align 2, !tbaa !5
  %4 = zext i16 %3 to i32
  %5 = load i32, ptr %0, align 8, !tbaa !9
  %6 = lshr i32 %5, 11
  %7 = mul i32 %6, %4
  %8 = getelementptr inbounds i8, ptr %0, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !14
  %10 = icmp ult i32 %9, %7
  br i1 %10, label %11, label %15

11:                                               ; preds = %2
  %12 = sub nsw i32 2048, %4
  %13 = lshr i32 %12, 5
  %14 = add nuw nsw i32 %13, %4
  br label %20

15:                                               ; preds = %2
  %16 = lshr i32 %4, 5
  %17 = sub nsw i32 %4, %16
  %18 = sub i32 %9, %7
  store i32 %18, ptr %8, align 4, !tbaa !14
  %19 = sub i32 %5, %7
  br label %20

20:                                               ; preds = %15, %11
  %storemerge = phi i32 [ %19, %15 ], [ %7, %11 ]
  %.013 = phi i32 [ %17, %15 ], [ %14, %11 ]
  %.0 = phi i32 [ 1, %15 ], [ 0, %11 ]
  store i32 %storemerge, ptr %0, align 8, !tbaa !9
  %21 = trunc i32 %.013 to i16
  store i16 %21, ptr %1, align 2, !tbaa !5
  tail call void @_ZN13CRangeDecoder9NormalizeEv(ptr noundef nonnull align 8 dereferenceable(17) %0)
  ret i32 %.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, !6, i64 0}
!6 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTS13CRangeDecoder", !11, i64 0, !11, i64 4, !12, i64 8, !13, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"bool", !7, i64 0}
!14 = !{!10, !11, i64 4}


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