; LLVM IR from Aryansh-S/USACO :: USACO/2022/January/Bronze/air_cownditioning_ii.ll (Z5worksv)

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

@m = external dso_local global i32, align 4
@stall = external dso_local global [100 x i32], align 16
@ac = external dso_local global [10 x [4 x i32]], align 16
@b = external dso_local global [10 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z5worksv() #0 {
  %1 = alloca i1, align 1
  %2 = alloca [100 x i32], align 16
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 400, ptr %2) #3
  call void @llvm.memset.p0.i64(ptr align 16 %2, i8 0, i64 400, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %47, %0
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = load i32, ptr @m, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [10 x [4 x i32]], ptr @ac, i64 0, i64 %14
  %16 = getelementptr inbounds [4 x i32], ptr %15, i64 0, i64 0
  %17 = load i32, ptr %16, align 16, !tbaa !5
  store i32 %17, ptr %5, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %43, %12
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [10 x [4 x i32]], ptr @ac, i64 0, i64 %21
  %23 = getelementptr inbounds [4 x i32], ptr %22, i64 0, i64 1
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = icmp sle i32 %19, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %18
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %46

27:                                               ; preds = %18
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [10 x i32], ptr @b, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [10 x [4 x i32]], ptr @ac, i64 0, i64 %33
  %35 = getelementptr inbounds [4 x i32], ptr %34, i64 0, i64 2
  %36 = load i32, ptr %35, align 8, !tbaa !5
  %37 = mul nsw i32 %31, %36
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [100 x i32], ptr %2, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = add nsw i32 %41, %37
  store i32 %42, ptr %40, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %27
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %5, align 4, !tbaa !5
  br label %18, !llvm.loop !9

46:                                               ; preds = %26
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %3, align 4, !tbaa !5
  br label %7, !llvm.loop !11

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

51:                                               ; preds = %67, %50
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = icmp slt i32 %52, 100
  br i1 %53, label %55, label %54

54:                                               ; preds = %51
  store i32 8, ptr %4, align 4
  br label %70

55:                                               ; preds = %51
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [100 x i32], ptr %2, i64 0, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !5
  %60 = load i32, ptr %6, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [100 x i32], ptr @stall, i64 0, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !5
  %64 = icmp slt i32 %59, %63
  br i1 %64, label %65, label %66

65:                                               ; preds = %55
  store i1 false, ptr %1, align 1
  store i32 1, ptr %4, align 4
  br label %70

66:                                               ; preds = %55
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %6, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %6, align 4, !tbaa !5
  br label %51, !llvm.loop !12

70:                                               ; preds = %65, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %71 = load i32, ptr %4, align 4
  switch i32 %71, label %73 [
    i32 8, label %72
  ]

72:                                               ; preds = %70
  store i1 true, ptr %1, align 1
  store i32 1, ptr %4, align 4
  br label %73

73:                                               ; preds = %72, %70
  call void @llvm.lifetime.end.p0(i64 400, ptr %2) #3
  %74 = load i1, ptr %1, align 1
  ret i1 %74
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = distinct !{!11, !10}
!12 = distinct !{!12, !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"

@m = external dso_local local_unnamed_addr global i32, align 4
@stall = external dso_local local_unnamed_addr global [100 x i32], align 16
@ac = external dso_local local_unnamed_addr global [10 x [4 x i32]], align 16
@b = external dso_local local_unnamed_addr global [10 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z5worksv() local_unnamed_addr #0 {
  %1 = alloca [100 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 400, ptr nonnull %1) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(400) %1, i8 0, i64 400, i1 false)
  %2 = load i32, ptr @m, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph22.preheader, label %.preheader

.lr.ph22.preheader:                               ; preds = %0
  %wide.trip.count = zext nneg i32 %2 to i64
  br label %.lr.ph22

.preheader:                                       ; preds = %._crit_edge, %0
  %4 = load i32, ptr %1, align 16, !tbaa !5
  %5 = load i32, ptr @stall, align 16, !tbaa !5
  %6 = icmp slt i32 %4, %5
  br i1 %6, label %.critedge, label %.lr.ph33

.lr.ph22:                                         ; preds = %.lr.ph22.preheader, %._crit_edge
  %indvars.iv25 = phi i64 [ 0, %.lr.ph22.preheader ], [ %indvars.iv.next26, %._crit_edge ]
  %7 = getelementptr inbounds [10 x [4 x i32]], ptr @ac, i64 0, i64 %indvars.iv25
  %8 = load i32, ptr %7, align 16, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %7, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.not18 = icmp sgt i32 %8, %10
  br i1 %.not18, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph22
  %11 = getelementptr inbounds [10 x i32], ptr @b, i64 0, i64 %indvars.iv25
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %7, i64 8
  %14 = load i32, ptr %13, align 8, !tbaa !5
  %15 = mul nsw i32 %14, %12
  %16 = sext i32 %8 to i64
  %17 = add i32 %10, 1
  %18 = sub i32 %10, %8
  %19 = zext i32 %18 to i64
  %20 = add nuw nsw i64 %19, 1
  %min.iters.check = icmp ult i32 %18, 7
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv.ph = phi i64 [ %16, %.lr.ph ], [ %ind.end, %middle.block ]
  br label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %20, 8589934584
  %ind.end = add nsw i64 %n.vec, %16
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %15, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = add i64 %index, %16
  %21 = getelementptr inbounds [100 x i32], ptr %1, i64 0, i64 %offset.idx
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  %wide.load = load <4 x i32>, ptr %21, align 4, !tbaa !5
  %wide.load36 = load <4 x i32>, ptr %22, align 4, !tbaa !5
  %23 = add nsw <4 x i32> %wide.load, %broadcast.splat
  %24 = add nsw <4 x i32> %wide.load36, %broadcast.splat
  store <4 x i32> %23, ptr %21, align 4, !tbaa !5
  store <4 x i32> %24, ptr %22, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %25 = icmp eq i64 %index.next, %n.vec
  br i1 %25, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %20, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %.lr.ph22
  %indvars.iv.next26 = add nuw nsw i64 %indvars.iv25, 1
  %exitcond28.not = icmp eq i64 %indvars.iv.next26, %wide.trip.count
  br i1 %exitcond28.not, label %.preheader, label %.lr.ph22, !llvm.loop !13

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %26 = getelementptr inbounds [100 x i32], ptr %1, i64 0, i64 %indvars.iv
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = add nsw i32 %27, %15
  store i32 %28, ptr %26, align 4, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %17, %lftr.wideiv
  br i1 %exitcond.not, label %._crit_edge, label %scalar.ph, !llvm.loop !14

.lr.ph33:                                         ; preds = %.preheader, %44
  %indvars.iv2932 = phi i64 [ %indvars.iv.next30.3, %44 ], [ 0, %.preheader ]
  %indvars.iv.next30 = or disjoint i64 %indvars.iv2932, 1
  %29 = getelementptr inbounds [100 x i32], ptr %1, i64 0, i64 %indvars.iv.next30
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = getelementptr inbounds [100 x i32], ptr @stall, i64 0, i64 %indvars.iv.next30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp slt i32 %30, %32
  br i1 %33, label %.critedge, label %.lr.ph33.1, !llvm.loop !15

.lr.ph33.1:                                       ; preds = %.lr.ph33
  %indvars.iv.next30.1 = or disjoint i64 %indvars.iv2932, 2
  %34 = getelementptr inbounds [100 x i32], ptr %1, i64 0, i64 %indvars.iv.next30.1
  %35 = load i32, ptr %34, align 8, !tbaa !5
  %36 = getelementptr inbounds [100 x i32], ptr @stall, i64 0, i64 %indvars.iv.next30.1
  %37 = load i32, ptr %36, align 8, !tbaa !5
  %38 = icmp slt i32 %35, %37
  br i1 %38, label %.critedge, label %.lr.ph33.2, !llvm.loop !15

.lr.ph33.2:                                       ; preds = %.lr.ph33.1
  %indvars.iv.next30.2 = or disjoint i64 %indvars.iv2932, 3
  %39 = getelementptr inbounds [100 x i32], ptr %1, i64 0, i64 %indvars.iv.next30.2
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = getelementptr inbounds [100 x i32], ptr @stall, i64 0, i64 %indvars.iv.next30.2
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = icmp slt i32 %40, %42
  br i1 %43, label %.critedge, label %.lr.ph33.3, !llvm.loop !15

.lr.ph33.3:                                       ; preds = %.lr.ph33.2
  %indvars.iv.next30.3 = add nuw nsw i64 %indvars.iv2932, 4
  %exitcond31.3 = icmp eq i64 %indvars.iv.next30.3, 100
  br i1 %exitcond31.3, label %.critedge.loopexit.split.loop.exit38, label %44, !llvm.loop !15

44:                                               ; preds = %.lr.ph33.3
  %45 = getelementptr inbounds [100 x i32], ptr %1, i64 0, i64 %indvars.iv.next30.3
  %46 = load i32, ptr %45, align 16, !tbaa !5
  %47 = getelementptr inbounds [100 x i32], ptr @stall, i64 0, i64 %indvars.iv.next30.3
  %48 = load i32, ptr %47, align 16, !tbaa !5
  %49 = icmp slt i32 %46, %48
  br i1 %49, label %.critedge.loopexit.split.loop.exit, label %.lr.ph33, !llvm.loop !15

.critedge.loopexit.split.loop.exit:               ; preds = %44
  %50 = icmp ugt i64 %indvars.iv.next30.2, 98
  br label %.critedge

.critedge.loopexit.split.loop.exit38:             ; preds = %.lr.ph33.3
  %51 = icmp ugt i64 %indvars.iv.next30.2, 98
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit.split.loop.exit, %.critedge.loopexit.split.loop.exit38, %.lr.ph33.2, %.lr.ph33.1, %.lr.ph33, %.preheader
  %.lcssa = phi i1 [ false, %.preheader ], [ %50, %.critedge.loopexit.split.loop.exit ], [ %51, %.critedge.loopexit.split.loop.exit38 ], [ false, %.lr.ph33.2 ], [ false, %.lr.ph33.1 ], [ false, %.lr.ph33 ]
  call void @llvm.lifetime.end.p0(i64 400, ptr nonnull %1) #3
  ret i1 %.lcssa
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10, !12, !11}
!15 = distinct !{!15, !10}


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