; LLVM IR from FOSS-UCSC/FOSSALGO :: algorithms/ar-knn/c (knn)

; 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 { float, float, i8 }

@distance = external hidden global ptr, align 8
@class_count = external hidden global ptr, align 8

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: nounwind uwtable
declare dso_local void @assign_classes(ptr noundef, ptr noundef, i64 noundef) #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @update_distance(ptr noundef, i64 noundef, <2 x float>, i8) #3

; Function Attrs: nounwind uwtable
declare dso_local void @sort_distance(ptr noalias noundef, ptr noalias noundef, i64 noundef) #1

; Function Attrs: nounwind
declare void @free(ptr noundef) #4

; Function Attrs: nounwind uwtable
declare dso_local void @update_class_score(ptr noundef, i64 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local { <2 x float>, i8 } @knn_classifier(ptr noundef %0, ptr noundef %1, i64 noundef %2, <2 x float> %3, i8 %4, i32 noundef %5) #3 {
  %7 = alloca %struct.Point, align 4
  %8 = alloca %struct.Point, align 4
  %9 = alloca { <2 x float>, i8 }, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  %14 = alloca { <2 x float>, i8 }, align 4
  %15 = alloca i8, align 1
  %16 = alloca i8, align 1
  %17 = alloca i32, align 4
  %18 = alloca { <2 x float>, i8 }, align 8
  %19 = getelementptr inbounds { <2 x float>, i8 }, ptr %9, i32 0, i32 0
  store <2 x float> %3, ptr %19, align 4
  %20 = getelementptr inbounds { <2 x float>, i8 }, ptr %9, i32 0, i32 1
  store i8 %4, ptr %20, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %8, ptr align 4 %9, i64 12, i1 false)
  store ptr %0, ptr %10, align 8, !tbaa !5
  store ptr %1, ptr %11, align 8, !tbaa !5
  store i64 %2, ptr %12, align 8, !tbaa !9
  store i32 %5, ptr %13, align 4, !tbaa !11
  %21 = load ptr, ptr %10, align 8, !tbaa !5
  %22 = load ptr, ptr %11, align 8, !tbaa !5
  %23 = load i64, ptr %12, align 8, !tbaa !9
  call void @assign_classes(ptr noundef %21, ptr noundef %22, i64 noundef %23)
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = load i64, ptr %12, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %14, ptr align 4 %8, i64 12, i1 false)
  %26 = getelementptr inbounds { <2 x float>, i8 }, ptr %14, i32 0, i32 0
  %27 = load <2 x float>, ptr %26, align 4
  %28 = getelementptr inbounds { <2 x float>, i8 }, ptr %14, i32 0, i32 1
  %29 = load i8, ptr %28, align 4
  call void @update_distance(ptr noundef %24, i64 noundef %25, <2 x float> %27, i8 %29)
  %30 = load ptr, ptr @distance, align 8, !tbaa !5
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = load i64, ptr %12, align 8, !tbaa !9
  call void @sort_distance(ptr noundef %30, ptr noundef %31, i64 noundef %32)
  %33 = load ptr, ptr %10, align 8, !tbaa !5
  %34 = load i64, ptr %12, align 8, !tbaa !9
  %35 = load i32, ptr %13, align 4, !tbaa !11
  call void @update_class_score(ptr noundef %33, i64 noundef %34, i32 noundef %35)
  call void @llvm.lifetime.start.p0(i64 1, ptr %15) #5
  store i8 -1, ptr %15, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #5
  store i8 0, ptr %16, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #5
  store i32 0, ptr %17, align 4, !tbaa !11
  br label %36

36:                                               ; preds = %61, %6
  %37 = load i32, ptr %17, align 4, !tbaa !11
  %38 = load i64, ptr %12, align 8, !tbaa !9
  %39 = trunc i64 %38 to i32
  %40 = icmp slt i32 %37, %39
  br i1 %40, label %42, label %41

41:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #5
  br label %64

42:                                               ; preds = %36
  %43 = load ptr, ptr @class_count, align 8, !tbaa !5
  %44 = load i32, ptr %17, align 4, !tbaa !11
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i8, ptr %43, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !13
  %48 = sext i8 %47 to i32
  %49 = load i8, ptr %15, align 1, !tbaa !13
  %50 = sext i8 %49 to i32
  %51 = icmp sgt i32 %48, %50
  br i1 %51, label %52, label %60

52:                                               ; preds = %42
  %53 = load ptr, ptr @class_count, align 8, !tbaa !5
  %54 = load i32, ptr %17, align 4, !tbaa !11
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i8, ptr %53, i64 %55
  %57 = load i8, ptr %56, align 1, !tbaa !13
  store i8 %57, ptr %15, align 1, !tbaa !13
  %58 = load i32, ptr %17, align 4, !tbaa !11
  %59 = trunc i32 %58 to i8
  store i8 %59, ptr %16, align 1, !tbaa !13
  br label %60

60:                                               ; preds = %52, %42
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %17, align 4, !tbaa !11
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %17, align 4, !tbaa !11
  br label %36, !llvm.loop !14

64:                                               ; preds = %41
  %65 = load ptr, ptr @class_count, align 8, !tbaa !5
  call void @free(ptr noundef %65) #5
  %66 = load i8, ptr %16, align 1, !tbaa !13
  %67 = getelementptr inbounds %struct.Point, ptr %8, i32 0, i32 2
  store i8 %66, ptr %67, align 4, !tbaa !16
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %7, ptr align 4 %8, i64 12, i1 false), !tbaa.struct !19
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %15) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %18, ptr align 4 %7, i64 12, i1 false)
  %68 = load { <2 x float>, i8 }, ptr %18, align 8
  ret { <2 x float>, i8 } %68
}

attributes #0 = { nocallback nofree 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind uwtable "min-legal-vector-width"="64" "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 "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 #5 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !7, i64 8}
!17 = !{!"", !18, i64 0, !18, i64 4, !7, i64 8}
!18 = !{!"float", !7, i64 0}
!19 = !{i64 0, i64 4, !20, i64 4, i64 4, !20, i64 8, i64 1, !13}
!20 = !{!18, !18, i64 0}


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

@distance = external hidden local_unnamed_addr global ptr, align 8
@class_count = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @assign_classes(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @update_distance(ptr noundef, i64 noundef, <2 x float>, i8) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @sort_distance(ptr noalias noundef, ptr noalias noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @update_class_score(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local { <2 x float>, i8 } @knn_classifier(ptr noundef %0, ptr noundef %1, i64 noundef %2, <2 x float> %3, i8 %4, i32 noundef %5) local_unnamed_addr #1 {
  tail call void @assign_classes(ptr noundef %0, ptr noundef %1, i64 noundef %2)
  tail call void @update_distance(ptr noundef %0, i64 noundef %2, <2 x float> %3, i8 %4)
  %7 = load ptr, ptr @distance, align 8, !tbaa !5
  tail call void @sort_distance(ptr noundef %7, ptr noundef %0, i64 noundef %2)
  tail call void @update_class_score(ptr noundef %0, i64 noundef %2, i32 noundef %5)
  %8 = trunc i64 %2 to i32
  %9 = icmp sgt i32 %8, 0
  %.pre = load ptr, ptr @class_count, align 8, !tbaa !5
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %6
  %wide.trip.count = and i64 %2, 2147483647
  %10 = add nsw i64 %wide.trip.count, -1
  %xtraiter = and i64 %2, 3
  %11 = icmp ult i64 %10, 3
  br i1 %11, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = sub nsw i64 %wide.trip.count, %xtraiter
  br label %16

._crit_edge.loopexit.unr-lcssa:                   ; preds = %16, %.lr.ph
  %spec.select.lcssa.ph = phi i8 [ poison, %.lr.ph ], [ %spec.select.3, %16 ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.3, %16 ]
  %.033.unr = phi i8 [ -1, %.lr.ph ], [ %spec.select30.3, %16 ]
  %.02831.unr = phi i8 [ 0, %.lr.ph ], [ %spec.select.3, %16 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.033.epil = phi i8 [ %spec.select30.epil, %.epil.preheader ], [ %.033.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.02831.epil = phi i8 [ %spec.select.epil, %.epil.preheader ], [ %.02831.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %12 = getelementptr inbounds i8, ptr %.pre, i64 %indvars.iv.epil
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %14 = icmp sgt i8 %13, %.033.epil
  %15 = trunc i64 %indvars.iv.epil to i8
  %spec.select.epil = select i1 %14, i8 %15, i8 %.02831.epil
  %spec.select30.epil = tail call i8 @llvm.smax.i8(i8 %13, i8 %.033.epil)
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.epil.preheader, !llvm.loop !10

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader, %6
  %.028.lcssa = phi i8 [ 0, %6 ], [ %spec.select.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %spec.select.epil, %.epil.preheader ]
  tail call void @free(ptr noundef %.pre) #4
  %.fca.0.insert = insertvalue { <2 x float>, i8 } poison, <2 x float> %3, 0
  %.fca.1.insert = insertvalue { <2 x float>, i8 } %.fca.0.insert, i8 %.028.lcssa, 1
  ret { <2 x float>, i8 } %.fca.1.insert

16:                                               ; preds = %16, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.3, %16 ]
  %.033 = phi i8 [ -1, %.lr.ph.new ], [ %spec.select30.3, %16 ]
  %.02831 = phi i8 [ 0, %.lr.ph.new ], [ %spec.select.3, %16 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.3, %16 ]
  %17 = getelementptr inbounds i8, ptr %.pre, i64 %indvars.iv
  %18 = load i8, ptr %17, align 1, !tbaa !9
  %19 = icmp sgt i8 %18, %.033
  %20 = trunc i64 %indvars.iv to i8
  %spec.select = select i1 %19, i8 %20, i8 %.02831
  %spec.select30 = tail call i8 @llvm.smax.i8(i8 %18, i8 %.033)
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %21 = getelementptr inbounds i8, ptr %.pre, i64 %indvars.iv.next
  %22 = load i8, ptr %21, align 1, !tbaa !9
  %23 = icmp sgt i8 %22, %spec.select30
  %24 = trunc i64 %indvars.iv.next to i8
  %spec.select.1 = select i1 %23, i8 %24, i8 %spec.select
  %spec.select30.1 = tail call i8 @llvm.smax.i8(i8 %22, i8 %spec.select30)
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %25 = getelementptr inbounds i8, ptr %.pre, i64 %indvars.iv.next.1
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = icmp sgt i8 %26, %spec.select30.1
  %28 = trunc i64 %indvars.iv.next.1 to i8
  %spec.select.2 = select i1 %27, i8 %28, i8 %spec.select.1
  %spec.select30.2 = tail call i8 @llvm.smax.i8(i8 %26, i8 %spec.select30.1)
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %29 = getelementptr inbounds i8, ptr %.pre, i64 %indvars.iv.next.2
  %30 = load i8, ptr %29, align 1, !tbaa !9
  %31 = icmp sgt i8 %30, %spec.select30.2
  %32 = trunc i64 %indvars.iv.next.2 to i8
  %spec.select.3 = select i1 %31, i8 %32, i8 %spec.select.2
  %spec.select30.3 = tail call i8 @llvm.smax.i8(i8 %30, i8 %spec.select30.2)
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %16, !llvm.loop !12
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.smax.i8(i8, i8) #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 = { nounwind uwtable "min-legal-vector-width"="64" "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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.unroll.disable"}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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