; LLVM IR from SSBMTonberry/pocketlzma :: ref_spec/lzma1900/CPP/7zip/Compress (PpmdEncoder)

; 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.NCompress::NPpmd::CEncProps" = type { i32, i32, i32 }

@_ZN9NCompress5NPpmdL7kOrdersE = external hidden constant [10 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN9NCompress5NPpmd9CEncProps9NormalizeEi(ptr noundef nonnull align 4 dereferenceable(12) %0, i32 noundef %1) #0 align 2 {
  %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
  %9 = load ptr, ptr %3, align 8
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store i32 5, ptr %4, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %12, %2
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = icmp sgt i32 %14, 9
  br i1 %15, label %16, label %17

16:                                               ; preds = %13
  store i32 9, ptr %4, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %16, %13
  %18 = getelementptr inbounds %"struct.NCompress::NPpmd::CEncProps", ptr %9, i32 0, i32 0
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = icmp eq i32 %19, -1
  br i1 %20, label %21, label %32

21:                                               ; preds = %17
  %22 = load i32, ptr %4, align 4, !tbaa !9
  %23 = icmp sge i32 %22, 9
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  br label %29

25:                                               ; preds = %21
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = add nsw i32 %26, 19
  %28 = shl i32 1, %27
  br label %29

29:                                               ; preds = %25, %24
  %30 = phi i32 [ 201326592, %24 ], [ %28, %25 ]
  %31 = getelementptr inbounds %"struct.NCompress::NPpmd::CEncProps", ptr %9, i32 0, i32 0
  store i32 %30, ptr %31, align 4, !tbaa !11
  br label %32

32:                                               ; preds = %29, %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 16, ptr %5, align 4, !tbaa !9
  %33 = getelementptr inbounds %"struct.NCompress::NPpmd::CEncProps", ptr %9, i32 0, i32 0
  %34 = load i32, ptr %33, align 4, !tbaa !11
  %35 = udiv i32 %34, 16
  %36 = getelementptr inbounds %"struct.NCompress::NPpmd::CEncProps", ptr %9, i32 0, i32 1
  %37 = load i32, ptr %36, align 4, !tbaa !13
  %38 = icmp ugt i32 %35, %37
  br i1 %38, label %39, label %70

39:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 16, ptr %6, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %65, %39
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = icmp ule i32 %41, 31
  br i1 %42, label %44, label %43

43:                                               ; preds = %40
  store i32 2, ptr %7, align 4
  br label %68

44:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = shl i32 1, %45
  store i32 %46, ptr %8, align 4, !tbaa !9
  %47 = getelementptr inbounds %"struct.NCompress::NPpmd::CEncProps", ptr %9, i32 0, i32 1
  %48 = load i32, ptr %47, align 4, !tbaa !13
  %49 = load i32, ptr %8, align 4, !tbaa !9
  %50 = udiv i32 %49, 16
  %51 = icmp ule i32 %48, %50
  br i1 %51, label %52, label %61

52:                                               ; preds = %44
  %53 = getelementptr inbounds %"struct.NCompress::NPpmd::CEncProps", ptr %9, i32 0, i32 0
  %54 = load i32, ptr %53, align 4, !tbaa !11
  %55 = load i32, ptr %8, align 4, !tbaa !9
  %56 = icmp ugt i32 %54, %55
  br i1 %56, label %57, label %60

57:                                               ; preds = %52
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = getelementptr inbounds %"struct.NCompress::NPpmd::CEncProps", ptr %9, i32 0, i32 0
  store i32 %58, ptr %59, align 4, !tbaa !11
  br label %60

60:                                               ; preds = %57, %52
  store i32 2, ptr %7, align 4
  br label %62

61:                                               ; preds = %44
  store i32 0, ptr %7, align 4
  br label %62

62:                                               ; preds = %61, %60
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %63 = load i32, ptr %7, align 4
  switch i32 %63, label %68 [
    i32 0, label %64
  ]

64:                                               ; preds = %62
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %6, align 4, !tbaa !9
  %67 = add i32 %66, 1
  store i32 %67, ptr %6, align 4, !tbaa !9
  br label %40, !llvm.loop !14

68:                                               ; preds = %62, %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %69

69:                                               ; preds = %68
  br label %70

70:                                               ; preds = %69, %32
  %71 = getelementptr inbounds %"struct.NCompress::NPpmd::CEncProps", ptr %9, i32 0, i32 2
  %72 = load i32, ptr %71, align 4, !tbaa !16
  %73 = icmp eq i32 %72, -1
  br i1 %73, label %74, label %81

74:                                               ; preds = %70
  %75 = load i32, ptr %4, align 4, !tbaa !9
  %76 = zext i32 %75 to i64
  %77 = getelementptr inbounds [10 x i8], ptr @_ZN9NCompress5NPpmdL7kOrdersE, i64 0, i64 %76
  %78 = load i8, ptr %77, align 1, !tbaa !17
  %79 = zext i8 %78 to i32
  %80 = getelementptr inbounds %"struct.NCompress::NPpmd::CEncProps", ptr %9, i32 0, i32 2
  store i32 %79, ptr %80, align 4, !tbaa !16
  br label %81

81:                                               ; preds = %74, %70
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret void
}

; 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

attributes #0 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTSN9NCompress5NPpmd9CEncPropsE", !10, i64 0, !10, i64 4, !10, i64 8}
!13 = !{!12, !10, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!12, !10, i64 8}
!17 = !{!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"

@_ZN9NCompress5NPpmdL7kOrdersE = external hidden local_unnamed_addr constant [10 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local void @_ZN9NCompress5NPpmd9CEncProps9NormalizeEi(ptr nocapture noundef nonnull align 4 dereferenceable(12) %0, i32 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = icmp slt i32 %1, 0
  %4 = tail call i32 @llvm.umin.i32(i32 %1, i32 9)
  %spec.store.select1 = select i1 %3, i32 5, i32 %4
  %5 = load i32, ptr %0, align 4, !tbaa !5
  %6 = icmp eq i32 %5, -1
  br i1 %6, label %7, label %11

7:                                                ; preds = %2
  %8 = icmp ugt i32 %spec.store.select1, 8
  %9 = shl nuw nsw i32 524288, %spec.store.select1
  %10 = select i1 %8, i32 201326592, i32 %9
  store i32 %10, ptr %0, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %7, %2
  %12 = phi i32 [ %10, %7 ], [ %5, %2 ]
  %13 = lshr i32 %12, 4
  %14 = getelementptr inbounds i8, ptr %0, i64 4
  %15 = load i32, ptr %14, align 4, !tbaa !10
  %16 = icmp ugt i32 %13, %15
  br i1 %16, label %.critedge.preheader, label %.loopexit

.critedge.preheader:                              ; preds = %11
  %.not = icmp ugt i32 %15, 4096
  br i1 %.not, label %.critedge.1, label %17

.critedge.1:                                      ; preds = %.critedge.preheader
  %.not.1 = icmp ugt i32 %15, 8192
  br i1 %.not.1, label %.critedge.2, label %17

.critedge.2:                                      ; preds = %.critedge.1
  %.not.2 = icmp ugt i32 %15, 16384
  br i1 %.not.2, label %.critedge.3, label %17

.critedge.3:                                      ; preds = %.critedge.2
  %.not.3 = icmp ugt i32 %15, 32768
  br i1 %.not.3, label %.critedge.4, label %17

.critedge.4:                                      ; preds = %.critedge.3
  %.not.4 = icmp ugt i32 %15, 65536
  br i1 %.not.4, label %.critedge.5, label %17

.critedge.5:                                      ; preds = %.critedge.4
  %.not.5 = icmp ugt i32 %15, 131072
  br i1 %.not.5, label %.critedge.6, label %17

.critedge.6:                                      ; preds = %.critedge.5
  %.not.6 = icmp ugt i32 %15, 262144
  br i1 %.not.6, label %.critedge.7, label %17

.critedge.7:                                      ; preds = %.critedge.6
  %.not.7 = icmp ugt i32 %15, 524288
  br i1 %.not.7, label %.critedge.8, label %17

.critedge.8:                                      ; preds = %.critedge.7
  %.not.8 = icmp ugt i32 %15, 1048576
  br i1 %.not.8, label %.critedge.9, label %17

.critedge.9:                                      ; preds = %.critedge.8
  %.not.9 = icmp ugt i32 %15, 2097152
  br i1 %.not.9, label %.critedge.10, label %17

.critedge.10:                                     ; preds = %.critedge.9
  %.not.10 = icmp ugt i32 %15, 4194304
  br i1 %.not.10, label %.critedge.11, label %17

.critedge.11:                                     ; preds = %.critedge.10
  %.not.11 = icmp ugt i32 %15, 8388608
  br i1 %.not.11, label %.critedge.12, label %17

.critedge.12:                                     ; preds = %.critedge.11
  %.not.12 = icmp ugt i32 %15, 16777216
  br i1 %.not.12, label %.critedge.13, label %17

.critedge.13:                                     ; preds = %.critedge.12
  %.not.13 = icmp ugt i32 %15, 33554432
  br i1 %.not.13, label %.critedge.14, label %17

.critedge.14:                                     ; preds = %.critedge.13
  %.not.14 = icmp ugt i32 %15, 67108864
  br i1 %.not.14, label %.critedge.15, label %17

.critedge.15:                                     ; preds = %.critedge.14
  %.not.15 = icmp ugt i32 %15, 134217728
  br i1 %.not.15, label %.loopexit, label %17

17:                                               ; preds = %.critedge.15, %.critedge.14, %.critedge.13, %.critedge.12, %.critedge.11, %.critedge.10, %.critedge.9, %.critedge.8, %.critedge.7, %.critedge.6, %.critedge.5, %.critedge.4, %.critedge.3, %.critedge.2, %.critedge.1, %.critedge.preheader
  %.lcssa = phi i32 [ 65536, %.critedge.preheader ], [ 131072, %.critedge.1 ], [ 262144, %.critedge.2 ], [ 524288, %.critedge.3 ], [ 1048576, %.critedge.4 ], [ 2097152, %.critedge.5 ], [ 4194304, %.critedge.6 ], [ 8388608, %.critedge.7 ], [ 16777216, %.critedge.8 ], [ 33554432, %.critedge.9 ], [ 67108864, %.critedge.10 ], [ 134217728, %.critedge.11 ], [ 268435456, %.critedge.12 ], [ 536870912, %.critedge.13 ], [ 1073741824, %.critedge.14 ], [ -2147483648, %.critedge.15 ]
  %18 = icmp ugt i32 %12, %.lcssa
  br i1 %18, label %19, label %.loopexit

19:                                               ; preds = %17
  store i32 %.lcssa, ptr %0, align 4, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.critedge.15, %19, %17, %11
  %20 = getelementptr inbounds i8, ptr %0, i64 8
  %21 = load i32, ptr %20, align 4, !tbaa !11
  %22 = icmp eq i32 %21, -1
  br i1 %22, label %23, label %28

23:                                               ; preds = %.loopexit
  %24 = zext nneg i32 %spec.store.select1 to i64
  %25 = getelementptr inbounds [10 x i8], ptr @_ZN9NCompress5NPpmdL7kOrdersE, i64 0, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !12
  %27 = zext i8 %26 to i32
  store i32 %27, ptr %20, align 4, !tbaa !11
  br label %28

28:                                               ; preds = %23, %.loopexit
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) 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 speculatable willreturn memory(none) }

!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 = !{!"_ZTSN9NCompress5NPpmd9CEncPropsE", !7, i64 0, !7, i64 4, !7, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!6, !7, i64 8}
!12 = !{!8, !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass