; LLVM IR from indygreg/python-zstandard :: zstd (zstd)

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

@kInverseProbabilityLog256 = external hidden constant [256 x i32], align 16

; 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 i64 @ZSTD_crossEntropyCost(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = sub i32 8, %14
  store i32 %15, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store i64 0, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %54, %4
  %17 = load i32, ptr %11, align 4, !tbaa !9
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = icmp ule i32 %17, %18
  br i1 %19, label %20, label %57

20:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = load i32, ptr %11, align 4, !tbaa !9
  %23 = zext i32 %22 to i64
  %24 = getelementptr inbounds i16, ptr %21, i64 %23
  %25 = load i16, ptr %24, align 2, !tbaa !13
  %26 = sext i16 %25 to i32
  %27 = icmp ne i32 %26, -1
  br i1 %27, label %28, label %35

28:                                               ; preds = %20
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = zext i32 %30 to i64
  %32 = getelementptr inbounds i16, ptr %29, i64 %31
  %33 = load i16, ptr %32, align 2, !tbaa !13
  %34 = sext i16 %33 to i32
  br label %36

35:                                               ; preds = %20
  br label %36

36:                                               ; preds = %35, %28
  %37 = phi i32 [ %34, %28 ], [ 1, %35 ]
  store i32 %37, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %38 = load i32, ptr %12, align 4, !tbaa !9
  %39 = load i32, ptr %9, align 4, !tbaa !9
  %40 = shl i32 %38, %39
  store i32 %40, ptr %13, align 4, !tbaa !9
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = zext i32 %42 to i64
  %44 = getelementptr inbounds i32, ptr %41, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = load i32, ptr %13, align 4, !tbaa !9
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds [256 x i32], ptr @kInverseProbabilityLog256, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !9
  %50 = mul i32 %45, %49
  %51 = zext i32 %50 to i64
  %52 = load i64, ptr %10, align 8, !tbaa !11
  %53 = add i64 %52, %51
  store i64 %53, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %54

54:                                               ; preds = %36
  %55 = load i32, ptr %11, align 4, !tbaa !9
  %56 = add i32 %55, 1
  store i32 %56, ptr %11, align 4, !tbaa !9
  br label %16, !llvm.loop !15

57:                                               ; preds = %16
  %58 = load i64, ptr %10, align 8, !tbaa !11
  %59 = lshr i64 %58, 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret i64 %59
}

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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

@kInverseProbabilityLog256 = external hidden local_unnamed_addr constant [256 x i32], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local range(i64 0, 72057594037927936) i64 @ZSTD_crossEntropyCost(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef readonly %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = sub i32 8, %1
  %6 = add i32 %3, 1
  %umax = tail call i32 @llvm.umax.i32(i32 %6, i32 1)
  %wide.trip.count = zext i32 %umax to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %7 = icmp ult i32 %6, 2
  br i1 %7, label %.unr-lcssa, label %.new

.new:                                             ; preds = %4
  %unroll_iter = and i64 %wide.trip.count, 4294967294
  br label %8

8:                                                ; preds = %8, %.new
  %indvars.iv = phi i64 [ 0, %.new ], [ %indvars.iv.next.1, %8 ]
  %.018 = phi i64 [ 0, %.new ], [ %30, %8 ]
  %niter = phi i64 [ 0, %.new ], [ %niter.next.1, %8 ]
  %9 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv
  %10 = load i16, ptr %9, align 2, !tbaa !5
  %.not16 = icmp eq i16 %10, -1
  %narrow = select i1 %.not16, i16 1, i16 %10
  %spec.select = sext i16 %narrow to i32
  %11 = shl i32 %spec.select, %5
  %12 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv
  %13 = load i32, ptr %12, align 4, !tbaa !9
  %14 = zext i32 %11 to i64
  %15 = getelementptr inbounds [256 x i32], ptr @kInverseProbabilityLog256, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = mul i32 %16, %13
  %18 = zext i32 %17 to i64
  %19 = add i64 %.018, %18
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %20 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv.next
  %21 = load i16, ptr %20, align 2, !tbaa !5
  %.not16.1 = icmp eq i16 %21, -1
  %narrow.1 = select i1 %.not16.1, i16 1, i16 %21
  %spec.select.1 = sext i16 %narrow.1 to i32
  %22 = shl i32 %spec.select.1, %5
  %23 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv.next
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = zext i32 %22 to i64
  %26 = getelementptr inbounds [256 x i32], ptr @kInverseProbabilityLog256, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = mul i32 %27, %24
  %29 = zext i32 %28 to i64
  %30 = add i64 %19, %29
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.unr-lcssa, label %8, !llvm.loop !11

.unr-lcssa:                                       ; preds = %8, %4
  %.lcssa.ph = phi i64 [ poison, %4 ], [ %30, %8 ]
  %indvars.iv.unr = phi i64 [ 0, %4 ], [ %indvars.iv.next.1, %8 ]
  %.018.unr = phi i64 [ 0, %4 ], [ %30, %8 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %42, label %.epilog-lcssa

.epilog-lcssa:                                    ; preds = %.unr-lcssa
  %31 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv.unr
  %32 = load i16, ptr %31, align 2, !tbaa !5
  %.not16.epil = icmp eq i16 %32, -1
  %narrow.epil = select i1 %.not16.epil, i16 1, i16 %32
  %spec.select.epil = sext i16 %narrow.epil to i32
  %33 = shl i32 %spec.select.epil, %5
  %34 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv.unr
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = zext i32 %33 to i64
  %37 = getelementptr inbounds [256 x i32], ptr @kInverseProbabilityLog256, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = mul i32 %38, %35
  %40 = zext i32 %39 to i64
  %41 = add i64 %.018.unr, %40
  br label %42

42:                                               ; preds = %.unr-lcssa, %.epilog-lcssa
  %.lcssa = phi i64 [ %.lcssa.ph, %.unr-lcssa ], [ %41, %.epilog-lcssa ]
  %43 = lshr i64 %.lcssa, 8
  ret i64 %43
}

; 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: read) 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 = !{!"short", !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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass