; LLVM IR from eyangch/competitive-programming :: Codeforces/1700/b.ll (Z3subx)

; 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
@a = external dso_local global [100001 x i64], align 16
@r = external dso_local global [100000 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3subx(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i8, align 1
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  store i8 0, ptr %3, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %6 = load i64, ptr @N, align 8, !tbaa !5
  %7 = sub nsw i64 %6, 1
  store i64 %7, ptr %4, align 8, !tbaa !5
  br label %8

8:                                                ; preds = %36, %1
  %9 = load i64, ptr %4, align 8, !tbaa !5
  %10 = icmp sge i64 %9, 0
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %13 = load i64, ptr %2, align 8, !tbaa !5
  store i64 %13, ptr %5, align 8, !tbaa !5
  %14 = load i8, ptr %3, align 1, !tbaa !9, !range !11, !noundef !12
  %15 = trunc i8 %14 to i1
  br i1 %15, label %16, label %19

16:                                               ; preds = %12
  %17 = load i64, ptr %5, align 8, !tbaa !5
  %18 = add nsw i64 %17, -1
  store i64 %18, ptr %5, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %16, %12
  store i8 0, ptr %3, align 1, !tbaa !9
  %20 = load i64, ptr %5, align 8, !tbaa !5
  %21 = load i64, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds [100001 x i64], ptr @a, i64 0, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = icmp slt i64 %20, %23
  br i1 %24, label %25, label %28

25:                                               ; preds = %19
  store i8 1, ptr %3, align 1, !tbaa !9
  %26 = load i64, ptr %5, align 8, !tbaa !5
  %27 = add nsw i64 %26, 10
  store i64 %27, ptr %5, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %25, %19
  %29 = load i64, ptr %5, align 8, !tbaa !5
  %30 = load i64, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds [100001 x i64], ptr @a, i64 0, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = sub nsw i64 %29, %32
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds [100000 x i64], ptr @r, i64 0, i64 %34
  store i64 %33, ptr %35, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %36

36:                                               ; preds = %28
  %37 = load i64, ptr %4, align 8, !tbaa !5
  %38 = add nsw i64 %37, -1
  store i64 %38, ptr %4, align 8, !tbaa !5
  br label %8, !llvm.loop !13

39:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #2
  ret void
}

; 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@N = external dso_local local_unnamed_addr global i64, align 8
@a = external dso_local local_unnamed_addr global [100001 x i64], align 16
@r = external dso_local local_unnamed_addr global [100000 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z3subx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr @N, align 8, !tbaa !5
  %3 = icmp sgt i64 %2, 0
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %xtraiter = and i64 %2, 1
  %4 = icmp eq i64 %2, 1
  br i1 %4, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %2, 9223372036854775806
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa.loopexit:          ; preds = %.lr.ph
  %5 = sext i1 %22 to i64
  br label %._crit_edge.loopexit.unr-lcssa

._crit_edge.loopexit.unr-lcssa:                   ; preds = %._crit_edge.loopexit.unr-lcssa.loopexit, %.lr.ph.preheader
  %.011.in15.unr = phi i64 [ %2, %.lr.ph.preheader ], [ %.011.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.01214.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %5, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %.011.epil = add nsw i64 %.011.in15.unr, -1
  %spec.select.epil = add nsw i64 %.01214.unr, %0
  %6 = getelementptr inbounds [100001 x i64], ptr @a, i64 0, i64 %.011.epil
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = icmp slt i64 %spec.select.epil, %7
  %9 = add nsw i64 %spec.select.epil, 10
  %.1.epil = select i1 %8, i64 %9, i64 %spec.select.epil
  %10 = sub nsw i64 %.1.epil, %7
  %11 = getelementptr inbounds [100000 x i64], ptr @r, i64 0, i64 %.011.epil
  store i64 %10, ptr %11, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %1
  ret void

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.011.in15 = phi i64 [ %2, %.lr.ph.preheader.new ], [ %.011.1, %.lr.ph ]
  %.01214 = phi i1 [ false, %.lr.ph.preheader.new ], [ %22, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %.011 = add nsw i64 %.011.in15, -1
  %12 = sext i1 %.01214 to i64
  %spec.select = add nsw i64 %12, %0
  %13 = getelementptr inbounds [100001 x i64], ptr @a, i64 0, i64 %.011
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = icmp slt i64 %spec.select, %14
  %16 = add nsw i64 %spec.select, 10
  %.1 = select i1 %15, i64 %16, i64 %spec.select
  %17 = sub nsw i64 %.1, %14
  %18 = getelementptr inbounds [100000 x i64], ptr @r, i64 0, i64 %.011
  store i64 %17, ptr %18, align 8, !tbaa !5
  %.011.1 = add nsw i64 %.011.in15, -2
  %19 = sext i1 %15 to i64
  %spec.select.1 = add nsw i64 %19, %0
  %20 = getelementptr inbounds [100001 x i64], ptr @a, i64 0, i64 %.011.1
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = icmp slt i64 %spec.select.1, %21
  %23 = add nsw i64 %spec.select.1, 10
  %.1.1 = select i1 %22, i64 %23, i64 %spec.select.1
  %24 = sub nsw i64 %.1.1, %21
  %25 = getelementptr inbounds [100000 x i64], ptr @r, i64 0, i64 %.011.1
  store i64 %24, ptr %25, align 8, !tbaa !5
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1.not = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1.not, label %._crit_edge.loopexit.unr-lcssa.loopexit, label %.lr.ph, !llvm.loop !9
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass