; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/101 Hacks/101 Hack 2017/101 Hack Jan/Problem D/D.ll (Z6updatexx)

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

@a = external dso_local global i64, align 8
@c = external dso_local global [100100 x i64], align 16
@F = external dso_local global i64, align 8
@B = external dso_local global i64, align 8
@block = external dso_local global [500 x i64], align 16

; 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
define dso_local void @_Z6updatexx(i64 noundef %0, i64 noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %10 = load i64, ptr @a, align 8, !tbaa !5
  %11 = load i64, ptr @F, align 8, !tbaa !5
  %12 = mul nsw i64 %10, %11
  %13 = srem i64 %12, 1000000007
  store i64 %13, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 1, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %14 = load i64, ptr %4, align 8, !tbaa !5
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %57, %2
  %17 = load i32, ptr %8, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = load i64, ptr %3, align 8, !tbaa !5
  %20 = icmp sge i64 %18, %19
  br i1 %20, label %22, label %21

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

22:                                               ; preds = %16
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = srem i32 %23, 2
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %26, label %38

26:                                               ; preds = %22
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [100100 x i64], ptr @c, i64 0, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !5
  %31 = load i64, ptr %7, align 8, !tbaa !5
  %32 = mul nsw i64 %30, %31
  %33 = srem i64 %32, 1000000007
  %34 = load i64, ptr %5, align 8, !tbaa !5
  %35 = add nsw i64 %34, %33
  store i64 %35, ptr %5, align 8, !tbaa !5
  %36 = load i64, ptr %5, align 8, !tbaa !5
  %37 = srem i64 %36, 1000000007
  store i64 %37, ptr %5, align 8, !tbaa !5
  br label %52

38:                                               ; preds = %22
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [100100 x i64], ptr @c, i64 0, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !5
  %43 = load i64, ptr %7, align 8, !tbaa !5
  %44 = mul nsw i64 %42, %43
  %45 = srem i64 %44, 1000000007
  %46 = load i64, ptr %5, align 8, !tbaa !5
  %47 = sub nsw i64 %46, %45
  store i64 %47, ptr %5, align 8, !tbaa !5
  %48 = load i64, ptr %5, align 8, !tbaa !5
  %49 = add nsw i64 %48, 1000000007
  store i64 %49, ptr %5, align 8, !tbaa !5
  %50 = load i64, ptr %5, align 8, !tbaa !5
  %51 = srem i64 %50, 1000000007
  store i64 %51, ptr %5, align 8, !tbaa !5
  br label %52

52:                                               ; preds = %38, %26
  %53 = load i64, ptr %7, align 8, !tbaa !5
  %54 = load i64, ptr %6, align 8, !tbaa !5
  %55 = mul nsw i64 %53, %54
  %56 = srem i64 %55, 1000000007
  store i64 %56, ptr %7, align 8, !tbaa !5
  br label %57

57:                                               ; preds = %52
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = add nsw i32 %58, -1
  store i32 %59, ptr %8, align 4, !tbaa !9
  %60 = load i32, ptr %9, align 4, !tbaa !9
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %9, align 4, !tbaa !9
  br label %16, !llvm.loop !11

62:                                               ; preds = %21
  %63 = load i64, ptr %5, align 8, !tbaa !5
  %64 = load i64, ptr %3, align 8, !tbaa !5
  %65 = load i64, ptr @B, align 8, !tbaa !5
  %66 = sdiv i64 %64, %65
  %67 = getelementptr inbounds [500 x i64], ptr @block, i64 0, i64 %66
  store i64 %63, ptr %67, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  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 = { 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 = !{!"long long", !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"

@a = external dso_local local_unnamed_addr global i64, align 8
@c = external dso_local local_unnamed_addr global [100100 x i64], align 16
@F = external dso_local local_unnamed_addr global i64, align 8
@B = external dso_local local_unnamed_addr global i64, align 8
@block = external dso_local local_unnamed_addr global [500 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z6updatexx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = load i64, ptr @a, align 8, !tbaa !5
  %4 = load i64, ptr @F, align 8, !tbaa !5
  %5 = mul nsw i64 %4, %3
  %6 = srem i64 %5, 1000000007
  %sext = shl i64 %1, 32
  %7 = ashr exact i64 %sext, 32
  %.not24 = icmp slt i64 %7, %0
  br i1 %.not24, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %2
  %.021.lcssa = phi i64 [ 0, %2 ], [ %.1, %.lr.ph ]
  %8 = load i64, ptr @B, align 8, !tbaa !5
  %9 = sdiv i64 %0, %8
  %10 = getelementptr inbounds [500 x i64], ptr @block, i64 0, i64 %9
  store i64 %.021.lcssa, ptr %10, align 8, !tbaa !5
  ret void

.lr.ph:                                           ; preds = %2, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %7, %2 ]
  %.028 = phi i32 [ %22, %.lr.ph ], [ 0, %2 ]
  %.02026 = phi i64 [ %21, %.lr.ph ], [ 1, %2 ]
  %.02125 = phi i64 [ %.1, %.lr.ph ], [ 0, %2 ]
  %11 = and i32 %.028, 1
  %12 = icmp eq i32 %11, 0
  %13 = getelementptr inbounds [100100 x i64], ptr @c, i64 0, i64 %indvars.iv
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = mul nsw i64 %14, %.02026
  %16 = srem i64 %15, 1000000007
  %17 = add nsw i64 %16, %.02125
  %18 = add nsw i64 %.02125, 1000000007
  %19 = sub nsw i64 %18, %16
  %.1.in = select i1 %12, i64 %17, i64 %19
  %.1 = srem i64 %.1.in, 1000000007
  %20 = mul nsw i64 %.02026, %6
  %21 = srem i64 %20, 1000000007
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %22 = add nuw nsw i32 %.028, 1
  %.not.not = icmp sgt i64 %indvars.iv, %0
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !9
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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 = !{!"long long", !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,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass