; LLVM IR from glaretechnologies/substrata :: opus/opus-1.3.1/silk (gain_quant)

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare i32 @silk_log2lin(i32 noundef) #1

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

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

14:                                               ; preds = %107, %5
  %15 = load i32, ptr %11, align 4, !tbaa !9
  %16 = load i32, ptr %10, align 4, !tbaa !9
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %18, label %110

18:                                               ; preds = %14
  %19 = load i32, ptr %11, align 4, !tbaa !9
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %38

21:                                               ; preds = %18
  %22 = load i32, ptr %9, align 4, !tbaa !9
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %38

24:                                               ; preds = %21
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = load i32, ptr %11, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i8, ptr %25, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !11
  %30 = sext i8 %29 to i32
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = load i8, ptr %31, align 1, !tbaa !11
  %33 = sext i8 %32 to i32
  %34 = sub nsw i32 %33, 16
  %35 = call i32 @silk_max_int(i32 noundef %30, i32 noundef %34)
  %36 = trunc i32 %35 to i8
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  store i8 %36, ptr %37, align 1, !tbaa !11
  br label %71

38:                                               ; preds = %21, %18
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = load i32, ptr %11, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i8, ptr %39, i64 %41
  %43 = load i8, ptr %42, align 1, !tbaa !11
  %44 = sext i8 %43 to i32
  %45 = add nsw i32 %44, -4
  store i32 %45, ptr %12, align 4, !tbaa !9
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = load i8, ptr %46, align 1, !tbaa !11
  %48 = sext i8 %47 to i32
  %49 = add nsw i32 8, %48
  store i32 %49, ptr %13, align 4, !tbaa !9
  %50 = load i32, ptr %12, align 4, !tbaa !9
  %51 = load i32, ptr %13, align 4, !tbaa !9
  %52 = icmp sgt i32 %50, %51
  br i1 %52, label %53, label %63

53:                                               ; preds = %38
  %54 = load i32, ptr %12, align 4, !tbaa !9
  %55 = shl i32 %54, 1
  %56 = load i32, ptr %13, align 4, !tbaa !9
  %57 = sub nsw i32 %55, %56
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = load i8, ptr %58, align 1, !tbaa !11
  %60 = sext i8 %59 to i32
  %61 = add nsw i32 %60, %57
  %62 = trunc i32 %61 to i8
  store i8 %62, ptr %58, align 1, !tbaa !11
  br label %70

63:                                               ; preds = %38
  %64 = load i32, ptr %12, align 4, !tbaa !9
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = load i8, ptr %65, align 1, !tbaa !11
  %67 = sext i8 %66 to i32
  %68 = add nsw i32 %67, %64
  %69 = trunc i32 %68 to i8
  store i8 %69, ptr %65, align 1, !tbaa !11
  br label %70

70:                                               ; preds = %63, %53
  br label %71

71:                                               ; preds = %70, %24
  %72 = load ptr, ptr %8, align 8, !tbaa !5
  %73 = load i8, ptr %72, align 1, !tbaa !11
  %74 = sext i8 %73 to i32
  %75 = icmp sgt i32 %74, 63
  br i1 %75, label %76, label %77

76:                                               ; preds = %71
  br label %89

77:                                               ; preds = %71
  %78 = load ptr, ptr %8, align 8, !tbaa !5
  %79 = load i8, ptr %78, align 1, !tbaa !11
  %80 = sext i8 %79 to i32
  %81 = icmp slt i32 %80, 0
  br i1 %81, label %82, label %83

82:                                               ; preds = %77
  br label %87

83:                                               ; preds = %77
  %84 = load ptr, ptr %8, align 8, !tbaa !5
  %85 = load i8, ptr %84, align 1, !tbaa !11
  %86 = sext i8 %85 to i32
  br label %87

87:                                               ; preds = %83, %82
  %88 = phi i32 [ 0, %82 ], [ %86, %83 ]
  br label %89

89:                                               ; preds = %87, %76
  %90 = phi i32 [ 63, %76 ], [ %88, %87 ]
  %91 = trunc i32 %90 to i8
  %92 = load ptr, ptr %8, align 8, !tbaa !5
  store i8 %91, ptr %92, align 1, !tbaa !11
  %93 = load ptr, ptr %8, align 8, !tbaa !5
  %94 = load i8, ptr %93, align 1, !tbaa !11
  %95 = sext i8 %94 to i16
  %96 = sext i16 %95 to i64
  %97 = mul nsw i64 1907825, %96
  %98 = ashr i64 %97, 16
  %99 = trunc i64 %98 to i32
  %100 = add nsw i32 %99, 2090
  %101 = call i32 @silk_min_32(i32 noundef %100, i32 noundef 3967)
  %102 = call i32 @silk_log2lin(i32 noundef %101)
  %103 = load ptr, ptr %6, align 8, !tbaa !5
  %104 = load i32, ptr %11, align 4, !tbaa !9
  %105 = sext i32 %104 to i64
  %106 = getelementptr inbounds i32, ptr %103, i64 %105
  store i32 %102, ptr %106, align 4, !tbaa !9
  br label %107

107:                                              ; preds = %89
  %108 = load i32, ptr %11, align 4, !tbaa !9
  %109 = add nsw i32 %108, 1
  store i32 %109, ptr %11, align 4, !tbaa !9
  br label %14, !llvm.loop !12

110:                                              ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  ret void
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #4 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

declare i32 @silk_log2lin(i32 noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @silk_min_32(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @silk_gains_dequant(ptr nocapture noundef writeonly %0, ptr nocapture noundef readonly %1, ptr nocapture noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #2 {
  %6 = icmp sgt i32 %4, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %5
  %wide.trip.count = zext nneg i32 %4 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %34
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %34 ]
  %7 = trunc nuw nsw i64 %indvars.iv to i32
  %8 = or i32 %7, %3
  %or.cond = icmp eq i32 %8, 0
  br i1 %or.cond, label %9, label %17

9:                                                ; preds = %.lr.ph
  %10 = load i8, ptr %1, align 1, !tbaa !5
  %11 = sext i8 %10 to i32
  %12 = load i8, ptr %2, align 1, !tbaa !5
  %13 = sext i8 %12 to i32
  %14 = add nsw i32 %13, -16
  %15 = tail call i32 @silk_max_int(i32 noundef %11, i32 noundef %14)
  %16 = trunc i32 %15 to i8
  br label %34

17:                                               ; preds = %.lr.ph
  %18 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv
  %19 = load i8, ptr %18, align 1, !tbaa !5
  %20 = sext i8 %19 to i32
  %21 = add nsw i32 %20, -4
  %22 = load i8, ptr %2, align 1, !tbaa !5
  %23 = sext i8 %22 to i32
  %24 = add nsw i32 %23, 8
  %25 = icmp sgt i32 %21, %24
  br i1 %25, label %26, label %31

26:                                               ; preds = %17
  %27 = shl nsw i32 %21, 1
  %28 = sub nsw i32 %27, %24
  %29 = trunc i32 %28 to i8
  %30 = add i8 %22, %29
  br label %34

31:                                               ; preds = %17
  %32 = trunc i32 %21 to i8
  %33 = add i8 %22, %32
  br label %34

34:                                               ; preds = %26, %31, %9
  %35 = phi i8 [ %30, %26 ], [ %33, %31 ], [ %16, %9 ]
  %spec.select = tail call i8 @llvm.smax.i8(i8 %35, i8 0)
  %36 = tail call i8 @llvm.umin.i8(i8 %spec.select, i8 63)
  store i8 %36, ptr %2, align 1, !tbaa !5
  %37 = zext nneg i8 %36 to i32
  %38 = mul nuw nsw i32 %37, 1907825
  %39 = lshr i32 %38, 16
  %40 = add nuw nsw i32 %39, 2090
  %41 = tail call i32 @silk_min_32(i32 noundef %40, i32 noundef 3967)
  %42 = tail call i32 @silk_log2lin(i32 noundef %41) #4
  %43 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  store i32 %42, ptr %43, align 4, !tbaa !8
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %34, %5
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @silk_max_int(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.smax.i8(i8, i8) #3

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

attributes #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 = { 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 #2 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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