; LLVM IR from radualexandrub/Optimization-Algorithms-Find-local-minimum :: 1D_DerivativeFree_Algorithms (grid_search)

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

@logfile_grid = external dso_local global ptr, align 8
@.str.9 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [13 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local double @grid_search(ptr noundef %0, double noundef %1, double noundef %2, double noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca double, align 8
  %7 = alloca double, align 8
  %8 = alloca double, align 8
  %9 = alloca double, align 8
  %10 = alloca double, align 8
  %11 = alloca double, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store double %1, ptr %6, align 8, !tbaa !9
  store double %2, ptr %7, align 8, !tbaa !9
  store double %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = load double, ptr %6, align 8, !tbaa !9
  %15 = call double %13(double noundef %14)
  store double %15, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !11
  %16 = load double, ptr %6, align 8, !tbaa !9
  store double %16, ptr %10, align 8, !tbaa !9
  br label %17

17:                                               ; preds = %45, %4
  %18 = load double, ptr %10, align 8, !tbaa !9
  %19 = load double, ptr %7, align 8, !tbaa !9
  %20 = fcmp olt double %18, %19
  br i1 %20, label %21, label %49

21:                                               ; preds = %17
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = load double, ptr %10, align 8, !tbaa !9
  %24 = call double %22(double noundef %23)
  %25 = load double, ptr %9, align 8, !tbaa !9
  %26 = fcmp olt double %24, %25
  br i1 %26, label %27, label %32

27:                                               ; preds = %21
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load double, ptr %10, align 8, !tbaa !9
  %30 = call double %28(double noundef %29)
  store double %30, ptr %9, align 8, !tbaa !9
  %31 = load double, ptr %10, align 8, !tbaa !9
  store double %31, ptr %11, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %27, %21
  %33 = load ptr, ptr @logfile_grid, align 8, !tbaa !5
  %34 = load double, ptr %10, align 8, !tbaa !9
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = load double, ptr %10, align 8, !tbaa !9
  %37 = call double %35(double noundef %36)
  %38 = load double, ptr %11, align 8, !tbaa !9
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = load double, ptr %11, align 8, !tbaa !9
  %41 = call double %39(double noundef %40)
  %42 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %33, ptr noundef @.str.9, double noundef %34, double noundef %37, double noundef %38, double noundef %41) #4
  %43 = load i32, ptr %12, align 4, !tbaa !11
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %12, align 4, !tbaa !11
  br label %45

45:                                               ; preds = %32
  %46 = load double, ptr %8, align 8, !tbaa !9
  %47 = load double, ptr %10, align 8, !tbaa !9
  %48 = fadd double %47, %46
  store double %48, ptr %10, align 8, !tbaa !9
  br label %17, !llvm.loop !13

49:                                               ; preds = %17
  %50 = load i32, ptr %12, align 4, !tbaa !11
  %51 = call i32 (ptr, ...) @printf(ptr noundef @.str.10, i32 noundef %50)
  %52 = load double, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  ret double %52
}

declare i32 @printf(ptr noundef, ...) #2

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

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #3

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

@logfile_grid = external dso_local local_unnamed_addr global ptr, align 8
@.str.9 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local double @grid_search(ptr nocapture noundef readonly %0, double noundef %1, double noundef %2, double noundef %3) local_unnamed_addr #0 {
  %5 = tail call double %0(double noundef %1) #2
  %6 = fcmp olt double %1, %2
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4, %11
  %.028 = phi i32 [ %16, %11 ], [ 0, %4 ]
  %.02127 = phi double [ %.1, %11 ], [ undef, %4 ]
  %.02226 = phi double [ %17, %11 ], [ %1, %4 ]
  %.02325 = phi double [ %.124, %11 ], [ %5, %4 ]
  %7 = tail call double %0(double noundef %.02226) #2
  %8 = fcmp olt double %7, %.02325
  br i1 %8, label %9, label %11

9:                                                ; preds = %.lr.ph
  %10 = tail call double %0(double noundef %.02226) #2
  br label %11

11:                                               ; preds = %9, %.lr.ph
  %.124 = phi double [ %10, %9 ], [ %.02325, %.lr.ph ]
  %.1 = phi double [ %.02226, %9 ], [ %.02127, %.lr.ph ]
  %12 = load ptr, ptr @logfile_grid, align 8, !tbaa !5
  %13 = tail call double %0(double noundef %.02226) #2
  %14 = tail call double %0(double noundef %.1) #2
  %15 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef nonnull @.str.9, double noundef %.02226, double noundef %13, double noundef %.1, double noundef %14) #2
  %16 = add nuw nsw i32 %.028, 1
  %17 = fadd double %.02226, %3
  %18 = fcmp olt double %17, %2
  br i1 %18, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %11, %4
  %.021.lcssa = phi double [ undef, %4 ], [ %.1, %11 ]
  %.0.lcssa = phi i32 [ 0, %4 ], [ %16, %11 ]
  %19 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.10, i32 noundef %.0.lcssa)
  ret double %.021.lcssa
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { 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 = { nofree 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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