; LLVM IR from MinaFaried3/Assiut-University-Training---Newcomers :: j-sheet7_recursion/K-MaxNumber.ll (Z9maxNumberPx)

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

@max_temp = external dso_local global i64, align 8
@length = external dso_local global i32, align 4

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

5:                                                ; preds = %1
  br label %23

6:                                                ; preds = %1
  %7 = load i64, ptr @max_temp, align 8, !tbaa !11
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = load i32, ptr @length, align 4, !tbaa !9
  %10 = add nsw i32 %9, -1
  store i32 %10, ptr @length, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds i64, ptr %8, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !11
  %14 = icmp slt i64 %7, %13
  br i1 %14, label %15, label %21

15:                                               ; preds = %6
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = load i32, ptr @length, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i64, ptr %16, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !11
  store i64 %20, ptr @max_temp, align 8, !tbaa !11
  br label %21

21:                                               ; preds = %15, %6
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  call void @_Z9maxNumberPx(ptr noundef %22)
  br label %23

23:                                               ; preds = %21, %5
  ret void
}

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"

@max_temp = external dso_local local_unnamed_addr global i64, align 8
@length = 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 void @_Z9maxNumberPx(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %length.promoted = load i32, ptr @length, align 4, !tbaa !5
  %2 = icmp eq i32 %length.promoted, 0
  br i1 %2, label %30, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %max_temp.promoted = load i64, ptr @max_temp, align 8, !tbaa !9
  %3 = sext i32 %length.promoted to i64
  %xtraiter = and i64 %3, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %tailrecurse.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %tailrecurse.prol ], [ %3, %.lr.ph.preheader ]
  %4 = phi i64 [ %9, %tailrecurse.prol ], [ %max_temp.promoted, %.lr.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %tailrecurse.prol ], [ 0, %.lr.ph.preheader ]
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.prol, -1
  %5 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv.next.prol
  %6 = load i64, ptr %5, align 8, !tbaa !9
  %7 = icmp slt i64 %4, %6
  br i1 %7, label %8, label %tailrecurse.prol

8:                                                ; preds = %.lr.ph.prol
  store i64 %6, ptr @max_temp, align 8, !tbaa !9
  br label %tailrecurse.prol

tailrecurse.prol:                                 ; preds = %8, %.lr.ph.prol
  %9 = phi i64 [ %6, %8 ], [ %4, %.lr.ph.prol ]
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !11

.lr.ph.prol.loopexit:                             ; preds = %tailrecurse.prol, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ %3, %.lr.ph.preheader ], [ %indvars.iv.next.prol, %tailrecurse.prol ]
  %.unr = phi i64 [ %max_temp.promoted, %.lr.ph.preheader ], [ %9, %tailrecurse.prol ]
  %10 = icmp ult i32 %length.promoted, 4
  br i1 %10, label %tailrecurse._crit_edge, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.prol.loopexit
  %invariant.gep = getelementptr i8, ptr %0, i64 -8
  %invariant.gep5 = getelementptr i8, ptr %0, i64 -16
  %invariant.gep7 = getelementptr i8, ptr %0, i64 -24
  br label %.lr.ph

.lr.ph:                                           ; preds = %tailrecurse.3, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ %indvars.iv.unr, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %tailrecurse.3 ]
  %11 = phi i64 [ %.unr, %.lr.ph.preheader.new ], [ %28, %tailrecurse.3 ]
  %gep = getelementptr i64, ptr %invariant.gep, i64 %indvars.iv
  %12 = load i64, ptr %gep, align 8, !tbaa !9
  %13 = icmp slt i64 %11, %12
  br i1 %13, label %14, label %tailrecurse

14:                                               ; preds = %.lr.ph
  store i64 %12, ptr @max_temp, align 8, !tbaa !9
  br label %tailrecurse

tailrecurse:                                      ; preds = %14, %.lr.ph
  %15 = phi i64 [ %12, %14 ], [ %11, %.lr.ph ]
  %gep6 = getelementptr i64, ptr %invariant.gep5, i64 %indvars.iv
  %16 = load i64, ptr %gep6, align 8, !tbaa !9
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %18, label %tailrecurse.1

18:                                               ; preds = %tailrecurse
  store i64 %16, ptr @max_temp, align 8, !tbaa !9
  br label %tailrecurse.1

tailrecurse.1:                                    ; preds = %18, %tailrecurse
  %19 = phi i64 [ %16, %18 ], [ %15, %tailrecurse ]
  %gep8 = getelementptr i64, ptr %invariant.gep7, i64 %indvars.iv
  %20 = load i64, ptr %gep8, align 8, !tbaa !9
  %21 = icmp slt i64 %19, %20
  br i1 %21, label %22, label %tailrecurse.2

22:                                               ; preds = %tailrecurse.1
  store i64 %20, ptr @max_temp, align 8, !tbaa !9
  br label %tailrecurse.2

tailrecurse.2:                                    ; preds = %22, %tailrecurse.1
  %23 = phi i64 [ %20, %22 ], [ %19, %tailrecurse.1 ]
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, -4
  %24 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv.next.3
  %25 = load i64, ptr %24, align 8, !tbaa !9
  %26 = icmp slt i64 %23, %25
  br i1 %26, label %27, label %tailrecurse.3

27:                                               ; preds = %tailrecurse.2
  store i64 %25, ptr @max_temp, align 8, !tbaa !9
  br label %tailrecurse.3

tailrecurse.3:                                    ; preds = %27, %tailrecurse.2
  %28 = phi i64 [ %25, %27 ], [ %23, %tailrecurse.2 ]
  %29 = icmp eq i64 %indvars.iv.next.3, 0
  br i1 %29, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge:                           ; preds = %tailrecurse.3, %.lr.ph.prol.loopexit
  store i32 0, ptr @length, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %tailrecurse._crit_edge, %1
  ret void
}

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

!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}
!12 = !{!"llvm.loop.unroll.disable"}


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