; LLVM IR from cornell-zhang/rosetta :: digit-recognition/src/sw/digitrec_sw.ll (Z10update_knnPKyS0_PiS1_i)

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z8popcounty(i64 noundef) #0

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

17:                                               ; preds = %37, %5
  %18 = load i32, ptr %12, align 4, !tbaa !9
  %19 = icmp slt i32 %18, 4
  br i1 %19, label %21, label %20

20:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %40

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = load i32, ptr %12, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i64, ptr %22, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !11
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load i32, ptr %12, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i64, ptr %27, i64 %29
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = xor i64 %26, %31
  store i64 %32, ptr %13, align 8, !tbaa !11
  %33 = load i64, ptr %13, align 8, !tbaa !11
  %34 = call noundef i32 @_Z8popcounty(i64 noundef %33)
  %35 = load i32, ptr %11, align 4, !tbaa !9
  %36 = add nsw i32 %35, %34
  store i32 %36, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %37

37:                                               ; preds = %21
  %38 = load i32, ptr %12, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %12, align 4, !tbaa !9
  br label %17, !llvm.loop !13

40:                                               ; preds = %20
  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
  store i32 4, ptr %15, align 4, !tbaa !9
  br label %41

41:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #2
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %42

42:                                               ; preds = %62, %41
  %43 = load i32, ptr %16, align 4, !tbaa !9
  %44 = icmp slt i32 %43, 3
  br i1 %44, label %46, label %45

45:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #2
  br label %65

46:                                               ; preds = %42
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = load i32, ptr %16, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i32, ptr %47, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !9
  %52 = load i32, ptr %14, align 4, !tbaa !9
  %53 = icmp sgt i32 %51, %52
  br i1 %53, label %54, label %61

54:                                               ; preds = %46
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = load i32, ptr %16, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i32, ptr %55, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !9
  store i32 %59, ptr %14, align 4, !tbaa !9
  %60 = load i32, ptr %16, align 4, !tbaa !9
  store i32 %60, ptr %15, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %54, %46
  br label %62

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

65:                                               ; preds = %45
  %66 = load i32, ptr %11, align 4, !tbaa !9
  %67 = load i32, ptr %14, align 4, !tbaa !9
  %68 = icmp slt i32 %66, %67
  br i1 %68, label %69, label %80

69:                                               ; preds = %65
  %70 = load i32, ptr %11, align 4, !tbaa !9
  %71 = load ptr, ptr %8, align 8, !tbaa !5
  %72 = load i32, ptr %15, align 4, !tbaa !9
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds i32, ptr %71, i64 %73
  store i32 %70, ptr %74, align 4, !tbaa !9
  %75 = load i32, ptr %10, align 4, !tbaa !9
  %76 = load ptr, ptr %9, align 8, !tbaa !5
  %77 = load i32, ptr %15, align 4, !tbaa !9
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds i32, ptr %76, i64 %78
  store i32 %75, ptr %79, align 4, !tbaa !9
  br label %80

80:                                               ; preds = %69, %65
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  ret void
}

; 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

attributes #0 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z8popcounty(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z10update_knnPKyS0_PiS1_i(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, ptr nocapture noundef %2, ptr nocapture noundef writeonly %3, i32 noundef %4) local_unnamed_addr #0 {
.preheader:
  %5 = load i64, ptr %1, align 8, !tbaa !5
  %6 = load i64, ptr %0, align 8, !tbaa !5
  %7 = xor i64 %6, %5
  %8 = tail call noundef i32 @_Z8popcounty(i64 noundef %7)
  %9 = getelementptr inbounds i8, ptr %1, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = xor i64 %12, %10
  %14 = tail call noundef i32 @_Z8popcounty(i64 noundef %13)
  %15 = getelementptr inbounds i8, ptr %1, i64 16
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = xor i64 %18, %16
  %20 = tail call noundef i32 @_Z8popcounty(i64 noundef %19)
  %21 = getelementptr inbounds i8, ptr %1, i64 24
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %0, i64 24
  %24 = load i64, ptr %23, align 8, !tbaa !5
  %25 = xor i64 %24, %22
  %26 = tail call noundef i32 @_Z8popcounty(i64 noundef %25)
  %27 = load i32, ptr %2, align 4, !tbaa !9
  %spec.select = tail call i32 @llvm.smax.i32(i32 %27, i32 0)
  %28 = getelementptr inbounds i8, ptr %2, i64 4
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %spec.select.1 = tail call i32 @llvm.smax.i32(i32 %29, i32 %spec.select)
  %30 = getelementptr inbounds i8, ptr %2, i64 8
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %spec.select.2 = tail call i32 @llvm.smax.i32(i32 %31, i32 %spec.select.1)
  %32 = add nsw i32 %14, %8
  %33 = add nsw i32 %20, %32
  %34 = add nsw i32 %26, %33
  %35 = icmp slt i32 %34, %spec.select.2
  br i1 %35, label %36, label %42

36:                                               ; preds = %.preheader
  %37 = icmp sgt i32 %31, %spec.select.1
  %38 = icmp sgt i32 %29, %spec.select
  %39 = icmp sgt i32 %27, 0
  %spec.select29 = select i1 %39, i64 0, i64 4
  %spec.select29.1 = select i1 %38, i64 1, i64 %spec.select29
  %spec.select29.2 = select i1 %37, i64 2, i64 %spec.select29.1
  %40 = getelementptr inbounds i32, ptr %2, i64 %spec.select29.2
  store i32 %34, ptr %40, align 4, !tbaa !9
  %41 = getelementptr inbounds i32, ptr %3, i64 %spec.select29.2
  store i32 %4, ptr %41, align 4, !tbaa !9
  br label %42

42:                                               ; preds = %36, %.preheader
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

attributes #0 = { mustprogress 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 speculatable willreturn memory(none) }

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass