; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/CROC 2016 - Elimination Round (Rated Unofficial Edition)/Problem F (F)

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

@k = external dso_local global i32, align 4
@d = external dso_local global [1001000 x i32], align 16
@p = external dso_local global [1001000 x i32], align 16
@ans = external dso_local global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z1Cii(i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z6updatei(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = sext i32 %3 to i64
  %5 = getelementptr inbounds [1001000 x i32], ptr @d, i64 0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = load i32, ptr @k, align 4, !tbaa !5
  %8 = call noundef i64 @_Z1Cii(i32 noundef %6, i32 noundef %7)
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [1001000 x i32], ptr @p, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = mul nsw i64 %8, %13
  %15 = srem i64 %14, 1000000007
  %16 = load i64, ptr @ans, align 8, !tbaa !9
  %17 = sub nsw i64 %16, %15
  store i64 %17, ptr @ans, align 8, !tbaa !9
  %18 = load i64, ptr @ans, align 8, !tbaa !9
  %19 = icmp slt i64 %18, 0
  br i1 %19, label %20, label %23

20:                                               ; preds = %1
  %21 = load i64, ptr @ans, align 8, !tbaa !9
  %22 = add nsw i64 %21, 1000000007
  store i64 %22, ptr @ans, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %20, %1
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [1001000 x i32], ptr @d, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %26, align 4, !tbaa !5
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [1001000 x i32], ptr @d, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = load i32, ptr @k, align 4, !tbaa !5
  %34 = call noundef i64 @_Z1Cii(i32 noundef %32, i32 noundef %33)
  %35 = load i32, ptr %2, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1001000 x i32], ptr @p, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = mul nsw i64 %34, %39
  %41 = srem i64 %40, 1000000007
  %42 = load i64, ptr @ans, align 8, !tbaa !9
  %43 = add nsw i64 %42, %41
  store i64 %43, ptr @ans, align 8, !tbaa !9
  %44 = load i64, ptr @ans, align 8, !tbaa !9
  %45 = icmp sge i64 %44, 1000000007
  br i1 %45, label %46, label %49

46:                                               ; preds = %23
  %47 = load i64, ptr @ans, align 8, !tbaa !9
  %48 = sub nsw i64 %47, 1000000007
  store i64 %48, ptr @ans, align 8, !tbaa !9
  br label %49

49:                                               ; preds = %46, %23
  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" }

!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 = !{!"long long", !7, i64 0}


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

@k = external dso_local local_unnamed_addr global i32, align 4
@d = external dso_local local_unnamed_addr global [1001000 x i32], align 16
@p = external dso_local local_unnamed_addr global [1001000 x i32], align 16
@ans = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z1Cii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z6updatei(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [1001000 x i32], ptr @d, i64 0, i64 %2
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = load i32, ptr @k, align 4, !tbaa !5
  %6 = tail call noundef i64 @_Z1Cii(i32 noundef %4, i32 noundef %5)
  %7 = getelementptr inbounds [1001000 x i32], ptr @p, i64 0, i64 %2
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = mul nsw i64 %6, %9
  %11 = srem i64 %10, 1000000007
  %12 = load i64, ptr @ans, align 8, !tbaa !9
  %13 = sub nsw i64 %12, %11
  %14 = icmp slt i64 %13, 0
  %15 = add nsw i64 %13, 1000000007
  %spec.select = select i1 %14, i64 %15, i64 %13
  store i64 %spec.select, ptr @ans, align 8, !tbaa !9
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %3, align 4, !tbaa !5
  %18 = load i32, ptr @k, align 4, !tbaa !5
  %19 = tail call noundef i64 @_Z1Cii(i32 noundef %17, i32 noundef %18)
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = mul nsw i64 %19, %21
  %23 = srem i64 %22, 1000000007
  %24 = load i64, ptr @ans, align 8, !tbaa !9
  %25 = add nsw i64 %23, %24
  %26 = icmp sgt i64 %25, 1000000006
  %27 = add nsw i64 %25, -1000000007
  %storemerge6 = select i1 %26, i64 %27, i64 %25
  store i64 %storemerge6, ptr @ans, align 8, !tbaa !9
  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" }

!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 = !{!"long long", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass