; LLVM IR from Leet-Us-Code/DSA-A-Z-Practice :: 60 Must Solve List/Pow(x,n)[Exponential Squaring (Fast Modulo Multiplication)].ll (Z5myPowdi)

; 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: mustprogress nounwind uwtable
define dso_local noundef double @_Z5myPowdi(double noundef %0, i32 noundef %1) #0 {
  %3 = alloca double, align 8
  %4 = alloca i32, align 4
  %5 = alloca double, align 8
  %6 = alloca i64, align 8
  store double %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store double 1.000000e+00, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = sext i32 %7 to i64
  store i64 %8, ptr %6, align 8, !tbaa !11
  %9 = load i64, ptr %6, align 8, !tbaa !11
  %10 = icmp slt i64 %9, 0
  br i1 %10, label %11, label %14

11:                                               ; preds = %2
  %12 = load i64, ptr %6, align 8, !tbaa !11
  %13 = mul nsw i64 -1, %12
  store i64 %13, ptr %6, align 8, !tbaa !11
  br label %14

14:                                               ; preds = %11, %2
  br label %15

15:                                               ; preds = %26, %14
  %16 = load i64, ptr %6, align 8, !tbaa !11
  %17 = icmp sgt i64 %16, 0
  br i1 %17, label %18, label %32

18:                                               ; preds = %15
  %19 = load i64, ptr %6, align 8, !tbaa !11
  %20 = srem i64 %19, 2
  %21 = icmp ne i64 %20, 0
  br i1 %21, label %22, label %26

22:                                               ; preds = %18
  %23 = load double, ptr %5, align 8, !tbaa !5
  %24 = load double, ptr %3, align 8, !tbaa !5
  %25 = fmul double %23, %24
  store double %25, ptr %5, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %22, %18
  %27 = load double, ptr %3, align 8, !tbaa !5
  %28 = load double, ptr %3, align 8, !tbaa !5
  %29 = fmul double %27, %28
  store double %29, ptr %3, align 8, !tbaa !5
  %30 = load i64, ptr %6, align 8, !tbaa !11
  %31 = sdiv i64 %30, 2
  store i64 %31, ptr %6, align 8, !tbaa !11
  br label %15, !llvm.loop !13

32:                                               ; preds = %15
  %33 = load i32, ptr %4, align 4, !tbaa !9
  %34 = icmp slt i32 %33, 0
  br i1 %34, label %35, label %38

35:                                               ; preds = %32
  %36 = load double, ptr %5, align 8, !tbaa !5
  %37 = fdiv double 1.000000e+00, %36
  store double %37, ptr %5, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %35, %32
  %39 = load double, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret double %39
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = !{!"double", !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", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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 nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef double @_Z5myPowdi(double noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not1718 = icmp eq i32 %1, 0
  br i1 %.not1718, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %3 = tail call i32 @llvm.abs.i32(i32 %1, i1 false)
  %spec.select = zext i32 %3 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.121 = phi i64 [ %7, %.lr.ph ], [ %spec.select, %.lr.ph.preheader ]
  %.01220 = phi double [ %.113, %.lr.ph ], [ 1.000000e+00, %.lr.ph.preheader ]
  %.01419 = phi double [ %6, %.lr.ph ], [ %0, %.lr.ph.preheader ]
  %4 = and i64 %.121, 1
  %.not = icmp eq i64 %4, 0
  %5 = fmul double %.01419, %.01220
  %.113 = select i1 %.not, double %.01220, double %5
  %6 = fmul double %.01419, %.01419
  %7 = lshr i64 %.121, 1
  %.not17 = icmp ult i64 %.121, 2
  br i1 %.not17, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %2
  %.012.lcssa = phi double [ 1.000000e+00, %2 ], [ %.113, %.lr.ph ]
  %8 = icmp slt i32 %1, 0
  %9 = fdiv double 1.000000e+00, %.012.lcssa
  %.2 = select i1 %8, double %9, double %.012.lcssa
  ret double %.2
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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