; LLVM IR from Ahmed-alkharusi/Interesting-problems- :: mandelbrot set (C++ wtih Python plots)/mandelbrot set/Source.ll (Z10mandelbrotR7ComplexRKj)

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

%class.Complex = type { double, double }

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

declare void @_ZN7ComplexC1Edd(ptr noundef nonnull align 8 dereferenceable(16), double noundef, double noundef) unnamed_addr #1

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z10mandelbrotR7ComplexRKj(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef nonnull align 4 dereferenceable(4) %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %class.Complex, align 8
  %7 = alloca %class.Complex, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca %class.Complex, align 8
  %11 = alloca %class.Complex, align 8
  %12 = alloca double, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #4
  call void @_ZN7ComplexC1Edd(ptr noundef nonnull align 8 dereferenceable(16) %6, double noundef 0.000000e+00, double noundef 0.000000e+00)
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #4
  call void @_ZN7ComplexC1Edd(ptr noundef nonnull align 8 dereferenceable(16) %7, double noundef 0.000000e+00, double noundef 0.000000e+00)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %13

13:                                               ; preds = %49, %2
  %14 = load i64, ptr %8, align 8, !tbaa !9
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = zext i32 %16 to i64
  %18 = icmp ult i64 %14, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %13
  store i32 2, ptr %9, align 4
  br label %52

20:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 16, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #4
  %21 = call { double, double } @_ZN7ComplexmlERKS_(ptr noundef nonnull align 8 dereferenceable(16) %6, ptr noundef nonnull align 8 dereferenceable(16) %6)
  %22 = getelementptr inbounds { double, double }, ptr %11, i32 0, i32 0
  %23 = extractvalue { double, double } %21, 0
  store double %23, ptr %22, align 8
  %24 = getelementptr inbounds { double, double }, ptr %11, i32 0, i32 1
  %25 = extractvalue { double, double } %21, 1
  store double %25, ptr %24, align 8
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = call { double, double } @_ZN7ComplexplERKS_(ptr noundef nonnull align 8 dereferenceable(16) %11, ptr noundef nonnull align 8 dereferenceable(16) %26)
  %28 = getelementptr inbounds { double, double }, ptr %10, i32 0, i32 0
  %29 = extractvalue { double, double } %27, 0
  store double %29, ptr %28, align 8
  %30 = getelementptr inbounds { double, double }, ptr %10, i32 0, i32 1
  %31 = extractvalue { double, double } %27, 1
  store double %31, ptr %30, align 8
  %32 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZN7ComplexaSERKS_(ptr noundef nonnull align 8 dereferenceable(16) %7, ptr noundef nonnull align 8 dereferenceable(16) %10)
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %10) #4
  %33 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZN7ComplexaSERKS_(ptr noundef nonnull align 8 dereferenceable(16) %6, ptr noundef nonnull align 8 dereferenceable(16) %7)
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %34 = call noundef double @_ZN7Complex2reEv(ptr noundef nonnull align 8 dereferenceable(16) %7)
  %35 = call noundef double @_ZN7Complex2reEv(ptr noundef nonnull align 8 dereferenceable(16) %7)
  %36 = call noundef double @_ZN7Complex2imEv(ptr noundef nonnull align 8 dereferenceable(16) %7)
  %37 = call noundef double @_ZN7Complex2imEv(ptr noundef nonnull align 8 dereferenceable(16) %7)
  %38 = fmul double %36, %37
  %39 = call double @llvm.fmuladd.f64(double %34, double %35, double %38)
  store double %39, ptr %12, align 8, !tbaa !13
  %40 = load double, ptr %12, align 8, !tbaa !13
  %41 = fcmp oge double %40, 4.000000e+00
  br i1 %41, label %42, label %45

42:                                               ; preds = %20
  %43 = load i64, ptr %8, align 8, !tbaa !9
  %44 = trunc i64 %43 to i32
  store i32 %44, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %46

45:                                               ; preds = %20
  store i32 0, ptr %9, align 4
  br label %46

46:                                               ; preds = %45, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  %47 = load i32, ptr %9, align 4
  switch i32 %47, label %52 [
    i32 0, label %48
  ]

48:                                               ; preds = %46
  br label %49

49:                                               ; preds = %48
  %50 = load i64, ptr %8, align 8, !tbaa !9
  %51 = add i64 %50, 1
  store i64 %51, ptr %8, align 8, !tbaa !9
  br label %13, !llvm.loop !15

52:                                               ; preds = %46, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %53 = load i32, ptr %9, align 4
  switch i32 %53, label %55 [
    i32 2, label %54
  ]

54:                                               ; preds = %52
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %55

55:                                               ; preds = %54, %52
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #4
  %56 = load i32, ptr %3, align 4
  ret i32 %56
}

declare { double, double } @_ZN7ComplexmlERKS_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) #1

declare { double, double } @_ZN7ComplexplERKS_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) #1

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZN7ComplexaSERKS_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) #1

declare noundef double @_ZN7Complex2reEv(ptr noundef nonnull align 8 dereferenceable(16)) #1

declare noundef double @_ZN7Complex2imEv(ptr noundef nonnull align 8 dereferenceable(16)) #1

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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" }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

%class.Complex = type { double, double }

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

declare void @_ZN7ComplexC1Edd(ptr noundef nonnull align 8 dereferenceable(16), double noundef, double noundef) unnamed_addr #1

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z10mandelbrotR7ComplexRKj(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr nocapture noundef nonnull readonly align 4 dereferenceable(4) %1) local_unnamed_addr #2 {
  %3 = alloca %class.Complex, align 8
  %4 = alloca %class.Complex, align 8
  %5 = alloca %class.Complex, align 8
  %6 = alloca %class.Complex, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #4
  call void @_ZN7ComplexC1Edd(ptr noundef nonnull align 8 dereferenceable(16) %3, double noundef 0.000000e+00, double noundef 0.000000e+00)
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #4
  call void @_ZN7ComplexC1Edd(ptr noundef nonnull align 8 dereferenceable(16) %4, double noundef 0.000000e+00, double noundef 0.000000e+00)
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %.not14.not = icmp eq i32 %7, 0
  br i1 %.not14.not, label %.split.loop.exit, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %8 = getelementptr inbounds i8, ptr %6, i64 8
  %9 = getelementptr inbounds i8, ptr %5, i64 8
  br label %10

10:                                               ; preds = %.lr.ph, %26
  %.01115 = phi i64 [ 0, %.lr.ph ], [ %27, %26 ]
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %6) #4
  %11 = call { double, double } @_ZN7ComplexmlERKS_(ptr noundef nonnull align 8 dereferenceable(16) %3, ptr noundef nonnull align 8 dereferenceable(16) %3)
  %12 = extractvalue { double, double } %11, 0
  store double %12, ptr %6, align 8
  %13 = extractvalue { double, double } %11, 1
  store double %13, ptr %8, align 8
  %14 = call { double, double } @_ZN7ComplexplERKS_(ptr noundef nonnull align 8 dereferenceable(16) %6, ptr noundef nonnull align 8 dereferenceable(16) %0)
  %15 = extractvalue { double, double } %14, 0
  store double %15, ptr %5, align 8
  %16 = extractvalue { double, double } %14, 1
  store double %16, ptr %9, align 8
  %17 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZN7ComplexaSERKS_(ptr noundef nonnull align 8 dereferenceable(16) %4, ptr noundef nonnull align 8 dereferenceable(16) %5)
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #4
  %18 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZN7ComplexaSERKS_(ptr noundef nonnull align 8 dereferenceable(16) %3, ptr noundef nonnull align 8 dereferenceable(16) %4)
  %19 = call noundef double @_ZN7Complex2reEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %20 = call noundef double @_ZN7Complex2reEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %21 = call noundef double @_ZN7Complex2imEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %22 = call noundef double @_ZN7Complex2imEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %23 = fmul double %21, %22
  %24 = call double @llvm.fmuladd.f64(double %19, double %20, double %23)
  %25 = fcmp ult double %24, 4.000000e+00
  br i1 %25, label %26, label %.split.loop.exit12

26:                                               ; preds = %10
  %27 = add nuw nsw i64 %.01115, 1
  %28 = load i32, ptr %1, align 4, !tbaa !5
  %29 = zext i32 %28 to i64
  %.not = icmp ult i64 %27, %29
  br i1 %.not, label %10, label %.split.loop.exit, !llvm.loop !9

.split.loop.exit12:                               ; preds = %10
  %30 = trunc nuw i64 %.01115 to i32
  br label %.split.loop.exit

.split.loop.exit:                                 ; preds = %26, %2, %.split.loop.exit12
  %spec.select = phi i32 [ %30, %.split.loop.exit12 ], [ 0, %2 ], [ 0, %26 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #4
  ret i32 %spec.select
}

declare { double, double } @_ZN7ComplexmlERKS_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1

declare { double, double } @_ZN7ComplexplERKS_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZN7ComplexaSERKS_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1

declare noundef double @_ZN7Complex2reEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1

declare noundef double @_ZN7Complex2imEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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" }
attributes #3 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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