; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2013-14/Mar 2014/14 Mar G2.ll (Z2okd)

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

@sum = external dso_local global [200001 x double], align 16
@N = external dso_local global i32, align 4
@num = external dso_local global [100001 x i32], align 16

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

9:                                                ; preds = %32, %1
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = load i32, ptr @N, align 4, !tbaa !9
  %12 = add nsw i32 %11, 1
  %13 = icmp slt i32 %10, %12
  br i1 %13, label %15, label %14

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

15:                                               ; preds = %9
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = sub nsw i32 %16, 1
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %18
  %20 = load double, ptr %19, align 8, !tbaa !5
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100001 x i32], ptr @num, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = sitofp i32 %24 to double
  %26 = fadd double %20, %25
  %27 = load double, ptr %3, align 8, !tbaa !5
  %28 = fsub double %26, %27
  %29 = load i32, ptr %4, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %30
  store double %28, ptr %31, align 8, !tbaa !5
  br label %32

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

35:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %36 = load i32, ptr @N, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %5, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %64, %35
  %39 = load i32, ptr %5, align 4, !tbaa !9
  %40 = load i32, ptr @N, align 4, !tbaa !9
  %41 = mul nsw i32 2, %40
  %42 = add nsw i32 %41, 1
  %43 = icmp slt i32 %39, %42
  br i1 %43, label %45, label %44

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

45:                                               ; preds = %38
  %46 = load i32, ptr %5, align 4, !tbaa !9
  %47 = sub nsw i32 %46, 1
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %48
  %50 = load double, ptr %49, align 8, !tbaa !5
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = load i32, ptr @N, align 4, !tbaa !9
  %53 = sub nsw i32 %51, %52
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [100001 x i32], ptr @num, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = sitofp i32 %56 to double
  %58 = fadd double %50, %57
  %59 = load double, ptr %3, align 8, !tbaa !5
  %60 = fsub double %58, %59
  %61 = load i32, ptr %5, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %62
  store double %60, ptr %63, align 8, !tbaa !5
  br label %64

64:                                               ; preds = %45
  %65 = load i32, ptr %5, align 4, !tbaa !9
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %5, align 4, !tbaa !9
  br label %38, !llvm.loop !13

67:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store double 0xC1CDCD6503800000, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %68 = load i32, ptr @N, align 4, !tbaa !9
  %69 = mul nsw i32 2, %68
  %70 = sub nsw i32 %69, 1
  %71 = sub nsw i32 %70, 1
  store i32 %71, ptr %7, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %95, %67
  %73 = load i32, ptr %7, align 4, !tbaa !9
  %74 = load i32, ptr @N, align 4, !tbaa !9
  %75 = add nsw i32 %74, 1
  %76 = icmp sge i32 %73, %75
  br i1 %76, label %78, label %77

77:                                               ; preds = %72
  store i32 8, ptr %8, align 4
  br label %98

78:                                               ; preds = %72
  %79 = load i32, ptr %7, align 4, !tbaa !9
  %80 = load i32, ptr @N, align 4, !tbaa !9
  %81 = sub nsw i32 %80, 1
  %82 = sub nsw i32 %79, %81
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %83
  %85 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIdERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %6, ptr noundef nonnull align 8 dereferenceable(8) %84)
  %86 = load double, ptr %85, align 8, !tbaa !5
  store double %86, ptr %6, align 8, !tbaa !5
  %87 = load double, ptr %6, align 8, !tbaa !5
  %88 = load i32, ptr %7, align 4, !tbaa !9
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %89
  %91 = load double, ptr %90, align 8, !tbaa !5
  %92 = fcmp ogt double %87, %91
  br i1 %92, label %93, label %94

93:                                               ; preds = %78
  store i1 true, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %98

94:                                               ; preds = %78
  br label %95

95:                                               ; preds = %94
  %96 = load i32, ptr %7, align 4, !tbaa !9
  %97 = add nsw i32 %96, -1
  store i32 %97, ptr %7, align 4, !tbaa !9
  br label %72, !llvm.loop !14

98:                                               ; preds = %93, %77
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %99 = load i32, ptr %8, align 4
  switch i32 %99, label %101 [
    i32 8, label %100
  ]

100:                                              ; preds = %98
  store i1 false, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %101

101:                                              ; preds = %100, %98
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %102 = load i1, ptr %2, align 1
  ret i1 %102
}

; 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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIdERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #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"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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

@sum = external dso_local global [200001 x double], align 16
@N = external dso_local local_unnamed_addr global i32, align 4
@num = external dso_local local_unnamed_addr global [100001 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z2okd(double noundef %0) local_unnamed_addr #0 {
  %2 = alloca double, align 8
  store double 0.000000e+00, ptr @sum, align 16, !tbaa !5
  %3 = load i32, ptr @N, align 4, !tbaa !9
  %.not25 = icmp slt i32 %3, 1
  br i1 %.not25, label %.preheader, label %.lr.ph.preheader

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

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

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

.lr.ph.epil:                                      ; preds = %.preheader.loopexit.unr-lcssa
  %6 = getelementptr inbounds [100001 x i32], ptr @num, i64 0, i64 %indvars.iv.unr
  %7 = load i32, ptr %6, align 4, !tbaa !9
  %8 = sitofp i32 %7 to double
  %9 = fadd double %.unr, %8
  %10 = fsub double %9, %0
  %11 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %indvars.iv.unr
  store double %10, ptr %11, align 8, !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %.lr.ph.epil, %.preheader.loopexit.unr-lcssa, %1
  %12 = shl nsw i32 %3, 1
  %13 = icmp slt i32 %3, %12
  br i1 %13, label %.lr.ph28.preheader, label %._crit_edge

.lr.ph28.preheader:                               ; preds = %.preheader
  %14 = sext i32 %3 to i64
  %wide.trip.count42 = sext i32 %12 to i64
  %.phi.trans.insert = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %14
  %.pre = load double, ptr %.phi.trans.insert, align 8, !tbaa !5
  %xtraiter47 = and i64 %14, 1
  %lcmp.mod48.not = icmp eq i64 %xtraiter47, 0
  br i1 %lcmp.mod48.not, label %.lr.ph28.prol.loopexit, label %.lr.ph28.prol

.lr.ph28.prol:                                    ; preds = %.lr.ph28.preheader
  %indvars.iv.next40.prol = add nsw i64 %14, 1
  %15 = load i32, ptr getelementptr inbounds (i8, ptr @num, i64 4), align 4, !tbaa !9
  %16 = sitofp i32 %15 to double
  %17 = fadd double %.pre, %16
  %18 = fsub double %17, %0
  %19 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %indvars.iv.next40.prol
  store double %18, ptr %19, align 8, !tbaa !5
  br label %.lr.ph28.prol.loopexit

.lr.ph28.prol.loopexit:                           ; preds = %.lr.ph28.prol, %.lr.ph28.preheader
  %.unr49 = phi double [ %.pre, %.lr.ph28.preheader ], [ %18, %.lr.ph28.prol ]
  %indvars.iv39.unr = phi i64 [ %14, %.lr.ph28.preheader ], [ %indvars.iv.next40.prol, %.lr.ph28.prol ]
  %20 = add nsw i64 %wide.trip.count42, -1
  %21 = icmp eq i64 %20, %14
  br i1 %21, label %._crit_edge, label %.lr.ph28

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %22 = phi double [ 0.000000e+00, %.lr.ph.preheader.new ], [ %33, %.lr.ph ]
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %23 = getelementptr inbounds [100001 x i32], ptr @num, i64 0, i64 %indvars.iv
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = sitofp i32 %24 to double
  %26 = fadd double %22, %25
  %27 = fsub double %26, %0
  %28 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %indvars.iv
  store double %27, ptr %28, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %29 = getelementptr inbounds [100001 x i32], ptr @num, i64 0, i64 %indvars.iv.next
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = sitofp i32 %30 to double
  %32 = fadd double %27, %31
  %33 = fsub double %32, %0
  %34 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %indvars.iv.next
  store double %33, ptr %34, align 8, !tbaa !5
  %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 %.preheader.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph28.prol.loopexit, %.lr.ph28, %.preheader
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  store double 0xC1CDCD6503800000, ptr %2, align 8, !tbaa !5
  %35 = add nsw i32 %12, -2
  %.not22.not.not30 = icmp sgt i32 %35, %3
  br i1 %.not22.not.not30, label %.lr.ph34.preheader, label %.critedge

.lr.ph34.preheader:                               ; preds = %._crit_edge
  %36 = sext i32 %35 to i64
  br label %.lr.ph34

.lr.ph28:                                         ; preds = %.lr.ph28.prol.loopexit, %.lr.ph28
  %37 = phi double [ %50, %.lr.ph28 ], [ %.unr49, %.lr.ph28.prol.loopexit ]
  %indvars.iv39 = phi i64 [ %indvars.iv.next40.1, %.lr.ph28 ], [ %indvars.iv39.unr, %.lr.ph28.prol.loopexit ]
  %indvars.iv.next40 = add nsw i64 %indvars.iv39, 1
  %38 = sub nsw i64 %indvars.iv.next40, %14
  %39 = getelementptr inbounds [100001 x i32], ptr @num, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = sitofp i32 %40 to double
  %42 = fadd double %37, %41
  %43 = fsub double %42, %0
  %44 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %indvars.iv.next40
  store double %43, ptr %44, align 8, !tbaa !5
  %indvars.iv.next40.1 = add nsw i64 %indvars.iv39, 2
  %45 = sub nsw i64 %indvars.iv.next40.1, %14
  %46 = getelementptr inbounds [100001 x i32], ptr @num, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !9
  %48 = sitofp i32 %47 to double
  %49 = fadd double %43, %48
  %50 = fsub double %49, %0
  %51 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %indvars.iv.next40.1
  store double %50, ptr %51, align 8, !tbaa !5
  %exitcond43.not.1 = icmp eq i64 %indvars.iv.next40.1, %wide.trip.count42
  br i1 %exitcond43.not.1, label %._crit_edge, label %.lr.ph28, !llvm.loop !13

52:                                               ; preds = %.lr.ph34
  %indvars.iv.next45 = add nsw i64 %indvars.iv44, -1
  %53 = load i32, ptr @N, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %.not22.not.not = icmp sgt i64 %indvars.iv.next45, %54
  br i1 %.not22.not.not, label %.lr.ph34, label %.critedge, !llvm.loop !14

.lr.ph34:                                         ; preds = %.lr.ph34.preheader, %52
  %indvars.iv44 = phi i64 [ %36, %.lr.ph34.preheader ], [ %indvars.iv.next45, %52 ]
  %55 = phi i32 [ %3, %.lr.ph34.preheader ], [ %53, %52 ]
  %56 = trunc i64 %indvars.iv44 to i32
  %57 = add i32 %56, 1
  %58 = sub i32 %57, %55
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %59
  %61 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIdERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %60)
  %62 = load double, ptr %61, align 8, !tbaa !5
  store double %62, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds [200001 x double], ptr @sum, i64 0, i64 %indvars.iv44
  %64 = load double, ptr %63, align 8, !tbaa !5
  %65 = fcmp ogt double %62, %64
  br i1 %65, label %.critedge, label %52

.critedge:                                        ; preds = %.lr.ph34, %52, %._crit_edge
  %.not22.not.not.lcssa = phi i1 [ false, %._crit_edge ], [ %65, %52 ], [ %65, %.lr.ph34 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  ret i1 %.not22.not.not.lcssa
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIdERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2

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 willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #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"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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