; LLVM IR from philanc/luazen :: src/lzma (LzmaDec)

; 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._CLzmaProps = type { i8, i8, i8, i8, i32 }
%struct.CLzmaDec = type { %struct._CLzmaProps, ptr, ptr, ptr, i64, i64, ptr, i32, i32, i32, i32, [4 x i32], i32, i32, i32, i32, [20 x i8] }
%struct.ISzAlloc = type { ptr, ptr }

; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare dso_local void @LzmaDec_FreeProbs(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @LzmaDec_FreeDict(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @LzmaProps_Decode(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @LzmaDec_AllocateProbs2(ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @LzmaDec_Allocate(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #2 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca %struct._CLzmaProps, align 4
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = call i32 @LzmaProps_Decode(ptr noundef %10, ptr noundef %17, i32 noundef %18)
  store i32 %19, ptr %12, align 4, !tbaa !9
  %20 = load i32, ptr %12, align 4, !tbaa !9
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %4
  %23 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %23, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %25

24:                                               ; preds = %4
  store i32 0, ptr %13, align 4
  br label %25

25:                                               ; preds = %24, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %26 = load i32, ptr %13, align 4
  switch i32 %26, label %100 [
    i32 0, label %27
  ]

27:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = call i32 @LzmaDec_AllocateProbs2(ptr noundef %28, ptr noundef %10, ptr noundef %29)
  store i32 %30, ptr %14, align 4, !tbaa !9
  %31 = load i32, ptr %14, align 4, !tbaa !9
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %35

33:                                               ; preds = %27
  %34 = load i32, ptr %14, align 4, !tbaa !9
  store i32 %34, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %36

35:                                               ; preds = %27
  store i32 0, ptr %13, align 4
  br label %36

36:                                               ; preds = %35, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  %37 = load i32, ptr %13, align 4
  switch i32 %37, label %100 [
    i32 0, label %38
  ]

38:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %39 = getelementptr inbounds %struct._CLzmaProps, ptr %10, i32 0, i32 4
  %40 = load i32, ptr %39, align 4, !tbaa !11
  store i32 %40, ptr %15, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #3
  store i64 4095, ptr %16, align 8, !tbaa !13
  %41 = load i32, ptr %15, align 4, !tbaa !9
  %42 = icmp uge i32 %41, 1073741824
  br i1 %42, label %43, label %44

43:                                               ; preds = %38
  store i64 4194303, ptr %16, align 8, !tbaa !13
  br label %49

44:                                               ; preds = %38
  %45 = load i32, ptr %15, align 4, !tbaa !9
  %46 = icmp uge i32 %45, 4194304
  br i1 %46, label %47, label %48

47:                                               ; preds = %44
  store i64 1048575, ptr %16, align 8, !tbaa !13
  br label %48

48:                                               ; preds = %47, %44
  br label %49

49:                                               ; preds = %48, %43
  %50 = load i32, ptr %15, align 4, !tbaa !9
  %51 = zext i32 %50 to i64
  %52 = load i64, ptr %16, align 8, !tbaa !13
  %53 = add i64 %51, %52
  %54 = load i64, ptr %16, align 8, !tbaa !13
  %55 = xor i64 %54, -1
  %56 = and i64 %53, %55
  store i64 %56, ptr %11, align 8, !tbaa !13
  %57 = load i64, ptr %11, align 8, !tbaa !13
  %58 = load i32, ptr %15, align 4, !tbaa !9
  %59 = zext i32 %58 to i64
  %60 = icmp ult i64 %57, %59
  br i1 %60, label %61, label %64

61:                                               ; preds = %49
  %62 = load i32, ptr %15, align 4, !tbaa !9
  %63 = zext i32 %62 to i64
  store i64 %63, ptr %11, align 8, !tbaa !13
  br label %64

64:                                               ; preds = %61, %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.CLzmaDec, ptr %65, i32 0, i32 3
  %67 = load ptr, ptr %66, align 8, !tbaa !15
  %68 = icmp ne ptr %67, null
  br i1 %68, label %69, label %75

69:                                               ; preds = %64
  %70 = load i64, ptr %11, align 8, !tbaa !13
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.CLzmaDec, ptr %71, i32 0, i32 4
  %73 = load i64, ptr %72, align 8, !tbaa !17
  %74 = icmp ne i64 %70, %73
  br i1 %74, label %75, label %94

75:                                               ; preds = %69, %64
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = load ptr, ptr %9, align 8, !tbaa !5
  call void @LzmaDec_FreeDict(ptr noundef %76, ptr noundef %77)
  %78 = load ptr, ptr %9, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.ISzAlloc, ptr %78, i32 0, i32 0
  %80 = load ptr, ptr %79, align 8, !tbaa !18
  %81 = load ptr, ptr %9, align 8, !tbaa !5
  %82 = load i64, ptr %11, align 8, !tbaa !13
  %83 = call ptr %80(ptr noundef %81, i64 noundef %82)
  %84 = load ptr, ptr %6, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.CLzmaDec, ptr %84, i32 0, i32 3
  store ptr %83, ptr %85, align 8, !tbaa !15
  %86 = load ptr, ptr %6, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.CLzmaDec, ptr %86, i32 0, i32 3
  %88 = load ptr, ptr %87, align 8, !tbaa !15
  %89 = icmp ne ptr %88, null
  br i1 %89, label %93, label %90

90:                                               ; preds = %75
  %91 = load ptr, ptr %6, align 8, !tbaa !5
  %92 = load ptr, ptr %9, align 8, !tbaa !5
  call void @LzmaDec_FreeProbs(ptr noundef %91, ptr noundef %92)
  store i32 2, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %100

93:                                               ; preds = %75
  br label %94

94:                                               ; preds = %93, %69
  %95 = load i64, ptr %11, align 8, !tbaa !13
  %96 = load ptr, ptr %6, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.CLzmaDec, ptr %96, i32 0, i32 4
  store i64 %95, ptr %97, align 8, !tbaa !17
  %98 = load ptr, ptr %6, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.CLzmaDec, ptr %98, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %99, ptr align 4 %10, i64 8, i1 false), !tbaa.struct !20
  store i32 0, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %100

100:                                              ; preds = %94, %90, %36, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %101 = load i32, ptr %5, align 4
  ret i32 %101
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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, !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 4}
!12 = !{!"_CLzmaProps", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !10, i64 4}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!16, !6, i64 24}
!16 = !{!"", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !14, i64 32, !14, i64 40, !6, i64 48, !10, i64 56, !10, i64 60, !10, i64 64, !10, i64 68, !7, i64 72, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !7, i64 104}
!17 = !{!16, !14, i64 32}
!18 = !{!19, !6, i64 0}
!19 = !{!"ISzAlloc", !6, i64 0, !6, i64 8}
!20 = !{i64 0, i64 1, !21, i64 1, i64 1, !21, i64 2, i64 1, !21, i64 3, i64 1, !21, i64 4, i64 4, !9}
!21 = !{!7, !7, i64 0}


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

%struct._CLzmaProps = type { i8, i8, i8, i8, i32 }

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

; Function Attrs: mustprogress 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 void @LzmaDec_FreeProbs(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @LzmaDec_FreeDict(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @LzmaProps_Decode(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @LzmaDec_AllocateProbs2(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @LzmaDec_Allocate(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = alloca %struct._CLzmaProps, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  %6 = call i32 @LzmaProps_Decode(ptr noundef nonnull %5, ptr noundef %1, i32 noundef %2)
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %7, label %30

7:                                                ; preds = %4
  %8 = call i32 @LzmaDec_AllocateProbs2(ptr noundef %0, ptr noundef nonnull %5, ptr noundef %3)
  %.not42 = icmp eq i32 %8, 0
  br i1 %.not42, label %9, label %30

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %5, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp ugt i32 %11, 1073741823
  %13 = icmp ugt i32 %11, 4194303
  %spec.select = select i1 %13, i64 1048575, i64 4095
  %.0 = select i1 %12, i64 4194303, i64 %spec.select
  %14 = zext i32 %11 to i64
  %15 = add nuw nsw i64 %.0, %14
  %16 = xor i64 %.0, -1
  %17 = and i64 %15, %16
  %spec.select46 = call i64 @llvm.umax.i64(i64 %17, i64 %14)
  %18 = getelementptr inbounds i8, ptr %0, i64 24
  %19 = load ptr, ptr %18, align 8, !tbaa !10
  %.not43 = icmp eq ptr %19, null
  br i1 %.not43, label %23, label %20

20:                                               ; preds = %9
  %21 = getelementptr inbounds i8, ptr %0, i64 32
  %22 = load i64, ptr %21, align 8, !tbaa !14
  %.not44 = icmp eq i64 %spec.select46, %22
  br i1 %.not44, label %27, label %23

23:                                               ; preds = %20, %9
  call void @LzmaDec_FreeDict(ptr noundef nonnull %0, ptr noundef %3)
  %24 = load ptr, ptr %3, align 8, !tbaa !15
  %25 = call ptr %24(ptr noundef nonnull %3, i64 noundef %spec.select46) #3
  store ptr %25, ptr %18, align 8, !tbaa !10
  %.not45 = icmp eq ptr %25, null
  br i1 %.not45, label %26, label %27

26:                                               ; preds = %23
  call void @LzmaDec_FreeProbs(ptr noundef nonnull %0, ptr noundef nonnull %3)
  br label %30

27:                                               ; preds = %23, %20
  %28 = getelementptr inbounds i8, ptr %0, i64 32
  store i64 %spec.select46, ptr %28, align 8, !tbaa !14
  %29 = load i64, ptr %5, align 8
  store i64 %29, ptr %0, align 8
  br label %30

30:                                               ; preds = %7, %4, %27, %26
  %.2 = phi i32 [ 0, %27 ], [ 2, %26 ], [ %8, %7 ], [ %6, %4 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  ret i32 %.2
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #2

attributes #0 = { mustprogress 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !9, i64 4}
!6 = !{!"_CLzmaProps", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !9, i64 4}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!11, !12, i64 24}
!11 = !{!"", !6, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !13, i64 32, !13, i64 40, !12, i64 48, !9, i64 56, !9, i64 60, !9, i64 64, !9, i64 68, !7, i64 72, !9, i64 88, !9, i64 92, !9, i64 96, !9, i64 100, !7, i64 104}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!11, !13, i64 32}
!15 = !{!16, !12, i64 0}
!16 = !{!"ISzAlloc", !12, i64 0, !12, i64 8}


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