; LLVM IR from JayXon/Leanify :: lib/LZMA (LzmaEnc)

; 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.CLzmaEnc = type { ptr, %struct._IMatchFinder, i32, i32, i32, i32, i32, i32, i32, i32, [4 x i32], i32, i32, ptr, %struct.CRangeEnc, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i64, i32, i32, i32, i32, i32, i32, %struct._CMatchFinderMt, %struct._CMatchFinder, [128 x i8], [128 x i32], [548 x i32], [16 x i32], [4 x [64 x i32]], [4 x [128 x i32]], [16 x i16], [12 x i16], [12 x i16], [12 x i16], [12 x i16], [12 x [16 x i16]], [12 x [16 x i16]], [4 x [64 x i16]], [128 x i16], %struct.CLenEnc, %struct.CLenEnc, [16384 x i8], %struct.CLenPriceEnc, %struct.CLenPriceEnc, [2048 x %struct.COptimal], %struct.CSaveState, [128 x i8] }
%struct._IMatchFinder = type { ptr, ptr, ptr, ptr, ptr }
%struct.CRangeEnc = type { i32, i32, i64, i64, ptr, ptr, ptr, ptr, i64, i32 }
%struct._CMatchFinderMt = type { ptr, ptr, ptr, ptr, i32, i32, ptr, i32, i32, ptr, ptr, i32, [1 x i32], %struct._CMtSync, [128 x i8], ptr, i32, i32, i32, i32, ptr, i32, i32, i32, ptr, i32, i32, i32, %struct._CMtSync, ptr, ptr }
%struct._CMtSync = type { i32, %struct._CThread, i64, i32, i32, i32, i32, i32, i32, %struct._CEvent, %struct._CEvent, %struct._CSemaphore, %struct._CSemaphore, %struct._CCriticalSection }
%struct._CThread = type { i64, i32 }
%struct._CEvent = type { i32, i32, i32, %union.pthread_mutex_t, %union.pthread_cond_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }
%struct._CSemaphore = type { i32, i32, i32, %union.pthread_mutex_t, %union.pthread_cond_t }
%struct._CCriticalSection = type { %union.pthread_mutex_t }
%struct._CMatchFinder = type { ptr, i32, i32, i32, i32, i32, i32, i8, i8, i8, i8, i32, ptr, ptr, i32, i32, ptr, ptr, i32, i32, i32, i32, i64, i32, i32, i32, i32, [256 x i32], i64, i64 }
%struct.CLenEnc = type { [256 x i16], [256 x i16] }
%struct.CLenPriceEnc = type { i32, [16 x [272 x i32]] }
%struct.COptimal = type { i32, i16, i16, i32, i32, [4 x i32] }
%struct.CSaveState = type { ptr, i32, [4 x i32], [16 x i16], [12 x i16], [12 x i16], [12 x i16], [12 x i16], [12 x [16 x i16]], [12 x [16 x i16]], [4 x [64 x i16]], [128 x i16], %struct.CLenEnc, %struct.CLenEnc }

; 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
define dso_local i32 @LzmaEnc_WriteProperties(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = load i64, ptr %13, align 8, !tbaa !9
  %15 = icmp ult i64 %14, 5
  br i1 %15, label %16, label %17

16:                                               ; preds = %3
  store i32 5, ptr %4, align 4
  br label %70

17:                                               ; preds = %3
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 5, ptr %18, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %19, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.CLzmaEnc, ptr %20, i32 0, i32 29
  %22 = load i32, ptr %21, align 4, !tbaa !11
  store i32 %22, ptr %9, align 4, !tbaa !27
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.CLzmaEnc, ptr %23, i32 0, i32 18
  %25 = load i32, ptr %24, align 4, !tbaa !28
  %26 = mul i32 %25, 5
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.CLzmaEnc, ptr %27, i32 0, i32 17
  %29 = load i32, ptr %28, align 8, !tbaa !29
  %30 = add i32 %26, %29
  %31 = mul i32 %30, 9
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.CLzmaEnc, ptr %32, i32 0, i32 16
  %34 = load i32, ptr %33, align 4, !tbaa !30
  %35 = add i32 %31, %34
  %36 = trunc i32 %35 to i8
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i64 0
  store i8 %36, ptr %38, align 1, !tbaa !31
  %39 = load i32, ptr %9, align 4, !tbaa !27
  %40 = icmp uge i32 %39, 2097152
  br i1 %40, label %41, label %51

41:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 1048575, ptr %11, align 4, !tbaa !27
  %42 = load i32, ptr %9, align 4, !tbaa !27
  %43 = add i32 %42, 1048575
  %44 = and i32 %43, -1048576
  store i32 %44, ptr %10, align 4, !tbaa !27
  %45 = load i32, ptr %10, align 4, !tbaa !27
  %46 = load i32, ptr %9, align 4, !tbaa !27
  %47 = icmp ult i32 %45, %46
  br i1 %47, label %48, label %50

48:                                               ; preds = %41
  %49 = load i32, ptr %9, align 4, !tbaa !27
  store i32 %49, ptr %10, align 4, !tbaa !27
  br label %50

50:                                               ; preds = %48, %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %66

51:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 22, ptr %12, align 4, !tbaa !27
  br label %52

52:                                               ; preds = %61, %51
  %53 = load i32, ptr %12, align 4, !tbaa !27
  %54 = and i32 %53, 1
  %55 = add i32 2, %54
  %56 = load i32, ptr %12, align 4, !tbaa !27
  %57 = lshr i32 %56, 1
  %58 = shl i32 %55, %57
  store i32 %58, ptr %10, align 4, !tbaa !27
  %59 = load i32, ptr %12, align 4, !tbaa !27
  %60 = add i32 %59, 1
  store i32 %60, ptr %12, align 4, !tbaa !27
  br label %61

61:                                               ; preds = %52
  %62 = load i32, ptr %10, align 4, !tbaa !27
  %63 = load i32, ptr %9, align 4, !tbaa !27
  %64 = icmp ult i32 %62, %63
  br i1 %64, label %52, label %65, !llvm.loop !32

65:                                               ; preds = %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %66

66:                                               ; preds = %65, %50
  %67 = load i32, ptr %10, align 4, !tbaa !27
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = getelementptr inbounds i8, ptr %68, i64 1
  store i32 %67, ptr %69, align 4, !tbaa !27
  store i32 0, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %70

70:                                               ; preds = %66, %16
  %71 = load i32, ptr %4, align 4
  ret i32 %71
}

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 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !14, i64 244}
!12 = !{!"", !6, i64 0, !13, i64 8, !14, i64 48, !14, i64 52, !14, i64 56, !14, i64 60, !14, i64 64, !14, i64 68, !14, i64 72, !14, i64 76, !7, i64 80, !14, i64 96, !14, i64 100, !6, i64 104, !15, i64 112, !14, i64 184, !14, i64 188, !14, i64 192, !14, i64 196, !14, i64 200, !14, i64 204, !14, i64 208, !14, i64 212, !14, i64 216, !14, i64 220, !16, i64 224, !14, i64 232, !14, i64 236, !14, i64 240, !14, i64 244, !14, i64 248, !14, i64 252, !17, i64 256, !23, i64 1576, !7, i64 2736, !7, i64 2864, !7, i64 3376, !7, i64 5568, !7, i64 5632, !7, i64 6656, !7, i64 8704, !7, i64 8736, !7, i64 8760, !7, i64 8784, !7, i64 8808, !7, i64 8832, !7, i64 9216, !7, i64 9600, !7, i64 10112, !24, i64 10368, !24, i64 11392, !7, i64 12416, !25, i64 28800, !25, i64 46212, !7, i64 63624, !26, i64 129160, !7, i64 132904}
!13 = !{!"_IMatchFinder", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!14 = !{!"int", !7, i64 0}
!15 = !{!"", !14, i64 0, !14, i64 4, !16, i64 8, !16, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !16, i64 56, !14, i64 64}
!16 = !{!"long long", !7, i64 0}
!17 = !{!"_CMatchFinderMt", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !14, i64 32, !14, i64 36, !6, i64 40, !14, i64 48, !14, i64 52, !6, i64 56, !6, i64 64, !14, i64 72, !7, i64 76, !18, i64 80, !7, i64 592, !6, i64 720, !14, i64 728, !14, i64 732, !14, i64 736, !14, i64 740, !6, i64 744, !14, i64 752, !14, i64 756, !14, i64 760, !6, i64 768, !14, i64 776, !14, i64 780, !14, i64 784, !18, i64 792, !6, i64 1304, !6, i64 1312}
!18 = !{!"_CMtSync", !14, i64 0, !19, i64 8, !16, i64 24, !14, i64 32, !14, i64 36, !14, i64 40, !14, i64 44, !14, i64 48, !14, i64 52, !20, i64 56, !20, i64 160, !21, i64 264, !21, i64 368, !22, i64 472}
!19 = !{!"_CThread", !10, i64 0, !14, i64 8}
!20 = !{!"_CEvent", !14, i64 0, !14, i64 4, !14, i64 8, !7, i64 16, !7, i64 56}
!21 = !{!"_CSemaphore", !14, i64 0, !14, i64 4, !14, i64 8, !7, i64 16, !7, i64 56}
!22 = !{!"_CCriticalSection", !7, i64 0}
!23 = !{!"_CMatchFinder", !6, i64 0, !14, i64 8, !14, i64 12, !14, i64 16, !14, i64 20, !14, i64 24, !14, i64 28, !7, i64 32, !7, i64 33, !7, i64 34, !7, i64 35, !14, i64 36, !6, i64 40, !6, i64 48, !14, i64 56, !14, i64 60, !6, i64 64, !6, i64 72, !14, i64 80, !14, i64 84, !14, i64 88, !14, i64 92, !10, i64 96, !14, i64 104, !14, i64 108, !14, i64 112, !14, i64 116, !7, i64 120, !10, i64 1144, !16, i64 1152}
!24 = !{!"", !7, i64 0, !7, i64 512}
!25 = !{!"", !14, i64 0, !7, i64 4}
!26 = !{!"", !6, i64 0, !14, i64 8, !7, i64 12, !7, i64 28, !7, i64 60, !7, i64 84, !7, i64 108, !7, i64 132, !7, i64 156, !7, i64 540, !7, i64 924, !7, i64 1436, !24, i64 1692, !24, i64 2716}
!27 = !{!14, !14, i64 0}
!28 = !{!12, !14, i64 196}
!29 = !{!12, !14, i64 192}
!30 = !{!12, !14, i64 188}
!31 = !{!7, !7, i64 0}
!32 = distinct !{!32, !33}
!33 = !{!"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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local range(i32 0, 6) i32 @LzmaEnc_WriteProperties(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef %2) local_unnamed_addr #0 {
  %4 = load i64, ptr %2, align 8, !tbaa !5
  %5 = icmp ult i64 %4, 5
  br i1 %5, label %31, label %6

6:                                                ; preds = %3
  store i64 5, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 244
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %0, i64 196
  %10 = load i32, ptr %9, align 4, !tbaa !26
  %11 = mul i32 %10, 5
  %12 = getelementptr inbounds i8, ptr %0, i64 192
  %13 = load i32, ptr %12, align 8, !tbaa !27
  %14 = add i32 %11, %13
  %15 = mul i32 %14, 9
  %16 = getelementptr inbounds i8, ptr %0, i64 188
  %17 = load i32, ptr %16, align 4, !tbaa !28
  %18 = add i32 %15, %17
  %19 = trunc i32 %18 to i8
  store i8 %19, ptr %1, align 1, !tbaa !29
  %20 = icmp ugt i32 %8, 2097151
  br i1 %20, label %21, label %.preheader

21:                                               ; preds = %6
  %22 = add i32 %8, 1048575
  %23 = and i32 %22, -1048576
  %spec.select = tail call i32 @llvm.umax.i32(i32 %23, i32 %8)
  br label %.loopexit

.preheader:                                       ; preds = %6, %.preheader
  %.0 = phi i32 [ %28, %.preheader ], [ 22, %6 ]
  %24 = and i32 %.0, 1
  %25 = or disjoint i32 %24, 2
  %26 = lshr i32 %.0, 1
  %27 = shl i32 %25, %26
  %28 = add i32 %.0, 1
  %29 = icmp ult i32 %27, %8
  br i1 %29, label %.preheader, label %.loopexit, !llvm.loop !30

.loopexit:                                        ; preds = %.preheader, %21
  %.1 = phi i32 [ %spec.select, %21 ], [ %27, %.preheader ]
  %30 = getelementptr inbounds i8, ptr %1, i64 1
  store i32 %.1, ptr %30, align 4, !tbaa !32
  br label %31

31:                                               ; preds = %3, %.loopexit
  %.020 = phi i32 [ 0, %.loopexit ], [ 5, %3 ]
  ret i32 %.020
}

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

attributes #0 = { nofree norecurse nosync nounwind 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, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !13, i64 244}
!10 = !{!"", !11, i64 0, !12, i64 8, !13, i64 48, !13, i64 52, !13, i64 56, !13, i64 60, !13, i64 64, !13, i64 68, !13, i64 72, !13, i64 76, !7, i64 80, !13, i64 96, !13, i64 100, !11, i64 104, !14, i64 112, !13, i64 184, !13, i64 188, !13, i64 192, !13, i64 196, !13, i64 200, !13, i64 204, !13, i64 208, !13, i64 212, !13, i64 216, !13, i64 220, !15, i64 224, !13, i64 232, !13, i64 236, !13, i64 240, !13, i64 244, !13, i64 248, !13, i64 252, !16, i64 256, !22, i64 1576, !7, i64 2736, !7, i64 2864, !7, i64 3376, !7, i64 5568, !7, i64 5632, !7, i64 6656, !7, i64 8704, !7, i64 8736, !7, i64 8760, !7, i64 8784, !7, i64 8808, !7, i64 8832, !7, i64 9216, !7, i64 9600, !7, i64 10112, !23, i64 10368, !23, i64 11392, !7, i64 12416, !24, i64 28800, !24, i64 46212, !7, i64 63624, !25, i64 129160, !7, i64 132904}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"_IMatchFinder", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}
!13 = !{!"int", !7, i64 0}
!14 = !{!"", !13, i64 0, !13, i64 4, !15, i64 8, !15, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !15, i64 56, !13, i64 64}
!15 = !{!"long long", !7, i64 0}
!16 = !{!"_CMatchFinderMt", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !13, i64 32, !13, i64 36, !11, i64 40, !13, i64 48, !13, i64 52, !11, i64 56, !11, i64 64, !13, i64 72, !7, i64 76, !17, i64 80, !7, i64 592, !11, i64 720, !13, i64 728, !13, i64 732, !13, i64 736, !13, i64 740, !11, i64 744, !13, i64 752, !13, i64 756, !13, i64 760, !11, i64 768, !13, i64 776, !13, i64 780, !13, i64 784, !17, i64 792, !11, i64 1304, !11, i64 1312}
!17 = !{!"_CMtSync", !13, i64 0, !18, i64 8, !15, i64 24, !13, i64 32, !13, i64 36, !13, i64 40, !13, i64 44, !13, i64 48, !13, i64 52, !19, i64 56, !19, i64 160, !20, i64 264, !20, i64 368, !21, i64 472}
!18 = !{!"_CThread", !6, i64 0, !13, i64 8}
!19 = !{!"_CEvent", !13, i64 0, !13, i64 4, !13, i64 8, !7, i64 16, !7, i64 56}
!20 = !{!"_CSemaphore", !13, i64 0, !13, i64 4, !13, i64 8, !7, i64 16, !7, i64 56}
!21 = !{!"_CCriticalSection", !7, i64 0}
!22 = !{!"_CMatchFinder", !11, i64 0, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20, !13, i64 24, !13, i64 28, !7, i64 32, !7, i64 33, !7, i64 34, !7, i64 35, !13, i64 36, !11, i64 40, !11, i64 48, !13, i64 56, !13, i64 60, !11, i64 64, !11, i64 72, !13, i64 80, !13, i64 84, !13, i64 88, !13, i64 92, !6, i64 96, !13, i64 104, !13, i64 108, !13, i64 112, !13, i64 116, !7, i64 120, !6, i64 1144, !15, i64 1152}
!23 = !{!"", !7, i64 0, !7, i64 512}
!24 = !{!"", !13, i64 0, !7, i64 4}
!25 = !{!"", !11, i64 0, !13, i64 8, !7, i64 12, !7, i64 28, !7, i64 60, !7, i64 84, !7, i64 108, !7, i64 132, !7, i64 156, !7, i64 540, !7, i64 924, !7, i64 1436, !23, i64 1692, !23, i64 2716}
!26 = !{!10, !13, i64 196}
!27 = !{!10, !13, i64 192}
!28 = !{!10, !13, i64 188}
!29 = !{!7, !7, i64 0}
!30 = distinct !{!30, !31}
!31 = !{!"llvm.loop.mustprogress"}
!32 = !{!13, !13, i64 0}


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