; LLVM IR from pranjalwalia/Competitive-programming :: AtCoder/ABC/173/ABC.ll (Z5checkv)

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

@n = external dso_local global i64, align 8
@m = external dso_local global i64, align 8
@k = external dso_local global i64, align 8
@b = external dso_local global [7 x [7 x i8]], align 16

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z5checkv() #1 {
  %1 = alloca i1, align 1
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  store i64 0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %33, %0
  %7 = load i64, ptr %3, align 8, !tbaa !5
  %8 = load i64, ptr @n, align 8, !tbaa !5
  %9 = icmp slt i64 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %36

11:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %29, %11
  %13 = load i64, ptr %5, align 8, !tbaa !5
  %14 = load i64, ptr @m, align 8, !tbaa !5
  %15 = icmp slt i64 %13, %14
  br i1 %15, label %17, label %16

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

17:                                               ; preds = %12
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds [7 x [7 x i8]], ptr @b, i64 0, i64 %18
  %20 = load i64, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds [7 x i8], ptr %19, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !9
  %23 = sext i8 %22 to i32
  %24 = icmp eq i32 %23, 35
  br i1 %24, label %25, label %28

25:                                               ; preds = %17
  %26 = load i64, ptr %2, align 8, !tbaa !5
  %27 = add nsw i64 %26, 1
  store i64 %27, ptr %2, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %25, %17
  br label %29

29:                                               ; preds = %28
  %30 = load i64, ptr %5, align 8, !tbaa !5
  %31 = add nsw i64 %30, 1
  store i64 %31, ptr %5, align 8, !tbaa !5
  br label %12, !llvm.loop !10

32:                                               ; preds = %16
  br label %33

33:                                               ; preds = %32
  %34 = load i64, ptr %3, align 8, !tbaa !5
  %35 = add nsw i64 %34, 1
  store i64 %35, ptr %3, align 8, !tbaa !5
  br label %6, !llvm.loop !12

36:                                               ; preds = %10
  %37 = load i64, ptr %2, align 8, !tbaa !5
  %38 = load i64, ptr @k, align 8, !tbaa !5
  %39 = icmp eq i64 %37, %38
  br i1 %39, label %40, label %41

40:                                               ; preds = %36
  store i1 true, ptr %1, align 1
  store i32 1, ptr %4, align 4
  br label %42

41:                                               ; preds = %36
  store i1 false, ptr %1, align 1
  store i32 1, ptr %4, align 4
  br label %42

42:                                               ; preds = %41, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  %43 = load i1, ptr %1, align 1
  ret i1 %43
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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

@n = external dso_local local_unnamed_addr global i64, align 8
@m = external dso_local local_unnamed_addr global i64, align 8
@k = external dso_local local_unnamed_addr global i64, align 8
@b = external dso_local local_unnamed_addr global [7 x [7 x i8]], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z5checkv() local_unnamed_addr #0 {
  %1 = load i64, ptr @n, align 8, !tbaa !5
  %2 = icmp sgt i64 %1, 0
  br i1 %2, label %.preheader.lr.ph, label %._crit_edge15

.preheader.lr.ph:                                 ; preds = %0
  %3 = load i64, ptr @m, align 8, !tbaa !5
  %4 = icmp sgt i64 %3, 0
  br i1 %4, label %.preheader.us.preheader, label %._crit_edge15

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %min.iters.check = icmp ult i64 %3, 4
  %n.vec = and i64 %3, 9223372036854775804
  %cmp.n = icmp eq i64 %3, %n.vec
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.0814.us = phi i64 [ %21, %._crit_edge.us ], [ 0, %.preheader.us.preheader ]
  %.0913.us = phi i64 [ %spec.select.us.lcssa, %._crit_edge.us ], [ 0, %.preheader.us.preheader ]
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.preheader.us
  %5 = insertelement <2 x i64> <i64 poison, i64 0>, i64 %.0913.us, i64 0
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <2 x i64> [ %5, %vector.ph ], [ %12, %vector.body ]
  %vec.phi19 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %13, %vector.body ]
  %6 = getelementptr inbounds [7 x [7 x i8]], ptr @b, i64 0, i64 %.0814.us, i64 %index
  %7 = getelementptr inbounds i8, ptr %6, i64 2
  %wide.load = load <2 x i8>, ptr %6, align 1, !tbaa !9
  %wide.load20 = load <2 x i8>, ptr %7, align 1, !tbaa !9
  %8 = icmp eq <2 x i8> %wide.load, <i8 35, i8 35>
  %9 = icmp eq <2 x i8> %wide.load20, <i8 35, i8 35>
  %10 = zext <2 x i1> %8 to <2 x i64>
  %11 = zext <2 x i1> %9 to <2 x i64>
  %12 = add <2 x i64> %vec.phi, %10
  %13 = add <2 x i64> %vec.phi19, %11
  %index.next = add nuw i64 %index, 4
  %14 = icmp eq i64 %index.next, %n.vec
  br i1 %14, label %middle.block, label %vector.body, !llvm.loop !10

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %13, %12
  %15 = tail call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.preheader.us
  %.012.us.ph = phi i64 [ 0, %.preheader.us ], [ %n.vec, %middle.block ]
  %.111.us.ph = phi i64 [ %.0913.us, %.preheader.us ], [ %15, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %.012.us = phi i64 [ %20, %scalar.ph ], [ %.012.us.ph, %scalar.ph.preheader ]
  %.111.us = phi i64 [ %spec.select.us, %scalar.ph ], [ %.111.us.ph, %scalar.ph.preheader ]
  %16 = getelementptr inbounds [7 x [7 x i8]], ptr @b, i64 0, i64 %.0814.us, i64 %.012.us
  %17 = load i8, ptr %16, align 1, !tbaa !9
  %18 = icmp eq i8 %17, 35
  %19 = zext i1 %18 to i64
  %spec.select.us = add nsw i64 %.111.us, %19
  %20 = add nuw nsw i64 %.012.us, 1
  %exitcond.not = icmp eq i64 %20, %3
  br i1 %exitcond.not, label %._crit_edge.us, label %scalar.ph, !llvm.loop !14

._crit_edge.us:                                   ; preds = %scalar.ph, %middle.block
  %spec.select.us.lcssa = phi i64 [ %15, %middle.block ], [ %spec.select.us, %scalar.ph ]
  %21 = add nuw nsw i64 %.0814.us, 1
  %exitcond18.not = icmp eq i64 %21, %1
  br i1 %exitcond18.not, label %._crit_edge15, label %.preheader.us, !llvm.loop !15

._crit_edge15:                                    ; preds = %._crit_edge.us, %.preheader.lr.ph, %0
  %.09.lcssa = phi i64 [ 0, %0 ], [ 0, %.preheader.lr.ph ], [ %spec.select.us.lcssa, %._crit_edge.us ]
  %22 = load i64, ptr @k, align 8, !tbaa !5
  %23 = icmp eq i64 %.09.lcssa, %22
  ret i1 %23
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #1

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" }
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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11, !12, !13}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = distinct !{!14, !11, !13, !12}
!15 = distinct !{!15, !11}


; Effective O3 passes: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass