; LLVM IR from tangjz/acm-icpc :: lydsy/4916.ll (Z9calc_iphii)

; 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 [6068321 x i32], align 16
@sz = external dso_local global i32, align 4
@fact = external dso_local global [31623 x i32], align 16
@g = external dso_local global [31623 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z9calc_iphii(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 6068321
  br i1 %10, label %11, label %16

11:                                               ; preds = %1
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [6068321 x i32], ptr @f, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  store i32 %15, ptr %2, align 4
  br label %84

16:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %17 = load i32, ptr @sz, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr @fact, i64 %18
  %20 = call noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef @fact, ptr noundef %19, ptr noundef nonnull align 4 dereferenceable(4) %3)
  %21 = ptrtoint ptr %20 to i64
  %22 = sub i64 %21, ptrtoint (ptr @fact to i64)
  %23 = sdiv exact i64 %22, 4
  %24 = trunc i64 %23 to i32
  store i32 %24, ptr %4, align 4, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [31623 x i32], ptr @g, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = icmp sge i32 %28, 0
  br i1 %29, label %30, label %35

30:                                               ; preds = %16
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [31623 x i32], ptr @g, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  store i32 %34, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %83

35:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = add nsw i64 %39, 1
  %41 = mul nsw i64 %37, %40
  %42 = srem i64 %41, 1000000007
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = shl i32 %43, 1
  %45 = add nsw i32 %44, 1
  %46 = sext i32 %45 to i64
  %47 = mul nsw i64 %42, %46
  %48 = srem i64 %47, 1000000007
  %49 = mul nsw i64 %48, 166666668
  %50 = srem i64 %49, 1000000007
  %51 = trunc i64 %50 to i32
  store i32 %51, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  br label %52

52:                                               ; preds = %75, %35
  %53 = load i32, ptr %7, align 4, !tbaa !5
  %54 = load i32, ptr %3, align 4, !tbaa !5
  %55 = icmp sle i32 %53, %54
  br i1 %55, label %57, label %56

56:                                               ; preds = %52
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %78

57:                                               ; preds = %52
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = sdiv i32 %59, %60
  %62 = sdiv i32 %58, %61
  store i32 %62, ptr %8, align 4, !tbaa !5
  %63 = load i32, ptr %3, align 4, !tbaa !5
  %64 = load i32, ptr %7, align 4, !tbaa !5
  %65 = sdiv i32 %63, %64
  %66 = call noundef i32 @_Z9calc_iphii(i32 noundef %65)
  %67 = sext i32 %66 to i64
  %68 = load i32, ptr %7, align 4, !tbaa !5
  %69 = load i32, ptr %8, align 4, !tbaa !5
  %70 = call noundef i32 @_Z4num1ii(i32 noundef %68, i32 noundef %69)
  %71 = sext i32 %70 to i64
  %72 = mul nsw i64 %67, %71
  %73 = srem i64 %72, 1000000007
  %74 = trunc i64 %73 to i32
  call void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4) %6, i32 noundef %74)
  br label %75

75:                                               ; preds = %57
  %76 = load i32, ptr %8, align 4, !tbaa !5
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %7, align 4, !tbaa !5
  br label %52, !llvm.loop !9

78:                                               ; preds = %56
  %79 = load i32, ptr %6, align 4, !tbaa !5
  %80 = load i32, ptr %4, align 4, !tbaa !5
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [31623 x i32], ptr @g, i64 0, i64 %81
  store i32 %79, ptr %82, align 4, !tbaa !5
  store i32 %79, ptr %2, align 4
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %83

83:                                               ; preds = %78, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %84

84:                                               ; preds = %83, %11
  %85 = load i32, ptr %2, align 4
  ret i32 %85
}

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z4num1ii(i32 noundef, i32 noundef) #3

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

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 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 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@f = external dso_local local_unnamed_addr global [6068321 x i32], align 16
@sz = external dso_local local_unnamed_addr global i32, align 4
@fact = external dso_local global [31623 x i32], align 16
@g = external dso_local local_unnamed_addr global [31623 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z9calc_iphii(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %4 = icmp slt i32 %0, 6068321
  br i1 %4, label %5, label %9

5:                                                ; preds = %1
  %6 = sext i32 %0 to i64
  %7 = getelementptr inbounds [6068321 x i32], ptr @f, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  br label %47

9:                                                ; preds = %1
  %10 = load i32, ptr @sz, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds i32, ptr @fact, i64 %11
  %13 = call noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef nonnull @fact, ptr noundef nonnull %12, ptr noundef nonnull align 4 dereferenceable(4) %2)
  %14 = ptrtoint ptr %13 to i64
  %15 = sub i64 %14, ptrtoint (ptr @fact to i64)
  %sext = shl i64 %15, 30
  %16 = ashr i64 %sext, 32
  %17 = getelementptr inbounds [31623 x i32], ptr @g, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp sgt i32 %18, -1
  br i1 %19, label %47, label %20

20:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #4
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = add nsw i64 %22, 1
  %24 = mul nsw i64 %23, %22
  %25 = srem i64 %24, 1000000007
  %26 = shl i32 %21, 1
  %27 = or disjoint i32 %26, 1
  %28 = sext i32 %27 to i64
  %29 = mul nsw i64 %25, %28
  %30 = srem i64 %29, 1000000007
  %31 = mul nsw i64 %30, 166666668
  %32 = srem i64 %31, 1000000007
  %33 = trunc nsw i64 %32 to i32
  store i32 %33, ptr %3, align 4, !tbaa !5
  %.not14 = icmp slt i32 %21, 2
  br i1 %.not14, label %._crit_edge, label %.lr.ph

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load i32, ptr %3, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %20
  %34 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %33, %20 ]
  store i32 %34, ptr %17, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #4
  br label %47

.lr.ph:                                           ; preds = %20, %.lr.ph
  %35 = phi i32 [ %46, %.lr.ph ], [ %21, %20 ]
  %.01015 = phi i32 [ %45, %.lr.ph ], [ 2, %20 ]
  %36 = udiv i32 %35, %.01015
  %37 = sdiv i32 %35, %36
  %38 = call noundef i32 @_Z9calc_iphii(i32 noundef %36)
  %39 = sext i32 %38 to i64
  %40 = call noundef i32 @_Z4num1ii(i32 noundef %.01015, i32 noundef %37)
  %41 = sext i32 %40 to i64
  %42 = mul nsw i64 %41, %39
  %43 = srem i64 %42, 1000000007
  %44 = trunc nsw i64 %43 to i32
  call void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4) %3, i32 noundef %44)
  %45 = add nuw nsw i32 %37, 1
  %46 = load i32, ptr %2, align 4, !tbaa !5
  %.not.not = icmp slt i32 %37, %46
  br i1 %.not.not, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !9

47:                                               ; preds = %._crit_edge, %9, %5
  %.1 = phi i32 [ %8, %5 ], [ %34, %._crit_edge ], [ %18, %9 ]
  ret i32 %.1
}

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z3decRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) local_unnamed_addr #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z4num1ii(i32 noundef, i32 noundef) local_unnamed_addr #3

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { 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 #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 = !{!"int", !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,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass