; LLVM IR from mncoppola/rpef :: utilities_src/squashfs2.1/lzma/C/7zip/Compress/LZMA_C (LzmaDecodeSize)

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

%struct._CRangeDecoder = type { ptr, ptr, i32, i32, i32 }

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @RangeDecoderReadByte(ptr noundef) #0

; 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: nounwind uwtable
define dso_local i32 @RangeDecoderDecodeDirectBits(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct._CRangeDecoder, ptr %9, i32 0, i32 2
  %11 = load i32, ptr %10, align 8, !tbaa !11
  store i32 %11, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct._CRangeDecoder, ptr %12, i32 0, i32 3
  %14 = load i32, ptr %13, align 4, !tbaa !13
  store i32 %14, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %15 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %15, ptr %8, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %46, %2
  %17 = load i32, ptr %8, align 4, !tbaa !9
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %49

19:                                               ; preds = %16
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = lshr i32 %20, 1
  store i32 %21, ptr %5, align 4, !tbaa !9
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = shl i32 %22, 1
  store i32 %23, ptr %7, align 4, !tbaa !9
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = icmp uge i32 %24, %25
  br i1 %26, label %27, label %33

27:                                               ; preds = %19
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = load i32, ptr %6, align 4, !tbaa !9
  %30 = sub i32 %29, %28
  store i32 %30, ptr %6, align 4, !tbaa !9
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = or i32 %31, 1
  store i32 %32, ptr %7, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %27, %19
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = icmp ult i32 %34, 16777216
  br i1 %35, label %36, label %45

36:                                               ; preds = %33
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = shl i32 %37, 8
  store i32 %38, ptr %5, align 4, !tbaa !9
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = shl i32 %39, 8
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = call zeroext i8 @RangeDecoderReadByte(ptr noundef %41)
  %43 = zext i8 %42 to i32
  %44 = or i32 %40, %43
  store i32 %44, ptr %6, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %36, %33
  br label %46

46:                                               ; preds = %45
  %47 = load i32, ptr %8, align 4, !tbaa !9
  %48 = add nsw i32 %47, -1
  store i32 %48, ptr %8, align 4, !tbaa !9
  br label %16, !llvm.loop !14

49:                                               ; preds = %16
  %50 = load i32, ptr %5, align 4, !tbaa !9
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct._CRangeDecoder, ptr %51, i32 0, i32 2
  store i32 %50, ptr %52, align 8, !tbaa !11
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct._CRangeDecoder, ptr %54, i32 0, i32 3
  store i32 %53, ptr %55, align 4, !tbaa !13
  %56 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  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 %56
}

attributes #0 = { 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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"_CRangeDecoder", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !10, i64 24}
!13 = !{!12, !10, i64 20}
!14 = distinct !{!14, !15}
!15 = !{!"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: nounwind uwtable
declare dso_local zeroext i8 @RangeDecoderReadByte(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @RangeDecoderDecodeDirectBits(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 20
  %6 = load i32, ptr %5, align 4, !tbaa !11
  %.not29 = icmp eq i32 %1, 0
  br i1 %.not29, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %18
  %.033 = phi i32 [ %19, %18 ], [ %1, %2 ]
  %.02032 = phi i32 [ %.1, %18 ], [ 0, %2 ]
  %.02131 = phi i32 [ %.2, %18 ], [ %6, %2 ]
  %.02330 = phi i32 [ %.124, %18 ], [ %4, %2 ]
  %7 = lshr i32 %.02330, 1
  %8 = shl i32 %.02032, 1
  %.not28 = icmp uge i32 %.02131, %7
  %9 = select i1 %.not28, i32 %7, i32 0
  %.122 = sub i32 %.02131, %9
  %10 = zext i1 %.not28 to i32
  %.1 = or disjoint i32 %8, %10
  %11 = icmp ult i32 %.02330, 33554432
  br i1 %11, label %12, label %18

12:                                               ; preds = %.lr.ph
  %13 = shl nuw i32 %7, 8
  %14 = shl i32 %.122, 8
  %15 = tail call zeroext i8 @RangeDecoderReadByte(ptr noundef %0)
  %16 = zext i8 %15 to i32
  %17 = or disjoint i32 %14, %16
  br label %18

18:                                               ; preds = %.lr.ph, %12
  %.124 = phi i32 [ %13, %12 ], [ %7, %.lr.ph ]
  %.2 = phi i32 [ %17, %12 ], [ %.122, %.lr.ph ]
  %19 = add nsw i32 %.033, -1
  %.not = icmp eq i32 %19, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %18, %2
  %.023.lcssa = phi i32 [ %4, %2 ], [ %.124, %18 ]
  %.021.lcssa = phi i32 [ %6, %2 ], [ %.2, %18 ]
  %.020.lcssa = phi i32 [ 0, %2 ], [ %.1, %18 ]
  store i32 %.023.lcssa, ptr %3, align 8, !tbaa !5
  store i32 %.021.lcssa, ptr %5, align 4, !tbaa !11
  ret i32 %.020.lcssa
}

attributes #0 = { 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" }

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


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