; LLVM IR from codee-com/open-catalog :: Checks/PWR031/benchmark (solution)

; 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: nounwind willreturn memory(none) uwtable
define dso_local double @midpoint_rule_x_pow_1_5_sin_x_improved(double noundef %0, double noundef %1, i32 noundef %2) #0 {
  %4 = alloca double, align 8
  %5 = alloca double, align 8
  %6 = alloca i32, align 4
  %7 = alloca double, align 8
  %8 = alloca double, align 8
  %9 = alloca i32, align 4
  %10 = alloca double, align 8
  store double %0, ptr %4, align 8, !tbaa !5
  store double %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store double 0.000000e+00, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %11 = load double, ptr %5, align 8, !tbaa !5
  %12 = load double, ptr %4, align 8, !tbaa !5
  %13 = fsub double %11, %12
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = sitofp i32 %14 to double
  %16 = fdiv double %13, %15
  store double %16, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %39, %3
  %18 = load i32, ptr %9, align 4, !tbaa !9
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %42

22:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = sitofp i32 %23 to double
  %25 = fadd double %24, 5.000000e-01
  %26 = load double, ptr %8, align 8, !tbaa !5
  %27 = load double, ptr %4, align 8, !tbaa !5
  %28 = call double @llvm.fma.f64(double %25, double %26, double %27)
  store double %28, ptr %10, align 8, !tbaa !5
  %29 = load double, ptr %10, align 8, !tbaa !5
  %30 = load double, ptr %10, align 8, !tbaa !5
  %31 = call double @sqrt(double noundef %30) #4
  %32 = fmul double %29, %31
  %33 = load double, ptr %10, align 8, !tbaa !5
  %34 = call double @sin(double noundef %33) #4
  %35 = fmul double %32, %34
  %36 = load double, ptr %8, align 8, !tbaa !5
  %37 = load double, ptr %7, align 8, !tbaa !5
  %38 = call double @llvm.fma.f64(double %35, double %36, double %37)
  store double %38, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %39

39:                                               ; preds = %22
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %9, align 4, !tbaa !9
  br label %17, !llvm.loop !11

42:                                               ; preds = %21
  %43 = load double, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret double %43
}

; 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 speculatable willreturn memory(none)
declare double @llvm.fma.f64(double, double, double) #2

; Function Attrs: nounwind
declare double @sqrt(double noundef) #3

; Function Attrs: nounwind
declare double @sin(double noundef) #3

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

attributes #0 = { 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 willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nounwind "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 #4 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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 nosync nounwind willreturn memory(none) uwtable
define dso_local double @midpoint_rule_x_pow_1_5_sin_x_improved(double noundef %0, double noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = fsub double %1, %0
  %5 = sitofp i32 %2 to double
  %6 = fdiv double %4, %5
  %7 = icmp sgt i32 %2, 0
  br i1 %7, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %xtraiter = and i32 %2, 1
  %8 = icmp eq i32 %2, 1
  br i1 %8, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %2, 2147483646
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa.loopexit:          ; preds = %.lr.ph
  %9 = uitofp nneg i32 %34 to double
  %10 = fadd double %9, 5.000000e-01
  br label %._crit_edge.loopexit.unr-lcssa

._crit_edge.loopexit.unr-lcssa:                   ; preds = %._crit_edge.loopexit.unr-lcssa.loopexit, %.lr.ph.preheader
  %.lcssa.ph = phi double [ poison, %.lr.ph.preheader ], [ %33, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.016.unr = phi double [ 0.000000e+00, %.lr.ph.preheader ], [ %33, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.01415.unr = phi double [ 5.000000e-01, %.lr.ph.preheader ], [ %10, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %11 = tail call double @llvm.fma.f64(double %.01415.unr, double %6, double %0)
  %12 = tail call double @sqrt(double noundef %11) #3
  %13 = fmul double %11, %12
  %14 = tail call double @sin(double noundef %11) #3
  %15 = fmul double %13, %14
  %16 = tail call double @llvm.fma.f64(double %15, double %6, double %.016.unr)
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %3
  %.0.lcssa = phi double [ 0.000000e+00, %3 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %16, %.lr.ph.epil ]
  ret double %.0.lcssa

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.016 = phi double [ 0.000000e+00, %.lr.ph.preheader.new ], [ %33, %.lr.ph ]
  %.01415 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %34, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %17 = uitofp nneg i32 %.01415 to double
  %18 = fadd double %17, 5.000000e-01
  %19 = tail call double @llvm.fma.f64(double %18, double %6, double %0)
  %20 = tail call double @sqrt(double noundef %19) #3
  %21 = fmul double %19, %20
  %22 = tail call double @sin(double noundef %19) #3
  %23 = fmul double %21, %22
  %24 = tail call double @llvm.fma.f64(double %23, double %6, double %.016)
  %25 = or disjoint i32 %.01415, 1
  %26 = uitofp nneg i32 %25 to double
  %27 = fadd double %26, 5.000000e-01
  %28 = tail call double @llvm.fma.f64(double %27, double %6, double %0)
  %29 = tail call double @sqrt(double noundef %28) #3
  %30 = fmul double %28, %29
  %31 = tail call double @sin(double noundef %28) #3
  %32 = fmul double %30, %31
  %33 = tail call double @llvm.fma.f64(double %32, double %6, double %24)
  %34 = add nuw nsw i32 %.01415, 2
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa.loopexit, label %.lr.ph, !llvm.loop !5
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fma.f64(double, double, double) #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sqrt(double noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sin(double noundef) local_unnamed_addr #2

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


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