; LLVM IR from kdrag0n/fxdsp :: external/oboe/src/flowgraph/resampler (IntegerRatio)

; 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.resampler::IntegerRatio" = type { i32, i32 }

@_ZL7kPrimes = external hidden constant [46 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN9resampler12IntegerRatio6reduceEv(ptr noundef nonnull align 4 dereferenceable(8) %0) #0 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %10 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store ptr @_ZL7kPrimes, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store ptr @_ZL7kPrimes, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store ptr getelementptr inbounds (i32, ptr @_ZL7kPrimes, i64 46), ptr %5, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %73, %1
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp ne ptr %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  store i32 2, ptr %6, align 4
  br label %76

16:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load i32, ptr %17, align 4, !tbaa !9
  store i32 %18, ptr %7, align 4, !tbaa !9
  %19 = getelementptr inbounds %"class.resampler::IntegerRatio", ptr %10, i32 0, i32 0
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %28, label %23

23:                                               ; preds = %16
  %24 = getelementptr inbounds %"class.resampler::IntegerRatio", ptr %10, i32 0, i32 1
  %25 = load i32, ptr %24, align 4, !tbaa !13
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %23, %16
  store i32 2, ptr %6, align 4
  br label %70

29:                                               ; preds = %23
  br label %30

30:                                               ; preds = %68, %29
  br label %31

31:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %32 = getelementptr inbounds %"class.resampler::IntegerRatio", ptr %10, i32 0, i32 0
  %33 = load i32, ptr %32, align 4, !tbaa !11
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = sdiv i32 %33, %34
  store i32 %35, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %36 = getelementptr inbounds %"class.resampler::IntegerRatio", ptr %10, i32 0, i32 1
  %37 = load i32, ptr %36, align 4, !tbaa !13
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = sdiv i32 %37, %38
  store i32 %39, ptr %9, align 4, !tbaa !9
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = icmp sge i32 %40, 1
  br i1 %41, label %42, label %64

42:                                               ; preds = %31
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = icmp sge i32 %43, 1
  br i1 %44, label %45, label %64

45:                                               ; preds = %42
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = mul nsw i32 %46, %47
  %49 = getelementptr inbounds %"class.resampler::IntegerRatio", ptr %10, i32 0, i32 0
  %50 = load i32, ptr %49, align 4, !tbaa !11
  %51 = icmp eq i32 %48, %50
  br i1 %51, label %52, label %64

52:                                               ; preds = %45
  %53 = load i32, ptr %9, align 4, !tbaa !9
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = mul nsw i32 %53, %54
  %56 = getelementptr inbounds %"class.resampler::IntegerRatio", ptr %10, i32 0, i32 1
  %57 = load i32, ptr %56, align 4, !tbaa !13
  %58 = icmp eq i32 %55, %57
  br i1 %58, label %59, label %64

59:                                               ; preds = %52
  %60 = load i32, ptr %8, align 4, !tbaa !9
  %61 = getelementptr inbounds %"class.resampler::IntegerRatio", ptr %10, i32 0, i32 0
  store i32 %60, ptr %61, align 4, !tbaa !11
  %62 = load i32, ptr %9, align 4, !tbaa !9
  %63 = getelementptr inbounds %"class.resampler::IntegerRatio", ptr %10, i32 0, i32 1
  store i32 %62, ptr %63, align 4, !tbaa !13
  br label %65

64:                                               ; preds = %52, %45, %42, %31
  store i32 5, ptr %6, align 4
  br label %66

65:                                               ; preds = %59
  store i32 0, ptr %6, align 4
  br label %66

66:                                               ; preds = %65, %64
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %67 = load i32, ptr %6, align 4
  switch i32 %67, label %78 [
    i32 0, label %68
    i32 5, label %69
  ]

68:                                               ; preds = %66
  br label %30, !llvm.loop !14

69:                                               ; preds = %66
  store i32 0, ptr %6, align 4
  br label %70

70:                                               ; preds = %69, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %71 = load i32, ptr %6, align 4
  switch i32 %71, label %76 [
    i32 0, label %72
  ]

72:                                               ; preds = %70
  br label %73

73:                                               ; preds = %72
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds i32, ptr %74, i32 1
  store ptr %75, ptr %4, align 8, !tbaa !5
  br label %11

76:                                               ; preds = %70, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %77

77:                                               ; preds = %76
  ret void

78:                                               ; preds = %66
  unreachable
}

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

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

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

@_ZL7kPrimes = external hidden local_unnamed_addr constant [46 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @_ZN9resampler12IntegerRatio6reduceEv(ptr nocapture noundef nonnull align 4 dereferenceable(8) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 4
  %.promoted30 = load i32, ptr %0, align 4, !tbaa !5
  %.promoted = load i32, ptr %2, align 4, !tbaa !10
  br label %3

3:                                                ; preds = %1, %16
  %.0.idx37 = phi i64 [ 0, %1 ], [ %.0.add, %16 ]
  %.promoted3236 = phi i32 [ %.promoted30, %1 ], [ %.promoted31, %16 ]
  %.promoted293435 = phi i32 [ %.promoted, %1 ], [ %.promoted2933, %16 ]
  %.0.ptr = getelementptr inbounds i8, ptr @_ZL7kPrimes, i64 %.0.idx37
  %4 = load i32, ptr %.0.ptr, align 4, !tbaa !11
  %5 = icmp slt i32 %.promoted3236, %4
  %6 = icmp slt i32 %.promoted293435, %4
  %or.cond38 = select i1 %5, i1 true, i1 %6
  br i1 %or.cond38, label %.critedge, label %.preheader

.preheader:                                       ; preds = %3, %15
  %.promoted2933 = phi i32 [ %8, %15 ], [ %.promoted293435, %3 ]
  %.promoted31 = phi i32 [ %7, %15 ], [ %.promoted3236, %3 ]
  %7 = sdiv i32 %.promoted31, %4
  %8 = sdiv i32 %.promoted2933, %4
  %9 = icmp sgt i32 %7, 0
  %10 = icmp sgt i32 %8, 0
  %or.cond = select i1 %9, i1 %10, i1 false
  %11 = mul nsw i32 %7, %4
  %12 = icmp eq i32 %11, %.promoted31
  %or.cond24 = select i1 %or.cond, i1 %12, i1 false
  %13 = mul nsw i32 %8, %4
  %14 = icmp eq i32 %13, %.promoted2933
  %or.cond26 = select i1 %or.cond24, i1 %14, i1 false
  br i1 %or.cond26, label %15, label %16

15:                                               ; preds = %.preheader
  store i32 %7, ptr %0, align 4, !tbaa !5
  store i32 %8, ptr %2, align 4, !tbaa !10
  br label %.preheader

16:                                               ; preds = %.preheader
  %.0.add = add nuw nsw i64 %.0.idx37, 4
  %.not = icmp eq i64 %.0.add, 184
  br i1 %.not, label %.critedge, label %3

.critedge:                                        ; preds = %3, %16
  ret void
}

attributes #0 = { mustprogress 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" }

!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, !7, i64 0}
!6 = !{!"_ZTSN9resampler12IntegerRatioE", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!7, !7, i64 0}


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