; LLVM IR from ravireddy07/cpp-book :: src/Coding-Blocks/ViratAndNcR.ll (Z3NcRxxx)

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

; 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
declare dso_local noundef i64 @_Z11FindInversexx(i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z3NcRxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  store i64 %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !5
  %12 = load i64, ptr %6, align 8, !tbaa !5
  %13 = icmp eq i64 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %3
  store i64 1, ptr %4, align 8
  br label %66

15:                                               ; preds = %3
  %16 = load i64, ptr %5, align 8, !tbaa !5
  %17 = add nsw i64 %16, 1
  %18 = call ptr @llvm.stacksave.p0()
  store ptr %18, ptr %8, align 8
  %19 = alloca i64, i64 %17, align 16
  store i64 %17, ptr %9, align 8
  %20 = getelementptr inbounds i64, ptr %19, i64 0
  store i64 1, ptr %20, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store i64 1, ptr %10, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %37, %15
  %22 = load i64, ptr %10, align 8, !tbaa !5
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = icmp sle i64 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %40

26:                                               ; preds = %21
  %27 = load i64, ptr %10, align 8, !tbaa !5
  %28 = sub nsw i64 %27, 1
  %29 = getelementptr inbounds i64, ptr %19, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !5
  %31 = load i64, ptr %10, align 8, !tbaa !5
  %32 = mul nsw i64 %30, %31
  %33 = load i64, ptr %7, align 8, !tbaa !5
  %34 = srem i64 %32, %33
  %35 = load i64, ptr %10, align 8, !tbaa !5
  %36 = getelementptr inbounds i64, ptr %19, i64 %35
  store i64 %34, ptr %36, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %26
  %38 = load i64, ptr %10, align 8, !tbaa !5
  %39 = add nsw i64 %38, 1
  store i64 %39, ptr %10, align 8, !tbaa !5
  br label %21, !llvm.loop !9

40:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %41 = load i64, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds i64, ptr %19, i64 %41
  %43 = load i64, ptr %42, align 8, !tbaa !5
  %44 = load i64, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds i64, ptr %19, i64 %44
  %46 = load i64, ptr %45, align 8, !tbaa !5
  %47 = load i64, ptr %7, align 8, !tbaa !5
  %48 = call noundef i64 @_Z11FindInversexx(i64 noundef %46, i64 noundef %47)
  %49 = mul nsw i64 %43, %48
  %50 = load i64, ptr %7, align 8, !tbaa !5
  %51 = srem i64 %49, %50
  %52 = load i64, ptr %5, align 8, !tbaa !5
  %53 = load i64, ptr %6, align 8, !tbaa !5
  %54 = sub nsw i64 %52, %53
  %55 = getelementptr inbounds i64, ptr %19, i64 %54
  %56 = load i64, ptr %55, align 8, !tbaa !5
  %57 = load i64, ptr %7, align 8, !tbaa !5
  %58 = call noundef i64 @_Z11FindInversexx(i64 noundef %56, i64 noundef %57)
  %59 = mul nsw i64 %51, %58
  %60 = load i64, ptr %7, align 8, !tbaa !5
  %61 = srem i64 %59, %60
  %62 = load i64, ptr %7, align 8, !tbaa !5
  %63 = srem i64 %61, %62
  store i64 %63, ptr %11, align 8, !tbaa !5
  %64 = load i64, ptr %11, align 8, !tbaa !5
  store i64 %64, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  %65 = load ptr, ptr %8, align 8
  call void @llvm.stackrestore.p0(ptr %65)
  br label %66

66:                                               ; preds = %40, %14
  %67 = load i64, ptr %4, align 8
  ret i64 %67
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

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 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z11FindInversexx(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z3NcRxxx(i64 noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq i64 %1, 0
  br i1 %4, label %32, label %5

5:                                                ; preds = %3
  %6 = add nuw i64 %0, 1
  %7 = tail call ptr @llvm.stacksave.p0()
  %8 = alloca i64, i64 %6, align 16
  store i64 1, ptr %8, align 16, !tbaa !5
  %.not23 = icmp sgt i64 %0, 0
  tail call void @llvm.assume(i1 %.not23)
  %xtraiter = and i64 %0, 1
  %9 = icmp eq i64 %0, 1
  br i1 %9, label %._crit_edge.loopexit.unr-lcssa, label %.new

.new:                                             ; preds = %5
  %unroll_iter = and i64 %0, 9223372036854775806
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %5
  %store_forwarded.unr = phi i64 [ 1, %5 ], [ %30, %.lr.ph ]
  %.02024.unr = phi i64 [ 1, %5 ], [ %31, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %10 = getelementptr i64, ptr %8, i64 %.02024.unr
  %11 = mul nsw i64 %store_forwarded.unr, %.02024.unr
  %12 = srem i64 %11, %2
  store i64 %12, ptr %10, align 8, !tbaa !5
  br label %._crit_edge.loopexit

._crit_edge.loopexit:                             ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %.phi.trans.insert = getelementptr inbounds i64, ptr %8, i64 %1
  %.pre = load i64, ptr %.phi.trans.insert, align 8, !tbaa !5
  %13 = getelementptr inbounds i64, ptr %8, i64 %0
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = tail call noundef i64 @_Z11FindInversexx(i64 noundef %.pre, i64 noundef %2)
  %16 = mul nsw i64 %15, %14
  %17 = srem i64 %16, %2
  %18 = sub nsw i64 %0, %1
  %19 = getelementptr inbounds i64, ptr %8, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = tail call noundef i64 @_Z11FindInversexx(i64 noundef %20, i64 noundef %2)
  %22 = mul nsw i64 %21, %17
  %23 = srem i64 %22, %2
  tail call void @llvm.stackrestore.p0(ptr %7)
  br label %32

.lr.ph:                                           ; preds = %.lr.ph, %.new
  %store_forwarded = phi i64 [ 1, %.new ], [ %30, %.lr.ph ]
  %.02024 = phi i64 [ 1, %.new ], [ %31, %.lr.ph ]
  %niter = phi i64 [ 0, %.new ], [ %niter.next.1, %.lr.ph ]
  %24 = getelementptr i64, ptr %8, i64 %.02024
  %25 = mul nsw i64 %store_forwarded, %.02024
  %26 = srem i64 %25, %2
  store i64 %26, ptr %24, align 8, !tbaa !5
  %27 = add nuw i64 %.02024, 1
  %28 = getelementptr i64, ptr %8, i64 %27
  %29 = mul nsw i64 %26, %27
  %30 = srem i64 %29, %2
  store i64 %30, ptr %28, align 8, !tbaa !5
  %31 = add nuw i64 %.02024, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9

32:                                               ; preds = %3, %._crit_edge.loopexit
  %.0 = phi i64 [ %23, %._crit_edge.loopexit ], [ 1, %3 ]
  ret i64 %.0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #2

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }

!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.mustprogress"}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass