; LLVM IR from upupming/algorithm :: kick-start/2020/2020D-C.ll (Z6getAnsv)

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

@n = external dso_local global i32, align 4
@p1 = external dso_local global [500010 x i32], align 16
@p2 = external dso_local global [500010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef double @_Z6getAnsv() #0 {
  %1 = alloca double, align 8
  %2 = alloca i32, align 4
  %3 = alloca double, align 8
  %4 = alloca double, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  store double 0.000000e+00, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 1, ptr %2, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %35, %0
  %6 = load i32, ptr %2, align 4, !tbaa !9
  %7 = load i32, ptr @n, align 4, !tbaa !9
  %8 = icmp sle i32 %6, %7
  br i1 %8, label %10, label %9

9:                                                ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %38

10:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %11 = load i32, ptr %2, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [500010 x i32], ptr @p1, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = sitofp i32 %14 to double
  %16 = load i32, ptr @n, align 4, !tbaa !9
  %17 = sitofp i32 %16 to double
  %18 = fdiv double %15, %17
  store double %18, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %19 = load i32, ptr %2, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [500010 x i32], ptr @p2, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !9
  %23 = sitofp i32 %22 to double
  %24 = load i32, ptr @n, align 4, !tbaa !9
  %25 = sitofp i32 %24 to double
  %26 = fdiv double %23, %25
  store double %26, ptr %4, align 8, !tbaa !5
  %27 = load double, ptr %3, align 8, !tbaa !5
  %28 = fsub double 1.000000e+00, %27
  %29 = load double, ptr %4, align 8, !tbaa !5
  %30 = fsub double 1.000000e+00, %29
  %31 = fneg double %28
  %32 = call double @llvm.fmuladd.f64(double %31, double %30, double 1.000000e+00)
  %33 = load double, ptr %1, align 8, !tbaa !5
  %34 = fadd double %33, %32
  store double %34, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %35

35:                                               ; preds = %10
  %36 = load i32, ptr %2, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %2, align 4, !tbaa !9
  br label %5, !llvm.loop !11

38:                                               ; preds = %9
  %39 = load double, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret double %39
}

; 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 double @llvm.fmuladd.f64(double, double, double) #2

; 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 = { 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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@n = external dso_local local_unnamed_addr global i32, align 4
@p1 = external dso_local local_unnamed_addr global [500010 x i32], align 16
@p2 = external dso_local local_unnamed_addr global [500010 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef double @_Z6getAnsv() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %.not9 = icmp slt i32 %1, 1
  br i1 %.not9, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0
  %2 = uitofp nneg i32 %1 to double
  %3 = zext nneg i32 %1 to i64
  %xtraiter = and i64 %3, 1
  %4 = icmp eq i32 %1, 1
  br i1 %4, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %3, 2147483646
  br label %18

._crit_edge.loopexit.unr-lcssa:                   ; preds = %18, %.lr.ph
  %.lcssa.ph = phi double [ poison, %.lr.ph ], [ %44, %18 ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next.1, %18 ]
  %.011.unr = phi double [ 0.000000e+00, %.lr.ph ], [ %44, %18 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %._crit_edge.loopexit.epilog-lcssa

._crit_edge.loopexit.epilog-lcssa:                ; preds = %._crit_edge.loopexit.unr-lcssa
  %5 = getelementptr inbounds [500010 x i32], ptr @p1, i64 0, i64 %indvars.iv.unr
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = sitofp i32 %6 to double
  %8 = fdiv double %7, %2
  %9 = getelementptr inbounds [500010 x i32], ptr @p2, i64 0, i64 %indvars.iv.unr
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = sitofp i32 %10 to double
  %12 = fdiv double %11, %2
  %13 = fsub double 1.000000e+00, %8
  %14 = fsub double 1.000000e+00, %12
  %15 = fneg double %13
  %16 = tail call double @llvm.fmuladd.f64(double %15, double %14, double 1.000000e+00)
  %17 = fadd double %.011.unr, %16
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.epilog-lcssa, %._crit_edge.loopexit.unr-lcssa, %0
  %.0.lcssa = phi double [ 0.000000e+00, %0 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %17, %._crit_edge.loopexit.epilog-lcssa ]
  ret double %.0.lcssa

18:                                               ; preds = %18, %.lr.ph.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.new ], [ %indvars.iv.next.1, %18 ]
  %.011 = phi double [ 0.000000e+00, %.lr.ph.new ], [ %44, %18 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %18 ]
  %19 = getelementptr inbounds [500010 x i32], ptr @p1, i64 0, i64 %indvars.iv
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sitofp i32 %20 to double
  %22 = fdiv double %21, %2
  %23 = getelementptr inbounds [500010 x i32], ptr @p2, i64 0, i64 %indvars.iv
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = sitofp i32 %24 to double
  %26 = fdiv double %25, %2
  %27 = fsub double 1.000000e+00, %22
  %28 = fsub double 1.000000e+00, %26
  %29 = fneg double %27
  %30 = tail call double @llvm.fmuladd.f64(double %29, double %28, double 1.000000e+00)
  %31 = fadd double %.011, %30
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %32 = getelementptr inbounds [500010 x i32], ptr @p1, i64 0, i64 %indvars.iv.next
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = sitofp i32 %33 to double
  %35 = fdiv double %34, %2
  %36 = getelementptr inbounds [500010 x i32], ptr @p2, i64 0, i64 %indvars.iv.next
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = sitofp i32 %37 to double
  %39 = fdiv double %38, %2
  %40 = fsub double 1.000000e+00, %35
  %41 = fsub double 1.000000e+00, %39
  %42 = fneg double %40
  %43 = tail call double @llvm.fmuladd.f64(double %42, double %41, double 1.000000e+00)
  %44 = fadd double %31, %43
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %18, !llvm.loop !9
}

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: 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 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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