; LLVM IR from shamadee/web-dsp-demo :: cpp/dft.ll (ZN16FourierTransform17calculateSpectrumEv)

; 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.FourierTransform = type { i32, i32, i32, double, ptr, ptr, ptr, i32, double, ptr, ptr }

@.str = external hidden unnamed_addr constant [29 x i8], align 1

; 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN16FourierTransform17calculateSpectrumEv(ptr noundef nonnull align 8 dereferenceable(80) %0) #2 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca double, align 8
  %4 = alloca double, align 8
  %5 = alloca double, align 8
  %6 = alloca double, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca double, align 8
  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) #5
  %11 = getelementptr inbounds %struct.FourierTransform, ptr %10, i32 0, i32 0
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = sitofp i32 %12 to double
  %14 = fdiv double 2.000000e+00, %13
  store double %14, ptr %3, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %15 = getelementptr inbounds %struct.FourierTransform, ptr %10, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !9
  %17 = ashr i32 %16, 1
  store i32 %17, ptr %7, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !14
  br label %18

18:                                               ; preds = %73, %1
  %19 = load i32, ptr %8, align 4, !tbaa !14
  %20 = load i32, ptr %7, align 4, !tbaa !14
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %76

23:                                               ; preds = %18
  %24 = getelementptr inbounds %struct.FourierTransform, ptr %10, i32 0, i32 5
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = load i32, ptr %8, align 4, !tbaa !14
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds double, ptr %25, i64 %27
  %29 = load double, ptr %28, align 8, !tbaa !13
  store double %29, ptr %4, align 8, !tbaa !13
  %30 = getelementptr inbounds %struct.FourierTransform, ptr %10, i32 0, i32 6
  %31 = load ptr, ptr %30, align 8, !tbaa !16
  %32 = load i32, ptr %8, align 4, !tbaa !14
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds double, ptr %31, i64 %33
  %35 = load double, ptr %34, align 8, !tbaa !13
  store double %35, ptr %5, align 8, !tbaa !13
  %36 = load double, ptr %3, align 8, !tbaa !13
  %37 = load double, ptr %4, align 8, !tbaa !13
  %38 = load double, ptr %4, align 8, !tbaa !13
  %39 = load double, ptr %5, align 8, !tbaa !13
  %40 = load double, ptr %5, align 8, !tbaa !13
  %41 = fmul double %39, %40
  %42 = call double @llvm.fmuladd.f64(double %37, double %38, double %41)
  %43 = call double @sqrt(double noundef %42) #5
  %44 = fmul double %36, %43
  store double %44, ptr %6, align 8, !tbaa !13
  %45 = load double, ptr %6, align 8, !tbaa !13
  %46 = getelementptr inbounds %struct.FourierTransform, ptr %10, i32 0, i32 8
  %47 = load double, ptr %46, align 8, !tbaa !17
  %48 = fcmp ogt double %45, %47
  br i1 %48, label %49, label %54

49:                                               ; preds = %23
  %50 = load i32, ptr %8, align 4, !tbaa !14
  %51 = getelementptr inbounds %struct.FourierTransform, ptr %10, i32 0, i32 7
  store i32 %50, ptr %51, align 8, !tbaa !18
  %52 = load double, ptr %6, align 8, !tbaa !13
  %53 = getelementptr inbounds %struct.FourierTransform, ptr %10, i32 0, i32 8
  store double %52, ptr %53, align 8, !tbaa !17
  br label %54

54:                                               ; preds = %49, %23
  %55 = load double, ptr %6, align 8, !tbaa !13
  %56 = getelementptr inbounds %struct.FourierTransform, ptr %10, i32 0, i32 4
  %57 = load ptr, ptr %56, align 8, !tbaa !19
  %58 = load i32, ptr %8, align 4, !tbaa !14
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds double, ptr %57, i64 %59
  store double %55, ptr %60, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %61 = load i32, ptr %8, align 4, !tbaa !14
  %62 = getelementptr inbounds %struct.FourierTransform, ptr %10, i32 0, i32 1
  %63 = load i32, ptr %62, align 4, !tbaa !20
  %64 = mul nsw i32 %61, %63
  %65 = load i32, ptr %7, align 4, !tbaa !14
  %66 = mul nsw i32 2, %65
  %67 = sdiv i32 %64, %66
  %68 = sitofp i32 %67 to double
  store double %68, ptr %9, align 8, !tbaa !13
  %69 = load i32, ptr %8, align 4, !tbaa !14
  %70 = load double, ptr %9, align 8, !tbaa !13
  %71 = load double, ptr %6, align 8, !tbaa !13
  %72 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %69, double noundef %70, double noundef %71)
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  br label %73

73:                                               ; preds = %54
  %74 = load i32, ptr %8, align 4, !tbaa !14
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %8, align 4, !tbaa !14
  br label %18, !llvm.loop !21

76:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  ret void
}

; Function Attrs: nounwind
declare double @sqrt(double noundef) #3

declare i32 @printf(ptr noundef, ...) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { mustprogress 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 "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 = { "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 #5 = { 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, !11, i64 0}
!10 = !{!"_ZTS16FourierTransform", !11, i64 0, !11, i64 4, !11, i64 8, !12, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !11, i64 48, !12, i64 56, !6, i64 64, !6, i64 72}
!11 = !{!"int", !7, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !6, i64 32}
!16 = !{!10, !6, i64 40}
!17 = !{!10, !12, i64 56}
!18 = !{!10, !11, i64 48}
!19 = !{!10, !6, i64 24}
!20 = !{!10, !11, i64 4}
!21 = distinct !{!21, !22}
!22 = !{!"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"

@.str = external hidden unnamed_addr constant [29 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #0

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_ZN16FourierTransform17calculateSpectrumEv(ptr nocapture noundef nonnull align 8 dereferenceable(80) %0) local_unnamed_addr #1 align 2 {
  %2 = load i32, ptr %0, align 8, !tbaa !5
  %3 = sitofp i32 %2 to double
  %4 = fdiv double 2.000000e+00, %3
  %5 = ashr i32 %2, 1
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %7 = getelementptr inbounds i8, ptr %0, i64 32
  %8 = getelementptr inbounds i8, ptr %0, i64 40
  %9 = getelementptr inbounds i8, ptr %0, i64 56
  %10 = getelementptr inbounds i8, ptr %0, i64 48
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = getelementptr inbounds i8, ptr %0, i64 4
  %13 = and i32 %2, -2
  %wide.trip.count = zext nneg i32 %5 to i64
  br label %14

._crit_edge:                                      ; preds = %._crit_edge24, %1
  ret void

14:                                               ; preds = %.lr.ph, %._crit_edge24
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %._crit_edge24 ]
  %15 = load ptr, ptr %7, align 8, !tbaa !12
  %16 = getelementptr inbounds double, ptr %15, i64 %indvars.iv
  %17 = load double, ptr %16, align 8, !tbaa !13
  %18 = load ptr, ptr %8, align 8, !tbaa !14
  %19 = getelementptr inbounds double, ptr %18, i64 %indvars.iv
  %20 = load double, ptr %19, align 8, !tbaa !13
  %21 = fmul double %20, %20
  %22 = tail call double @llvm.fmuladd.f64(double %17, double %17, double %21)
  %sqrt = tail call double @llvm.sqrt.f64(double %22)
  %23 = fmul double %4, %sqrt
  %24 = load double, ptr %9, align 8, !tbaa !15
  %25 = fcmp ogt double %23, %24
  %26 = trunc nuw nsw i64 %indvars.iv to i32
  br i1 %25, label %27, label %._crit_edge24

27:                                               ; preds = %14
  store i32 %26, ptr %10, align 8, !tbaa !16
  store double %23, ptr %9, align 8, !tbaa !15
  br label %._crit_edge24

._crit_edge24:                                    ; preds = %14, %27
  %28 = load ptr, ptr %11, align 8, !tbaa !17
  %29 = getelementptr inbounds double, ptr %28, i64 %indvars.iv
  store double %23, ptr %29, align 8, !tbaa !13
  %30 = load i32, ptr %12, align 4, !tbaa !18
  %31 = mul nsw i32 %30, %26
  %32 = sdiv i32 %31, %13
  %33 = sitofp i32 %32 to double
  %34 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %26, double noundef %33, double noundef %23)
  %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 %14, !llvm.loop !19
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.sqrt.f64(double) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { mustprogress nofree 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 = { nofree nounwind "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) }

!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 = !{!"_ZTS16FourierTransform", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !7, i64 48, !10, i64 56, !11, i64 64, !11, i64 72}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"double", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !11, i64 32}
!13 = !{!10, !10, i64 0}
!14 = !{!6, !11, i64 40}
!15 = !{!6, !10, i64 56}
!16 = !{!6, !7, i64 48}
!17 = !{!6, !11, i64 24}
!18 = !{!6, !7, i64 4}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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