; LLVM IR from tangjz/acm-icpc :: TDL_training/2016-04-10/K - Average Modulo - lzy.ll (Z5checkd)

; 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
@P = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@m = external dso_local global i32, align 4
@s = external dso_local global [100009 x i32], align 16
@t = external dso_local global [100009 x i32], align 16
@c = external dso_local global [100009 x double], align 16
@d = external dso_local global [100009 x double], align 16
@f = external dso_local global [100009 x double], align 16

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3upcid(i32 noundef, double noundef) #1

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3updid(i32 noundef, double noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef double @_Z4getci(i32 noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef double @_Z4getdi(i32 noundef) #1

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

8:                                                ; preds = %20, %1
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = load i32, ptr @m, align 4, !tbaa !9
  %11 = icmp sle i32 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %23

13:                                               ; preds = %8
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [100009 x double], ptr @d, i64 0, i64 %15
  store double 1.000000e+12, ptr %16, align 8, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [100009 x double], ptr @c, i64 0, i64 %18
  store double 1.000000e+12, ptr %19, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %13
  %21 = load i32, ptr %4, align 4, !tbaa !9
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %4, align 4, !tbaa !9
  br label %8, !llvm.loop !11

23:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %111, %23
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = load i32, ptr @n, align 4, !tbaa !9
  %27 = icmp sle i32 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 5, ptr %6, align 4
  br label %114

29:                                               ; preds = %24
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [100009 x i32], ptr @s, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = sitofp i32 %33 to double
  %35 = load i32, ptr %5, align 4, !tbaa !9
  %36 = sitofp i32 %35 to double
  %37 = load double, ptr %3, align 8, !tbaa !5
  %38 = fneg double %36
  %39 = call double @llvm.fmuladd.f64(double %38, double %37, double %34)
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100009 x double], ptr @f, i64 0, i64 %41
  store double %39, ptr %42, align 8, !tbaa !5
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = load i32, ptr @k, align 4, !tbaa !9
  %45 = icmp sge i32 %43, %44
  br i1 %45, label %46, label %110

46:                                               ; preds = %29
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = load i32, ptr @k, align 4, !tbaa !9
  %49 = sub nsw i32 %47, %48
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [100009 x i32], ptr @t, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !9
  %53 = load i32, ptr %5, align 4, !tbaa !9
  %54 = load i32, ptr @k, align 4, !tbaa !9
  %55 = sub nsw i32 %53, %54
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [100009 x double], ptr @f, i64 0, i64 %56
  %58 = load double, ptr %57, align 8, !tbaa !5
  call void @_Z3upcid(i32 noundef %52, double noundef %58)
  %59 = load i32, ptr %5, align 4, !tbaa !9
  %60 = load i32, ptr @k, align 4, !tbaa !9
  %61 = sub nsw i32 %59, %60
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [100009 x i32], ptr @t, i64 0, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !9
  %65 = load i32, ptr %5, align 4, !tbaa !9
  %66 = load i32, ptr @k, align 4, !tbaa !9
  %67 = sub nsw i32 %65, %66
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [100009 x double], ptr @f, i64 0, i64 %68
  %70 = load double, ptr %69, align 8, !tbaa !5
  call void @_Z3updid(i32 noundef %64, double noundef %70)
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %71 = load i32, ptr %5, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [100009 x i32], ptr @t, i64 0, i64 %72
  %74 = load i32, ptr %73, align 4, !tbaa !9
  %75 = call noundef double @_Z4getci(i32 noundef %74)
  store double %75, ptr %7, align 8, !tbaa !5
  %76 = load i32, ptr %5, align 4, !tbaa !9
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds [100009 x double], ptr @f, i64 0, i64 %77
  %79 = load double, ptr %78, align 8, !tbaa !5
  %80 = load double, ptr %7, align 8, !tbaa !5
  %81 = fsub double %79, %80
  %82 = call noundef i32 @_Z4dcmpd(double noundef %81)
  %83 = icmp sge i32 %82, 0
  br i1 %83, label %84, label %86

84:                                               ; preds = %46
  %85 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %85, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %107

86:                                               ; preds = %46
  %87 = load i32, ptr %5, align 4, !tbaa !9
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds [100009 x i32], ptr @t, i64 0, i64 %88
  %90 = load i32, ptr %89, align 4, !tbaa !9
  %91 = add nsw i32 %90, 1
  %92 = call noundef double @_Z4getdi(i32 noundef %91)
  store double %92, ptr %7, align 8, !tbaa !5
  %93 = load i32, ptr %5, align 4, !tbaa !9
  %94 = sext i32 %93 to i64
  %95 = getelementptr inbounds [100009 x double], ptr @f, i64 0, i64 %94
  %96 = load double, ptr %95, align 8, !tbaa !5
  %97 = load i32, ptr @P, align 4, !tbaa !9
  %98 = sitofp i32 %97 to double
  %99 = fadd double %96, %98
  %100 = load double, ptr %7, align 8, !tbaa !5
  %101 = fsub double %99, %100
  %102 = call noundef i32 @_Z4dcmpd(double noundef %101)
  %103 = icmp sge i32 %102, 0
  br i1 %103, label %104, label %106

104:                                              ; preds = %86
  %105 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %105, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %107

106:                                              ; preds = %86
  store i32 0, ptr %6, align 4
  br label %107

107:                                              ; preds = %106, %104, %84
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  %108 = load i32, ptr %6, align 4
  switch i32 %108, label %114 [
    i32 0, label %109
  ]

109:                                              ; preds = %107
  br label %110

110:                                              ; preds = %109, %29
  br label %111

111:                                              ; preds = %110
  %112 = load i32, ptr %5, align 4, !tbaa !9
  %113 = add nsw i32 %112, 1
  store i32 %113, ptr %5, align 4, !tbaa !9
  br label %24, !llvm.loop !13

114:                                              ; preds = %107, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  %115 = load i32, ptr %6, align 4
  switch i32 %115, label %119 [
    i32 5, label %116
    i32 1, label %117
  ]

116:                                              ; preds = %114
  store i32 -1, ptr %2, align 4
  br label %117

117:                                              ; preds = %116, %114
  %118 = load i32, ptr %2, align 4
  ret i32 %118

119:                                              ; preds = %114
  unreachable
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #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 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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"}
!13 = distinct !{!13, !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"

@n = external dso_local local_unnamed_addr global i32, align 4
@P = external dso_local local_unnamed_addr global i32, align 4
@k = external dso_local local_unnamed_addr global i32, align 4
@m = external dso_local local_unnamed_addr global i32, align 4
@s = external dso_local local_unnamed_addr global [100009 x i32], align 16
@t = external dso_local local_unnamed_addr global [100009 x i32], align 16
@c = external dso_local local_unnamed_addr global [100009 x double], align 16
@d = external dso_local local_unnamed_addr global [100009 x double], align 16
@f = external dso_local local_unnamed_addr global [100009 x double], align 16

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3upcid(i32 noundef, double noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3updid(i32 noundef, double noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef double @_Z4getci(i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef double @_Z4getdi(i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5checkd(double noundef %0) local_unnamed_addr #1 {
  %2 = load i32, ptr @m, align 4, !tbaa !5
  %.not34 = icmp slt i32 %2, 1
  br i1 %.not34, label %.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %3 = add nuw i32 %2, 1
  %wide.trip.count = zext i32 %3 to i64
  %4 = zext nneg i32 %2 to i64
  %min.iters.check = icmp ult i32 %2, 4
  br i1 %min.iters.check, label %.lr.ph.preheader50, label %vector.ph

.lr.ph.preheader50:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 1, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %4, 2147483644
  %ind.end = or disjoint i64 %n.vec, 1
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = or disjoint i64 %index, 1
  %5 = getelementptr inbounds [100009 x double], ptr @d, i64 0, i64 %offset.idx
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  store <2 x double> <double 1.000000e+12, double 1.000000e+12>, ptr %5, align 8, !tbaa !9
  store <2 x double> <double 1.000000e+12, double 1.000000e+12>, ptr %6, align 8, !tbaa !9
  %7 = getelementptr inbounds [100009 x double], ptr @c, i64 0, i64 %offset.idx
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  store <2 x double> <double 1.000000e+12, double 1.000000e+12>, ptr %7, align 8, !tbaa !9
  store <2 x double> <double 1.000000e+12, double 1.000000e+12>, ptr %8, align 8, !tbaa !9
  %index.next = add nuw i64 %index, 4
  %9 = icmp eq i64 %index.next, %n.vec
  br i1 %9, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %4
  br i1 %cmp.n, label %.preheader, label %.lr.ph.preheader50

.preheader:                                       ; preds = %.lr.ph, %middle.block, %1
  %10 = load i32, ptr @n, align 4, !tbaa !5
  %.not3036 = icmp slt i32 %10, 0
  br i1 %.not3036, label %.thread, label %.lr.ph39.preheader

.lr.ph39.preheader:                               ; preds = %.preheader
  %.pre48 = load i32, ptr @k, align 4, !tbaa !5
  br label %.lr.ph39

.lr.ph:                                           ; preds = %.lr.ph.preheader50, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader50 ]
  %11 = getelementptr inbounds [100009 x double], ptr @d, i64 0, i64 %indvars.iv
  store double 1.000000e+12, ptr %11, align 8, !tbaa !9
  %12 = getelementptr inbounds [100009 x double], ptr @c, i64 0, i64 %indvars.iv
  store double 1.000000e+12, ptr %12, align 8, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !15

.lr.ph39:                                         ; preds = %.lr.ph39.preheader, %56
  %13 = phi i32 [ %10, %.lr.ph39.preheader ], [ %57, %56 ]
  %14 = phi i32 [ %.pre48, %.lr.ph39.preheader ], [ %58, %56 ]
  %indvars.iv45 = phi i64 [ 0, %.lr.ph39.preheader ], [ %indvars.iv.next46, %56 ]
  %15 = getelementptr inbounds [100009 x i32], ptr @s, i64 0, i64 %indvars.iv45
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = sitofp i32 %16 to double
  %18 = trunc nuw nsw i64 %indvars.iv45 to i32
  %19 = uitofp nneg i32 %18 to double
  %20 = fneg double %19
  %21 = tail call double @llvm.fmuladd.f64(double %20, double %0, double %17)
  %22 = getelementptr inbounds [100009 x double], ptr @f, i64 0, i64 %indvars.iv45
  store double %21, ptr %22, align 8, !tbaa !9
  %23 = sext i32 %14 to i64
  %.not31 = icmp slt i64 %indvars.iv45, %23
  br i1 %.not31, label %56, label %24

24:                                               ; preds = %.lr.ph39
  %25 = sub nsw i32 %18, %14
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [100009 x i32], ptr @t, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = getelementptr inbounds [100009 x double], ptr @f, i64 0, i64 %26
  %30 = load double, ptr %29, align 8, !tbaa !9
  tail call void @_Z3upcid(i32 noundef %28, double noundef %30)
  %31 = load i32, ptr @k, align 4, !tbaa !5
  %32 = sub nsw i32 %18, %31
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [100009 x i32], ptr @t, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = getelementptr inbounds [100009 x double], ptr @f, i64 0, i64 %33
  %37 = load double, ptr %36, align 8, !tbaa !9
  tail call void @_Z3updid(i32 noundef %35, double noundef %37)
  %38 = getelementptr inbounds [100009 x i32], ptr @t, i64 0, i64 %indvars.iv45
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = tail call noundef double @_Z4getci(i32 noundef %39)
  %41 = load double, ptr %22, align 8, !tbaa !9
  %42 = fsub double %41, %40
  %43 = tail call noundef i32 @_Z4dcmpd(double noundef %42)
  %44 = icmp sgt i32 %43, -1
  br i1 %44, label %.thread, label %45

45:                                               ; preds = %24
  %46 = load i32, ptr %38, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  %48 = tail call noundef double @_Z4getdi(i32 noundef %47)
  %49 = load double, ptr %22, align 8, !tbaa !9
  %50 = load i32, ptr @P, align 4, !tbaa !5
  %51 = sitofp i32 %50 to double
  %52 = fadd double %49, %51
  %53 = fsub double %52, %48
  %54 = tail call noundef i32 @_Z4dcmpd(double noundef %53)
  %55 = icmp slt i32 %54, 0
  br i1 %55, label %._crit_edge, label %.thread

._crit_edge:                                      ; preds = %45
  %.pre = load i32, ptr @k, align 4, !tbaa !5
  %.pre49 = load i32, ptr @n, align 4, !tbaa !5
  br label %56

56:                                               ; preds = %._crit_edge, %.lr.ph39
  %57 = phi i32 [ %.pre49, %._crit_edge ], [ %13, %.lr.ph39 ]
  %58 = phi i32 [ %.pre, %._crit_edge ], [ %14, %.lr.ph39 ]
  %indvars.iv.next46 = add nuw nsw i64 %indvars.iv45, 1
  %59 = sext i32 %57 to i64
  %.not30.not = icmp slt i64 %indvars.iv45, %59
  br i1 %.not30.not, label %.lr.ph39, label %.thread, !llvm.loop !16

.thread:                                          ; preds = %45, %56, %24, %.preheader
  %spec.select = phi i32 [ -1, %.preheader ], [ %18, %24 ], [ -1, %56 ], [ %18, %45 ]
  ret i32 %spec.select
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #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 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 = { 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 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12, !14, !13}
!16 = distinct !{!16, !12}


; Effective O3 passes: ADCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,SROAPass,SimplifyCFGPass,TailCallElimPass