; LLVM IR from tangjz/acm-icpc :: luogu (1349)

; 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, [2 x [2 x i64]] }

@p = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@a1 = external dso_local global i32, align 4
@a2 = external dso_local global i32, align 4
@n = external dso_local global i64, align 8
@mod = external dso_local global i64, align 8
@ans = external dso_local global %struct.Matrix, align 8
@sum = external dso_local global %struct.Matrix, align 8
@.str = external hidden unnamed_addr constant [17 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.Matrix, align 8
  %3 = alloca %struct.Matrix, align 8
  store i32 0, ptr %1, align 4
  %4 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @p, ptr noundef @q, ptr noundef @a1, ptr noundef @a2, ptr noundef @n, ptr noundef @mod)
  %5 = load i64, ptr @mod, align 8, !tbaa !5
  %6 = load i32, ptr @p, align 4, !tbaa !9
  %7 = sext i32 %6 to i64
  %8 = srem i64 %7, %5
  %9 = trunc i64 %8 to i32
  store i32 %9, ptr @p, align 4, !tbaa !9
  %10 = load i64, ptr @mod, align 8, !tbaa !5
  %11 = load i32, ptr @q, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = srem i64 %12, %10
  %14 = trunc i64 %13 to i32
  store i32 %14, ptr @q, align 4, !tbaa !9
  %15 = load i64, ptr @mod, align 8, !tbaa !5
  %16 = load i32, ptr @a1, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = srem i64 %17, %15
  %19 = trunc i64 %18 to i32
  store i32 %19, ptr @a1, align 4, !tbaa !9
  %20 = load i64, ptr @mod, align 8, !tbaa !5
  %21 = load i32, ptr @a2, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = srem i64 %22, %20
  %24 = trunc i64 %23 to i32
  store i32 %24, ptr @a2, align 4, !tbaa !9
  %25 = load i64, ptr @n, align 8, !tbaa !5
  %26 = icmp eq i64 %25, 1
  br i1 %26, label %27, label %30

27:                                               ; preds = %0
  %28 = load i32, ptr @a1, align 4, !tbaa !9
  %29 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %28)
  br label %64

30:                                               ; preds = %0
  %31 = load i64, ptr @n, align 8, !tbaa !5
  %32 = icmp eq i64 %31, 2
  br i1 %32, label %33, label %36

33:                                               ; preds = %30
  %34 = load i32, ptr @a2, align 4, !tbaa !9
  %35 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %34)
  br label %63

36:                                               ; preds = %30
  %37 = load i64, ptr @n, align 8, !tbaa !5
  %38 = sub nsw i64 %37, 2
  store i64 %38, ptr @n, align 8, !tbaa !5
  store i32 1, ptr @ans, align 8, !tbaa !11
  store i32 2, ptr getelementptr inbounds (%struct.Matrix, ptr @ans, i32 0, i32 1), align 4, !tbaa !13
  %39 = load i32, ptr @a1, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  store i64 %40, ptr getelementptr inbounds (%struct.Matrix, ptr @ans, i32 0, i32 2), align 8, !tbaa !5
  %41 = load i32, ptr @a2, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  store i64 %42, ptr getelementptr inbounds ([2 x i64], ptr getelementptr inbounds (%struct.Matrix, ptr @ans, i32 0, i32 2), i64 0, i64 1), align 8, !tbaa !5
  store i32 2, ptr getelementptr inbounds (%struct.Matrix, ptr @sum, i32 0, i32 1), align 4, !tbaa !13
  store i32 2, ptr @sum, align 8, !tbaa !11
  %43 = load i32, ptr @q, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  store i64 %44, ptr getelementptr inbounds ([2 x i64], ptr getelementptr inbounds (%struct.Matrix, ptr @sum, i32 0, i32 2), i64 0, i64 1), align 8, !tbaa !5
  %45 = load i64, ptr @mod, align 8, !tbaa !5
  %46 = srem i64 1, %45
  store i64 %46, ptr getelementptr inbounds ([2 x [2 x i64]], ptr getelementptr inbounds (%struct.Matrix, ptr @sum, i32 0, i32 2), i64 0, i64 1), align 8, !tbaa !5
  %47 = load i32, ptr @p, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  store i64 %48, ptr getelementptr inbounds ([2 x i64], ptr getelementptr inbounds ([2 x [2 x i64]], ptr getelementptr inbounds (%struct.Matrix, ptr @sum, i32 0, i32 2), i64 0, i64 1), i64 0, i64 1), align 8, !tbaa !5
  br label %49

49:                                               ; preds = %57, %36
  %50 = load i64, ptr @n, align 8, !tbaa !5
  %51 = icmp ne i64 %50, 0
  br i1 %51, label %52, label %60

52:                                               ; preds = %49
  %53 = load i64, ptr @n, align 8, !tbaa !5
  %54 = and i64 %53, 1
  %55 = icmp ne i64 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %52
  call void @llvm.lifetime.start.p0(i64 40, ptr %2) #4
  call void @_ZN6MatrixmLERKS_(ptr dead_on_unwind writable sret(%struct.Matrix) align 8 %2, ptr noundef nonnull align 8 dereferenceable(40) @ans, ptr noundef nonnull align 8 dereferenceable(40) @sum)
  call void @llvm.lifetime.end.p0(i64 40, ptr %2) #4
  br label %57

57:                                               ; preds = %56, %52
  call void @llvm.lifetime.start.p0(i64 40, ptr %3) #4
  call void @_ZN6MatrixmLERKS_(ptr dead_on_unwind writable sret(%struct.Matrix) align 8 %3, ptr noundef nonnull align 8 dereferenceable(40) @sum, ptr noundef nonnull align 8 dereferenceable(40) @sum)
  call void @llvm.lifetime.end.p0(i64 40, ptr %3) #4
  %58 = load i64, ptr @n, align 8, !tbaa !5
  %59 = ashr i64 %58, 1
  store i64 %59, ptr @n, align 8, !tbaa !5
  br label %49, !llvm.loop !14

60:                                               ; preds = %49
  %61 = load i64, ptr getelementptr inbounds ([2 x i64], ptr getelementptr inbounds (%struct.Matrix, ptr @ans, i32 0, i32 2), i64 0, i64 1), align 8, !tbaa !5
  %62 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i64 noundef %61)
  br label %63

63:                                               ; preds = %60, %33
  br label %64

64:                                               ; preds = %63, %27
  %65 = load i32, ptr %1, align 4
  ret i32 %65
}

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN6MatrixmLERKS_(ptr dead_on_unwind noalias writable sret(%struct.Matrix) align 8, ptr noundef nonnull align 8 dereferenceable(40), ptr noundef nonnull align 8 dereferenceable(40)) #2 align 2

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

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

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 = { "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 = { 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 #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS6Matrix", !10, i64 0, !10, i64 4, !7, i64 8}
!13 = !{!12, !10, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"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"

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

@p = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@a1 = external dso_local global i32, align 4
@a2 = external dso_local global i32, align 4
@n = external dso_local global i64, align 8
@mod = external dso_local global i64, align 8
@ans = external dso_local global %struct.Matrix, align 8
@sum = external dso_local global %struct.Matrix, align 8
@.str = external hidden unnamed_addr constant [17 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca %struct.Matrix, align 8
  %2 = alloca %struct.Matrix, align 8
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @p, ptr noundef nonnull @q, ptr noundef nonnull @a1, ptr noundef nonnull @a2, ptr noundef nonnull @n, ptr noundef nonnull @mod)
  %4 = load i64, ptr @mod, align 8, !tbaa !5
  %5 = load i32, ptr @p, align 4, !tbaa !9
  %6 = sext i32 %5 to i64
  %7 = srem i64 %6, %4
  %8 = trunc nsw i64 %7 to i32
  store i32 %8, ptr @p, align 4, !tbaa !9
  %9 = load i32, ptr @q, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = srem i64 %10, %4
  %12 = trunc nsw i64 %11 to i32
  store i32 %12, ptr @q, align 4, !tbaa !9
  %13 = load i32, ptr @a1, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = srem i64 %14, %4
  %16 = trunc nsw i64 %15 to i32
  store i32 %16, ptr @a1, align 4, !tbaa !9
  %17 = load i32, ptr @a2, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = srem i64 %18, %4
  %20 = trunc nsw i64 %19 to i32
  store i32 %20, ptr @a2, align 4, !tbaa !9
  %21 = load i64, ptr @n, align 8, !tbaa !5
  switch i64 %21, label %.lr.ph.preheader [
    i64 1, label %22
    i64 2, label %24
  ]

22:                                               ; preds = %0
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %16)
  br label %35

24:                                               ; preds = %0
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %20)
  br label %35

.lr.ph.preheader:                                 ; preds = %0
  %26 = add nsw i64 %21, -2
  store i64 %26, ptr @n, align 8, !tbaa !5
  store i32 1, ptr @ans, align 8, !tbaa !11
  store i32 2, ptr getelementptr inbounds (i8, ptr @ans, i64 4), align 4, !tbaa !13
  store i64 %15, ptr getelementptr inbounds (i8, ptr @ans, i64 8), align 8, !tbaa !5
  store i64 %19, ptr getelementptr inbounds (i8, ptr @ans, i64 16), align 8, !tbaa !5
  store i32 2, ptr getelementptr inbounds (i8, ptr @sum, i64 4), align 4, !tbaa !13
  store i32 2, ptr @sum, align 8, !tbaa !11
  store i64 %11, ptr getelementptr inbounds (i8, ptr @sum, i64 16), align 8, !tbaa !5
  %27 = srem i64 1, %4
  store i64 %27, ptr getelementptr inbounds (i8, ptr @sum, i64 24), align 8, !tbaa !5
  store i64 %7, ptr getelementptr inbounds (i8, ptr @sum, i64 32), align 8, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %31
  %28 = phi i64 [ %33, %31 ], [ %26, %.lr.ph.preheader ]
  %29 = and i64 %28, 1
  %.not2 = icmp eq i64 %29, 0
  br i1 %.not2, label %31, label %30

30:                                               ; preds = %.lr.ph
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %1) #4
  call void @_ZN6MatrixmLERKS_(ptr dead_on_unwind nonnull writable sret(%struct.Matrix) align 8 %1, ptr noundef nonnull align 8 dereferenceable(40) @ans, ptr noundef nonnull align 8 dereferenceable(40) @sum)
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %1) #4
  br label %31

31:                                               ; preds = %30, %.lr.ph
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %2) #4
  call void @_ZN6MatrixmLERKS_(ptr dead_on_unwind nonnull writable sret(%struct.Matrix) align 8 %2, ptr noundef nonnull align 8 dereferenceable(40) @sum, ptr noundef nonnull align 8 dereferenceable(40) @sum)
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %2) #4
  %32 = load i64, ptr @n, align 8, !tbaa !5
  %33 = ashr i64 %32, 1
  store i64 %33, ptr @n, align 8, !tbaa !5
  %.not = icmp ult i64 %32, 2
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %31
  %.pre = load i64, ptr getelementptr inbounds (i8, ptr @ans, i64 16), align 8, !tbaa !5
  %34 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i64 noundef %.pre)
  br label %35

35:                                               ; preds = %24, %._crit_edge, %22
  ret i32 0
}

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN6MatrixmLERKS_(ptr dead_on_unwind noalias writable sret(%struct.Matrix) align 8, ptr noundef nonnull align 8 dereferenceable(40), ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #2 align 2

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

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

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 = { 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 #2 = { 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 #3 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS6Matrix", !10, i64 0, !10, i64 4, !7, i64 8}
!13 = !{!12, !10, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass