; LLVM IR from AlgoGenesis/C :: Greedy Algorithms/Ramer-Douglas-Peucker AlgorithmRamer-Douglas-Peucker Algorithm (ramerDouglasPeucker)

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

%struct.Point = type { double, double }

; Function Attrs: nounwind uwtable
declare dso_local double @perpendicularDistance(double, double, double, double, double, double) #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 void @ramerDouglasPeucker(ptr noundef %0, i32 noundef %1, i32 noundef %2, double noundef %3, ptr noundef %4, ptr noundef %5) #0 {
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca double, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca double, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca double, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store i32 %2, ptr %9, align 4, !tbaa !9
  store double %3, ptr %10, align 8, !tbaa !11
  store ptr %4, ptr %11, align 8, !tbaa !5
  store ptr %5, ptr %12, align 8, !tbaa !5
  %17 = load i32, ptr %9, align 4, !tbaa !9
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = add nsw i32 %18, 1
  %20 = icmp sle i32 %17, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %6
  br label %91

22:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  store double 0.000000e+00, ptr %13, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  store i32 0, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #2
  %23 = load i32, ptr %8, align 4, !tbaa !9
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %15, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %63, %22
  %26 = load i32, ptr %15, align 4, !tbaa !9
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  br label %66

30:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #2
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = load i32, ptr %15, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.Point, ptr %31, i64 %33
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds %struct.Point, ptr %35, i64 %37
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds %struct.Point, ptr %39, i64 %41
  %43 = getelementptr inbounds { double, double }, ptr %34, i32 0, i32 0
  %44 = load double, ptr %43, align 8
  %45 = getelementptr inbounds { double, double }, ptr %34, i32 0, i32 1
  %46 = load double, ptr %45, align 8
  %47 = getelementptr inbounds { double, double }, ptr %38, i32 0, i32 0
  %48 = load double, ptr %47, align 8
  %49 = getelementptr inbounds { double, double }, ptr %38, i32 0, i32 1
  %50 = load double, ptr %49, align 8
  %51 = getelementptr inbounds { double, double }, ptr %42, i32 0, i32 0
  %52 = load double, ptr %51, align 8
  %53 = getelementptr inbounds { double, double }, ptr %42, i32 0, i32 1
  %54 = load double, ptr %53, align 8
  %55 = call double @perpendicularDistance(double %44, double %46, double %48, double %50, double %52, double %54)
  store double %55, ptr %16, align 8, !tbaa !11
  %56 = load double, ptr %16, align 8, !tbaa !11
  %57 = load double, ptr %13, align 8, !tbaa !11
  %58 = fcmp ogt double %56, %57
  br i1 %58, label %59, label %62

59:                                               ; preds = %30
  %60 = load double, ptr %16, align 8, !tbaa !11
  store double %60, ptr %13, align 8, !tbaa !11
  %61 = load i32, ptr %15, align 4, !tbaa !9
  store i32 %61, ptr %14, align 4, !tbaa !9
  br label %62

62:                                               ; preds = %59, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #2
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr %15, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %15, align 4, !tbaa !9
  br label %25, !llvm.loop !13

66:                                               ; preds = %29
  %67 = load double, ptr %13, align 8, !tbaa !11
  %68 = load double, ptr %10, align 8, !tbaa !11
  %69 = fcmp ogt double %67, %68
  br i1 %69, label %70, label %90

70:                                               ; preds = %66
  %71 = load i32, ptr %14, align 4, !tbaa !9
  %72 = load ptr, ptr %11, align 8, !tbaa !5
  %73 = load ptr, ptr %12, align 8, !tbaa !5
  %74 = load i32, ptr %73, align 4, !tbaa !9
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %73, align 4, !tbaa !9
  %76 = sext i32 %74 to i64
  %77 = getelementptr inbounds i32, ptr %72, i64 %76
  store i32 %71, ptr %77, align 4, !tbaa !9
  %78 = load ptr, ptr %7, align 8, !tbaa !5
  %79 = load i32, ptr %8, align 4, !tbaa !9
  %80 = load i32, ptr %14, align 4, !tbaa !9
  %81 = load double, ptr %10, align 8, !tbaa !11
  %82 = load ptr, ptr %11, align 8, !tbaa !5
  %83 = load ptr, ptr %12, align 8, !tbaa !5
  call void @ramerDouglasPeucker(ptr noundef %78, i32 noundef %79, i32 noundef %80, double noundef %81, ptr noundef %82, ptr noundef %83)
  %84 = load ptr, ptr %7, align 8, !tbaa !5
  %85 = load i32, ptr %14, align 4, !tbaa !9
  %86 = load i32, ptr %9, align 4, !tbaa !9
  %87 = load double, ptr %10, align 8, !tbaa !11
  %88 = load ptr, ptr %11, align 8, !tbaa !5
  %89 = load ptr, ptr %12, align 8, !tbaa !5
  call void @ramerDouglasPeucker(ptr noundef %84, i32 noundef %85, i32 noundef %86, double noundef %87, ptr noundef %88, ptr noundef %89)
  br label %90

90:                                               ; preds = %70, %66
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %91

91:                                               ; preds = %90, %21
  ret void
}

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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !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"

%struct.Point = type { double, double }

; Function Attrs: nounwind uwtable
declare dso_local double @perpendicularDistance(double, double, double, double, double, double) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @ramerDouglasPeucker(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, double noundef %3, ptr nocapture noundef writeonly %4, ptr nocapture noundef %5) local_unnamed_addr #0 {
  %7 = add nsw i32 %1, 1
  %.not47 = icmp slt i32 %7, %2
  br i1 %.not47, label %.preheader.lr.ph, label %._crit_edge

.preheader.lr.ph:                                 ; preds = %6
  %8 = sext i32 %2 to i64
  %9 = getelementptr inbounds %struct.Point, ptr %0, i64 %8
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %tailrecurse
  %11 = phi i32 [ %7, %.preheader.lr.ph ], [ %35, %tailrecurse ]
  %.tr3948 = phi i32 [ %1, %.preheader.lr.ph ], [ %.134, %tailrecurse ]
  %12 = sext i32 %.tr3948 to i64
  %13 = getelementptr inbounds %struct.Point, ptr %0, i64 %12
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = sext i32 %11 to i64
  br label %18

16:                                               ; preds = %18
  %17 = fcmp ogt double %.1, %3
  br i1 %17, label %tailrecurse, label %._crit_edge

18:                                               ; preds = %.preheader, %18
  %indvars.iv = phi i64 [ %15, %.preheader ], [ %indvars.iv.next, %18 ]
  %.046 = phi double [ 0.000000e+00, %.preheader ], [ %.1, %18 ]
  %.03344 = phi i32 [ 0, %.preheader ], [ %.134, %18 ]
  %19 = getelementptr inbounds %struct.Point, ptr %0, i64 %indvars.iv
  %20 = load double, ptr %19, align 8
  %21 = getelementptr inbounds i8, ptr %19, i64 8
  %22 = load double, ptr %21, align 8
  %23 = load double, ptr %13, align 8
  %24 = load double, ptr %14, align 8
  %25 = load double, ptr %9, align 8
  %26 = load double, ptr %10, align 8
  %27 = tail call double @perpendicularDistance(double %20, double %22, double %23, double %24, double %25, double %26)
  %28 = fcmp ogt double %27, %.046
  %29 = trunc nsw i64 %indvars.iv to i32
  %.134 = select i1 %28, i32 %29, i32 %.03344
  %.1 = select i1 %28, double %27, double %.046
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %30 = icmp slt i64 %indvars.iv.next, %8
  br i1 %30, label %18, label %16, !llvm.loop !5

tailrecurse:                                      ; preds = %16
  %31 = load i32, ptr %5, align 4, !tbaa !7
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %5, align 4, !tbaa !7
  %33 = sext i32 %31 to i64
  %34 = getelementptr inbounds i32, ptr %4, i64 %33
  store i32 %.134, ptr %34, align 4, !tbaa !7
  tail call void @ramerDouglasPeucker(ptr noundef nonnull %0, i32 noundef %.tr3948, i32 noundef %.134, double noundef %3, ptr noundef %4, ptr noundef nonnull %5)
  %35 = add nsw i32 %.134, 1
  %.not = icmp slt i32 %35, %2
  br i1 %.not, label %.preheader, label %._crit_edge

._crit_edge:                                      ; preds = %tailrecurse, %16, %6
  ret void
}

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

!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"}
!7 = !{!8, !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}


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