; LLVM IR from upupming/algorithm :: kick-start/2019/2019G-B.ll (Z9solve_oldv)

; 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 i64, align 8
@m = external dso_local global i64, align 8
@a = external dso_local global [1005 x i64], align 16

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z9solve_oldv() #1 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  store i64 10000, ptr %2, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %39, %0
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %8 = icmp sge i64 %7, 0
  br i1 %8, label %9, label %40

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

10:                                               ; preds = %25, %9
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = load i64, ptr @n, align 8, !tbaa !5
  %14 = icmp slt i64 %12, %13
  br i1 %14, label %16, label %15

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

16:                                               ; preds = %10
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [1005 x i64], ptr @a, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = load i64, ptr %2, align 8, !tbaa !5
  %22 = xor i64 %20, %21
  %23 = load i64, ptr %3, align 8, !tbaa !5
  %24 = add nsw i64 %23, %22
  store i64 %24, ptr %3, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %16
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %4, align 4, !tbaa !9
  br label %10, !llvm.loop !11

28:                                               ; preds = %15
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = load i64, ptr @m, align 8, !tbaa !5
  %31 = icmp sle i64 %29, %30
  br i1 %31, label %32, label %34

32:                                               ; preds = %28
  %33 = load i64, ptr %2, align 8, !tbaa !5
  store i64 %33, ptr %1, align 8
  store i32 1, ptr %5, align 4
  br label %37

34:                                               ; preds = %28
  %35 = load i64, ptr %2, align 8, !tbaa !5
  %36 = add nsw i64 %35, -1
  store i64 %36, ptr %2, align 8, !tbaa !5
  store i32 0, ptr %5, align 4
  br label %37

37:                                               ; preds = %34, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  %38 = load i32, ptr %5, align 4
  switch i32 %38, label %41 [
    i32 0, label %39
  ]

39:                                               ; preds = %37
  br label %6, !llvm.loop !13

40:                                               ; preds = %6
  store i64 -1, ptr %1, align 8
  store i32 1, ptr %5, align 4
  br label %41

41:                                               ; preds = %40, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  %42 = load i64, ptr %1, align 8
  ret i64 %42
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"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"

@n = external dso_local local_unnamed_addr global i64, align 8
@m = external dso_local local_unnamed_addr global i64, align 8
@a = external dso_local local_unnamed_addr global [1005 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z9solve_oldv() local_unnamed_addr #0 {
  %1 = load i64, ptr @n, align 8
  %.fr25 = freeze i64 %1
  %2 = icmp sgt i64 %.fr25, 0
  %3 = load i64, ptr @m, align 8
  %.fr = freeze i64 %3
  br i1 %2, label %.split.us.preheader, label %.split

.split.us.preheader:                              ; preds = %0
  %min.iters.check = icmp ult i64 %.fr25, 4
  %n.vec = and i64 %.fr25, 9223372036854775804
  %cmp.n = icmp eq i64 %.fr25, %n.vec
  br label %.split.us

.split.us:                                        ; preds = %.split.us.preheader, %._crit_edge.us
  %.013.us = phi i64 [ %17, %._crit_edge.us ], [ 10000, %.split.us.preheader ]
  %4 = icmp sgt i64 %.013.us, -1
  br i1 %4, label %.preheader.us.preheader, label %.split20.us

.preheader.us.preheader:                          ; preds = %.split.us
  br i1 %min.iters.check, label %.preheader.us.preheader30, label %vector.ph

vector.ph:                                        ; preds = %.preheader.us.preheader
  %broadcast.splatinsert = insertelement <2 x i64> poison, i64 %.013.us, i64 0
  %broadcast.splat = shufflevector <2 x i64> %broadcast.splatinsert, <2 x i64> poison, <2 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %9, %vector.body ]
  %vec.phi28 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %10, %vector.body ]
  %5 = getelementptr inbounds [1005 x i64], ptr @a, i64 0, i64 %index
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  %wide.load = load <2 x i64>, ptr %5, align 16, !tbaa !5
  %wide.load29 = load <2 x i64>, ptr %6, align 16, !tbaa !5
  %7 = xor <2 x i64> %wide.load, %broadcast.splat
  %8 = xor <2 x i64> %wide.load29, %broadcast.splat
  %9 = add <2 x i64> %7, %vec.phi
  %10 = add <2 x i64> %8, %vec.phi28
  %index.next = add nuw i64 %index, 4
  %11 = icmp eq i64 %index.next, %n.vec
  br i1 %11, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %10, %9
  %12 = tail call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  br i1 %cmp.n, label %._crit_edge.us, label %.preheader.us.preheader30

.preheader.us.preheader30:                        ; preds = %middle.block, %.preheader.us.preheader
  %indvars.iv.ph = phi i64 [ 0, %.preheader.us.preheader ], [ %n.vec, %middle.block ]
  %.01217.us.ph = phi i64 [ 0, %.preheader.us.preheader ], [ %12, %middle.block ]
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader30, %.preheader.us
  %indvars.iv = phi i64 [ %indvars.iv.next, %.preheader.us ], [ %indvars.iv.ph, %.preheader.us.preheader30 ]
  %.01217.us = phi i64 [ %16, %.preheader.us ], [ %.01217.us.ph, %.preheader.us.preheader30 ]
  %13 = getelementptr inbounds [1005 x i64], ptr @a, i64 0, i64 %indvars.iv
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = xor i64 %14, %.013.us
  %16 = add nsw i64 %15, %.01217.us
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %.fr25
  br i1 %exitcond.not, label %._crit_edge.us, label %.preheader.us, !llvm.loop !13

._crit_edge.us:                                   ; preds = %.preheader.us, %middle.block
  %.lcssa = phi i64 [ %12, %middle.block ], [ %16, %.preheader.us ]
  %.not.us = icmp sgt i64 %.lcssa, %.fr
  %17 = add nsw i64 %.013.us, -1
  br i1 %.not.us, label %.split.us, label %.split20.us, !llvm.loop !14

.split:                                           ; preds = %0
  %.not.inv = icmp sgt i64 %.fr, -1
  %spec.select = select i1 %.not.inv, i64 10000, i64 -1
  br label %.split20.us, !llvm.loop !14

.split20.us:                                      ; preds = %.split.us, %._crit_edge.us, %.split
  %.us-phi = phi i64 [ %spec.select, %.split ], [ %.013.us, %._crit_edge.us ], [ -1, %.split.us ]
  ret i64 %.us-phi
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = 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, !12, !11}
!14 = distinct !{!14, !10}


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