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

%struct.FSE_CState_t = type { i64, ptr, ptr, i32 }

; 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_fseBitCost(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca %struct.FSE_CState_t, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 8, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 32, ptr %11) #3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  call void @FSE_initCState(ptr noundef %11, ptr noundef %16)
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = call i32 @ZSTD_getFSEMaxSymbolValue(ptr noundef %17)
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = icmp ult i32 %18, %19
  br i1 %20, label %21, label %25

21:                                               ; preds = %3
  br label %22

22:                                               ; preds = %21
  br label %23

23:                                               ; preds = %22
  br label %24

24:                                               ; preds = %23
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %77

25:                                               ; preds = %3
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %71, %25
  %27 = load i32, ptr %10, align 4, !tbaa !9
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = icmp ule i32 %27, %28
  br i1 %29, label %30, label %74

30:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %31 = getelementptr inbounds %struct.FSE_CState_t, ptr %11, i32 0, i32 3
  %32 = load i32, ptr %31, align 8, !tbaa !13
  store i32 %32, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %33 = load i32, ptr %13, align 4, !tbaa !9
  %34 = add i32 %33, 1
  %35 = shl i32 %34, 8
  store i32 %35, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %36 = getelementptr inbounds %struct.FSE_CState_t, ptr %11, i32 0, i32 2
  %37 = load ptr, ptr %36, align 8, !tbaa !15
  %38 = load i32, ptr %13, align 4, !tbaa !9
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = call i32 @FSE_bitCost(ptr noundef %37, i32 noundef %38, i32 noundef %39, i32 noundef 8)
  store i32 %40, ptr %15, align 4, !tbaa !9
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load i32, ptr %10, 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 = icmp eq i32 %45, 0
  br i1 %46, label %47, label %48

47:                                               ; preds = %30
  store i32 6, ptr %12, align 4
  br label %68

48:                                               ; preds = %30
  %49 = load i32, ptr %15, align 4, !tbaa !9
  %50 = load i32, ptr %14, align 4, !tbaa !9
  %51 = icmp uge i32 %49, %50
  br i1 %51, label %52, label %56

52:                                               ; preds = %48
  br label %53

53:                                               ; preds = %52
  br label %54

54:                                               ; preds = %53
  br label %55

55:                                               ; preds = %54
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %68

56:                                               ; preds = %48
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = load i32, ptr %10, align 4, !tbaa !9
  %59 = zext i32 %58 to i64
  %60 = getelementptr inbounds i32, ptr %57, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !9
  %62 = zext i32 %61 to i64
  %63 = load i32, ptr %15, align 4, !tbaa !9
  %64 = zext i32 %63 to i64
  %65 = mul i64 %62, %64
  %66 = load i64, ptr %9, align 8, !tbaa !11
  %67 = add i64 %66, %65
  store i64 %67, ptr %9, align 8, !tbaa !11
  store i32 0, ptr %12, align 4
  br label %68

68:                                               ; preds = %56, %55, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  %69 = load i32, ptr %12, align 4
  switch i32 %69, label %77 [
    i32 0, label %70
    i32 6, label %71
  ]

70:                                               ; preds = %68
  br label %71

71:                                               ; preds = %70, %68
  %72 = load i32, ptr %10, align 4, !tbaa !9
  %73 = add i32 %72, 1
  store i32 %73, ptr %10, align 4, !tbaa !9
  br label %26, !llvm.loop !16

74:                                               ; preds = %26
  %75 = load i64, ptr %9, align 8, !tbaa !11
  %76 = lshr i64 %75, 8
  store i64 %76, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %77

77:                                               ; preds = %74, %68, %24
  call void @llvm.lifetime.end.p0(i64 32, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %78 = load i64, ptr %4, align 8
  ret i64 %78
}

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

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

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

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 = { inlinehint 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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 24}
!14 = !{!"", !12, i64 0, !6, i64 8, !6, i64 16, !10, i64 24}
!15 = !{!14, !6, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"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"

%struct.FSE_CState_t = type { i64, ptr, ptr, 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
define dso_local i64 @ZSTD_fseBitCost(ptr noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = alloca %struct.FSE_CState_t, align 8
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %4) #3
  call void @FSE_initCState(ptr noundef nonnull %4, ptr noundef %0)
  %5 = call i32 @ZSTD_getFSEMaxSymbolValue(ptr noundef %0)
  %6 = icmp ult i32 %5, %2
  br i1 %6, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3
  %7 = getelementptr inbounds i8, ptr %4, i64 24
  %8 = getelementptr inbounds i8, ptr %4, i64 16
  br label %9

9:                                                ; preds = %.preheader, %25
  %.01932 = phi i64 [ 0, %.preheader ], [ %.120.ph, %25 ]
  %.02131 = phi i32 [ 0, %.preheader ], [ %26, %25 ]
  %10 = load i32, ptr %7, align 8, !tbaa !5
  %11 = load ptr, ptr %8, align 8, !tbaa !12
  %12 = call i32 @FSE_bitCost(ptr noundef %11, i32 noundef %10, i32 noundef %.02131, i32 noundef 8)
  %13 = zext i32 %.02131 to i64
  %14 = getelementptr inbounds i32, ptr %1, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !13
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %25, label %17

17:                                               ; preds = %9
  %18 = shl i32 %10, 8
  %19 = add i32 %18, 256
  %.not26 = icmp ult i32 %12, %19
  br i1 %.not26, label %20, label %.loopexit

20:                                               ; preds = %17
  %21 = zext i32 %15 to i64
  %22 = zext i32 %12 to i64
  %23 = mul nuw i64 %21, %22
  %24 = add i64 %23, %.01932
  br label %25

25:                                               ; preds = %20, %9
  %.120.ph = phi i64 [ %.01932, %9 ], [ %24, %20 ]
  %26 = add i32 %.02131, 1
  %.not = icmp ugt i32 %26, %2
  br i1 %.not, label %27, label %9, !llvm.loop !14

27:                                               ; preds = %25
  %28 = lshr i64 %.120.ph, 8
  br label %.loopexit

.loopexit:                                        ; preds = %17, %3, %27
  %.2 = phi i64 [ %28, %27 ], [ -1, %3 ], [ -1, %17 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %4) #3
  ret i64 %.2
}

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @FSE_bitCost(ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #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 = { inlinehint 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, !11, i64 24}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 16, !11, i64 24}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !10, i64 16}
!13 = !{!11, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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