; LLVM IR from attractivechaos/klib :: test (kmin_test)

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

@n_evals = external hidden global i32, align 4
@.str = external hidden unnamed_addr constant [26 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local double @f_Chebyquad(i32 noundef, ptr noundef, ptr noundef) #0

; 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

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca [20 x double], align 16
  %3 = alloca double, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 160, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %6 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  store i32 2, ptr %4, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %36, %0
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = icmp sle i32 %8, 8
  br i1 %9, label %10, label %39

10:                                               ; preds = %7
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %25, %10
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = icmp ne i32 %12, %13
  br i1 %14, label %15, label %28

15:                                               ; preds = %11
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  %18 = sitofp i32 %17 to double
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = sitofp i32 %19 to double
  %21 = fdiv double %18, %20
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [20 x double], ptr %2, i64 0, i64 %23
  store double %21, ptr %24, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %15
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %5, align 4, !tbaa !5
  br label %11, !llvm.loop !11

28:                                               ; preds = %11
  store i32 0, ptr @n_evals, align 4, !tbaa !5
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = getelementptr inbounds [20 x double], ptr %2, i64 0, i64 0
  %31 = call double @kmin_hj(ptr noundef @f_Chebyquad, i32 noundef %29, ptr noundef %30, ptr noundef null, double noundef 5.000000e-01, double noundef 0x3E7AD7F29ABCAF48, i32 noundef 50000)
  store double %31, ptr %3, align 8, !tbaa !9
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = load double, ptr %3, align 8, !tbaa !9
  %34 = load i32, ptr @n_evals, align 4, !tbaa !5
  %35 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %32, double noundef %33, i32 noundef %34)
  br label %36

36:                                               ; preds = %28
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = add nsw i32 %37, 2
  store i32 %38, ptr %4, align 4, !tbaa !5
  br label %7, !llvm.loop !13

39:                                               ; preds = %7
  %40 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 160, ptr %2) #3
  ret i32 0
}

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

declare double @kmin_hj(ptr noundef, i32 noundef, ptr noundef, ptr noundef, double noundef, double noundef, i32 noundef) #2

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 }

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@n_evals = external hidden local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [26 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local double @f_Chebyquad(i32 noundef, ptr noundef, ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
.preheader:
  %0 = alloca [20 x double], align 16
  call void @llvm.lifetime.start.p0(i64 160, ptr nonnull %0) #4
  %1 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  store <2 x double> <double 5.000000e-01, double 1.000000e+00>, ptr %0, align 16, !tbaa !5
  store i32 0, ptr @n_evals, align 4, !tbaa !9
  %2 = call double @kmin_hj(ptr noundef nonnull @f_Chebyquad, i32 noundef 2, ptr noundef nonnull %0, ptr noundef null, double noundef 5.000000e-01, double noundef 0x3E7AD7F29ABCAF48, i32 noundef 50000) #4
  %3 = load i32, ptr @n_evals, align 4, !tbaa !9
  %4 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef 2, double noundef %2, i32 noundef %3)
  store <2 x double> <double 2.500000e-01, double 5.000000e-01>, ptr %0, align 16, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  store <2 x double> <double 7.500000e-01, double 1.000000e+00>, ptr %5, align 16, !tbaa !5
  store i32 0, ptr @n_evals, align 4, !tbaa !9
  %6 = call double @kmin_hj(ptr noundef nonnull @f_Chebyquad, i32 noundef 4, ptr noundef nonnull %0, ptr noundef null, double noundef 5.000000e-01, double noundef 0x3E7AD7F29ABCAF48, i32 noundef 50000) #4
  %7 = load i32, ptr @n_evals, align 4, !tbaa !9
  %8 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef 4, double noundef %6, i32 noundef %7)
  store <2 x double> <double 0x3FC5555555555555, double 0x3FD5555555555555>, ptr %0, align 16, !tbaa !5
  store <2 x double> <double 5.000000e-01, double 0x3FE5555555555555>, ptr %5, align 16, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 32
  store <2 x double> <double 0x3FEAAAAAAAAAAAAB, double 1.000000e+00>, ptr %9, align 16, !tbaa !5
  store i32 0, ptr @n_evals, align 4, !tbaa !9
  %10 = call double @kmin_hj(ptr noundef nonnull @f_Chebyquad, i32 noundef 6, ptr noundef nonnull %0, ptr noundef null, double noundef 5.000000e-01, double noundef 0x3E7AD7F29ABCAF48, i32 noundef 50000) #4
  %11 = load i32, ptr @n_evals, align 4, !tbaa !9
  %12 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef 6, double noundef %10, i32 noundef %11)
  store <2 x double> <double 1.250000e-01, double 2.500000e-01>, ptr %0, align 16, !tbaa !5
  store <2 x double> <double 3.750000e-01, double 5.000000e-01>, ptr %5, align 16, !tbaa !5
  store <2 x double> <double 6.250000e-01, double 7.500000e-01>, ptr %9, align 16, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %0, i64 48
  store <2 x double> <double 8.750000e-01, double 1.000000e+00>, ptr %13, align 16, !tbaa !5
  store i32 0, ptr @n_evals, align 4, !tbaa !9
  %14 = call double @kmin_hj(ptr noundef nonnull @f_Chebyquad, i32 noundef 8, ptr noundef nonnull %0, ptr noundef null, double noundef 5.000000e-01, double noundef 0x3E7AD7F29ABCAF48, i32 noundef 50000) #4
  %15 = load i32, ptr @n_evals, align 4, !tbaa !9
  %16 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef 8, double noundef %14, i32 noundef %15)
  %17 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  call void @llvm.lifetime.end.p0(i64 160, ptr nonnull %0) #4
  ret i32 0
}

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

declare double @kmin_hj(ptr noundef, i32 noundef, ptr noundef, ptr noundef, double noundef, double noundef, i32 noundef) local_unnamed_addr #3

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { "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}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass