; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/SHORT/COOK73/Problem B/B.ll (Z3prev)

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

@f = external dso_local global [100100 x i64], align 16
@invf = external dso_local global [100100 x i64], align 16
@k = external dso_local global [131072 x i32], align 16
@o = external dso_local global [131072 x i32], align 16

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

; 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: mustprogress nounwind uwtable
define dso_local void @_Z3prev() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i64 1, ptr @f, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 1, ptr %1, align 4, !tbaa !9
  br label %4

4:                                                ; preds = %21, %0
  %5 = load i32, ptr %1, align 4, !tbaa !9
  %6 = icmp slt i32 %5, 100100
  br i1 %6, label %8, label %7

7:                                                ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %24

8:                                                ; preds = %4
  %9 = load i32, ptr %1, align 4, !tbaa !9
  %10 = sub nsw i32 %9, 1
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [100100 x i64], ptr @f, i64 0, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = load i32, ptr %1, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = mul nsw i64 %13, %15
  %17 = srem i64 %16, 1000000007
  %18 = load i32, ptr %1, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [100100 x i64], ptr @f, i64 0, i64 %19
  store i64 %17, ptr %20, align 8, !tbaa !5
  br label %21

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

24:                                               ; preds = %7
  %25 = load i64, ptr getelementptr inbounds ([100100 x i64], ptr @f, i64 0, i64 100099), align 8, !tbaa !5
  %26 = call noundef i64 @_Z4fastxx(i64 noundef %25, i64 noundef 1000000005)
  store i64 %26, ptr getelementptr inbounds ([100100 x i64], ptr @invf, i64 0, i64 100099), align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 100098, ptr %2, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %45, %24
  %28 = load i32, ptr %2, align 4, !tbaa !9
  %29 = icmp sge i32 %28, 0
  br i1 %29, label %31, label %30

30:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  br label %48

31:                                               ; preds = %27
  %32 = load i32, ptr %2, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [100100 x i64], ptr @invf, i64 0, i64 %34
  %36 = load i64, ptr %35, align 8, !tbaa !5
  %37 = load i32, ptr %2, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  %39 = sext i32 %38 to i64
  %40 = mul nsw i64 %36, %39
  %41 = srem i64 %40, 1000000007
  %42 = load i32, ptr %2, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [100100 x i64], ptr @invf, i64 0, i64 %43
  store i64 %41, ptr %44, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %31
  %46 = load i32, ptr %2, align 4, !tbaa !9
  %47 = add nsw i32 %46, -1
  store i32 %47, ptr %2, align 4, !tbaa !9
  br label %27, !llvm.loop !13

48:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 2, ptr %3, align 4, !tbaa !9
  br label %49

49:                                               ; preds = %101, %48
  %50 = load i32, ptr %3, align 4, !tbaa !9
  %51 = icmp slt i32 %50, 100100
  br i1 %51, label %53, label %52

52:                                               ; preds = %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %104

53:                                               ; preds = %49
  %54 = load i32, ptr %3, align 4, !tbaa !9
  %55 = sub nsw i32 %54, 1
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [131072 x i32], ptr @k, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !9
  %59 = load i32, ptr %3, align 4, !tbaa !9
  %60 = load i32, ptr %3, align 4, !tbaa !9
  %61 = sub nsw i32 %60, 1
  %62 = and i32 %59, %61
  %63 = icmp eq i32 %62, 0
  %64 = zext i1 %63 to i32
  %65 = add nsw i32 %58, %64
  %66 = load i32, ptr %3, align 4, !tbaa !9
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [131072 x i32], ptr @k, i64 0, i64 %67
  store i32 %65, ptr %68, align 4, !tbaa !9
  %69 = load i32, ptr %3, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [131072 x i32], ptr @k, i64 0, i64 %70
  %72 = load i32, ptr %71, align 4, !tbaa !9
  %73 = load i32, ptr %3, align 4, !tbaa !9
  %74 = sub nsw i32 %73, 1
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [131072 x i32], ptr @k, i64 0, i64 %75
  %77 = load i32, ptr %76, align 4, !tbaa !9
  %78 = icmp eq i32 %72, %77
  br i1 %78, label %79, label %88

79:                                               ; preds = %53
  %80 = load i32, ptr %3, align 4, !tbaa !9
  %81 = sub nsw i32 %80, 1
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [131072 x i32], ptr @o, i64 0, i64 %82
  %84 = load i32, ptr %83, align 4, !tbaa !9
  %85 = load i32, ptr %3, align 4, !tbaa !9
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds [131072 x i32], ptr @o, i64 0, i64 %86
  store i32 %84, ptr %87, align 4, !tbaa !9
  br label %100

88:                                               ; preds = %53
  %89 = load i32, ptr %3, align 4, !tbaa !9
  %90 = sub nsw i32 %89, 1
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds [131072 x i32], ptr @o, i64 0, i64 %91
  %93 = load i32, ptr %92, align 4, !tbaa !9
  %94 = add nsw i32 %93, 1
  %95 = shl i32 %94, 1
  %96 = sub nsw i32 %95, 1
  %97 = load i32, ptr %3, align 4, !tbaa !9
  %98 = sext i32 %97 to i64
  %99 = getelementptr inbounds [131072 x i32], ptr @o, i64 0, i64 %98
  store i32 %96, ptr %99, align 4, !tbaa !9
  br label %100

100:                                              ; preds = %88, %79
  br label %101

101:                                              ; preds = %100
  %102 = load i32, ptr %3, align 4, !tbaa !9
  %103 = add nsw i32 %102, 1
  store i32 %103, ptr %3, align 4, !tbaa !9
  br label %49, !llvm.loop !14

104:                                              ; preds = %52
  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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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"}
!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"

@f = external dso_local local_unnamed_addr global [100100 x i64], align 16
@invf = external dso_local local_unnamed_addr global [100100 x i64], align 16
@k = external dso_local local_unnamed_addr global [131072 x i32], align 16
@o = external dso_local local_unnamed_addr global [131072 x i32], align 16

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3prev() local_unnamed_addr #0 {
  store i64 1, ptr @f, align 16, !tbaa !5
  br label %4

1:                                                ; preds = %4
  %2 = load i64, ptr getelementptr inbounds (i8, ptr @f, i64 800792), align 8, !tbaa !5
  %3 = tail call noundef i64 @_Z4fastxx(i64 noundef %2, i64 noundef 1000000005)
  store i64 %3, ptr getelementptr inbounds (i8, ptr @invf, i64 800792), align 8, !tbaa !5
  br label %13

4:                                                ; preds = %9, %0
  %5 = phi i64 [ 1, %0 ], [ %11, %9 ]
  %indvars.iv = phi i64 [ 1, %0 ], [ %indvars.iv.next.1, %9 ]
  %6 = mul nsw i64 %5, %indvars.iv
  %7 = srem i64 %6, 1000000007
  %8 = getelementptr inbounds [100100 x i64], ptr @f, i64 0, i64 %indvars.iv
  store i64 %7, ptr %8, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 100100
  br i1 %exitcond.not, label %1, label %9, !llvm.loop !9

9:                                                ; preds = %4
  %10 = mul nsw i64 %7, %indvars.iv.next
  %11 = srem i64 %10, 1000000007
  %12 = getelementptr inbounds [100100 x i64], ptr @f, i64 0, i64 %indvars.iv.next
  store i64 %11, ptr %12, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  br label %4

13:                                               ; preds = %19, %1
  %14 = phi i64 [ %3, %1 ], [ %21, %19 ]
  %indvars.iv28 = phi i64 [ 100098, %1 ], [ %indvars.iv.next29.1, %19 ]
  %15 = or disjoint i64 %indvars.iv28, 1
  %16 = mul nsw i64 %14, %15
  %17 = srem i64 %16, 1000000007
  %18 = getelementptr inbounds [100100 x i64], ptr @invf, i64 0, i64 %indvars.iv28
  store i64 %17, ptr %18, align 16, !tbaa !5
  %.not = icmp eq i64 %indvars.iv28, 0
  br i1 %.not, label %.preheader.preheader, label %19, !llvm.loop !11

19:                                               ; preds = %13
  %indvars.iv.next29 = add nsw i64 %indvars.iv28, -1
  %20 = mul nsw i64 %17, %indvars.iv28
  %21 = srem i64 %20, 1000000007
  %22 = getelementptr inbounds [100100 x i64], ptr @invf, i64 0, i64 %indvars.iv.next29
  store i64 %21, ptr %22, align 8, !tbaa !5
  %indvars.iv.next29.1 = add nsw i64 %indvars.iv28, -2
  br label %13

.preheader.preheader:                             ; preds = %13
  %.pre = load i32, ptr getelementptr inbounds (i8, ptr @k, i64 4), align 4, !tbaa !12
  %load_initial = load i32, ptr getelementptr inbounds (i8, ptr @o, i64 4), align 4
  br label %.preheader

23:                                               ; preds = %.preheader
  ret void

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %store_forwarded = phi i32 [ %load_initial, %.preheader.preheader ], [ %.sink, %.preheader ]
  %24 = phi i32 [ %.pre, %.preheader.preheader ], [ %29, %.preheader ]
  %indvars.iv31 = phi i64 [ 2, %.preheader.preheader ], [ %indvars.iv.next32, %.preheader ]
  %25 = trunc nuw nsw i64 %indvars.iv31 to i32
  %26 = tail call range(i32 0, 18) i32 @llvm.ctpop.i32(i32 %25)
  %27 = icmp ult i32 %26, 2
  %28 = zext i1 %27 to i32
  %29 = add nsw i32 %24, %28
  %30 = getelementptr inbounds [131072 x i32], ptr @k, i64 0, i64 %indvars.iv31
  store i32 %29, ptr %30, align 4, !tbaa !12
  %31 = shl i32 %store_forwarded, 1
  %32 = or disjoint i32 %31, 1
  %.sink = select i1 %27, i32 %32, i32 %store_forwarded
  %33 = getelementptr inbounds [131072 x i32], ptr @o, i64 0, i64 %indvars.iv31
  store i32 %.sink, ptr %33, align 4
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %exitcond34.not = icmp eq i64 %indvars.iv.next32, 100100
  br i1 %exitcond34.not, label %23, label %.preheader, !llvm.loop !14
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctpop.i32(i32) #1

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 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !10}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass