; LLVM IR from MinaFaried3/Assiut-University-Training---Newcomers :: j-sheet7_recursion/M-SuffixSum.ll (Z3sumPx)

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

@counter = external dso_local global i32, align 4
@last_num = external dso_local global i32, align 4
@size_ = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z3sumPx(ptr noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %4 = load i32, ptr @counter, align 4, !tbaa !9
  %5 = load i32, ptr @last_num, align 4, !tbaa !9
  %6 = icmp eq i32 %4, %5
  br i1 %6, label %7, label %8

7:                                                ; preds = %1
  store i64 0, ptr %2, align 8
  br label %20

8:                                                ; preds = %1
  %9 = load i32, ptr @counter, align 4, !tbaa !9
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr @counter, align 4, !tbaa !9
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = load i32, ptr @size_, align 4, !tbaa !9
  %13 = add nsw i32 %12, -1
  store i32 %13, ptr @size_, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i64, ptr %11, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = call noundef i64 @_Z3sumPx(ptr noundef %17)
  %19 = add nsw i64 %16, %18
  store i64 %19, ptr %2, align 8
  br label %20

20:                                               ; preds = %8, %7
  %21 = load i64, ptr %2, align 8
  ret i64 %21
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}


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

@counter = external dso_local local_unnamed_addr global i32, align 4
@last_num = external dso_local local_unnamed_addr global i32, align 4
@size_ = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z3sumPx(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @last_num, align 4, !tbaa !5
  %counter.promoted = load i32, ptr @counter, align 4, !tbaa !5
  %3 = icmp eq i32 %counter.promoted, %2
  br i1 %3, label %23, label %tailrecurse.preheader

tailrecurse.preheader:                            ; preds = %1
  %size_.promoted = load i32, ptr @size_, align 4, !tbaa !5
  %4 = sext i32 %size_.promoted to i64
  %5 = xor i32 %counter.promoted, -1
  %6 = add i32 %2, %5
  %7 = zext i32 %6 to i64
  %8 = add nuw nsw i64 %7, 1
  %min.iters.check = icmp ult i32 %6, 3
  br i1 %min.iters.check, label %tailrecurse.preheader13, label %vector.ph

vector.ph:                                        ; preds = %tailrecurse.preheader
  %n.vec = and i64 %8, 8589934588
  %ind.end = sub nsw i64 %4, %n.vec
  %.cast = trunc i64 %n.vec to i32
  %ind.end8 = add i32 %counter.promoted, %.cast
  %invariant.gep = getelementptr i64, ptr %0, i64 %4
  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 ], [ %12, %vector.body ]
  %vec.phi10 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %13, %vector.body ]
  %9 = xor i64 %index, -1
  %gep = getelementptr i64, ptr %invariant.gep, i64 %9
  %10 = getelementptr inbounds i8, ptr %gep, i64 -8
  %11 = getelementptr inbounds i8, ptr %gep, i64 -24
  %wide.load = load <2 x i64>, ptr %10, align 8, !tbaa !9
  %reverse = shufflevector <2 x i64> %wide.load, <2 x i64> poison, <2 x i32> <i32 1, i32 0>
  %wide.load11 = load <2 x i64>, ptr %11, align 8, !tbaa !9
  %reverse12 = shufflevector <2 x i64> %wide.load11, <2 x i64> poison, <2 x i32> <i32 1, i32 0>
  %12 = add <2 x i64> %reverse, %vec.phi
  %13 = add <2 x i64> %reverse12, %vec.phi10
  %index.next = add nuw i64 %index, 4
  %14 = icmp eq i64 %index.next, %n.vec
  br i1 %14, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %13, %12
  %15 = tail call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  %cmp.n = icmp eq i64 %8, %n.vec
  br i1 %cmp.n, label %tailrecurse._crit_edge, label %tailrecurse.preheader13

tailrecurse.preheader13:                          ; preds = %middle.block, %tailrecurse.preheader
  %indvars.iv.ph = phi i64 [ %4, %tailrecurse.preheader ], [ %ind.end, %middle.block ]
  %accumulator.tr3.ph = phi i64 [ 0, %tailrecurse.preheader ], [ %15, %middle.block ]
  %.ph = phi i32 [ %counter.promoted, %tailrecurse.preheader ], [ %ind.end8, %middle.block ]
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.preheader13, %tailrecurse
  %indvars.iv = phi i64 [ %indvars.iv.next, %tailrecurse ], [ %indvars.iv.ph, %tailrecurse.preheader13 ]
  %accumulator.tr3 = phi i64 [ %20, %tailrecurse ], [ %accumulator.tr3.ph, %tailrecurse.preheader13 ]
  %16 = phi i32 [ %17, %tailrecurse ], [ %.ph, %tailrecurse.preheader13 ]
  %17 = add nsw i32 %16, 1
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %18 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv.next
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %20 = add nsw i64 %19, %accumulator.tr3
  %21 = icmp eq i32 %17, %2
  br i1 %21, label %tailrecurse._crit_edge, label %tailrecurse, !llvm.loop !14

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %middle.block
  %indvars.iv.next.lcssa = phi i64 [ %ind.end, %middle.block ], [ %indvars.iv.next, %tailrecurse ]
  %.lcssa = phi i64 [ %15, %middle.block ], [ %20, %tailrecurse ]
  %22 = trunc nsw i64 %indvars.iv.next.lcssa to i32
  store i32 %2, ptr @counter, align 4, !tbaa !5
  store i32 %22, ptr @size_, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %tailrecurse._crit_edge, %1
  %accumulator.tr.lcssa = phi i64 [ %.lcssa, %tailrecurse._crit_edge ], [ 0, %1 ]
  ret i64 %accumulator.tr.lcssa
}

; 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 memory(readwrite, argmem: read, 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 = !{!"int", !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 = distinct !{!11, !12, !13}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = distinct !{!14, !13, !12}


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