; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2019-20/Feb/Plat/triangles.ll (Z3rotv)

; 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 [300 x [300 x i8]], align 16
@GG = external dso_local global [300 x [300 x i8]], align 16

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3rotv() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %40, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = load i32, ptr @N, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %43

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

12:                                               ; preds = %36, %11
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = load i32, ptr @N, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  store i32 5, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %39

17:                                               ; preds = %12
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [300 x [300 x i8]], ptr @G, i64 0, i64 %19
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [300 x i8], ptr %20, i64 0, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !9, !range !11, !noundef !12
  %25 = trunc i8 %24 to i1
  %26 = load i32, ptr @N, align 4, !tbaa !5
  %27 = sub nsw i32 %26, 1
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sub nsw i32 %27, %28
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [300 x [300 x i8]], ptr @GG, i64 0, i64 %30
  %32 = load i32, ptr %1, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [300 x i8], ptr %31, i64 0, i64 %33
  %35 = zext i1 %25 to i8
  store i8 %35, ptr %34, align 1, !tbaa !9
  br label %36

36:                                               ; preds = %17
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %3, align 4, !tbaa !5
  br label %12, !llvm.loop !13

39:                                               ; preds = %16
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %1, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !15

43:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %44

44:                                               ; preds = %75, %43
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = load i32, ptr @N, align 4, !tbaa !5
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %49, label %48

48:                                               ; preds = %44
  store i32 8, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %78

49:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %71, %49
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = load i32, ptr @N, align 4, !tbaa !5
  %53 = icmp slt i32 %51, %52
  br i1 %53, label %55, label %54

54:                                               ; preds = %50
  store i32 11, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %74

55:                                               ; preds = %50
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [300 x [300 x i8]], ptr @GG, i64 0, i64 %57
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [300 x i8], ptr %58, i64 0, i64 %60
  %62 = load i8, ptr %61, align 1, !tbaa !9, !range !11, !noundef !12
  %63 = trunc i8 %62 to i1
  %64 = load i32, ptr %4, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [300 x [300 x i8]], ptr @G, i64 0, i64 %65
  %67 = load i32, ptr %5, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [300 x i8], ptr %66, i64 0, i64 %68
  %70 = zext i1 %63 to i8
  store i8 %70, ptr %69, align 1, !tbaa !9
  br label %71

71:                                               ; preds = %55
  %72 = load i32, ptr %5, align 4, !tbaa !5
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %5, align 4, !tbaa !5
  br label %50, !llvm.loop !16

74:                                               ; preds = %54
  br label %75

75:                                               ; preds = %74
  %76 = load i32, ptr %4, align 4, !tbaa !5
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %4, align 4, !tbaa !5
  br label %44, !llvm.loop !17

78:                                               ; preds = %48
  ret void
}

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


; 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 local_unnamed_addr global [300 x [300 x i8]], align 16
@GG = external dso_local local_unnamed_addr global [300 x [300 x i8]], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z3rotv() local_unnamed_addr #0 {
  %1 = load i32, ptr @N, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.preheader21.us.preheader, label %._crit_edge26

.preheader21.us.preheader:                        ; preds = %0
  %3 = zext nneg i32 %1 to i64
  %wide.trip.count32 = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count32, 1
  %4 = icmp eq i32 %1, 1
  %unroll_iter = and i64 %wide.trip.count32, 2147483646
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader21.us

.preheader21.us:                                  ; preds = %.preheader21.us.preheader, %._crit_edge.us
  %indvars.iv29 = phi i64 [ 0, %.preheader21.us.preheader ], [ %indvars.iv.next30, %._crit_edge.us ]
  br i1 %4, label %._crit_edge.us.unr-lcssa, label %.preheader21.us.new

.preheader21.us.new:                              ; preds = %.preheader21.us, %.preheader21.us.new
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %.preheader21.us.new ], [ 0, %.preheader21.us ]
  %niter = phi i64 [ %niter.next.1, %.preheader21.us.new ], [ 0, %.preheader21.us ]
  %5 = getelementptr inbounds [300 x [300 x i8]], ptr @G, i64 0, i64 %indvars.iv29, i64 %indvars.iv
  %6 = load i8, ptr %5, align 2, !tbaa !9, !range !11, !noundef !12
  %7 = xor i64 %indvars.iv, -1
  %8 = add nsw i64 %3, %7
  %9 = getelementptr inbounds [300 x [300 x i8]], ptr @GG, i64 0, i64 %8, i64 %indvars.iv29
  store i8 %6, ptr %9, align 1, !tbaa !9
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %10 = getelementptr inbounds [300 x [300 x i8]], ptr @G, i64 0, i64 %indvars.iv29, i64 %indvars.iv.next
  %11 = load i8, ptr %10, align 1, !tbaa !9, !range !11, !noundef !12
  %12 = xor i64 %indvars.iv, -2
  %13 = add nsw i64 %12, %3
  %14 = getelementptr inbounds [300 x [300 x i8]], ptr @GG, i64 0, i64 %13, i64 %indvars.iv29
  store i8 %11, ptr %14, align 1, !tbaa !9
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 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_edge.us.unr-lcssa, label %.preheader21.us.new, !llvm.loop !13

._crit_edge.us.unr-lcssa:                         ; preds = %.preheader21.us.new, %.preheader21.us
  %indvars.iv.unr = phi i64 [ 0, %.preheader21.us ], [ %indvars.iv.next.1, %.preheader21.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %._crit_edge.us.epilog-lcssa

._crit_edge.us.epilog-lcssa:                      ; preds = %._crit_edge.us.unr-lcssa
  %15 = getelementptr inbounds [300 x [300 x i8]], ptr @G, i64 0, i64 %indvars.iv29, i64 %indvars.iv.unr
  %16 = load i8, ptr %15, align 1, !tbaa !9, !range !11, !noundef !12
  %17 = xor i64 %indvars.iv.unr, -1
  %18 = add nsw i64 %3, %17
  %19 = getelementptr inbounds [300 x [300 x i8]], ptr @GG, i64 0, i64 %18, i64 %indvars.iv29
  store i8 %16, ptr %19, align 1, !tbaa !9
  br label %._crit_edge.us

._crit_edge.us:                                   ; preds = %._crit_edge.us.unr-lcssa, %._crit_edge.us.epilog-lcssa
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, 1
  %exitcond33.not = icmp eq i64 %indvars.iv.next30, %wide.trip.count32
  br i1 %exitcond33.not, label %.preheader20, label %.preheader21.us, !llvm.loop !15

.preheader20:                                     ; preds = %._crit_edge.us
  br i1 %2, label %.preheader.us.preheader, label %._crit_edge26

.preheader.us.preheader:                          ; preds = %.preheader20
  %20 = zext nneg i32 %1 to i64
  %xtraiter41 = and i64 %wide.trip.count32, 3
  %21 = icmp ult i32 %1, 4
  br i1 %21, label %._crit_edge26.loopexit.unr-lcssa, label %.preheader.us.preheader.new

.preheader.us.preheader.new:                      ; preds = %.preheader.us.preheader
  %unroll_iter43 = and i64 %wide.trip.count32, 2147483644
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us, %.preheader.us.preheader.new
  %indvar = phi i64 [ 0, %.preheader.us.preheader.new ], [ %indvar.next.3, %.preheader.us ]
  %niter44 = phi i64 [ 0, %.preheader.us.preheader.new ], [ %niter44.next.3, %.preheader.us ]
  %22 = mul nuw nsw i64 %indvar, 300
  %scevgep = getelementptr i8, ptr @G, i64 %22
  %scevgep34 = getelementptr i8, ptr @GG, i64 %22
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 16 %scevgep, ptr align 16 %scevgep34, i64 %20, i1 false), !tbaa !9
  %23 = mul nuw i64 %indvar, 300
  %24 = add nuw i64 %23, 300
  %scevgep.1 = getelementptr i8, ptr @G, i64 %24
  %scevgep34.1 = getelementptr i8, ptr @GG, i64 %24
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep.1, ptr align 4 %scevgep34.1, i64 %20, i1 false), !tbaa !9
  %25 = mul nuw i64 %indvar, 300
  %26 = add nuw i64 %25, 600
  %scevgep.2 = getelementptr i8, ptr @G, i64 %26
  %scevgep34.2 = getelementptr i8, ptr @GG, i64 %26
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 8 %scevgep.2, ptr align 8 %scevgep34.2, i64 %20, i1 false), !tbaa !9
  %27 = mul nuw i64 %indvar, 300
  %28 = add nuw i64 %27, 900
  %scevgep.3 = getelementptr i8, ptr @G, i64 %28
  %scevgep34.3 = getelementptr i8, ptr @GG, i64 %28
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep.3, ptr align 4 %scevgep34.3, i64 %20, i1 false), !tbaa !9
  %indvar.next.3 = add nuw nsw i64 %indvar, 4
  %niter44.next.3 = add i64 %niter44, 4
  %niter44.ncmp.3 = icmp eq i64 %niter44.next.3, %unroll_iter43
  br i1 %niter44.ncmp.3, label %._crit_edge26.loopexit.unr-lcssa, label %.preheader.us, !llvm.loop !16

._crit_edge26.loopexit.unr-lcssa:                 ; preds = %.preheader.us, %.preheader.us.preheader
  %indvar.unr = phi i64 [ 0, %.preheader.us.preheader ], [ %indvar.next.3, %.preheader.us ]
  %lcmp.mod42.not = icmp eq i64 %xtraiter41, 0
  br i1 %lcmp.mod42.not, label %._crit_edge26, label %.preheader.us.epil

.preheader.us.epil:                               ; preds = %._crit_edge26.loopexit.unr-lcssa, %.preheader.us.epil
  %indvar.epil = phi i64 [ %indvar.next.epil, %.preheader.us.epil ], [ %indvar.unr, %._crit_edge26.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader.us.epil ], [ 0, %._crit_edge26.loopexit.unr-lcssa ]
  %29 = mul nuw nsw i64 %indvar.epil, 300
  %scevgep.epil = getelementptr i8, ptr @G, i64 %29
  %scevgep34.epil = getelementptr i8, ptr @GG, i64 %29
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 4 %scevgep.epil, ptr align 4 %scevgep34.epil, i64 %20, i1 false), !tbaa !9
  %indvar.next.epil = add nuw nsw i64 %indvar.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter41
  br i1 %epil.iter.cmp.not, label %._crit_edge26, label %.preheader.us.epil, !llvm.loop !17

._crit_edge26:                                    ; preds = %._crit_edge26.loopexit.unr-lcssa, %.preheader.us.epil, %0, %.preheader20
  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 nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.unroll.disable"}


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