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

; 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
@a = external dso_local global [7 x [7 x i8]], align 16
@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 void @_Z4copyv() #1 {
  %1 = alloca i64, align 8
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  store i64 0, ptr %1, align 8, !tbaa !5
  br label %4

4:                                                ; preds = %29, %0
  %5 = load i64, ptr %1, align 8, !tbaa !5
  %6 = load i64, ptr @n, align 8, !tbaa !5
  %7 = icmp slt i64 %5, %6
  br i1 %7, label %9, label %8

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

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

10:                                               ; preds = %25, %9
  %11 = load i64, ptr %3, align 8, !tbaa !5
  %12 = load i64, ptr @m, align 8, !tbaa !5
  %13 = icmp slt i64 %11, %12
  br i1 %13, label %15, label %14

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

15:                                               ; preds = %10
  %16 = load i64, ptr %1, align 8, !tbaa !5
  %17 = getelementptr inbounds [7 x [7 x i8]], ptr @a, i64 0, i64 %16
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds [7 x i8], ptr %17, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %21 = load i64, ptr %1, align 8, !tbaa !5
  %22 = getelementptr inbounds [7 x [7 x i8]], ptr @b, i64 0, i64 %21
  %23 = load i64, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds [7 x i8], ptr %22, i64 0, i64 %23
  store i8 %20, ptr %24, align 1, !tbaa !9
  br label %25

25:                                               ; preds = %15
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = add nsw i64 %26, 1
  store i64 %27, ptr %3, align 8, !tbaa !5
  br label %10, !llvm.loop !10

28:                                               ; preds = %14
  br label %29

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

32:                                               ; preds = %8
  ret void
}

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
@a = external dso_local local_unnamed_addr global [7 x [7 x i8]], align 16
@b = external dso_local local_unnamed_addr global [7 x [7 x i8]], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z4copyv() 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_edge11

.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_edge11

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %xtraiter = and i64 %1, 3
  %5 = icmp ult i64 %1, 4
  br i1 %5, label %._crit_edge11.loopexit.unr-lcssa, label %.preheader.us.preheader.new

.preheader.us.preheader.new:                      ; preds = %.preheader.us.preheader
  %unroll_iter = and i64 %1, 9223372036854775804
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us, %.preheader.us.preheader.new
  %.0810.us = phi i64 [ 0, %.preheader.us.preheader.new ], [ %13, %.preheader.us ]
  %niter = phi i64 [ 0, %.preheader.us.preheader.new ], [ %niter.next.3, %.preheader.us ]
  %6 = mul nuw nsw i64 %.0810.us, 7
  %scevgep = getelementptr i8, ptr @b, i64 %6
  %scevgep13 = getelementptr i8, ptr @a, i64 %6
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep, ptr align 4 %scevgep13, i64 %3, i1 false), !tbaa !9
  %7 = mul nuw i64 %.0810.us, 7
  %8 = add nuw i64 %7, 7
  %scevgep.1 = getelementptr i8, ptr @b, i64 %8
  %scevgep13.1 = getelementptr i8, ptr @a, i64 %8
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %scevgep.1, ptr align 1 %scevgep13.1, i64 %3, i1 false), !tbaa !9
  %9 = mul nuw i64 %.0810.us, 7
  %10 = add nuw i64 %9, 14
  %scevgep.2 = getelementptr i8, ptr @b, i64 %10
  %scevgep13.2 = getelementptr i8, ptr @a, i64 %10
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 2 %scevgep.2, ptr align 2 %scevgep13.2, i64 %3, i1 false), !tbaa !9
  %11 = mul nuw i64 %.0810.us, 7
  %12 = add nuw i64 %11, 21
  %scevgep.3 = getelementptr i8, ptr @b, i64 %12
  %scevgep13.3 = getelementptr i8, ptr @a, i64 %12
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %scevgep.3, ptr align 1 %scevgep13.3, i64 %3, i1 false), !tbaa !9
  %13 = add nuw nsw i64 %.0810.us, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge11.loopexit.unr-lcssa, label %.preheader.us, !llvm.loop !10

._crit_edge11.loopexit.unr-lcssa:                 ; preds = %.preheader.us, %.preheader.us.preheader
  %.0810.us.unr = phi i64 [ 0, %.preheader.us.preheader ], [ %13, %.preheader.us ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge11, label %.preheader.us.epil

.preheader.us.epil:                               ; preds = %._crit_edge11.loopexit.unr-lcssa, %.preheader.us.epil
  %.0810.us.epil = phi i64 [ %15, %.preheader.us.epil ], [ %.0810.us.unr, %._crit_edge11.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader.us.epil ], [ 0, %._crit_edge11.loopexit.unr-lcssa ]
  %14 = mul nuw nsw i64 %.0810.us.epil, 7
  %scevgep.epil = getelementptr i8, ptr @b, i64 %14
  %scevgep13.epil = getelementptr i8, ptr @a, i64 %14
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %scevgep.epil, ptr align 1 %scevgep13.epil, i64 %3, i1 false), !tbaa !9
  %15 = add nuw nsw i64 %.0810.us.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge11, label %.preheader.us.epil, !llvm.loop !12

._crit_edge11:                                    ; preds = %._crit_edge11.loopexit.unr-lcssa, %.preheader.us.epil, %.preheader.lr.ph, %0
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !13}
!13 = !{!"llvm.loop.unroll.disable"}


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