; LLVM IR from glaretechnologies/substrata :: opus/opus-1.3.1/celt (mathops)

; 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: nounwind uwtable
define dso_local i32 @isqrt32(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = call i32 @llvm.ctlz.i32(i32 %7, i1 true)
  %9 = sub nsw i32 32, %8
  %10 = sub nsw i32 %9, 1
  %11 = ashr i32 %10, 1
  store i32 %11, ptr %5, align 4, !tbaa !5
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = shl i32 1, %12
  store i32 %13, ptr %3, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %36, %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = shl i32 %15, 1
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = add i32 %16, %17
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = shl i32 %18, %19
  store i32 %20, ptr %6, align 4, !tbaa !5
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = icmp ule i32 %21, %22
  br i1 %23, label %24, label %31

24:                                               ; preds = %14
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = add i32 %26, %25
  store i32 %27, ptr %4, align 4, !tbaa !5
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = sub i32 %29, %28
  store i32 %30, ptr %2, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %24, %14
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = lshr i32 %32, 1
  store i32 %33, ptr %3, align 4, !tbaa !5
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = add nsw i32 %34, -1
  store i32 %35, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %36

36:                                               ; preds = %31
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = icmp sge i32 %37, 0
  br i1 %38, label %14, label %39, !llvm.loop !9

39:                                               ; preds = %36
  %40 = load i32, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 %40
}

; 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 speculatable willreturn memory(none)
declare i32 @llvm.ctlz.i32(i32, i1 immarg) #2

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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: nofree norecurse nosync nounwind memory(none) uwtable
define dso_local i32 @isqrt32(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 %0, i1 true)
  %3 = lshr i32 %2, 1
  %4 = xor i32 %3, 15
  %5 = shl nuw nsw i32 1, %4
  %6 = sub nuw nsw i32 16, %3
  %xtraiter = and i32 %6, 1
  %7 = icmp eq i32 %3, 15
  br i1 %7, label %.unr-lcssa, label %.new

.new:                                             ; preds = %1
  %unroll_iter = and i32 %6, 30
  br label %8

8:                                                ; preds = %8, %.new
  %.018 = phi i32 [ %5, %.new ], [ %21, %8 ]
  %.016 = phi i32 [ 0, %.new ], [ %.117.1, %8 ]
  %.015 = phi i32 [ %4, %.new ], [ %22, %8 ]
  %.0 = phi i32 [ %0, %.new ], [ %.1.1, %8 ]
  %niter = phi i32 [ 0, %.new ], [ %niter.next.1, %8 ]
  %9 = shl i32 %.016, 1
  %10 = add i32 %9, %.018
  %11 = shl i32 %10, %.015
  %.not = icmp ugt i32 %11, %.0
  %12 = select i1 %.not, i32 0, i32 %.018
  %.117 = add i32 %12, %.016
  %13 = select i1 %.not, i32 0, i32 %11
  %.1 = sub i32 %.0, %13
  %14 = lshr i32 %.018, 1
  %15 = add nsw i32 %.015, -1
  %16 = shl i32 %.117, 1
  %17 = add i32 %16, %14
  %18 = shl i32 %17, %15
  %.not.1 = icmp ugt i32 %18, %.1
  %19 = select i1 %.not.1, i32 0, i32 %14
  %.117.1 = add i32 %19, %.117
  %20 = select i1 %.not.1, i32 0, i32 %18
  %.1.1 = sub i32 %.1, %20
  %21 = lshr i32 %.018, 2
  %22 = add nsw i32 %.015, -2
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.unr-lcssa, label %8, !llvm.loop !5

.unr-lcssa:                                       ; preds = %8, %1
  %.117.lcssa.ph = phi i32 [ poison, %1 ], [ %.117.1, %8 ]
  %.018.unr = phi i32 [ %5, %1 ], [ %21, %8 ]
  %.016.unr = phi i32 [ 0, %1 ], [ %.117.1, %8 ]
  %.015.unr = phi i32 [ %4, %1 ], [ %22, %8 ]
  %.0.unr = phi i32 [ %0, %1 ], [ %.1.1, %8 ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %27, label %.epilog-lcssa

.epilog-lcssa:                                    ; preds = %.unr-lcssa
  %23 = shl i32 %.016.unr, 1
  %24 = add i32 %23, %.018.unr
  %25 = shl i32 %24, %.015.unr
  %.not.epil = icmp ugt i32 %25, %.0.unr
  %26 = select i1 %.not.epil, i32 0, i32 %.018.unr
  %.117.epil = add i32 %26, %.016.unr
  br label %27

27:                                               ; preds = %.unr-lcssa, %.epilog-lcssa
  %.117.lcssa = phi i32 [ %.117.lcssa.ph, %.unr-lcssa ], [ %.117.epil, %.epilog-lcssa ]
  ret i32 %.117.lcssa
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctlz.i32(i32, i1 immarg) #1

attributes #0 = { nofree norecurse nosync nounwind memory(none) 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 = { mustprogress 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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