; 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 @_ZN13CRangeDecoder16DecodeDirectBitsEj(ptr noundef nonnull align 8 dereferenceable(17) %0, i32 noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %7 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %8

8:                                                ; preds = %42, %2
  %9 = getelementptr inbounds %class.CRangeDecoder, ptr %7, i32 0, i32 0
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %11 = lshr i32 %10, 1
  store i32 %11, ptr %9, align 8, !tbaa !11
  %12 = getelementptr inbounds %class.CRangeDecoder, ptr %7, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = getelementptr inbounds %class.CRangeDecoder, ptr %7, i32 0, i32 1
  %15 = load i32, ptr %14, align 4, !tbaa !14
  %16 = sub i32 %15, %13
  store i32 %16, ptr %14, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %17 = getelementptr inbounds %class.CRangeDecoder, ptr %7, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !14
  %19 = lshr i32 %18, 31
  %20 = sub i32 0, %19
  store i32 %20, ptr %6, align 4, !tbaa !9
  %21 = getelementptr inbounds %class.CRangeDecoder, ptr %7, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = and i32 %22, %23
  %25 = getelementptr inbounds %class.CRangeDecoder, ptr %7, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !14
  %27 = add i32 %26, %24
  store i32 %27, ptr %25, align 4, !tbaa !14
  %28 = getelementptr inbounds %class.CRangeDecoder, ptr %7, i32 0, i32 1
  %29 = load i32, ptr %28, align 4, !tbaa !14
  %30 = getelementptr inbounds %class.CRangeDecoder, ptr %7, i32 0, i32 0
  %31 = load i32, ptr %30, align 8, !tbaa !11
  %32 = icmp eq i32 %29, %31
  br i1 %32, label %33, label %35

33:                                               ; preds = %8
  %34 = getelementptr inbounds %class.CRangeDecoder, ptr %7, i32 0, i32 3
  store i8 1, ptr %34, align 8, !tbaa !15
  br label %35

35:                                               ; preds = %33, %8
  call void @_ZN13CRangeDecoder9NormalizeEv(ptr noundef nonnull align 8 dereferenceable(17) %7)
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = shl i32 %36, 1
  store i32 %37, ptr %5, align 4, !tbaa !9
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = add i32 %38, 1
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = add i32 %40, %39
  store i32 %41, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %42

42:                                               ; preds = %35
  %43 = load i32, ptr %4, align 4, !tbaa !9
  %44 = add i32 %43, -1
  store i32 %44, ptr %4, align 4, !tbaa !9
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %8, label %46, !llvm.loop !16

46:                                               ; preds = %42
  %47 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %47
}

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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS13CRangeDecoder", !10, i64 0, !10, i64 4, !6, i64 8, !13, i64 16}
!13 = !{!"bool", !7, i64 0}
!14 = !{!12, !10, i64 4}
!15 = !{!12, !13, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; 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 i32 @_ZN13CRangeDecoder16DecodeDirectBitsEj(ptr noundef nonnull align 8 dereferenceable(17) %0, i32 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 4
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  br label %5

5:                                                ; preds = %14, %2
  %.06 = phi i32 [ 0, %2 ], [ %17, %14 ]
  %.0 = phi i32 [ %1, %2 ], [ %18, %14 ]
  %6 = load i32, ptr %0, align 8, !tbaa !5
  %7 = lshr i32 %6, 1
  store i32 %7, ptr %0, align 8, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !12
  %9 = sub i32 %8, %7
  %.neg = ashr i32 %9, 31
  %10 = and i32 %.neg, %7
  %11 = add i32 %10, %9
  store i32 %11, ptr %3, align 4, !tbaa !12
  %12 = icmp eq i32 %11, %7
  br i1 %12, label %13, label %14

13:                                               ; preds = %5
  store i8 1, ptr %4, align 8, !tbaa !13
  br label %14

14:                                               ; preds = %13, %5
  tail call void @_ZN13CRangeDecoder9NormalizeEv(ptr noundef nonnull align 8 dereferenceable(17) %0)
  %15 = shl i32 %.06, 1
  %16 = or disjoint i32 %15, 1
  %17 = add nsw i32 %16, %.neg
  %18 = add i32 %.0, -1
  %.not = icmp eq i32 %18, 0
  br i1 %.not, label %19, label %5, !llvm.loop !14

19:                                               ; preds = %14
  ret i32 %17
}

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 = !{!"_ZTS13CRangeDecoder", !7, i64 0, !7, i64 4, !10, i64 8, !11, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"bool", !8, i64 0}
!12 = !{!6, !7, i64 4}
!13 = !{!6, !11, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass