; LLVM IR from MinaFaried3/Assiut-University-Training---Newcomers :: j-sheet7_recursion/S-ArrayAverage.ll (Z11average_arrid)

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

@arr = external dso_local global [100 x double], align 16
@size_ = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef double @_Z11average_arrid(i32 noundef %0, double noundef %1) #0 {
  %3 = alloca double, align 8
  %4 = alloca i32, align 4
  %5 = alloca double, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store double %1, ptr %5, align 8, !tbaa !9
  %6 = load i32, ptr @size_, align 4, !tbaa !5
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = icmp eq i32 %6, %7
  br i1 %8, label %9, label %14

9:                                                ; preds = %2
  %10 = load double, ptr %5, align 8, !tbaa !9
  %11 = load i32, ptr @size_, align 4, !tbaa !5
  %12 = sitofp i32 %11 to double
  %13 = fdiv double %10, %12
  store double %13, ptr %3, align 8
  br label %24

14:                                               ; preds = %2
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = add nsw i32 %15, 1
  %17 = load double, ptr %5, align 8, !tbaa !9
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [100 x double], ptr @arr, i64 0, i64 %19
  %21 = load double, ptr %20, align 8, !tbaa !9
  %22 = fadd double %17, %21
  %23 = call noundef double @_Z11average_arrid(i32 noundef %16, double noundef %22)
  store double %23, ptr %3, align 8
  br label %24

24:                                               ; preds = %14, %9
  %25 = load double, ptr %3, align 8
  ret double %25
}

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 = !{!10, !10, i64 0}
!10 = !{!"double", !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"

@arr = external dso_local local_unnamed_addr global [100 x double], align 16
@size_ = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef double @_Z11average_arrid(i32 noundef %0, double noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @size_, align 4, !tbaa !5
  %4 = icmp eq i32 %3, %0
  br i1 %4, label %tailrecurse._crit_edge, label %tailrecurse.preheader

tailrecurse.preheader:                            ; preds = %2
  %5 = sext i32 %0 to i64
  %6 = sub i32 %3, %0
  %xtraiter = and i32 %6, 7
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %tailrecurse.prol.loopexit, label %tailrecurse.prol

tailrecurse.prol:                                 ; preds = %tailrecurse.preheader, %tailrecurse.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %tailrecurse.prol ], [ %5, %tailrecurse.preheader ]
  %.tr79.prol = phi double [ %9, %tailrecurse.prol ], [ %1, %tailrecurse.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %tailrecurse.prol ], [ 0, %tailrecurse.preheader ]
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.prol, 1
  %7 = getelementptr inbounds [100 x double], ptr @arr, i64 0, i64 %indvars.iv.prol
  %8 = load double, ptr %7, align 8, !tbaa !9
  %9 = fadd double %.tr79.prol, %8
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %tailrecurse.prol.loopexit, label %tailrecurse.prol, !llvm.loop !11

tailrecurse.prol.loopexit:                        ; preds = %tailrecurse.prol, %tailrecurse.preheader
  %.lcssa.unr = phi double [ poison, %tailrecurse.preheader ], [ %9, %tailrecurse.prol ]
  %indvars.iv.unr = phi i64 [ %5, %tailrecurse.preheader ], [ %indvars.iv.next.prol, %tailrecurse.prol ]
  %.tr79.unr = phi double [ %1, %tailrecurse.preheader ], [ %9, %tailrecurse.prol ]
  %10 = sub i32 %0, %3
  %11 = icmp ugt i32 %10, -8
  br i1 %11, label %tailrecurse._crit_edge, label %tailrecurse

tailrecurse._crit_edge:                           ; preds = %tailrecurse.prol.loopexit, %tailrecurse, %2
  %.tr7.lcssa = phi double [ %1, %2 ], [ %.lcssa.unr, %tailrecurse.prol.loopexit ], [ %37, %tailrecurse ]
  %12 = sitofp i32 %3 to double
  %13 = fdiv double %.tr7.lcssa, %12
  ret double %13

tailrecurse:                                      ; preds = %tailrecurse.prol.loopexit, %tailrecurse
  %indvars.iv = phi i64 [ %indvars.iv.next.7, %tailrecurse ], [ %indvars.iv.unr, %tailrecurse.prol.loopexit ]
  %.tr79 = phi double [ %37, %tailrecurse ], [ %.tr79.unr, %tailrecurse.prol.loopexit ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %14 = getelementptr inbounds [100 x double], ptr @arr, i64 0, i64 %indvars.iv
  %15 = load double, ptr %14, align 8, !tbaa !9
  %16 = fadd double %.tr79, %15
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, 2
  %17 = getelementptr inbounds [100 x double], ptr @arr, i64 0, i64 %indvars.iv.next
  %18 = load double, ptr %17, align 8, !tbaa !9
  %19 = fadd double %16, %18
  %indvars.iv.next.2 = add nsw i64 %indvars.iv, 3
  %20 = getelementptr inbounds [100 x double], ptr @arr, i64 0, i64 %indvars.iv.next.1
  %21 = load double, ptr %20, align 8, !tbaa !9
  %22 = fadd double %19, %21
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, 4
  %23 = getelementptr inbounds [100 x double], ptr @arr, i64 0, i64 %indvars.iv.next.2
  %24 = load double, ptr %23, align 8, !tbaa !9
  %25 = fadd double %22, %24
  %indvars.iv.next.4 = add nsw i64 %indvars.iv, 5
  %26 = getelementptr inbounds [100 x double], ptr @arr, i64 0, i64 %indvars.iv.next.3
  %27 = load double, ptr %26, align 8, !tbaa !9
  %28 = fadd double %25, %27
  %indvars.iv.next.5 = add nsw i64 %indvars.iv, 6
  %29 = getelementptr inbounds [100 x double], ptr @arr, i64 0, i64 %indvars.iv.next.4
  %30 = load double, ptr %29, align 8, !tbaa !9
  %31 = fadd double %28, %30
  %indvars.iv.next.6 = add nsw i64 %indvars.iv, 7
  %32 = getelementptr inbounds [100 x double], ptr @arr, i64 0, i64 %indvars.iv.next.5
  %33 = load double, ptr %32, align 8, !tbaa !9
  %34 = fadd double %31, %33
  %indvars.iv.next.7 = add nsw i64 %indvars.iv, 8
  %35 = getelementptr inbounds [100 x double], ptr @arr, i64 0, i64 %indvars.iv.next.6
  %36 = load double, ptr %35, align 8, !tbaa !9
  %37 = fadd double %34, %36
  %38 = trunc nsw i64 %indvars.iv.next.7 to i32
  %39 = icmp eq i32 %3, %38
  br i1 %39, label %tailrecurse._crit_edge, label %tailrecurse
}

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

!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 = !{!"double", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}


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