; LLVM IR from upupming/algorithm :: kick-start/2020/2020A-B.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"

@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@p = external dso_local global i32, align 4
@v = external dso_local global [55 x [35 x i32]], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z3dfsii(i32 noundef, i32 noundef) #0

; 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 noundef i32 @_Z5solvev() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = 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 %4

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

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

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

10:                                               ; preds = %42, %9
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = load i32, ptr @k, align 4, !tbaa !5
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %15, label %14

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

15:                                               ; preds = %10
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %41

18:                                               ; preds = %15
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [55 x [35 x i32]], ptr @v, i64 0, i64 %20
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = sub nsw i32 %22, 1
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [35 x i32], ptr %21, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = load i32, ptr %1, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [55 x [35 x i32]], ptr @v, i64 0, i64 %28
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [35 x i32], ptr %29, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %26, %33
  %35 = load i32, ptr %1, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [55 x [35 x i32]], ptr @v, i64 0, i64 %36
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [35 x i32], ptr %37, i64 0, i64 %39
  store i32 %34, ptr %40, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %18, %15
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %3, align 4, !tbaa !5
  br label %10, !llvm.loop !9

45:                                               ; preds = %14
  br label %46

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

49:                                               ; preds = %8
  %50 = load i32, ptr @p, align 4, !tbaa !5
  %51 = call noundef i32 @_Z3dfsii(i32 noundef 0, i32 noundef %50)
  ret i32 %51
}

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


; 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
@k = external dso_local local_unnamed_addr global i32, align 4
@p = external dso_local local_unnamed_addr global i32, align 4
@v = external dso_local local_unnamed_addr global [55 x [35 x i32]], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z3dfsii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5solvev() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.preheader.lr.ph, label %._crit_edge14

.preheader.lr.ph:                                 ; preds = %0
  %3 = load i32, ptr @k, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.preheader.us.preheader, label %._crit_edge14

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %wide.trip.count21 = zext nneg i32 %1 to i64
  %wide.trip.count = zext nneg i32 %3 to i64
  %exitcond.peel.not = icmp eq i32 %3, 1
  %5 = add nsw i64 %wide.trip.count, -1
  %6 = add nsw i64 %wide.trip.count, -2
  %xtraiter = and i64 %5, 3
  %7 = icmp ult i64 %6, 3
  %unroll_iter = and i64 %5, -4
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %8

8:                                                ; preds = %._crit_edge.us, %.preheader.us.preheader
  %indvars.iv18 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next19, %._crit_edge.us ]
  br i1 %exitcond.peel.not, label %._crit_edge.us, label %.peel.next.preheader

.peel.next.preheader:                             ; preds = %8
  %9 = mul nuw nsw i64 %indvars.iv18, 140
  %scevgep = getelementptr i8, ptr @v, i64 %9
  %load_initial = load i32, ptr %scevgep, align 4
  br i1 %7, label %._crit_edge.us.loopexit.unr-lcssa, label %.peel.next

.peel.next:                                       ; preds = %.peel.next.preheader, %.peel.next
  %store_forwarded = phi i32 [ %21, %.peel.next ], [ %load_initial, %.peel.next.preheader ]
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.peel.next ], [ 1, %.peel.next.preheader ]
  %niter = phi i64 [ %niter.next.3, %.peel.next ], [ 0, %.peel.next.preheader ]
  %10 = getelementptr inbounds [55 x [35 x i32]], ptr @v, i64 0, i64 %indvars.iv18, i64 %indvars.iv
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = add nsw i32 %11, %store_forwarded
  store i32 %12, ptr %10, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %13 = getelementptr inbounds [55 x [35 x i32]], ptr @v, i64 0, i64 %indvars.iv18, i64 %indvars.iv.next
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = add nsw i32 %14, %12
  store i32 %15, ptr %13, align 4, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %16 = getelementptr inbounds [55 x [35 x i32]], ptr @v, i64 0, i64 %indvars.iv18, i64 %indvars.iv.next.1
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = add nsw i32 %17, %15
  store i32 %18, ptr %16, align 4, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %19 = getelementptr inbounds [55 x [35 x i32]], ptr @v, i64 0, i64 %indvars.iv18, i64 %indvars.iv.next.2
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = add nsw i32 %20, %18
  store i32 %21, ptr %19, align 4, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.us.loopexit.unr-lcssa, label %.peel.next, !llvm.loop !9

._crit_edge.us.loopexit.unr-lcssa:                ; preds = %.peel.next, %.peel.next.preheader
  %store_forwarded.unr = phi i32 [ %load_initial, %.peel.next.preheader ], [ %21, %.peel.next ]
  %indvars.iv.unr = phi i64 [ 1, %.peel.next.preheader ], [ %indvars.iv.next.3, %.peel.next ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.peel.next.epil

.peel.next.epil:                                  ; preds = %._crit_edge.us.loopexit.unr-lcssa, %.peel.next.epil
  %store_forwarded.epil = phi i32 [ %24, %.peel.next.epil ], [ %store_forwarded.unr, %._crit_edge.us.loopexit.unr-lcssa ]
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.peel.next.epil ], [ %indvars.iv.unr, %._crit_edge.us.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.peel.next.epil ], [ 0, %._crit_edge.us.loopexit.unr-lcssa ]
  %22 = getelementptr inbounds [55 x [35 x i32]], ptr @v, i64 0, i64 %indvars.iv18, i64 %indvars.iv.epil
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = add nsw i32 %23, %store_forwarded.epil
  store i32 %24, ptr %22, align 4, !tbaa !5
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge.us, label %.peel.next.epil, !llvm.loop !12

._crit_edge.us:                                   ; preds = %._crit_edge.us.loopexit.unr-lcssa, %.peel.next.epil, %8
  %indvars.iv.next19 = add nuw nsw i64 %indvars.iv18, 1
  %exitcond22.not = icmp eq i64 %indvars.iv.next19, %wide.trip.count21
  br i1 %exitcond22.not, label %._crit_edge14, label %8, !llvm.loop !14

._crit_edge14:                                    ; preds = %._crit_edge.us, %.preheader.lr.ph, %0
  %25 = load i32, ptr @p, align 4, !tbaa !5
  %26 = tail call noundef i32 @_Z3dfsii(i32 noundef 0, i32 noundef %25)
  ret i32 %26
}

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

!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}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.peeled.count", i32 1}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}
!14 = distinct !{!14, !10}


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