; LLVM IR from upupming/algorithm :: kick-start/2020/2020D-D-naive.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
@d = external dso_local global [100010 x i32], align 16
@s = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@startIdx = external dso_local global i32, align 4
@endIdx = external dso_local global i32, align 4
@curIdx = external dso_local global i32, align 4

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

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

10:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %56

11:                                               ; preds = %5
  %12 = load i32, ptr @startIdx, align 4, !tbaa !5
  %13 = sub nsw i32 %12, 1
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %15, label %41

15:                                               ; preds = %11
  %16 = load i32, ptr @endIdx, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  %18 = load i32, ptr @n, align 4, !tbaa !5
  %19 = icmp sle i32 %17, %18
  br i1 %19, label %20, label %41

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  %21 = load i32, ptr @startIdx, align 4, !tbaa !5
  %22 = sub nsw i32 %21, 2
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [100010 x i32], ptr @d, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  store i32 %25, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %26 = load i32, ptr @endIdx, align 4, !tbaa !5
  %27 = sub nsw i32 %26, 1
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [100010 x i32], ptr @d, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  store i32 %30, ptr %3, align 4, !tbaa !5
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = icmp slt i32 %31, %32
  br i1 %33, label %34, label %37

34:                                               ; preds = %20
  %35 = load i32, ptr @startIdx, align 4, !tbaa !5
  %36 = add nsw i32 %35, -1
  store i32 %36, ptr @startIdx, align 4, !tbaa !5
  store i32 %36, ptr @curIdx, align 4, !tbaa !5
  br label %40

37:                                               ; preds = %20
  %38 = load i32, ptr @endIdx, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr @endIdx, align 4, !tbaa !5
  store i32 %39, ptr @curIdx, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %37, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  br label %52

41:                                               ; preds = %15, %11
  %42 = load i32, ptr @startIdx, align 4, !tbaa !5
  %43 = sub nsw i32 %42, 1
  %44 = icmp sle i32 %43, 0
  br i1 %44, label %45, label %48

45:                                               ; preds = %41
  %46 = load i32, ptr @endIdx, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr @endIdx, align 4, !tbaa !5
  store i32 %47, ptr @curIdx, align 4, !tbaa !5
  br label %51

48:                                               ; preds = %41
  %49 = load i32, ptr @startIdx, align 4, !tbaa !5
  %50 = add nsw i32 %49, -1
  store i32 %50, ptr @startIdx, align 4, !tbaa !5
  store i32 %50, ptr @curIdx, align 4, !tbaa !5
  br label %51

51:                                               ; preds = %48, %45
  br label %52

52:                                               ; preds = %51, %40
  br label %53

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

56:                                               ; preds = %10
  %57 = load i32, ptr @curIdx, align 4, !tbaa !5
  ret i32 %57
}

; 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

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


; 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
@d = external dso_local local_unnamed_addr global [100010 x i32], align 16
@s = external dso_local local_unnamed_addr global i32, align 4
@k = external dso_local local_unnamed_addr global i32, align 4
@startIdx = external dso_local local_unnamed_addr global i32, align 4
@endIdx = external dso_local local_unnamed_addr global i32, align 4
@curIdx = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5solvev() local_unnamed_addr #0 {
  %1 = load i32, ptr @s, align 4, !tbaa !5
  store i32 %1, ptr @endIdx, align 4, !tbaa !5
  store i32 %1, ptr @startIdx, align 4, !tbaa !5
  store i32 %1, ptr @curIdx, align 4, !tbaa !5
  %2 = load i32, ptr @k, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 1
  br i1 %3, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0
  %4 = load i32, ptr @n, align 4
  %5 = add nsw i32 %2, -2
  br label %7

._crit_edge.loopexit:                             ; preds = %29
  store i32 %storemerge.sink, ptr @curIdx, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %0
  %6 = phi i32 [ %1, %0 ], [ %storemerge.sink, %._crit_edge.loopexit ]
  ret i32 %6

7:                                                ; preds = %.lr.ph, %29
  %.05 = phi i32 [ 0, %.lr.ph ], [ %32, %29 ]
  %8 = phi i32 [ %1, %.lr.ph ], [ %31, %29 ]
  %9 = phi i32 [ %1, %.lr.ph ], [ %30, %29 ]
  %10 = icmp sgt i32 %8, 1
  %11 = add nsw i32 %9, 1
  br i1 %10, label %12, label %26

12:                                               ; preds = %7
  %.not.not = icmp slt i32 %9, %4
  br i1 %.not.not, label %13, label %27

13:                                               ; preds = %12
  %14 = add nsw i32 %8, -2
  %15 = zext nneg i32 %14 to i64
  %16 = getelementptr inbounds [100010 x i32], ptr @d, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = add nsw i32 %9, -1
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [100010 x i32], ptr @d, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = icmp slt i32 %17, %21
  br i1 %22, label %23, label %25

23:                                               ; preds = %13
  %24 = add nsw i32 %8, -1
  store i32 %24, ptr @startIdx, align 4, !tbaa !5
  br label %29

25:                                               ; preds = %13
  store i32 %11, ptr @endIdx, align 4, !tbaa !5
  br label %29

26:                                               ; preds = %7
  store i32 %11, ptr @endIdx, align 4, !tbaa !5
  br label %29

27:                                               ; preds = %12
  %28 = add nsw i32 %8, -1
  store i32 %28, ptr @startIdx, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %23, %25, %27, %26
  %storemerge.sink = phi i32 [ %28, %27 ], [ %11, %26 ], [ %11, %25 ], [ %24, %23 ]
  %30 = phi i32 [ %9, %27 ], [ %11, %26 ], [ %11, %25 ], [ %9, %23 ]
  %31 = phi i32 [ %28, %27 ], [ %8, %26 ], [ %8, %25 ], [ %24, %23 ]
  %32 = add nuw nsw i32 %.05, 1
  %exitcond.not = icmp eq i32 %.05, %5
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %7, !llvm.loop !9
}

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

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


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