; LLVM IR from tangjz/acm-icpc :: lydsy/3231.ll (Z4calcx)

; 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, [16 x [16 x i32]] }

@k = external dso_local global i32, align 4
@p = external dso_local global i32, align 4
@A = external dso_local global %struct.Matrix, align 4
@B = external dso_local global %struct.Matrix, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z4calcx(i64 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %struct.Matrix, align 4
  %7 = alloca %struct.Matrix, align 4
  %8 = alloca %struct.Matrix, align 4
  %9 = alloca %struct.Matrix, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = load i32, ptr @k, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = icmp slt i64 %10, %12
  br i1 %13, label %14, label %41

14:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %36, %14
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = icmp slt i64 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %39

21:                                               ; preds = %15
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [16 x i32], ptr getelementptr inbounds (%struct.Matrix, ptr @A, i32 0, i32 2), i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = add nsw i32 %26, %25
  store i32 %27, ptr %4, align 4, !tbaa !9
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = load i32, ptr @p, align 4, !tbaa !9
  %30 = icmp sge i32 %28, %29
  br i1 %30, label %31, label %35

31:                                               ; preds = %21
  %32 = load i32, ptr @p, align 4, !tbaa !9
  %33 = load i32, ptr %4, align 4, !tbaa !9
  %34 = sub nsw i32 %33, %32
  store i32 %34, ptr %4, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %31, %21
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %5, align 4, !tbaa !9
  br label %15, !llvm.loop !11

39:                                               ; preds = %20
  %40 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %40, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %64

41:                                               ; preds = %1
  %42 = load i32, ptr @k, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = load i64, ptr %3, align 8, !tbaa !5
  %45 = sub nsw i64 %44, %43
  store i64 %45, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1032, ptr %6) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 @A, i64 1032, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.start.p0(i64 1032, ptr %7) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %7, ptr align 4 @B, i64 1032, i1 false), !tbaa.struct !13
  br label %46

46:                                               ; preds = %54, %41
  %47 = load i64, ptr %3, align 8, !tbaa !5
  %48 = icmp ne i64 %47, 0
  br i1 %48, label %49, label %57

49:                                               ; preds = %46
  %50 = load i64, ptr %3, align 8, !tbaa !5
  %51 = and i64 %50, 1
  %52 = icmp ne i64 %51, 0
  br i1 %52, label %53, label %54

53:                                               ; preds = %49
  call void @llvm.lifetime.start.p0(i64 1032, ptr %8) #4
  call void @_ZNK6MatrixmlERKS_(ptr dead_on_unwind writable sret(%struct.Matrix) align 4 %8, ptr noundef nonnull align 4 dereferenceable(1032) %6, ptr noundef nonnull align 4 dereferenceable(1032) %7)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %8, i64 1032, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 1032, ptr %8) #4
  br label %54

54:                                               ; preds = %53, %49
  call void @llvm.lifetime.start.p0(i64 1032, ptr %9) #4
  call void @_ZNK6MatrixmlERKS_(ptr dead_on_unwind writable sret(%struct.Matrix) align 4 %9, ptr noundef nonnull align 4 dereferenceable(1032) %7, ptr noundef nonnull align 4 dereferenceable(1032) %7)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %7, ptr align 4 %9, i64 1032, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 1032, ptr %9) #4
  %55 = load i64, ptr %3, align 8, !tbaa !5
  %56 = ashr i64 %55, 1
  store i64 %56, ptr %3, align 8, !tbaa !5
  br label %46, !llvm.loop !15

57:                                               ; preds = %46
  %58 = getelementptr inbounds %struct.Matrix, ptr %6, i32 0, i32 2
  %59 = getelementptr inbounds [16 x [16 x i32]], ptr %58, i64 0, i64 0
  %60 = load i32, ptr @k, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [16 x i32], ptr %59, i64 0, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !9
  store i32 %63, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 1032, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 1032, ptr %6) #4
  br label %64

64:                                               ; preds = %57, %39
  %65 = load i32, ptr %2, align 4
  ret i32 %65
}

; 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: 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) #2

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

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


; 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, [16 x [16 x i32]] }

@k = external dso_local local_unnamed_addr global i32, align 4
@p = external dso_local local_unnamed_addr global i32, align 4
@A = external dso_local local_unnamed_addr global %struct.Matrix, align 4
@B = external dso_local local_unnamed_addr global %struct.Matrix, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z4calcx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = alloca %struct.Matrix, align 4
  %3 = alloca %struct.Matrix, align 4
  %4 = alloca %struct.Matrix, align 4
  %5 = alloca %struct.Matrix, align 4
  %6 = load i32, ptr @k, align 4, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = icmp sgt i64 %7, %0
  br i1 %8, label %.preheader, label %29

.preheader:                                       ; preds = %1
  %9 = icmp sgt i64 %0, 0
  br i1 %9, label %.lr.ph30, label %.loopexit

.lr.ph30:                                         ; preds = %.preheader
  %10 = load i32, ptr @p, align 4, !tbaa !5
  %xtraiter = and i64 %0, 3
  %11 = icmp ult i64 %0, 4
  br i1 %11, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph30.new

.lr.ph30.new:                                     ; preds = %.lr.ph30
  %unroll_iter = and i64 %0, 9223372036854775804
  br label %12

12:                                               ; preds = %12, %.lr.ph30.new
  %indvars.iv = phi i64 [ 0, %.lr.ph30.new ], [ %indvars.iv.next.3, %12 ]
  %.01428 = phi i32 [ 0, %.lr.ph30.new ], [ %spec.select.3, %12 ]
  %niter = phi i64 [ 0, %.lr.ph30.new ], [ %niter.next.3, %12 ]
  %13 = getelementptr inbounds [16 x i32], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 %indvars.iv
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = add nsw i32 %14, %.01428
  %.not25 = icmp slt i32 %15, %10
  %16 = select i1 %.not25, i32 0, i32 %10
  %spec.select = sub nsw i32 %15, %16
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %17 = getelementptr inbounds [16 x i32], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 %indvars.iv.next
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, %spec.select
  %.not25.1 = icmp slt i32 %19, %10
  %20 = select i1 %.not25.1, i32 0, i32 %10
  %spec.select.1 = sub nsw i32 %19, %20
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %21 = getelementptr inbounds [16 x i32], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 %indvars.iv.next.1
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = add nsw i32 %22, %spec.select.1
  %.not25.2 = icmp slt i32 %23, %10
  %24 = select i1 %.not25.2, i32 0, i32 %10
  %spec.select.2 = sub nsw i32 %23, %24
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %25 = getelementptr inbounds [16 x i32], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 %indvars.iv.next.2
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = add nsw i32 %26, %spec.select.2
  %.not25.3 = icmp slt i32 %27, %10
  %28 = select i1 %.not25.3, i32 0, i32 %10
  %spec.select.3 = sub nsw i32 %27, %28
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 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 %.loopexit.loopexit.unr-lcssa, label %12, !llvm.loop !9

29:                                               ; preds = %1
  %30 = sub nsw i64 %0, %7
  call void @llvm.lifetime.start.p0(i64 1032, ptr nonnull %2) #3
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(1032) %2, ptr noundef nonnull align 4 dereferenceable(1032) @A, i64 1032, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.start.p0(i64 1032, ptr nonnull %3) #3
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(1032) %3, ptr noundef nonnull align 4 dereferenceable(1032) @B, i64 1032, i1 false), !tbaa.struct !11
  %.not26 = icmp eq i64 %30, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %29, %33
  %.01527 = phi i64 [ %34, %33 ], [ %30, %29 ]
  %31 = and i64 %.01527, 1
  %.not24 = icmp eq i64 %31, 0
  br i1 %.not24, label %33, label %32

32:                                               ; preds = %.lr.ph
  call void @llvm.lifetime.start.p0(i64 1032, ptr nonnull %4) #3
  call void @_ZNK6MatrixmlERKS_(ptr dead_on_unwind nonnull writable sret(%struct.Matrix) align 4 %4, ptr noundef nonnull align 4 dereferenceable(1032) %2, ptr noundef nonnull align 4 dereferenceable(1032) %3)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(1032) %2, ptr noundef nonnull align 4 dereferenceable(1032) %4, i64 1032, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 1032, ptr nonnull %4) #3
  br label %33

33:                                               ; preds = %32, %.lr.ph
  call void @llvm.lifetime.start.p0(i64 1032, ptr nonnull %5) #3
  call void @_ZNK6MatrixmlERKS_(ptr dead_on_unwind nonnull writable sret(%struct.Matrix) align 4 %5, ptr noundef nonnull align 4 dereferenceable(1032) %3, ptr noundef nonnull align 4 dereferenceable(1032) %3)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(1032) %3, ptr noundef nonnull align 4 dereferenceable(1032) %5, i64 1032, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 1032, ptr nonnull %5) #3
  %34 = ashr i64 %.01527, 1
  %.not = icmp ult i64 %.01527, 2
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !13

._crit_edge.loopexit:                             ; preds = %33
  %.pre = load i32, ptr @k, align 4, !tbaa !5
  %.pre32 = sext i32 %.pre to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %29
  %.pre-phi = phi i64 [ %.pre32, %._crit_edge.loopexit ], [ %7, %29 ]
  %35 = getelementptr inbounds i8, ptr %2, i64 8
  %36 = getelementptr inbounds [16 x i32], ptr %35, i64 0, i64 %.pre-phi
  %37 = load i32, ptr %36, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1032, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 1032, ptr nonnull %2) #3
  br label %.loopexit

.loopexit.loopexit.unr-lcssa:                     ; preds = %12, %.lr.ph30
  %spec.select.lcssa.ph = phi i32 [ poison, %.lr.ph30 ], [ %spec.select.3, %12 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph30 ], [ %indvars.iv.next.3, %12 ]
  %.01428.unr = phi i32 [ 0, %.lr.ph30 ], [ %spec.select.3, %12 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.epil.preheader

.epil.preheader:                                  ; preds = %.loopexit.loopexit.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %.loopexit.loopexit.unr-lcssa ]
  %.01428.epil = phi i32 [ %spec.select.epil, %.epil.preheader ], [ %.01428.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  %38 = getelementptr inbounds [16 x i32], ptr getelementptr inbounds (i8, ptr @A, i64 8), i64 0, i64 %indvars.iv.epil
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = add nsw i32 %39, %.01428.epil
  %.not25.epil = icmp slt i32 %40, %10
  %41 = select i1 %.not25.epil, i32 0, i32 %10
  %spec.select.epil = sub nsw i32 %40, %41
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.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 %.loopexit, label %.epil.preheader, !llvm.loop !14

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.epil.preheader, %.preheader, %._crit_edge
  %.016 = phi i32 [ %37, %._crit_edge ], [ 0, %.preheader ], [ %spec.select.lcssa.ph, %.loopexit.loopexit.unr-lcssa ], [ %spec.select.epil, %.epil.preheader ]
  ret i32 %.016
}

; 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: 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) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNK6MatrixmlERKS_(ptr dead_on_unwind noalias writable sret(%struct.Matrix) align 4, ptr noundef nonnull align 4 dereferenceable(1032), ptr noundef nonnull align 4 dereferenceable(1032)) local_unnamed_addr #0 align 2

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


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass