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

; 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: nounwind uwtable
declare dso_local i32 @RangeDecoderBitDecode(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @LzmaLiteralDecodeMatch(ptr noundef %0, ptr noundef %1, i8 noundef zeroext %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i8 %2, ptr %6, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 1, ptr %7, align 4, !tbaa !10
  br label %11

11:                                               ; preds = %58, %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %12 = load i8, ptr %6, align 1, !tbaa !9
  %13 = zext i8 %12 to i32
  %14 = ashr i32 %13, 7
  %15 = and i32 %14, 1
  store i32 %15, ptr %9, align 4, !tbaa !10
  %16 = load i8, ptr %6, align 1, !tbaa !9
  %17 = zext i8 %16 to i32
  %18 = shl i32 %17, 1
  %19 = trunc i32 %18 to i8
  store i8 %19, ptr %6, align 1, !tbaa !9
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds i16, ptr %20, i64 256
  %22 = load i32, ptr %9, align 4, !tbaa !10
  %23 = shl i32 %22, 8
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i16, ptr %21, i64 %24
  %26 = load i32, ptr %7, align 4, !tbaa !10
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i16, ptr %25, i64 %27
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = call i32 @RangeDecoderBitDecode(ptr noundef %28, ptr noundef %29)
  store i32 %30, ptr %8, align 4, !tbaa !10
  %31 = load i32, ptr %7, align 4, !tbaa !10
  %32 = shl i32 %31, 1
  %33 = load i32, ptr %8, align 4, !tbaa !10
  %34 = or i32 %32, %33
  store i32 %34, ptr %7, align 4, !tbaa !10
  %35 = load i32, ptr %9, align 4, !tbaa !10
  %36 = load i32, ptr %8, align 4, !tbaa !10
  %37 = icmp ne i32 %35, %36
  br i1 %37, label %38, label %54

38:                                               ; preds = %11
  br label %39

39:                                               ; preds = %42, %38
  %40 = load i32, ptr %7, align 4, !tbaa !10
  %41 = icmp slt i32 %40, 256
  br i1 %41, label %42, label %53

42:                                               ; preds = %39
  %43 = load i32, ptr %7, align 4, !tbaa !10
  %44 = load i32, ptr %7, align 4, !tbaa !10
  %45 = add nsw i32 %43, %44
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = load i32, ptr %7, align 4, !tbaa !10
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i16, ptr %46, i64 %48
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = call i32 @RangeDecoderBitDecode(ptr noundef %49, ptr noundef %50)
  %52 = or i32 %45, %51
  store i32 %52, ptr %7, align 4, !tbaa !10
  br label %39, !llvm.loop !12

53:                                               ; preds = %39
  store i32 2, ptr %10, align 4
  br label %55

54:                                               ; preds = %11
  store i32 0, ptr %10, align 4
  br label %55

55:                                               ; preds = %54, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %56 = load i32, ptr %10, align 4
  switch i32 %56, label %64 [
    i32 0, label %57
    i32 2, label %61
  ]

57:                                               ; preds = %55
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %7, align 4, !tbaa !10
  %60 = icmp slt i32 %59, 256
  br i1 %60, label %11, label %61, !llvm.loop !14

61:                                               ; preds = %58, %55
  %62 = load i32, ptr %7, align 4, !tbaa !10
  %63 = trunc i32 %62 to i8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i8 %63

64:                                               ; preds = %55
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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 i32 @RangeDecoderBitDecode(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @LzmaLiteralDecodeMatch(ptr noundef %0, ptr noundef %1, i8 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 512
  br label %5

5:                                                ; preds = %23, %3
  %.019 = phi i32 [ 1, %3 ], [ %15, %23 ]
  %.018 = phi i8 [ %2, %3 ], [ %24, %23 ]
  %6 = lshr i8 %.018, 7
  %7 = zext nneg i8 %6 to i32
  %8 = shl nuw nsw i32 %7, 8
  %9 = zext nneg i32 %8 to i64
  %10 = getelementptr inbounds i16, ptr %4, i64 %9
  %11 = sext i32 %.019 to i64
  %12 = getelementptr inbounds i16, ptr %10, i64 %11
  %13 = tail call i32 @RangeDecoderBitDecode(ptr noundef nonnull %12, ptr noundef %1)
  %14 = shl i32 %.019, 1
  %15 = or i32 %13, %14
  %.not = icmp eq i32 %13, %7
  br i1 %.not, label %23, label %.preheader

.preheader:                                       ; preds = %5
  %16 = icmp slt i32 %15, 256
  br i1 %16, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.127 = phi i32 [ %21, %.lr.ph ], [ %15, %.preheader ]
  %17 = shl nsw i32 %.127, 1
  %18 = sext i32 %.127 to i64
  %19 = getelementptr inbounds i16, ptr %0, i64 %18
  %20 = tail call i32 @RangeDecoderBitDecode(ptr noundef %19, ptr noundef %1)
  %21 = or i32 %20, %17
  %22 = icmp slt i32 %21, 256
  br i1 %22, label %.lr.ph, label %.thread, !llvm.loop !5

23:                                               ; preds = %5
  %24 = shl i8 %.018, 1
  %25 = icmp slt i32 %15, 256
  br i1 %25, label %5, label %.thread, !llvm.loop !7

.thread:                                          ; preds = %23, %.lr.ph, %.preheader
  %.224 = phi i32 [ %15, %.preheader ], [ %21, %.lr.ph ], [ %15, %23 ]
  %26 = trunc i32 %.224 to i8
  ret i8 %26
}

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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


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