; LLVM IR from kazunetakahashi/atcoder :: 2017/0325_yahoo-procon2017-final/C2.ll (Z11count_kukanii)

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

@M = external dso_local global i32, align 4
@cnt = external dso_local global [317 x [100010 x i32]], align 16
@added = external dso_local global [317 x i32], align 16

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

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

16:                                               ; preds = %12
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = srem i32 %17, 317
  %19 = icmp ne i32 %18, 0
  br label %20

20:                                               ; preds = %16, %12
  %21 = phi i1 [ false, %12 ], [ %19, %16 ]
  br i1 %21, label %22, label %31

22:                                               ; preds = %20
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %5, align 4, !tbaa !5
  %25 = call noundef i32 @_Z3vali(i32 noundef %23)
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %27, label %30

27:                                               ; preds = %22
  %28 = load i32, ptr %7, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %7, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %27, %22
  br label %12, !llvm.loop !9

31:                                               ; preds = %20
  br label %32

32:                                               ; preds = %50, %31
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %36, label %40

36:                                               ; preds = %32
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = srem i32 %37, 317
  %39 = icmp ne i32 %38, 0
  br label %40

40:                                               ; preds = %36, %32
  %41 = phi i1 [ false, %32 ], [ %39, %36 ]
  br i1 %41, label %42, label %51

42:                                               ; preds = %40
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = add nsw i32 %43, -1
  store i32 %44, ptr %6, align 4, !tbaa !5
  %45 = call noundef i32 @_Z3vali(i32 noundef %44)
  %46 = icmp eq i32 %45, 0
  br i1 %46, label %47, label %50

47:                                               ; preds = %42
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %7, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %47, %42
  br label %32, !llvm.loop !11

51:                                               ; preds = %40
  br label %52

52:                                               ; preds = %56, %51
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = load i32, ptr %6, align 4, !tbaa !5
  %55 = icmp slt i32 %53, %54
  br i1 %55, label %56, label %78

56:                                               ; preds = %52
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = sdiv i32 %57, 317
  store i32 %58, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %59 = load i32, ptr @M, align 4, !tbaa !5
  %60 = load i32, ptr %8, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [317 x i32], ptr @added, i64 0, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !5
  %64 = sub nsw i32 %59, %63
  %65 = load i32, ptr @M, align 4, !tbaa !5
  %66 = srem i32 %64, %65
  store i32 %66, ptr %9, align 4, !tbaa !5
  %67 = load i32, ptr %8, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [317 x [100010 x i32]], ptr @cnt, i64 0, i64 %68
  %70 = load i32, ptr %9, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [100010 x i32], ptr %69, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !5
  %74 = load i32, ptr %7, align 4, !tbaa !5
  %75 = add nsw i32 %74, %73
  store i32 %75, ptr %7, align 4, !tbaa !5
  %76 = load i32, ptr %5, align 4, !tbaa !5
  %77 = add nsw i32 %76, 317
  store i32 %77, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %52, !llvm.loop !12

78:                                               ; preds = %52
  %79 = load i32, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %79
}

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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !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"

@M = external dso_local local_unnamed_addr global i32, align 4
@cnt = external dso_local local_unnamed_addr global [317 x [100010 x i32]], align 16
@added = external dso_local local_unnamed_addr global [317 x i32], align 16

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z11count_kukanii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sge i32 %0, %1
  %4 = srem i32 %0, 317
  %.not29 = icmp eq i32 %4, 0
  %or.cond30 = or i1 %3, %.not29
  br i1 %or.cond30, label %.critedge.preheader, label %.lr.ph

.critedge.preheader:                              ; preds = %.lr.ph, %2
  %.024.lcssa = phi i32 [ 0, %2 ], [ %spec.select, %.lr.ph ]
  %.0.lcssa = phi i32 [ %0, %2 ], [ %7, %.lr.ph ]
  %5 = icmp sge i32 %.0.lcssa, %1
  %6 = srem i32 %1, 317
  %.not2634 = icmp eq i32 %6, 0
  %or.cond2735 = or i1 %5, %.not2634
  br i1 %or.cond2735, label %.critedge2.preheader, label %.critedge

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.032 = phi i32 [ %7, %.lr.ph ], [ %0, %2 ]
  %.02431 = phi i32 [ %spec.select, %.lr.ph ], [ 0, %2 ]
  %7 = add nsw i32 %.032, 1
  %8 = tail call noundef i32 @_Z3vali(i32 noundef %.032)
  %9 = icmp eq i32 %8, 0
  %10 = zext i1 %9 to i32
  %spec.select = add nuw nsw i32 %.02431, %10
  %11 = icmp sge i32 %7, %1
  %12 = srem i32 %7, 317
  %.not = icmp eq i32 %12, 0
  %or.cond = or i1 %11, %.not
  br i1 %or.cond, label %.critedge.preheader, label %.lr.ph, !llvm.loop !5

.critedge2.preheader:                             ; preds = %.critedge, %.critedge.preheader
  %.2.lcssa = phi i32 [ %.024.lcssa, %.critedge.preheader ], [ %spec.select28, %.critedge ]
  %.023.lcssa = phi i32 [ %1, %.critedge.preheader ], [ %15, %.critedge ]
  %13 = icmp slt i32 %.0.lcssa, %.023.lcssa
  br i1 %13, label %.lr.ph43, label %.critedge2._crit_edge

.lr.ph43:                                         ; preds = %.critedge2.preheader
  %14 = load i32, ptr @M, align 4, !tbaa !7
  br label %.critedge2

.critedge:                                        ; preds = %.critedge.preheader, %.critedge
  %.02337 = phi i32 [ %15, %.critedge ], [ %1, %.critedge.preheader ]
  %.236 = phi i32 [ %spec.select28, %.critedge ], [ %.024.lcssa, %.critedge.preheader ]
  %15 = add nsw i32 %.02337, -1
  %16 = tail call noundef i32 @_Z3vali(i32 noundef %15)
  %17 = icmp eq i32 %16, 0
  %18 = zext i1 %17 to i32
  %spec.select28 = add nuw nsw i32 %.236, %18
  %19 = icmp sge i32 %.0.lcssa, %15
  %20 = srem i32 %15, 317
  %.not26 = icmp eq i32 %20, 0
  %or.cond27 = or i1 %19, %.not26
  br i1 %or.cond27, label %.critedge2.preheader, label %.critedge, !llvm.loop !11

.critedge2:                                       ; preds = %.lr.ph43, %.critedge2
  %.142 = phi i32 [ %.0.lcssa, %.lr.ph43 ], [ %31, %.critedge2 ]
  %.441 = phi i32 [ %.2.lcssa, %.lr.ph43 ], [ %30, %.critedge2 ]
  %21 = sdiv i32 %.142, 317
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [317 x i32], ptr @added, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !7
  %25 = sub nsw i32 %14, %24
  %26 = srem i32 %25, %14
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [317 x [100010 x i32]], ptr @cnt, i64 0, i64 %22, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !7
  %30 = add nsw i32 %29, %.441
  %31 = add nsw i32 %.142, 317
  %32 = icmp slt i32 %31, %.023.lcssa
  br i1 %32, label %.critedge2, label %.critedge2._crit_edge, !llvm.loop !12

.critedge2._crit_edge:                            ; preds = %.critedge2, %.critedge2.preheader
  %.4.lcssa = phi i32 [ %.2.lcssa, %.critedge2.preheader ], [ %30, %.critedge2 ]
  ret i32 %.4.lcssa
}

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

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


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