; LLVM IR from upupming/algorithm :: kick-start/2020/RoundG/B-Maximum_Coins.ll (Z5solvev)

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

@c = external dso_local global [1010 x [1010 x i32]], align 16
@n = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z5solvev() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  store i64 0, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %7 = load i32, ptr @n, align 4, !tbaa !9
  %8 = sub nsw i32 %7, 1
  %9 = sub nsw i32 0, %8
  store i32 %9, ptr %2, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %53, %0
  %11 = load i32, ptr %2, align 4, !tbaa !9
  %12 = load i32, ptr @n, align 4, !tbaa !9
  %13 = sub nsw i32 %12, 1
  %14 = icmp sle i32 %11, %13
  br i1 %14, label %16, label %15

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

16:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 0, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %47, %16
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = load i32, ptr @n, align 4, !tbaa !9
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %22, label %21

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

22:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = load i32, ptr %2, align 4, !tbaa !9
  %25 = add nsw i32 %23, %24
  store i32 %25, ptr %6, align 4, !tbaa !9
  %26 = load i32, ptr %6, align 4, !tbaa !9
  %27 = icmp slt i32 %26, 0
  br i1 %27, label %32, label %28

28:                                               ; preds = %22
  %29 = load i32, ptr %6, align 4, !tbaa !9
  %30 = load i32, ptr @n, align 4, !tbaa !9
  %31 = icmp sge i32 %29, %30
  br i1 %31, label %32, label %33

32:                                               ; preds = %28, %22
  store i32 7, ptr %3, align 4
  br label %44

33:                                               ; preds = %28
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [1010 x [1010 x i32]], ptr @c, i64 0, i64 %35
  %37 = load i32, ptr %6, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [1010 x i32], ptr %36, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = load i64, ptr %4, align 8, !tbaa !5
  %43 = add nsw i64 %42, %41
  store i64 %43, ptr %4, align 8, !tbaa !5
  store i32 0, ptr %3, align 4
  br label %44

44:                                               ; preds = %33, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %45 = load i32, ptr %3, align 4
  switch i32 %45, label %58 [
    i32 0, label %46
    i32 7, label %47
  ]

46:                                               ; preds = %44
  br label %47

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

50:                                               ; preds = %21
  %51 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %4, ptr noundef nonnull align 8 dereferenceable(8) %1)
  %52 = load i64, ptr %51, align 8, !tbaa !5
  store i64 %52, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %53

53:                                               ; preds = %50
  %54 = load i32, ptr %2, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %2, align 4, !tbaa !9
  br label %10, !llvm.loop !13

56:                                               ; preds = %15
  %57 = load i64, ptr %1, align 8, !tbaa !5
  store i32 1, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret i64 %57

58:                                               ; preds = %44
  unreachable
}

; 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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #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 = { inlinehint 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 #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 = !{!"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 = distinct !{!13, !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"

@c = external dso_local local_unnamed_addr global [1010 x [1010 x i32]], align 16
@n = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z5solvev() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #3
  store i64 0, ptr %1, align 8, !tbaa !5
  %3 = load i32, ptr @n, align 4, !tbaa !9
  %4 = sub nsw i32 1, %3
  %.not.not14 = icmp slt i32 %4, %3
  br i1 %.not.not14, label %.lr.ph17.preheader, label %._crit_edge18

.lr.ph17.preheader:                               ; preds = %0
  %5 = sub nsw i32 0, %3
  %6 = sext i32 %5 to i64
  %7 = add nsw i64 %6, 1
  br label %.lr.ph17

._crit_edge18:                                    ; preds = %._crit_edge, %0
  %8 = phi i64 [ 0, %0 ], [ %20, %._crit_edge ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #3
  ret i64 %8

.lr.ph17:                                         ; preds = %.lr.ph17.preheader, %._crit_edge
  %indvars.iv20 = phi i64 [ %7, %.lr.ph17.preheader ], [ %indvars.iv.next21, %._crit_edge ]
  %9 = phi i32 [ %3, %.lr.ph17.preheader ], [ %21, %._crit_edge ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  store i64 0, ptr %2, align 8, !tbaa !5
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.lr.ph17
  %wide.trip.count = zext nneg i32 %9 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %11 = icmp eq i32 %9, 1
  br i1 %11, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %39, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %39 ]
  %.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %40, %39 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %12 = add nsw i64 %indvars.iv.unr, %indvars.iv20
  %13 = trunc nsw i64 %12 to i32
  %or.cond.epil = icmp ugt i32 %9, %13
  br i1 %or.cond.epil, label %14, label %._crit_edge

14:                                               ; preds = %.lr.ph.epil
  %15 = getelementptr inbounds [1010 x [1010 x i32]], ptr @c, i64 0, i64 %indvars.iv.unr, i64 %12
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = add nsw i64 %.unr, %17
  store i64 %18, ptr %2, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %14, %.lr.ph.epil, %.lr.ph17
  %19 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %1)
  %20 = load i64, ptr %19, align 8, !tbaa !5
  store i64 %20, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  %indvars.iv.next21 = add nsw i64 %indvars.iv20, 1
  %21 = load i32, ptr @n, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %.not.not = icmp slt i64 %indvars.iv.next21, %22
  br i1 %.not.not, label %.lr.ph17, label %._crit_edge18, !llvm.loop !11

.lr.ph:                                           ; preds = %39, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %39 ]
  %23 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %40, %39 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %39 ]
  %24 = add nsw i64 %indvars.iv, %indvars.iv20
  %25 = trunc nsw i64 %24 to i32
  %or.cond = icmp ugt i32 %9, %25
  br i1 %or.cond, label %26, label %.lr.ph.1

26:                                               ; preds = %.lr.ph
  %27 = getelementptr inbounds [1010 x [1010 x i32]], ptr @c, i64 0, i64 %indvars.iv, i64 %24
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = add nsw i64 %23, %29
  store i64 %30, ptr %2, align 8, !tbaa !5
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %26
  %31 = phi i64 [ %23, %.lr.ph ], [ %30, %26 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %32 = add nsw i64 %indvars.iv.next, %indvars.iv20
  %33 = trunc nsw i64 %32 to i32
  %or.cond.1 = icmp ugt i32 %9, %33
  br i1 %or.cond.1, label %34, label %39

34:                                               ; preds = %.lr.ph.1
  %35 = getelementptr inbounds [1010 x [1010 x i32]], ptr @c, i64 0, i64 %indvars.iv.next, i64 %32
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = add nsw i64 %31, %37
  store i64 %38, ptr %2, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %34, %.lr.ph.1
  %40 = phi i64 [ %31, %.lr.ph.1 ], [ %38, %34 ]
  %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.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !13
}

; 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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #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 = { inlinehint 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 #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 = !{!"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 = distinct !{!13, !12}


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