; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/323 Division 2/Problem D/D.ll (Z4fasti)

; 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 i32, align 4
@g = external dso_local global [101 x [101 x i32]], align 16
@ret = external dso_local global [101 x [101 x i32]], align 16

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3mulPA101_iS0_(ptr noundef, ptr 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 uwtable
define dso_local void @_Z4fasti(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %14, %1
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %9, label %17

9:                                                ; preds = %6
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = srem i32 %10, 2
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %9
  call void @_Z3mulPA101_iS0_(ptr noundef @ret, ptr noundef @g)
  br label %14

14:                                               ; preds = %13, %9
  call void @_Z3mulPA101_iS0_(ptr noundef @g, ptr noundef @g)
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = ashr i32 %15, 1
  store i32 %16, ptr %2, align 4, !tbaa !5
  br label %6, !llvm.loop !9

17:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %47, %17
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = load i32, ptr @n, align 4, !tbaa !5
  %21 = icmp sle i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 4, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %50

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %43, %23
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = load i32, ptr @n, align 4, !tbaa !5
  %27 = icmp sle i32 %25, %26
  br i1 %27, label %29, label %28

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

29:                                               ; preds = %24
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [101 x [101 x i32]], ptr @ret, i64 0, i64 %31
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [101 x i32], ptr %32, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [101 x [101 x i32]], ptr @g, i64 0, i64 %38
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [101 x i32], ptr %39, i64 0, i64 %41
  store i32 %36, ptr %42, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %29
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %5, align 4, !tbaa !5
  br label %24, !llvm.loop !11

46:                                               ; preds = %28
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %3, align 4, !tbaa !5
  br label %18, !llvm.loop !12

50:                                               ; preds = %22
  ret void
}

attributes #0 = { mustprogress 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"

@n = external dso_local local_unnamed_addr global i32, align 4
@g = external dso_local global [101 x [101 x i32]], align 16
@ret = external dso_local global [101 x [101 x i32]], align 16

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3mulPA101_iS0_(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z4fasti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp sgt i32 %0, 0
  br i1 %2, label %.lr.ph, label %.preheader15

.preheader15:                                     ; preds = %9, %1
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %.not20 = icmp slt i32 %3, 1
  br i1 %.not20, label %._crit_edge22, label %.preheader.preheader

.preheader.preheader:                             ; preds = %.preheader15
  %4 = zext nneg i32 %3 to i64
  %5 = shl nuw nsw i64 %4, 2
  %xtraiter = and i64 %4, 1
  %6 = icmp eq i32 %3, 1
  br i1 %6, label %._crit_edge22.loopexit.unr-lcssa, label %.preheader.preheader.new

.preheader.preheader.new:                         ; preds = %.preheader.preheader
  %unroll_iter = and i64 %4, 2147483646
  br label %.preheader

.lr.ph:                                           ; preds = %1, %9
  %.01216 = phi i32 [ %10, %9 ], [ %0, %1 ]
  %7 = and i32 %.01216, 1
  %.not14 = icmp eq i32 %7, 0
  br i1 %.not14, label %9, label %8

8:                                                ; preds = %.lr.ph
  tail call void @_Z3mulPA101_iS0_(ptr noundef nonnull @ret, ptr noundef nonnull @g)
  br label %9

9:                                                ; preds = %8, %.lr.ph
  tail call void @_Z3mulPA101_iS0_(ptr noundef nonnull @g, ptr noundef nonnull @g)
  %10 = lshr i32 %.01216, 1
  %.not23 = icmp ult i32 %.01216, 2
  br i1 %.not23, label %.preheader15, label %.lr.ph, !llvm.loop !9

.preheader:                                       ; preds = %.preheader, %.preheader.preheader.new
  %indvar = phi i64 [ 0, %.preheader.preheader.new ], [ %indvar.next.1, %.preheader ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.1, %.preheader ]
  %11 = mul nuw nsw i64 %indvar, 404
  %12 = add nuw nsw i64 %11, 408
  %scevgep = getelementptr i8, ptr @g, i64 %12
  %scevgep24 = getelementptr i8, ptr @ret, i64 %12
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 8 %scevgep, ptr align 8 %scevgep24, i64 %5, i1 false), !tbaa !5
  %13 = mul nuw i64 %indvar, 404
  %14 = add nuw i64 %13, 812
  %scevgep.1 = getelementptr i8, ptr @g, i64 %14
  %scevgep24.1 = getelementptr i8, ptr @ret, i64 %14
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep.1, ptr align 4 %scevgep24.1, i64 %5, i1 false), !tbaa !5
  %indvar.next.1 = add nuw nsw i64 %indvar, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge22.loopexit.unr-lcssa.loopexit, label %.preheader, !llvm.loop !11

._crit_edge22.loopexit.unr-lcssa.loopexit:        ; preds = %.preheader
  %15 = mul nuw nsw i64 %indvar.next.1, 404
  %16 = add nuw nsw i64 %15, 408
  br label %._crit_edge22.loopexit.unr-lcssa

._crit_edge22.loopexit.unr-lcssa:                 ; preds = %._crit_edge22.loopexit.unr-lcssa.loopexit, %.preheader.preheader
  %indvar.unr = phi i64 [ 408, %.preheader.preheader ], [ %16, %._crit_edge22.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge22, label %.preheader.epil

.preheader.epil:                                  ; preds = %._crit_edge22.loopexit.unr-lcssa
  %scevgep.epil = getelementptr i8, ptr @g, i64 %indvar.unr
  %scevgep24.epil = getelementptr i8, ptr @ret, i64 %indvar.unr
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep.epil, ptr align 4 %scevgep24.epil, i64 %5, i1 false), !tbaa !5
  br label %._crit_edge22

._crit_edge22:                                    ; preds = %.preheader.epil, %._crit_edge22.loopexit.unr-lcssa, %.preheader15
  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 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 = !{!"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}


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