; LLVM IR from tangjz/acm-icpc :: lydsy/2671.ll (Z5calc1ii)

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

@T = external dso_local global i32, align 4
@cnt = external dso_local global i32, align 4
@d = external dso_local global [11 x i32], align 16

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

11:                                               ; preds = %47, %2
  %12 = load i32, ptr %6, align 4, !tbaa !5
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  store i32 2, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %50

16:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #2
  store i8 1, ptr %8, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 1, ptr %9, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %32, %16
  %18 = load i32, ptr %9, align 4, !tbaa !5
  %19 = load i32, ptr @cnt, align 4, !tbaa !5
  %20 = icmp sle i32 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  store i32 5, ptr %7, align 4
  br label %35

22:                                               ; preds = %17
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %9, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [11 x i32], ptr @d, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = srem i32 %23, %27
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %22
  store i8 0, ptr %8, align 1, !tbaa !11
  store i32 5, ptr %7, align 4
  br label %35

31:                                               ; preds = %22
  br label %32

32:                                               ; preds = %31
  %33 = load i32, ptr %9, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %9, align 4, !tbaa !5
  br label %17, !llvm.loop !13

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

36:                                               ; preds = %35
  %37 = load i8, ptr %8, align 1, !tbaa !11, !range !15, !noundef !16
  %38 = trunc i8 %37 to i1
  br i1 %38, label %39, label %46

39:                                               ; preds = %36
  %40 = load i32, ptr @T, align 4, !tbaa !5
  %41 = load i32, ptr %6, align 4, !tbaa !5
  %42 = sdiv i32 %40, %41
  %43 = sext i32 %42 to i64
  %44 = load i64, ptr %5, align 8, !tbaa !9
  %45 = add nsw i64 %44, %43
  store i64 %45, ptr %5, align 8, !tbaa !9
  br label %46

46:                                               ; preds = %39, %36
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #2
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %6, align 4, !tbaa !5
  br label %11, !llvm.loop !17

50:                                               ; preds = %15
  %51 = load i64, ptr %5, align 8, !tbaa !9
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i64 %51
}

; 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

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 = !{!"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}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !14}


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

@T = external dso_local local_unnamed_addr global i32, align 4
@cnt = external dso_local local_unnamed_addr global i32, align 4
@d = external dso_local local_unnamed_addr global [11 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z5calc1ii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not18 = icmp sgt i32 %0, %1
  br i1 %.not18, label %._crit_edge, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %2
  %3 = load i32, ptr @cnt, align 4, !tbaa !5
  %.not1516 = icmp slt i32 %3, 1
  %4 = load i32, ptr @T, align 4
  br i1 %.not1516, label %.preheader.us.preheader, label %.preheader.preheader

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %5 = add i32 %1, 1
  %6 = sub i32 %5, %0
  %xtraiter = and i32 %6, 1
  %7 = icmp eq i32 %1, %0
  br i1 %7, label %._crit_edge.loopexit.unr-lcssa, label %.preheader.us.preheader.new

.preheader.us.preheader.new:                      ; preds = %.preheader.us.preheader
  %unroll_iter = and i32 %6, -2
  br label %.preheader.us

.preheader.preheader:                             ; preds = %.preheader.lr.ph
  %8 = add nuw i32 %3, 1
  %wide.trip.count = zext i32 %8 to i64
  br label %.preheader

.preheader.us:                                    ; preds = %.preheader.us, %.preheader.us.preheader.new
  %.01320.us = phi i32 [ %0, %.preheader.us.preheader.new ], [ %16, %.preheader.us ]
  %.01419.us = phi i64 [ 0, %.preheader.us.preheader.new ], [ %15, %.preheader.us ]
  %niter = phi i32 [ 0, %.preheader.us.preheader.new ], [ %niter.next.1, %.preheader.us ]
  %9 = sdiv i32 %4, %.01320.us
  %10 = sext i32 %9 to i64
  %11 = add nsw i64 %.01419.us, %10
  %12 = add i32 %.01320.us, 1
  %13 = sdiv i32 %4, %12
  %14 = sext i32 %13 to i64
  %15 = add nsw i64 %11, %14
  %16 = add i32 %.01320.us, 2
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.preheader.us, !llvm.loop !9

.preheader:                                       ; preds = %.preheader.preheader, %.loopexit
  %.01320 = phi i32 [ %29, %.loopexit ], [ %0, %.preheader.preheader ]
  %.01419 = phi i64 [ %.1, %.loopexit ], [ 0, %.preheader.preheader ]
  br label %21

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.preheader.us, %.preheader.us.preheader
  %.lcssa.ph = phi i64 [ poison, %.preheader.us.preheader ], [ %15, %.preheader.us ]
  %.01320.us.unr = phi i32 [ %0, %.preheader.us.preheader ], [ %16, %.preheader.us ]
  %.01419.us.unr = phi i64 [ 0, %.preheader.us.preheader ], [ %15, %.preheader.us ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.preheader.us.epil

.preheader.us.epil:                               ; preds = %._crit_edge.loopexit.unr-lcssa
  %17 = sdiv i32 %4, %.01320.us.unr
  %18 = sext i32 %17 to i64
  %19 = add nsw i64 %.01419.us.unr, %18
  br label %._crit_edge

._crit_edge:                                      ; preds = %.loopexit, %.preheader.us.epil, %._crit_edge.loopexit.unr-lcssa, %2
  %.014.lcssa = phi i64 [ 0, %2 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %19, %.preheader.us.epil ], [ %.1, %.loopexit ]
  ret i64 %.014.lcssa

20:                                               ; preds = %21
  %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 %..critedge_crit_edge, label %21, !llvm.loop !11

21:                                               ; preds = %.preheader, %20
  %indvars.iv = phi i64 [ 1, %.preheader ], [ %indvars.iv.next, %20 ]
  %22 = getelementptr inbounds [11 x i32], ptr @d, i64 0, i64 %indvars.iv
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = srem i32 %.01320, %23
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %.loopexit, label %20

..critedge_crit_edge:                             ; preds = %20
  %26 = sdiv i32 %4, %.01320
  %27 = sext i32 %26 to i64
  %28 = add nsw i64 %.01419, %27
  br label %.loopexit

.loopexit:                                        ; preds = %21, %..critedge_crit_edge
  %.1 = phi i64 [ %28, %..critedge_crit_edge ], [ %.01419, %21 ]
  %29 = add i32 %.01320, 1
  %exitcond23.not = icmp eq i32 %.01320, %1
  br i1 %exitcond23.not, label %._crit_edge, label %.preheader, !llvm.loop !9
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = !{!"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,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass