; LLVM IR from Tunghohin/Competitive_Coding :: Codeforces/cf1701_C.ll (Z5checkii)

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

@cnt = external dso_local global [200010 x i32], align 16

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

8:                                                ; preds = %42, %2
  %9 = load i32, ptr %7, align 4, !tbaa !5
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = icmp sle i32 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %45

13:                                               ; preds = %8
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [200010 x i32], ptr @cnt, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp sge i32 %14, %18
  br i1 %19, label %20, label %31

20:                                               ; preds = %13
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [200010 x i32], ptr @cnt, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sub nsw i32 %21, %25
  %27 = sdiv i32 %26, 2
  %28 = sext i32 %27 to i64
  %29 = load i64, ptr %6, align 8, !tbaa !9
  %30 = add nsw i64 %29, %28
  store i64 %30, ptr %6, align 8, !tbaa !9
  br label %41

31:                                               ; preds = %13
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [200010 x i32], ptr @cnt, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sub nsw i32 %35, %36
  %38 = sext i32 %37 to i64
  %39 = load i64, ptr %5, align 8, !tbaa !9
  %40 = add nsw i64 %39, %38
  store i64 %40, ptr %5, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %31, %20
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %7, align 4, !tbaa !5
  br label %8, !llvm.loop !11

45:                                               ; preds = %12
  %46 = load i64, ptr %5, align 8, !tbaa !9
  %47 = load i64, ptr %6, align 8, !tbaa !9
  %48 = icmp sle i64 %46, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i1 %48
}

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

@cnt = external dso_local local_unnamed_addr global [200010 x i32], align 16

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

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

._crit_edge:                                      ; preds = %16, %2
  %.014.lcssa = phi i64 [ 0, %2 ], [ %.115, %16 ]
  %.013.lcssa = phi i64 [ 0, %2 ], [ %.1, %16 ]
  %4 = icmp sle i64 %.014.lcssa, %.013.lcssa
  ret i1 %4

.lr.ph:                                           ; preds = %.lr.ph.preheader, %16
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next, %16 ]
  %.01323 = phi i64 [ 0, %.lr.ph.preheader ], [ %.1, %16 ]
  %.01422 = phi i64 [ 0, %.lr.ph.preheader ], [ %.115, %16 ]
  %5 = getelementptr inbounds [200010 x i32], ptr @cnt, i64 0, i64 %indvars.iv
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not20 = icmp sgt i32 %6, %0
  br i1 %.not20, label %12, label %7

7:                                                ; preds = %.lr.ph
  %8 = sub nsw i32 %0, %6
  %9 = sdiv i32 %8, 2
  %10 = sext i32 %9 to i64
  %11 = add nsw i64 %.01323, %10
  br label %16

12:                                               ; preds = %.lr.ph
  %13 = sub nsw i32 %6, %0
  %14 = sext i32 %13 to i64
  %15 = add nsw i64 %.01422, %14
  br label %16

16:                                               ; preds = %7, %12
  %.115 = phi i64 [ %.01422, %7 ], [ %15, %12 ]
  %.1 = phi i64 [ %11, %7 ], [ %.01323, %12 ]
  %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_edge, label %.lr.ph, !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"}


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