; LLVM IR from tangjz/acm-icpc :: 51nod (1582)

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

%struct.Matrix = type { i32, i32, [101 x [101 x i32]] }

@A = external dso_local global %struct.Matrix, align 4
@n = external dso_local global i32, align 4
@m = external dso_local global i32, align 4
@d = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca %struct.Matrix, align 4
  store i32 0, ptr %1, align 4
  store i32 101, ptr getelementptr inbounds (%struct.Matrix, ptr @A, i32 0, i32 1), align 4, !tbaa !5
  store i32 101, ptr @A, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !11
  br label %4

4:                                                ; preds = %16, %0
  %5 = load i32, ptr %2, align 4, !tbaa !11
  %6 = icmp slt i32 %5, 99
  br i1 %6, label %8, label %7

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

8:                                                ; preds = %4
  %9 = load i32, ptr %2, align 4, !tbaa !11
  %10 = add nsw i32 %9, 1
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [101 x [101 x i32]], ptr getelementptr inbounds (%struct.Matrix, ptr @A, i32 0, i32 2), i64 0, i64 %11
  %13 = load i32, ptr %2, align 4, !tbaa !11
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [101 x i32], ptr %12, i64 0, i64 %14
  store i32 1, ptr %15, align 4, !tbaa !11
  br label %16

16:                                               ; preds = %8
  %17 = load i32, ptr %2, align 4, !tbaa !11
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %2, align 4, !tbaa !11
  br label %4, !llvm.loop !12

19:                                               ; preds = %7
  store i32 1, ptr getelementptr inbounds ([101 x i32], ptr getelementptr inbounds ([101 x [101 x i32]], ptr getelementptr inbounds (%struct.Matrix, ptr @A, i32 0, i32 2), i64 0, i64 100), i64 0, i64 100), align 4, !tbaa !11
  store i32 1, ptr getelementptr inbounds ([101 x i32], ptr getelementptr inbounds ([101 x [101 x i32]], ptr getelementptr inbounds (%struct.Matrix, ptr @A, i32 0, i32 2), i64 0, i64 100), i64 0, i64 99), align 4, !tbaa !11
  %20 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n, ptr noundef @m)
  br label %21

21:                                               ; preds = %25, %19
  %22 = load i32, ptr @n, align 4, !tbaa !11
  %23 = add nsw i32 %22, -1
  store i32 %23, ptr @n, align 4, !tbaa !11
  %24 = icmp ne i32 %22, 0
  br i1 %24, label %25, label %34

25:                                               ; preds = %21
  %26 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @d)
  %27 = load i32, ptr @d, align 4, !tbaa !11
  %28 = sub nsw i32 100, %27
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [101 x [101 x i32]], ptr getelementptr inbounds (%struct.Matrix, ptr @A, i32 0, i32 2), i64 0, i64 %29
  %31 = getelementptr inbounds [101 x i32], ptr %30, i64 0, i64 99
  %32 = load i32, ptr %31, align 4, !tbaa !11
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %31, align 4, !tbaa !11
  br label %21, !llvm.loop !14

34:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 40812, ptr %3) #5
  %35 = load i32, ptr @m, align 4, !tbaa !11
  call void @_ZNK6Matrix3powEi(ptr dead_on_unwind writable sret(%struct.Matrix) align 4 %3, ptr noundef nonnull align 4 dereferenceable(40812) @A, i32 noundef %35)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 @A, ptr align 4 %3, i64 40812, i1 false), !tbaa.struct !15
  call void @llvm.lifetime.end.p0(i64 40812, ptr %3) #5
  %36 = load i32, ptr getelementptr inbounds ([101 x i32], ptr getelementptr inbounds ([101 x [101 x i32]], ptr getelementptr inbounds (%struct.Matrix, ptr @A, i32 0, i32 2), i64 0, i64 99), i64 0, i64 99), align 4, !tbaa !11
  %37 = load i32, ptr getelementptr inbounds ([101 x i32], ptr getelementptr inbounds ([101 x [101 x i32]], ptr getelementptr inbounds (%struct.Matrix, ptr @A, i32 0, i32 2), i64 0, i64 100), i64 0, i64 99), align 4, !tbaa !11
  %38 = add nsw i32 %36, %37
  store i32 %38, ptr @d, align 4, !tbaa !11
  %39 = load i32, ptr @d, align 4, !tbaa !11
  %40 = icmp slt i32 %39, 1000000007
  br i1 %40, label %41, label %43

41:                                               ; preds = %34
  %42 = load i32, ptr @d, align 4, !tbaa !11
  br label %46

43:                                               ; preds = %34
  %44 = load i32, ptr @d, align 4, !tbaa !11
  %45 = sub nsw i32 %44, 1000000007
  br label %46

46:                                               ; preds = %43, %41
  %47 = phi i32 [ %42, %41 ], [ %45, %43 ]
  %48 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %47)
  ret i32 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

declare i32 @__isoc99_scanf(ptr noundef, ...) #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNK6Matrix3powEi(ptr dead_on_unwind noalias writable sret(%struct.Matrix) align 4, ptr noundef nonnull align 4 dereferenceable(40812), i32 noundef) #3 align 2

; 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) #4

declare i32 @printf(ptr noundef, ...) #2

attributes #0 = { mustprogress norecurse 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 = { "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 #3 = { 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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, !7, i64 4}
!6 = !{!"_ZTS6Matrix", !7, i64 0, !7, i64 4, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = !{i64 0, i64 4, !11, i64 4, i64 4, !11, i64 8, i64 40804, !16}
!16 = !{!8, !8, i64 0}


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

%struct.Matrix = type { i32, i32, [101 x [101 x i32]] }

@A = external dso_local global %struct.Matrix, align 4
@n = external dso_local global i32, align 4
@m = external dso_local global i32, align 4
@d = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca %struct.Matrix, align 4
  store i32 101, ptr getelementptr inbounds (i8, ptr @A, i64 4), align 4, !tbaa !5
  store i32 101, ptr @A, align 4, !tbaa !10
  br label %6

2:                                                ; preds = %6
  store i32 1, ptr getelementptr inbounds (i8, ptr @A, i64 40808), align 4, !tbaa !11
  store i32 1, ptr getelementptr inbounds (i8, ptr @A, i64 40804), align 4, !tbaa !11
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @m)
  %4 = load i32, ptr @n, align 4, !tbaa !11
  %5 = add nsw i32 %4, -1
  store i32 %5, ptr @n, align 4, !tbaa !11
  %.not5 = icmp eq i32 %4, 0
  br i1 %.not5, label %._crit_edge, label %.lr.ph

6:                                                ; preds = %6, %0
  %indvars.iv = phi i64 [ 0, %0 ], [ %indvars.iv.next.2, %6 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %7 = getelementptr inbounds [101 x [101 x i32]], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 %indvars.iv.next, i64 %indvars.iv
  store i32 1, ptr %7, align 4, !tbaa !11
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %8 = getelementptr inbounds [101 x [101 x i32]], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 %indvars.iv.next.1, i64 %indvars.iv.next
  store i32 1, ptr %8, align 4, !tbaa !11
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %9 = getelementptr inbounds [101 x [101 x i32]], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 %indvars.iv.next.2, i64 %indvars.iv.next.1
  store i32 1, ptr %9, align 4, !tbaa !11
  %exitcond.not.2 = icmp eq i64 %indvars.iv.next.2, 99
  br i1 %exitcond.not.2, label %2, label %6, !llvm.loop !12

.lr.ph:                                           ; preds = %2, %.lr.ph
  %10 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @d)
  %11 = load i32, ptr @d, align 4, !tbaa !11
  %12 = sub nsw i32 100, %11
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [101 x [101 x i32]], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 %13, i64 99
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %14, align 4, !tbaa !11
  %17 = load i32, ptr @n, align 4, !tbaa !11
  %18 = add nsw i32 %17, -1
  store i32 %18, ptr @n, align 4, !tbaa !11
  %.not = icmp eq i32 %17, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph, %2
  call void @llvm.lifetime.start.p0(i64 40812, ptr nonnull %1) #5
  %19 = load i32, ptr @m, align 4, !tbaa !11
  call void @_ZNK6Matrix3powEi(ptr dead_on_unwind nonnull writable sret(%struct.Matrix) align 4 %1, ptr noundef nonnull align 4 dereferenceable(40812) @A, i32 noundef %19)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(40812) @A, ptr noundef nonnull align 4 dereferenceable(40812) %1, i64 40812, i1 false), !tbaa.struct !15
  call void @llvm.lifetime.end.p0(i64 40812, ptr nonnull %1) #5
  %20 = load i32, ptr getelementptr inbounds (i8, ptr @A, i64 40400), align 4, !tbaa !11
  %21 = load i32, ptr getelementptr inbounds (i8, ptr @A, i64 40804), align 4, !tbaa !11
  %22 = add nsw i32 %21, %20
  store i32 %22, ptr @d, align 4, !tbaa !11
  %23 = icmp slt i32 %22, 1000000007
  %24 = add nsw i32 %22, -1000000007
  %25 = select i1 %23, i32 %22, i32 %24
  %26 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %25)
  ret i32 0
}

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

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

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNK6Matrix3powEi(ptr dead_on_unwind noalias writable sret(%struct.Matrix) align 4, ptr noundef nonnull align 4 dereferenceable(40812), i32 noundef) local_unnamed_addr #3 align 2

; Function Attrs: mustprogress 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) #4

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

attributes #0 = { mustprogress norecurse 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nofree nounwind "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 #3 = { 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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, !7, i64 4}
!6 = !{!"_ZTS6Matrix", !7, i64 0, !7, i64 4, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = !{i64 0, i64 4, !11, i64 4, i64 4, !11, i64 8, i64 40804, !16}
!16 = !{!8, !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass