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

@class_count = external hidden global ptr, align 8

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

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @update_class_score(ptr noundef %0, i64 noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !11
  %9 = load i32, ptr %6, align 4, !tbaa !11
  %10 = load i64, ptr %5, align 8, !tbaa !9
  %11 = trunc i64 %10 to i32
  %12 = icmp ule i32 %9, %11
  br i1 %12, label %13, label %53

13:                                               ; preds = %3
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = mul i64 %14, 1
  %16 = call noalias ptr @malloc(i64 noundef %15) #3
  store ptr %16, ptr @class_count, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %17

17:                                               ; preds = %28, %13
  %18 = load i32, ptr %7, align 4, !tbaa !11
  %19 = load i64, ptr %5, align 8, !tbaa !9
  %20 = trunc i64 %19 to i32
  %21 = icmp slt i32 %18, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %31

23:                                               ; preds = %17
  %24 = load ptr, ptr @class_count, align 8, !tbaa !5
  %25 = load i32, ptr %7, align 4, !tbaa !11
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i8, ptr %24, i64 %26
  store i8 0, ptr %27, align 1, !tbaa !13
  br label %28

28:                                               ; preds = %23
  %29 = load i32, ptr %7, align 4, !tbaa !11
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %7, align 4, !tbaa !11
  br label %17, !llvm.loop !14

31:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %32

32:                                               ; preds = %49, %31
  %33 = load i32, ptr %8, align 4, !tbaa !11
  %34 = load i32, ptr %6, align 4, !tbaa !11
  %35 = icmp ult i32 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %52

37:                                               ; preds = %32
  %38 = load ptr, ptr @class_count, align 8, !tbaa !5
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = load i32, ptr %8, align 4, !tbaa !11
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds %struct.Point, ptr %39, i64 %41
  %43 = getelementptr inbounds %struct.Point, ptr %42, i32 0, i32 2
  %44 = load i8, ptr %43, align 4, !tbaa !16
  %45 = sext i8 %44 to i64
  %46 = getelementptr inbounds i8, ptr %38, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !13
  %48 = add i8 %47, 1
  store i8 %48, ptr %46, align 1, !tbaa !13
  br label %49

49:                                               ; preds = %37
  %50 = load i32, ptr %8, align 4, !tbaa !11
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %8, align 4, !tbaa !11
  br label %32, !llvm.loop !19

52:                                               ; preds = %36
  br label %53

53:                                               ; preds = %52, %3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 = { 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 #3 = { nounwind allocsize(0) }
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 = !{!"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 = distinct !{!19, !15}


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

@class_count = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind uwtable
define dso_local void @update_class_score(ptr nocapture noundef readonly %0, i64 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = trunc i64 %1 to i32
  %.not = icmp ult i32 %4, %2
  br i1 %.not, label %.loopexit, label %5

5:                                                ; preds = %3
  %6 = tail call noalias ptr @malloc(i64 noundef %1) #2
  store ptr %6, ptr @class_count, align 8, !tbaa !5
  %7 = icmp sgt i32 %4, 0
  br i1 %7, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %5
  %wide.trip.count = and i64 %1, 2147483647
  %8 = add nsw i64 %wide.trip.count, -1
  %xtraiter = and i64 %1, 3
  %9 = icmp ult i64 %8, 3
  br i1 %9, label %.preheader.loopexit.unr-lcssa, label %.lr.ph.preheader.new

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

.preheader.loopexit.unr-lcssa:                    ; preds = %.lr.ph, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.preheader.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %.preheader.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.preheader.loopexit.unr-lcssa ]
  %10 = load ptr, ptr @class_count, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %10, i64 %indvars.iv.epil
  store i8 0, ptr %11, align 1, !tbaa !9
  %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 %.preheader, label %.lr.ph.epil, !llvm.loop !10

.preheader:                                       ; preds = %.preheader.loopexit.unr-lcssa, %.lr.ph.epil, %5
  %.not17 = icmp eq i32 %2, 0
  br i1 %.not17, label %.loopexit, label %.lr.ph16.preheader

.lr.ph16.preheader:                               ; preds = %.preheader
  %wide.trip.count22 = zext i32 %2 to i64
  %xtraiter24 = and i64 %wide.trip.count22, 1
  %12 = icmp eq i32 %2, 1
  br i1 %12, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph16.preheader.new

.lr.ph16.preheader.new:                           ; preds = %.lr.ph16.preheader
  %unroll_iter27 = and i64 %wide.trip.count22, 4294967294
  br label %.lr.ph16

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %13 = load ptr, ptr @class_count, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 %indvars.iv
  store i8 0, ptr %14, align 1, !tbaa !9
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %15 = load ptr, ptr @class_count, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 %indvars.iv.next
  store i8 0, ptr %16, align 1, !tbaa !9
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %17 = load ptr, ptr @class_count, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 %indvars.iv.next.1
  store i8 0, ptr %18, align 1, !tbaa !9
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %19 = load ptr, ptr @class_count, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i64 %indvars.iv.next.2
  store i8 0, ptr %20, align 1, !tbaa !9
  %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 %.preheader.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !12

.lr.ph16:                                         ; preds = %.lr.ph16, %.lr.ph16.preheader.new
  %indvars.iv19 = phi i64 [ 0, %.lr.ph16.preheader.new ], [ %indvars.iv.next20.1, %.lr.ph16 ]
  %niter28 = phi i64 [ 0, %.lr.ph16.preheader.new ], [ %niter28.next.1, %.lr.ph16 ]
  %21 = load ptr, ptr @class_count, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Point, ptr %0, i64 %indvars.iv19, i32 2
  %23 = load i8, ptr %22, align 4, !tbaa !14
  %24 = sext i8 %23 to i64
  %25 = getelementptr inbounds i8, ptr %21, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = add i8 %26, 1
  store i8 %27, ptr %25, align 1, !tbaa !9
  %indvars.iv.next20 = or disjoint i64 %indvars.iv19, 1
  %28 = load ptr, ptr @class_count, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.Point, ptr %0, i64 %indvars.iv.next20, i32 2
  %30 = load i8, ptr %29, align 4, !tbaa !14
  %31 = sext i8 %30 to i64
  %32 = getelementptr inbounds i8, ptr %28, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !9
  %34 = add i8 %33, 1
  store i8 %34, ptr %32, align 1, !tbaa !9
  %indvars.iv.next20.1 = add nuw nsw i64 %indvars.iv19, 2
  %niter28.next.1 = add i64 %niter28, 2
  %niter28.ncmp.1 = icmp eq i64 %niter28.next.1, %unroll_iter27
  br i1 %niter28.ncmp.1, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph16, !llvm.loop !17

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph16, %.lr.ph16.preheader
  %indvars.iv19.unr = phi i64 [ 0, %.lr.ph16.preheader ], [ %indvars.iv.next20.1, %.lr.ph16 ]
  %lcmp.mod26.not = icmp eq i64 %xtraiter24, 0
  br i1 %lcmp.mod26.not, label %.loopexit, label %.lr.ph16.epil

.lr.ph16.epil:                                    ; preds = %.loopexit.loopexit.unr-lcssa
  %35 = load ptr, ptr @class_count, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.Point, ptr %0, i64 %indvars.iv19.unr, i32 2
  %37 = load i8, ptr %36, align 4, !tbaa !14
  %38 = sext i8 %37 to i64
  %39 = getelementptr inbounds i8, ptr %35, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !9
  %41 = add i8 %40, 1
  store i8 %41, ptr %39, align 1, !tbaa !9
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph16.epil, %.loopexit.loopexit.unr-lcssa, %.preheader, %3
  ret void
}

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #1 = { nofree 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 = { nounwind allocsize(0) }

!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"}
!14 = !{!15, !7, i64 8}
!15 = !{!"", !16, i64 0, !16, i64 4, !7, i64 8}
!16 = !{!"float", !7, i64 0}
!17 = distinct !{!17, !13}


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