; LLVM IR from tangjz/acm-icpc :: codeforces/gym/101385I.ll (Z4calcv)

; 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
@m = external dso_local global i32, align 4
@a = external dso_local global [19 x double], align 16

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

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

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

8:                                                ; preds = %3
  %9 = load i32, ptr %2, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [19 x double], ptr @a, i64 0, i64 %10
  %12 = load double, ptr %11, align 8, !tbaa !5
  %13 = load i32, ptr @m, align 4, !tbaa !9
  %14 = sitofp i32 %13 to double
  %15 = fsub double %12, %14
  %16 = load i32, ptr %2, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [19 x double], ptr @a, i64 0, i64 %17
  %19 = load double, ptr %18, align 8, !tbaa !5
  %20 = load i32, ptr @m, align 4, !tbaa !9
  %21 = sitofp i32 %20 to double
  %22 = fsub double %19, %21
  %23 = load double, ptr %1, align 8, !tbaa !5
  %24 = call double @llvm.fmuladd.f64(double %15, double %22, double %23)
  store double %24, ptr %1, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %8
  %26 = load i32, ptr %2, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %2, align 4, !tbaa !9
  br label %3, !llvm.loop !11

28:                                               ; preds = %7
  %29 = load double, ptr %1, align 8, !tbaa !5
  %30 = load i32, ptr @n, align 4, !tbaa !9
  %31 = sitofp i32 %30 to double
  %32 = fdiv double %29, %31
  %33 = call double @sqrt(double noundef %32) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  ret double %33
}

; 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

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

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 "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 = !{!"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
@m = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local local_unnamed_addr global [19 x double], align 16

; Function Attrs: mustprogress nofree nounwind memory(readwrite, argmem: write, inaccessiblemem: write) uwtable
define dso_local noundef double @_Z4calcv() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0
  %3 = load i32, ptr @m, align 4, !tbaa !5
  %4 = sitofp i32 %3 to double
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %5 = icmp ult i32 %1, 4
  br i1 %5, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %wide.trip.count, 2147483644
  br label %13

._crit_edge.loopexit.unr-lcssa:                   ; preds = %13, %.lr.ph
  %.lcssa.ph = phi double [ poison, %.lr.ph ], [ %29, %13 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.3, %13 ]
  %.057.unr = phi double [ 0.000000e+00, %.lr.ph ], [ %29, %13 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.057.epil = phi double [ %9, %.epil.preheader ], [ %.057.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %6 = getelementptr inbounds [19 x double], ptr @a, i64 0, i64 %indvars.iv.epil
  %7 = load double, ptr %6, align 8, !tbaa !9
  %8 = fsub double %7, %4
  %9 = tail call double @llvm.fmuladd.f64(double %8, double %8, double %.057.epil)
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.epil.preheader, !llvm.loop !11

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader, %0
  %.05.lcssa = phi double [ 0.000000e+00, %0 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %9, %.epil.preheader ]
  %10 = sitofp i32 %1 to double
  %11 = fdiv double %.05.lcssa, %10
  %12 = tail call double @sqrt(double noundef %11) #3
  ret double %12

13:                                               ; preds = %13, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.3, %13 ]
  %.057 = phi double [ 0.000000e+00, %.lr.ph.new ], [ %29, %13 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.3, %13 ]
  %14 = getelementptr inbounds [19 x double], ptr @a, i64 0, i64 %indvars.iv
  %15 = load double, ptr %14, align 16, !tbaa !9
  %16 = fsub double %15, %4
  %17 = tail call double @llvm.fmuladd.f64(double %16, double %16, double %.057)
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %18 = getelementptr inbounds [19 x double], ptr @a, i64 0, i64 %indvars.iv.next
  %19 = load double, ptr %18, align 8, !tbaa !9
  %20 = fsub double %19, %4
  %21 = tail call double @llvm.fmuladd.f64(double %20, double %20, double %17)
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %22 = getelementptr inbounds [19 x double], ptr @a, i64 0, i64 %indvars.iv.next.1
  %23 = load double, ptr %22, align 16, !tbaa !9
  %24 = fsub double %23, %4
  %25 = tail call double @llvm.fmuladd.f64(double %24, double %24, double %21)
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %26 = getelementptr inbounds [19 x double], ptr @a, i64 0, i64 %indvars.iv.next.2
  %27 = load double, ptr %26, align 8, !tbaa !9
  %28 = fsub double %27, %4
  %29 = tail call double @llvm.fmuladd.f64(double %28, double %28, double %25)
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %13, !llvm.loop !13
}

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sqrt(double noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nofree nounwind memory(readwrite, argmem: write, inaccessiblemem: write) 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) }
attributes #2 = { mustprogress nofree nounwind willreturn memory(write) "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 }

!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 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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