; LLVM IR from lzyrapx/Competitive-Programming :: LibreOJ/6161（二分）.ll (Z4calcd)

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

@c = external dso_local global [123456 x i32], align 16
@t = external dso_local global [123456 x i32], align 16
@n = external dso_local global i32, align 4
@T = external dso_local global i32, align 4

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

5:                                                ; preds = %31, %1
  %6 = load i32, ptr %4, align 4, !tbaa !9
  %7 = load i32, ptr @n, align 4, !tbaa !9
  %8 = icmp slt i32 %6, %7
  br i1 %8, label %10, label %9

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

10:                                               ; preds = %5
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [123456 x i32], ptr @c, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = sitofp i32 %14 to double
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [123456 x i32], ptr @t, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = sitofp i32 %19 to double
  %21 = load double, ptr %2, align 8, !tbaa !5
  %22 = fsub double %20, %21
  %23 = fmul double %15, %22
  %24 = load double, ptr %2, align 8, !tbaa !5
  %25 = load i32, ptr @T, align 4, !tbaa !9
  %26 = sitofp i32 %25 to double
  %27 = fsub double %24, %26
  %28 = fdiv double %23, %27
  %29 = load double, ptr %3, align 8, !tbaa !5
  %30 = fadd double %29, %28
  store double %30, ptr %3, align 8, !tbaa !5
  br label %31

31:                                               ; preds = %10
  %32 = load i32, ptr %4, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %4, align 4, !tbaa !9
  br label %5, !llvm.loop !11

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

; 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 = !{!"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"

@c = external dso_local local_unnamed_addr global [123456 x i32], align 16
@t = external dso_local local_unnamed_addr global [123456 x i32], align 16
@n = external dso_local local_unnamed_addr global i32, align 4
@T = external dso_local local_unnamed_addr global i32, align 4

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

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

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

._crit_edge.loopexit.unr-lcssa:                   ; preds = %18, %.lr.ph
  %.lcssa.ph = phi double [ poison, %.lr.ph ], [ %38, %18 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %18 ]
  %.078.unr = phi double [ 0.000000e+00, %.lr.ph ], [ %38, %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
  %8 = getelementptr inbounds [123456 x i32], ptr @c, i64 0, i64 %indvars.iv.unr
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = sitofp i32 %9 to double
  %11 = getelementptr inbounds [123456 x i32], ptr @t, i64 0, i64 %indvars.iv.unr
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = sitofp i32 %12 to double
  %14 = fsub double %13, %0
  %15 = fmul double %14, %10
  %16 = fdiv double %15, %6
  %17 = fadd double %.078.unr, %16
  br label %._crit_edge

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

18:                                               ; preds = %18, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %18 ]
  %.078 = phi double [ 0.000000e+00, %.lr.ph.new ], [ %38, %18 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %18 ]
  %19 = getelementptr inbounds [123456 x i32], ptr @c, i64 0, i64 %indvars.iv
  %20 = load i32, ptr %19, align 8, !tbaa !5
  %21 = sitofp i32 %20 to double
  %22 = getelementptr inbounds [123456 x i32], ptr @t, i64 0, i64 %indvars.iv
  %23 = load i32, ptr %22, align 8, !tbaa !5
  %24 = sitofp i32 %23 to double
  %25 = fsub double %24, %0
  %26 = fmul double %25, %21
  %27 = fdiv double %26, %6
  %28 = fadd double %.078, %27
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %29 = getelementptr inbounds [123456 x i32], ptr @c, i64 0, i64 %indvars.iv.next
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = sitofp i32 %30 to double
  %32 = getelementptr inbounds [123456 x i32], ptr @t, i64 0, i64 %indvars.iv.next
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = sitofp i32 %33 to double
  %35 = fsub double %34, %0
  %36 = fmul double %35, %31
  %37 = fdiv double %36, %6
  %38 = fadd double %28, %37
  %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
}

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

!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: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass