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

; 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

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

7:                                                ; preds = %2
  br label %26

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

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

22:                                               ; preds = %16, %8
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = sub nsw i32 %24, 1
  call void @_Z7max_numPxi(ptr noundef %23, i32 noundef %25)
  br label %26

26:                                               ; preds = %22, %7
  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

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local void @_Z7max_numPxi(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %1, -1
  br i1 %3, label %tailrecurse._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %max_temp.promoted = load i64, ptr @max_temp, align 8, !tbaa !5
  %4 = sext i32 %1 to i64
  %5 = add nsw i64 %4, 1
  %xtraiter = and i64 %5, 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 ], [ %4, %.lr.ph.preheader ]
  %6 = phi i64 [ %11, %tailrecurse.prol ], [ %max_temp.promoted, %.lr.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %tailrecurse.prol ], [ 0, %.lr.ph.preheader ]
  %7 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv.prol
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = icmp slt i64 %6, %8
  br i1 %9, label %10, label %tailrecurse.prol

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

tailrecurse.prol:                                 ; preds = %10, %.lr.ph.prol
  %11 = phi i64 [ %8, %10 ], [ %6, %.lr.ph.prol ]
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.prol, -1
  %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 !9

.lr.ph.prol.loopexit:                             ; preds = %tailrecurse.prol, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ %4, %.lr.ph.preheader ], [ %indvars.iv.next.prol, %tailrecurse.prol ]
  %.unr = phi i64 [ %max_temp.promoted, %.lr.ph.preheader ], [ %11, %tailrecurse.prol ]
  %12 = icmp ult i32 %1, 3
  br i1 %12, 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.gep10 = getelementptr i8, ptr %0, i64 -16
  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 ]
  %13 = phi i64 [ %.unr, %.lr.ph.preheader.new ], [ %31, %tailrecurse.3 ]
  %14 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = icmp slt i64 %13, %15
  br i1 %16, label %17, label %tailrecurse

17:                                               ; preds = %.lr.ph
  store i64 %15, ptr @max_temp, align 8, !tbaa !5
  br label %tailrecurse

tailrecurse:                                      ; preds = %17, %.lr.ph
  %18 = phi i64 [ %15, %17 ], [ %13, %.lr.ph ]
  %gep = getelementptr i64, ptr %invariant.gep, i64 %indvars.iv
  %19 = load i64, ptr %gep, align 8, !tbaa !5
  %20 = icmp slt i64 %18, %19
  br i1 %20, label %21, label %tailrecurse.1

21:                                               ; preds = %tailrecurse
  store i64 %19, ptr @max_temp, align 8, !tbaa !5
  br label %tailrecurse.1

tailrecurse.1:                                    ; preds = %21, %tailrecurse
  %22 = phi i64 [ %19, %21 ], [ %18, %tailrecurse ]
  %gep11 = getelementptr i64, ptr %invariant.gep10, i64 %indvars.iv
  %23 = load i64, ptr %gep11, align 8, !tbaa !5
  %24 = icmp slt i64 %22, %23
  br i1 %24, label %25, label %tailrecurse.2

25:                                               ; preds = %tailrecurse.1
  store i64 %23, ptr @max_temp, align 8, !tbaa !5
  br label %tailrecurse.2

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

30:                                               ; preds = %tailrecurse.2
  store i64 %28, ptr @max_temp, align 8, !tbaa !5
  br label %tailrecurse.3

tailrecurse.3:                                    ; preds = %30, %tailrecurse.2
  %31 = phi i64 [ %28, %30 ], [ %26, %tailrecurse.2 ]
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, -4
  %32 = icmp eq i64 %indvars.iv.next.2, 0
  br i1 %32, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge:                           ; preds = %.lr.ph.prol.loopexit, %tailrecurse.3, %2
  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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}


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