; LLVM IR from fwip/fastqz :: libzpaq.ll (ZN7libzpaq7Encoder6encodeEii)

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

%"class.libzpaq::Encoder" = type { ptr, i32, i32, %"class.libzpaq::Predictor", %"class.libzpaq::Array.4" }
%"class.libzpaq::Predictor" = type <{ i32, i32, [256 x i32], [256 x i32], ptr, [256 x %"struct.libzpaq::Component"], [256 x i32], [1024 x i32], [4096 x i16], [32768 x i16], %"class.libzpaq::StateTable", ptr, i32, [4 x i8] }>
%"struct.libzpaq::Component" = type { i64, i64, i64, i64, i64, %"class.libzpaq::Array", %"class.libzpaq::Array.0", %"class.libzpaq::Array.2" }
%"class.libzpaq::Array" = type <{ ptr, i64, i32, [4 x i8] }>
%"class.libzpaq::Array.0" = type <{ ptr, i64, i32, [4 x i8] }>
%"class.libzpaq::Array.2" = type <{ ptr, i64, i32, [4 x i8] }>
%"class.libzpaq::StateTable" = type { [1024 x i8] }
%"class.libzpaq::Array.4" = type <{ ptr, i64, i32, [4 x i8] }>

; 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: mustprogress uwtable
define dso_local void @_ZN7libzpaq7Encoder6encodeEii(ptr noundef nonnull align 8 dereferenceable(110664) %0, i32 noundef %1, i32 noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %8 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %9 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 1
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %11 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 2
  %12 = load i32, ptr %11, align 4, !tbaa !17
  %13 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 1
  %14 = load i32, ptr %13, align 8, !tbaa !11
  %15 = sub i32 %12, %14
  %16 = zext i32 %15 to i64
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = zext i32 %17 to i64
  %19 = mul i64 %16, %18
  %20 = lshr i64 %19, 16
  %21 = trunc i64 %20 to i32
  %22 = add i32 %10, %21
  store i32 %22, ptr %7, align 4, !tbaa !9
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %28

25:                                               ; preds = %3
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 2
  store i32 %26, ptr %27, align 4, !tbaa !17
  br label %32

28:                                               ; preds = %3
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = add i32 %29, 1
  %31 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 1
  store i32 %30, ptr %31, align 8, !tbaa !11
  br label %32

32:                                               ; preds = %28, %25
  br label %33

33:                                               ; preds = %40, %32
  %34 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 2
  %35 = load i32, ptr %34, align 4, !tbaa !17
  %36 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 1
  %37 = load i32, ptr %36, align 8, !tbaa !11
  %38 = xor i32 %35, %37
  %39 = icmp ult i32 %38, 16777216
  br i1 %39, label %40, label %65

40:                                               ; preds = %33
  %41 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !18
  %43 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 2
  %44 = load i32, ptr %43, align 4, !tbaa !17
  %45 = lshr i32 %44, 24
  %46 = load ptr, ptr %42, align 8, !tbaa !19
  %47 = getelementptr inbounds ptr, ptr %46, i64 0
  %48 = load ptr, ptr %47, align 8
  call void %48(ptr noundef nonnull align 8 dereferenceable(8) %42, i32 noundef %45)
  %49 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 2
  %50 = load i32, ptr %49, align 4, !tbaa !17
  %51 = shl i32 %50, 8
  %52 = or i32 %51, 255
  %53 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 2
  store i32 %52, ptr %53, align 4, !tbaa !17
  %54 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 1
  %55 = load i32, ptr %54, align 8, !tbaa !11
  %56 = shl i32 %55, 8
  %57 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 1
  store i32 %56, ptr %57, align 8, !tbaa !11
  %58 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 1
  %59 = load i32, ptr %58, align 8, !tbaa !11
  %60 = icmp eq i32 %59, 0
  %61 = zext i1 %60 to i32
  %62 = getelementptr inbounds %"class.libzpaq::Encoder", ptr %8, i32 0, i32 1
  %63 = load i32, ptr %62, align 8, !tbaa !11
  %64 = add i32 %63, %61
  store i32 %64, ptr %62, align 8, !tbaa !11
  br label %33, !llvm.loop !21

65:                                               ; preds = %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTSN7libzpaq7EncoderE", !6, i64 0, !10, i64 8, !10, i64 12, !13, i64 16, !15, i64 110640}
!13 = !{!"_ZTSN7libzpaq9PredictorE", !10, i64 0, !10, i64 4, !7, i64 8, !7, i64 1032, !6, i64 2056, !7, i64 2064, !7, i64 30736, !7, i64 31760, !7, i64 35856, !7, i64 44048, !14, i64 109584, !6, i64 110608, !10, i64 110616}
!14 = !{!"_ZTSN7libzpaq10StateTableE", !7, i64 0}
!15 = !{!"_ZTSN7libzpaq5ArrayIcEE", !6, i64 0, !16, i64 8, !10, i64 16}
!16 = !{!"long", !7, i64 0}
!17 = !{!12, !10, i64 12}
!18 = !{!12, !6, i64 0}
!19 = !{!20, !20, i64 0}
!20 = !{!"vtable pointer", !8, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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: mustprogress uwtable
define dso_local void @_ZN7libzpaq7Encoder6encodeEii(ptr nocapture noundef nonnull align 8 dereferenceable(110664) %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 12
  %7 = load i32, ptr %6, align 4, !tbaa !15
  %8 = sub i32 %7, %5
  %9 = zext i32 %8 to i64
  %10 = zext i32 %2 to i64
  %11 = mul nuw i64 %9, %10
  %12 = lshr i64 %11, 16
  %13 = trunc i64 %12 to i32
  %14 = add i32 %5, %13
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %16, label %15

15:                                               ; preds = %3
  store i32 %14, ptr %6, align 4, !tbaa !15
  br label %18

16:                                               ; preds = %3
  %17 = add i32 %14, 1
  store i32 %17, ptr %4, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %16, %15
  %19 = phi i32 [ %17, %16 ], [ %5, %15 ]
  %20 = phi i32 [ %7, %16 ], [ %14, %15 ]
  %21 = xor i32 %19, %20
  %22 = icmp ult i32 %21, 16777216
  br i1 %22, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %18, %.lr.ph
  %23 = phi i32 [ %30, %.lr.ph ], [ %20, %18 ]
  %24 = load ptr, ptr %0, align 8, !tbaa !16
  %25 = lshr i32 %23, 24
  %26 = load ptr, ptr %24, align 8, !tbaa !17
  %27 = load ptr, ptr %26, align 8
  tail call void %27(ptr noundef nonnull align 8 dereferenceable(8) %24, i32 noundef %25)
  %28 = load i32, ptr %6, align 4, !tbaa !15
  %29 = shl i32 %28, 8
  %30 = or disjoint i32 %29, 255
  store i32 %30, ptr %6, align 4, !tbaa !15
  %31 = load i32, ptr %4, align 8, !tbaa !5
  %32 = shl i32 %31, 8
  %33 = icmp eq i32 %32, 0
  %34 = zext i1 %33 to i32
  %35 = or disjoint i32 %32, %34
  store i32 %35, ptr %4, align 8, !tbaa !5
  %36 = xor i32 %32, %29
  %37 = icmp ult i32 %36, 16777216
  br i1 %37, label %.lr.ph, label %._crit_edge, !llvm.loop !19

._crit_edge:                                      ; preds = %.lr.ph, %18
  ret void
}

attributes #0 = { mustprogress 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 8}
!6 = !{!"_ZTSN7libzpaq7EncoderE", !7, i64 0, !10, i64 8, !10, i64 12, !11, i64 16, !13, i64 110640}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"_ZTSN7libzpaq9PredictorE", !10, i64 0, !10, i64 4, !8, i64 8, !8, i64 1032, !7, i64 2056, !8, i64 2064, !8, i64 30736, !8, i64 31760, !8, i64 35856, !8, i64 44048, !12, i64 109584, !7, i64 110608, !10, i64 110616}
!12 = !{!"_ZTSN7libzpaq10StateTableE", !8, i64 0}
!13 = !{!"_ZTSN7libzpaq5ArrayIcEE", !7, i64 0, !14, i64 8, !10, i64 16}
!14 = !{!"long", !8, i64 0}
!15 = !{!6, !10, i64 12}
!16 = !{!6, !7, i64 0}
!17 = !{!18, !18, i64 0}
!18 = !{!"vtable pointer", !9, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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