; LLVM IR from tangjz/acm-icpc :: lydsy/4587.ll (Z6updateii)

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

@mod = external dso_local global i32, align 4
@cnt = external dso_local global i32, align 4
@fact = external dso_local global [11 x i32], align 16
@Exp = external dso_local global [11 x i32], align 16
@Coeff = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z7mod_invii(i32 noundef, i32 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 uwtable
define dso_local void @_Z6updateii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %35, %2
  %7 = load i32, ptr %5, align 4, !tbaa !5
  %8 = load i32, ptr @cnt, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %38

11:                                               ; preds = %6
  br label %12

12:                                               ; preds = %21, %11
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [11 x i32], ptr @fact, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = srem i32 %13, %17
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %34

20:                                               ; preds = %12
  br label %21

21:                                               ; preds = %20
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [11 x i32], ptr @Exp, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = add nsw i32 %26, %22
  store i32 %27, ptr %25, align 4, !tbaa !5
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [11 x i32], ptr @fact, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = sdiv i32 %32, %31
  store i32 %33, ptr %3, align 4, !tbaa !5
  br label %12, !llvm.loop !9

34:                                               ; preds = %12
  br label %35

35:                                               ; preds = %34
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %5, align 4, !tbaa !5
  br label %6, !llvm.loop !11

38:                                               ; preds = %10
  %39 = load i32, ptr @Coeff, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = icmp eq i32 %41, 1
  br i1 %42, label %43, label %45

43:                                               ; preds = %38
  %44 = load i32, ptr %3, align 4, !tbaa !5
  br label %49

45:                                               ; preds = %38
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = load i32, ptr @mod, align 4, !tbaa !5
  %48 = call noundef i32 @_Z7mod_invii(i32 noundef %46, i32 noundef %47)
  br label %49

49:                                               ; preds = %45, %43
  %50 = phi i32 [ %44, %43 ], [ %48, %45 ]
  %51 = sext i32 %50 to i64
  %52 = mul nsw i64 %40, %51
  %53 = load i32, ptr @mod, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = srem i64 %52, %54
  %56 = trunc i64 %55 to i32
  store i32 %56, ptr @Coeff, align 4, !tbaa !5
  ret void
}

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 = { 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"}
!11 = distinct !{!11, !10}


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

@mod = external dso_local local_unnamed_addr global i32, align 4
@cnt = external dso_local local_unnamed_addr global i32, align 4
@fact = external dso_local local_unnamed_addr global [11 x i32], align 16
@Exp = external dso_local local_unnamed_addr global [11 x i32], align 16
@Coeff = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6updateii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @cnt, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.preheader.preheader, label %._crit_edge15

.preheader.preheader:                             ; preds = %2
  %wide.trip.count = zext nneg i32 %3 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %18
  %indvars.iv = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next, %18 ]
  %.01113 = phi i32 [ %0, %.preheader.preheader ], [ %.1.lcssa, %18 ]
  %5 = getelementptr inbounds [11 x i32], ptr @fact, i64 0, i64 %indvars.iv
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = srem i32 %.01113, %6
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %.lr.ph, label %18

.lr.ph:                                           ; preds = %.preheader
  %9 = getelementptr inbounds [11 x i32], ptr @Exp, i64 0, i64 %indvars.iv
  %.promoted = load i32, ptr %9, align 4, !tbaa !5
  br label %12

._crit_edge15:                                    ; preds = %18, %2
  %.011.lcssa = phi i32 [ %0, %2 ], [ %.1.lcssa, %18 ]
  %10 = load i32, ptr @Coeff, align 4, !tbaa !5
  %11 = icmp eq i32 %1, 1
  br i1 %11, label %22, label %19

12:                                               ; preds = %.lr.ph, %12
  %13 = phi i32 [ %.promoted, %.lr.ph ], [ %14, %12 ]
  %.112 = phi i32 [ %.01113, %.lr.ph ], [ %15, %12 ]
  %14 = add nsw i32 %13, %1
  %15 = sdiv i32 %.112, %6
  %16 = srem i32 %15, %6
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %12, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %12
  store i32 %14, ptr %9, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %._crit_edge, %.preheader
  %.1.lcssa = phi i32 [ %15, %._crit_edge ], [ %.01113, %.preheader ]
  %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 %._crit_edge15, label %.preheader, !llvm.loop !11

19:                                               ; preds = %._crit_edge15
  %20 = load i32, ptr @mod, align 4, !tbaa !5
  %21 = tail call noundef i32 @_Z7mod_invii(i32 noundef %.011.lcssa, i32 noundef %20)
  br label %22

22:                                               ; preds = %._crit_edge15, %19
  %23 = phi i32 [ %21, %19 ], [ %.011.lcssa, %._crit_edge15 ]
  %24 = sext i32 %10 to i64
  %25 = sext i32 %23 to i64
  %26 = mul nsw i64 %25, %24
  %27 = load i32, ptr @mod, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = srem i64 %26, %28
  %30 = trunc nsw i64 %29 to i32
  store i32 %30, ptr @Coeff, align 4, !tbaa !5
  ret void
}

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

!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"}
!11 = distinct !{!11, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass