; LLVM IR from tangjz/acm-icpc :: usaco/Training/Chapter 2 Bigger challenges (money)

; 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
@m = external dso_local global i32, align 4
@w = external dso_local global i32, align 4
@f = external dso_local global [10001 x i64], align 16
@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.6 = 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 i32, align 4
  store i32 0, ptr %1, align 4
  %3 = load ptr, ptr @stdin, align 8, !tbaa !5
  %4 = call ptr @freopen(ptr noundef @.str, ptr noundef @.str.1, ptr noundef %3)
  %5 = load ptr, ptr @stdout, align 8, !tbaa !5
  %6 = call ptr @freopen(ptr noundef @.str.2, ptr noundef @.str.3, ptr noundef %5)
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef @n, ptr noundef @m)
  store i64 1, ptr @f, align 16, !tbaa !9
  br label %8

8:                                                ; preds = %35, %0
  %9 = load i32, ptr @n, align 4, !tbaa !11
  %10 = add nsw i32 %9, -1
  store i32 %10, ptr @n, align 4, !tbaa !11
  %11 = icmp ne i32 %9, 0
  br i1 %11, label %12, label %36

12:                                               ; preds = %8
  %13 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.5, ptr noundef @w)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %14 = load i32, ptr @w, align 4, !tbaa !11
  store i32 %14, ptr %2, align 4, !tbaa !11
  br label %15

15:                                               ; preds = %32, %12
  %16 = load i32, ptr %2, align 4, !tbaa !11
  %17 = load i32, ptr @m, align 4, !tbaa !11
  %18 = icmp sle i32 %16, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %35

20:                                               ; preds = %15
  %21 = load i32, ptr %2, align 4, !tbaa !11
  %22 = load i32, ptr @w, align 4, !tbaa !11
  %23 = sub nsw i32 %21, %22
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = load i32, ptr %2, align 4, !tbaa !11
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !9
  %31 = add nsw i64 %30, %26
  store i64 %31, ptr %29, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %20
  %33 = load i32, ptr %2, align 4, !tbaa !11
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %2, align 4, !tbaa !11
  br label %15, !llvm.loop !13

35:                                               ; preds = %19
  br label %8, !llvm.loop !15

36:                                               ; preds = %8
  %37 = load i32, ptr @m, align 4, !tbaa !11
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %38
  %40 = load i64, ptr %39, align 8, !tbaa !9
  %41 = call i32 (ptr, ...) @printf(ptr noundef @.str.6, i64 noundef %40)
  ret i32 0
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) #1

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

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

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

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

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 = { nocallback nofree nosync 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !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 global i32, align 4
@m = external dso_local global i32, align 4
@w = external dso_local global i32, align 4
@f = external dso_local local_unnamed_addr global [10001 x i64], align 16
@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.6 = 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 = load ptr, ptr @stdin, align 8, !tbaa !5
  %2 = tail call ptr @freopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, ptr noundef %1)
  %3 = load ptr, ptr @stdout, align 8, !tbaa !5
  %4 = tail call ptr @freopen(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.3, ptr noundef %3)
  %5 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull @n, ptr noundef nonnull @m)
  store i64 1, ptr @f, align 16, !tbaa !9
  %6 = load i32, ptr @n, align 4, !tbaa !11
  %7 = add nsw i32 %6, -1
  store i32 %7, ptr @n, align 4, !tbaa !11
  %.not7 = icmp eq i32 %6, 0
  br i1 %.not7, label %.._crit_edge_crit_edge, label %.lr.ph8

.._crit_edge_crit_edge:                           ; preds = %0
  %.pre = load i32, ptr @m, align 4, !tbaa !11
  br label %._crit_edge

.loopexit:                                        ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %.lr.ph8
  %8 = load i32, ptr @n, align 4, !tbaa !11
  %9 = add nsw i32 %8, -1
  store i32 %9, ptr @n, align 4, !tbaa !11
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph8, !llvm.loop !13

.lr.ph8:                                          ; preds = %0, %.loopexit
  %10 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.5, ptr noundef nonnull @w)
  %11 = load i32, ptr @w, align 4, !tbaa !11
  %12 = load i32, ptr @m, align 4, !tbaa !11
  %.not45 = icmp sgt i32 %11, %12
  br i1 %.not45, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.lr.ph8
  %13 = sext i32 %11 to i64
  %14 = add i32 %12, 1
  %15 = sub i32 %12, %11
  %16 = zext i32 %15 to i64
  %17 = add nuw nsw i64 %16, 1
  %min.iters.check = icmp ult i32 %15, 5
  br i1 %min.iters.check, label %.lr.ph.preheader15, label %vector.memcheck

vector.memcheck:                                  ; preds = %.lr.ph.preheader
  %18 = shl nsw i64 %13, 3
  %scevgep = getelementptr i8, ptr @f, i64 %18
  %19 = sub i32 %12, %11
  %20 = zext i32 %19 to i64
  %21 = shl nuw nsw i64 %20, 3
  %22 = getelementptr i8, ptr getelementptr inbounds (i8, ptr @f, i64 8), i64 %18
  %scevgep10 = getelementptr i8, ptr %22, i64 %21
  %scevgep11 = getelementptr i8, ptr getelementptr inbounds (i8, ptr @f, i64 8), i64 %21
  %bound0 = icmp ult ptr %scevgep, %scevgep11
  %bound1 = icmp ugt ptr %scevgep10, @f
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph.preheader15, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %17, 8589934588
  %ind.end = add nsw i64 %n.vec, %13
  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, %13
  %23 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %index
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %wide.load = load <2 x i64>, ptr %23, align 16, !tbaa !9, !alias.scope !15
  %wide.load12 = load <2 x i64>, ptr %24, align 16, !tbaa !9, !alias.scope !15
  %25 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %offset.idx
  %26 = getelementptr inbounds i8, ptr %25, i64 16
  %wide.load13 = load <2 x i64>, ptr %25, align 8, !tbaa !9, !alias.scope !18, !noalias !15
  %wide.load14 = load <2 x i64>, ptr %26, align 8, !tbaa !9, !alias.scope !18, !noalias !15
  %27 = add nsw <2 x i64> %wide.load13, %wide.load
  %28 = add nsw <2 x i64> %wide.load14, %wide.load12
  store <2 x i64> %27, ptr %25, align 8, !tbaa !9, !alias.scope !18, !noalias !15
  store <2 x i64> %28, ptr %26, align 8, !tbaa !9, !alias.scope !18, !noalias !15
  %index.next = add nuw i64 %index, 4
  %29 = icmp eq i64 %index.next, %n.vec
  br i1 %29, label %middle.block, label %vector.body, !llvm.loop !20

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %17, %n.vec
  br i1 %cmp.n, label %.loopexit, label %.lr.ph.preheader15

.lr.ph.preheader15:                               ; preds = %middle.block, %vector.memcheck, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ %13, %vector.memcheck ], [ %13, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %30 = trunc i64 %indvars.iv.ph to i32
  %31 = add i32 %12, %30
  %32 = and i32 %31, 1
  %lcmp.mod.not.not = icmp eq i32 %32, 0
  br i1 %lcmp.mod.not.not, label %.lr.ph.prol, label %.lr.ph.prol.loopexit

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader15
  %33 = sub nsw i64 %indvars.iv.ph, %13
  %34 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %indvars.iv.ph
  %37 = load i64, ptr %36, align 8, !tbaa !9
  %38 = add nsw i64 %37, %35
  store i64 %38, ptr %36, align 8, !tbaa !9
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.ph, 1
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader15
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %.lr.ph.preheader15 ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %39 = icmp eq i32 %12, %30
  br i1 %39, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %.lr.ph ], [ %indvars.iv.unr, %.lr.ph.prol.loopexit ]
  %40 = sub nsw i64 %indvars.iv, %13
  %41 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !9
  %43 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %indvars.iv
  %44 = load i64, ptr %43, align 8, !tbaa !9
  %45 = add nsw i64 %44, %42
  store i64 %45, ptr %43, align 8, !tbaa !9
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %46 = sub nsw i64 %indvars.iv.next, %13
  %47 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %46
  %48 = load i64, ptr %47, align 8, !tbaa !9
  %49 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %indvars.iv.next
  %50 = load i64, ptr %49, align 8, !tbaa !9
  %51 = add nsw i64 %50, %48
  store i64 %51, ptr %49, align 8, !tbaa !9
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, 2
  %lftr.wideiv.1 = trunc i64 %indvars.iv.next.1 to i32
  %exitcond.not.1 = icmp eq i32 %14, %lftr.wideiv.1
  br i1 %exitcond.not.1, label %.loopexit, label %.lr.ph, !llvm.loop !23

._crit_edge:                                      ; preds = %.loopexit, %.._crit_edge_crit_edge
  %52 = phi i32 [ %.pre, %.._crit_edge_crit_edge ], [ %12, %.loopexit ]
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [10001 x i64], ptr @f, i64 0, i64 %53
  %55 = load i64, ptr %54, align 8, !tbaa !9
  %56 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6, i64 noundef %55)
  ret i32 0
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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

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

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 = { 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" }

!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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16}
!16 = distinct !{!16, !17}
!17 = distinct !{!17, !"LVerDomain"}
!18 = !{!19}
!19 = distinct !{!19, !17}
!20 = distinct !{!20, !14, !21, !22}
!21 = !{!"llvm.loop.isvectorized", i32 1}
!22 = !{!"llvm.loop.unroll.runtime.disable"}
!23 = distinct !{!23, !14, !21}


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