; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2011-12/Jan/12 Jan G1.ll (Z2po3mati)

; 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.mat = type { [300 x [300 x i32]] }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3matC2Ev(ptr noundef nonnull align 4 dereferenceable(360000)) unnamed_addr #0 align 2

; 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 @_Z2po3mati(ptr dead_on_unwind noalias writable sret(%struct.mat) align 4 %0, ptr noundef byval(%struct.mat) align 8 %1, i32 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %struct.mat, align 4
  %7 = alloca %struct.mat, align 4
  store i32 %2, ptr %4, align 4, !tbaa !5
  call void @_ZN3matC2Ev(ptr noundef nonnull align 4 dereferenceable(360000) %0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %20, %3
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 300
  br i1 %10, label %12, label %11

11:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %23

12:                                               ; preds = %8
  %13 = getelementptr inbounds %struct.mat, ptr %0, i32 0, i32 0
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [300 x [300 x i32]], ptr %13, i64 0, i64 %15
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [300 x i32], ptr %16, i64 0, i64 %18
  store i32 0, ptr %19, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %12
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %5, align 4, !tbaa !5
  br label %8, !llvm.loop !9

23:                                               ; preds = %11
  br label %24

24:                                               ; preds = %32, %23
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %35

27:                                               ; preds = %24
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = and i32 %28, 1
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 360000, ptr %6) #4
  call void @_ZN3matmlERKS_(ptr dead_on_unwind writable sret(%struct.mat) align 4 %6, ptr noundef nonnull align 4 dereferenceable(360000) %0, ptr noundef nonnull align 4 dereferenceable(360000) %1)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %0, ptr align 4 %6, i64 360000, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 360000, ptr %6) #4
  br label %32

32:                                               ; preds = %31, %27
  call void @llvm.lifetime.start.p0(i64 360000, ptr %7) #4
  call void @_ZN3matmlERKS_(ptr dead_on_unwind writable sret(%struct.mat) align 4 %7, ptr noundef nonnull align 4 dereferenceable(360000) %1, ptr noundef nonnull align 4 dereferenceable(360000) %1)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %1, ptr align 4 %7, i64 360000, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 360000, ptr %7) #4
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = sdiv i32 %33, 2
  store i32 %34, ptr %4, align 4, !tbaa !5
  br label %24, !llvm.loop !13

35:                                               ; preds = %24
  ret void
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3matmlERKS_(ptr dead_on_unwind noalias writable sret(%struct.mat) align 4, ptr noundef nonnull align 4 dereferenceable(360000), ptr noundef nonnull align 4 dereferenceable(360000)) #2 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) #3

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!"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 360000, !12}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !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"

%struct.mat = type { [300 x [300 x i32]] }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3matC2Ev(ptr noundef nonnull align 4 dereferenceable(360000)) unnamed_addr #0 align 2

; 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 uwtable
define dso_local void @_Z2po3mati(ptr dead_on_unwind noalias nonnull writable sret(%struct.mat) align 4 %0, ptr noundef byval(%struct.mat) align 8 %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = alloca %struct.mat, align 4
  %5 = alloca %struct.mat, align 4
  tail call void @_ZN3matC2Ev(ptr noundef nonnull align 4 dereferenceable(360000) %0)
  br label %6

.preheader:                                       ; preds = %6
  %.not10 = icmp eq i32 %2, 0
  br i1 %.not10, label %._crit_edge, label %.lr.ph

6:                                                ; preds = %6, %3
  %indvars.iv = phi i64 [ 0, %3 ], [ %indvars.iv.next.5, %6 ]
  %7 = getelementptr inbounds [300 x [300 x i32]], ptr %0, i64 0, i64 %indvars.iv, i64 %indvars.iv
  store i32 0, ptr %7, align 4, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %8 = getelementptr inbounds [300 x [300 x i32]], ptr %0, i64 0, i64 %indvars.iv.next, i64 %indvars.iv.next
  store i32 0, ptr %8, align 4, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %9 = getelementptr inbounds [300 x [300 x i32]], ptr %0, i64 0, i64 %indvars.iv.next.1, i64 %indvars.iv.next.1
  store i32 0, ptr %9, align 4, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %10 = getelementptr inbounds [300 x [300 x i32]], ptr %0, i64 0, i64 %indvars.iv.next.2, i64 %indvars.iv.next.2
  store i32 0, ptr %10, align 4, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %11 = getelementptr inbounds [300 x [300 x i32]], ptr %0, i64 0, i64 %indvars.iv.next.3, i64 %indvars.iv.next.3
  store i32 0, ptr %11, align 4, !tbaa !5
  %indvars.iv.next.4 = add nuw nsw i64 %indvars.iv, 5
  %12 = getelementptr inbounds [300 x [300 x i32]], ptr %0, i64 0, i64 %indvars.iv.next.4, i64 %indvars.iv.next.4
  store i32 0, ptr %12, align 4, !tbaa !5
  %indvars.iv.next.5 = add nuw nsw i64 %indvars.iv, 6
  %exitcond.not.5 = icmp eq i64 %indvars.iv.next.5, 300
  br i1 %exitcond.not.5, label %.preheader, label %6, !llvm.loop !9

.lr.ph:                                           ; preds = %.preheader, %15
  %.0711 = phi i32 [ %16, %15 ], [ %2, %.preheader ]
  %13 = and i32 %.0711, 1
  %.not8 = icmp eq i32 %13, 0
  br i1 %.not8, label %15, label %14

14:                                               ; preds = %.lr.ph
  call void @llvm.lifetime.start.p0(i64 360000, ptr nonnull %4) #4
  call void @_ZN3matmlERKS_(ptr dead_on_unwind nonnull writable sret(%struct.mat) align 4 %4, ptr noundef nonnull align 4 dereferenceable(360000) %0, ptr noundef nonnull align 4 dereferenceable(360000) %1)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(360000) %0, ptr noundef nonnull align 4 dereferenceable(360000) %4, i64 360000, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 360000, ptr nonnull %4) #4
  br label %15

15:                                               ; preds = %14, %.lr.ph
  call void @llvm.lifetime.start.p0(i64 360000, ptr nonnull %5) #4
  call void @_ZN3matmlERKS_(ptr dead_on_unwind nonnull writable sret(%struct.mat) align 4 %5, ptr noundef nonnull align 4 dereferenceable(360000) %1, ptr noundef nonnull align 4 dereferenceable(360000) %1)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(360000) %1, ptr noundef nonnull align 4 dereferenceable(360000) %5, i64 360000, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 360000, ptr nonnull %5) #4
  %16 = sdiv i32 %.0711, 2
  %.0711.off = add i32 %.0711, 1
  %.not = icmp ult i32 %.0711.off, 3
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %15, %.preheader
  ret void
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3matmlERKS_(ptr dead_on_unwind noalias writable sret(%struct.mat) align 4, ptr noundef nonnull align 4 dereferenceable(360000), ptr noundef nonnull align 4 dereferenceable(360000)) local_unnamed_addr #2 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) #3

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 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 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 = !{!"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 360000, !12}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !10}


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