; LLVM IR from bqi343/cp-notebook :: Contests/Other Solutions/VT HSPC/2017/Stopcard.ll (Z4testi)

; 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
@a = external dso_local global [70 x i32], align 16
@prob = external dso_local global [70 x double], align 16
@ans = external dso_local global double, align 8

; 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: mustprogress nounwind uwtable
declare dso_local noundef double @_Z5getexi(i32 noundef) #1

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4testi(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca double, align 8
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = icmp eq i32 %5, %6
  br i1 %7, label %8, label %37

8:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store double 0.000000e+00, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %22, %8
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %14, label %13

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

14:                                               ; preds = %9
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [70 x i32], ptr @a, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = sitofp i32 %18 to double
  %20 = load double, ptr %3, align 8, !tbaa !9
  %21 = fadd double %20, %19
  store double %21, ptr %3, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %14
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %4, align 4, !tbaa !5
  br label %9, !llvm.loop !11

25:                                               ; preds = %13
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [70 x double], ptr @prob, i64 0, i64 %27
  %29 = load double, ptr %28, align 8, !tbaa !9
  %30 = load double, ptr %3, align 8, !tbaa !9
  %31 = fmul double %29, %30
  %32 = load i32, ptr @n, align 4, !tbaa !5
  %33 = sitofp i32 %32 to double
  %34 = fdiv double %31, %33
  %35 = load double, ptr @ans, align 8, !tbaa !9
  %36 = fadd double %35, %34
  store double %36, ptr @ans, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %52

37:                                               ; preds = %1
  %38 = load i32, ptr %2, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [70 x double], ptr @prob, i64 0, i64 %39
  %41 = load double, ptr %40, align 8, !tbaa !9
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [70 x double], ptr @prob, i64 0, i64 %44
  %46 = load double, ptr %45, align 8, !tbaa !9
  %47 = fsub double %41, %46
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = call noundef double @_Z5getexi(i32 noundef %48)
  %50 = load double, ptr @ans, align 8, !tbaa !9
  %51 = call double @llvm.fmuladd.f64(double %47, double %49, double %50)
  store double %51, ptr @ans, align 8, !tbaa !9
  br label %52

52:                                               ; preds = %37, %25
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !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
@a = external dso_local local_unnamed_addr global [70 x i32], align 16
@prob = external dso_local local_unnamed_addr global [70 x double], align 16
@ans = external dso_local local_unnamed_addr global double, align 8

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef double @_Z5getexi(i32 noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4testi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp eq i32 %2, %0
  br i1 %3, label %.preheader, label %34

.preheader:                                       ; preds = %1
  %4 = icmp sgt i32 %0, 0
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %wide.trip.count = zext nneg i32 %0 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %5 = icmp ult i32 %0, 4
  br i1 %5, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

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

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

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.0912.epil = phi double [ %9, %.lr.ph.epil ], [ %.0912.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %6 = getelementptr inbounds [70 x i32], ptr @a, i64 0, i64 %indvars.iv.epil
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = sitofp i32 %7 to double
  %9 = fadd double %.0912.epil, %8
  %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 %.lr.ph.epil, !llvm.loop !9

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %.preheader
  %.09.lcssa = phi double [ 0.000000e+00, %.preheader ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %9, %.lr.ph.epil ]
  %10 = sext i32 %0 to i64
  %11 = getelementptr inbounds [70 x double], ptr @prob, i64 0, i64 %10
  %12 = load double, ptr %11, align 8, !tbaa !11
  %13 = fmul double %.09.lcssa, %12
  %14 = sitofp i32 %0 to double
  %15 = fdiv double %13, %14
  %16 = load double, ptr @ans, align 8, !tbaa !11
  %17 = fadd double %16, %15
  br label %46

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %.0912 = phi double [ 0.000000e+00, %.lr.ph.preheader.new ], [ %33, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %18 = getelementptr inbounds [70 x i32], ptr @a, i64 0, i64 %indvars.iv
  %19 = load i32, ptr %18, align 16, !tbaa !5
  %20 = sitofp i32 %19 to double
  %21 = fadd double %.0912, %20
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %22 = getelementptr inbounds [70 x i32], ptr @a, i64 0, i64 %indvars.iv.next
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = sitofp i32 %23 to double
  %25 = fadd double %21, %24
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %26 = getelementptr inbounds [70 x i32], ptr @a, i64 0, i64 %indvars.iv.next.1
  %27 = load i32, ptr %26, align 8, !tbaa !5
  %28 = sitofp i32 %27 to double
  %29 = fadd double %25, %28
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %30 = getelementptr inbounds [70 x i32], ptr @a, i64 0, i64 %indvars.iv.next.2
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = sitofp i32 %31 to double
  %33 = fadd double %29, %32
  %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 %.lr.ph, !llvm.loop !13

34:                                               ; preds = %1
  %35 = sext i32 %0 to i64
  %36 = getelementptr inbounds [70 x double], ptr @prob, i64 0, i64 %35
  %37 = load double, ptr %36, align 8, !tbaa !11
  %38 = add nsw i32 %0, 1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [70 x double], ptr @prob, i64 0, i64 %39
  %41 = load double, ptr %40, align 8, !tbaa !11
  %42 = fsub double %37, %41
  %43 = tail call noundef double @_Z5getexi(i32 noundef %0)
  %44 = load double, ptr @ans, align 8, !tbaa !11
  %45 = tail call double @llvm.fmuladd.f64(double %42, double %43, double %44)
  br label %46

46:                                               ; preds = %34, %._crit_edge
  %storemerge = phi double [ %45, %34 ], [ %17, %._crit_edge ]
  store double %storemerge, ptr @ans, align 8, !tbaa !11
  ret void
}

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


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