; LLVM IR from ermig1979/Simd :: src/Simd/SimdBaseHistogram.ll (ZN4Simd4Base16NormalizedColorsEPKjPh)

; 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: 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: mustprogress nounwind uwtable
define dso_local void @_ZN4Simd4Base16NormalizedColorsEPKjPh(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca [256 x i32], align 16
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1024, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 0, ptr %9, align 8, !tbaa !11
  br label %13

13:                                               ; preds = %43, %2
  %14 = load i64, ptr %9, align 8, !tbaa !11
  %15 = icmp ult i64 %14, 256
  br i1 %15, label %17, label %16

16:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %46

17:                                               ; preds = %13
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %33

20:                                               ; preds = %17
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = load i64, ptr %9, align 8, !tbaa !11
  %23 = getelementptr inbounds i32, ptr %21, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %33

26:                                               ; preds = %20
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = load i64, ptr %9, align 8, !tbaa !11
  %29 = getelementptr inbounds i32, ptr %27, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !9
  store i32 %30, ptr %7, align 4, !tbaa !9
  %31 = load i64, ptr %9, align 8, !tbaa !11
  %32 = trunc i64 %31 to i32
  store i32 %32, ptr %8, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %26, %20, %17
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load i64, ptr %9, align 8, !tbaa !11
  %36 = getelementptr inbounds i32, ptr %34, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = add i32 %38, %37
  store i32 %39, ptr %6, align 4, !tbaa !9
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = load i64, ptr %9, align 8, !tbaa !11
  %42 = getelementptr inbounds [256 x i32], ptr %5, i64 0, i64 %41
  store i32 %40, ptr %42, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %33
  %44 = load i64, ptr %9, align 8, !tbaa !11
  %45 = add i64 %44, 1
  store i64 %45, ptr %9, align 8, !tbaa !11
  br label %13, !llvm.loop !13

46:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %47 = load i32, ptr %6, align 4, !tbaa !9
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = sub i32 %47, %48
  store i32 %49, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %50 = load i32, ptr %6, align 4, !tbaa !9
  %51 = load i32, ptr %7, align 4, !tbaa !9
  %52 = sub i32 %50, %51
  %53 = udiv i32 %52, 2
  store i32 %53, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  store i64 0, ptr %12, align 8, !tbaa !11
  br label %54

54:                                               ; preds = %88, %46
  %55 = load i64, ptr %12, align 8, !tbaa !11
  %56 = icmp ult i64 %55, 256
  br i1 %56, label %58, label %57

57:                                               ; preds = %54
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  br label %91

58:                                               ; preds = %54
  %59 = load i64, ptr %12, align 8, !tbaa !11
  %60 = load i32, ptr %8, align 4, !tbaa !9
  %61 = zext i32 %60 to i64
  %62 = icmp ult i64 %59, %61
  br i1 %62, label %63, label %64

63:                                               ; preds = %58
  br label %82

64:                                               ; preds = %58
  %65 = load i32, ptr %10, align 4, !tbaa !9
  %66 = icmp ne i32 %65, 0
  br i1 %66, label %67, label %78

67:                                               ; preds = %64
  %68 = load i64, ptr %12, align 8, !tbaa !11
  %69 = getelementptr inbounds [256 x i32], ptr %5, i64 0, i64 %68
  %70 = load i32, ptr %69, align 4, !tbaa !9
  %71 = load i32, ptr %7, align 4, !tbaa !9
  %72 = sub i32 %70, %71
  %73 = mul i32 255, %72
  %74 = load i32, ptr %11, align 4, !tbaa !9
  %75 = add i32 %73, %74
  %76 = load i32, ptr %10, align 4, !tbaa !9
  %77 = udiv i32 %75, %76
  br label %80

78:                                               ; preds = %64
  %79 = load i32, ptr %8, align 4, !tbaa !9
  br label %80

80:                                               ; preds = %78, %67
  %81 = phi i32 [ %77, %67 ], [ %79, %78 ]
  br label %82

82:                                               ; preds = %80, %63
  %83 = phi i32 [ 0, %63 ], [ %81, %80 ]
  %84 = trunc i32 %83 to i8
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  %86 = load i64, ptr %12, align 8, !tbaa !11
  %87 = getelementptr inbounds i8, ptr %85, i64 %86
  store i8 %84, ptr %87, align 1, !tbaa !15
  br label %88

88:                                               ; preds = %82
  %89 = load i64, ptr %12, align 8, !tbaa !11
  %90 = add i64 %89, 1
  store i64 %90, ptr %12, align 8, !tbaa !11
  br label %54, !llvm.loop !16

91:                                               ; preds = %57
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 1024, ptr %5) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @_ZN4Simd4Base16NormalizedColorsEPKjPh(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  %3 = alloca [256 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %3) #2
  br label %._crit_edge

4:                                                ; preds = %._crit_edge
  %5 = sub i32 %24, %.1.1
  %6 = lshr i32 %5, 1
  %7 = zext i32 %.132.1 to i64
  %.not = icmp eq i32 %24, %.1.1
  %.not.fr = freeze i1 %.not
  br i1 %.not.fr, label %vector.ph, label %.split

vector.ph:                                        ; preds = %4
  %8 = trunc i32 %.132.1 to i8
  %broadcast.splatinsert = insertelement <16 x i64> poison, i64 %7, i64 0
  %broadcast.splat = shufflevector <16 x i64> %broadcast.splatinsert, <16 x i64> poison, <16 x i32> zeroinitializer
  %broadcast.splatinsert50 = insertelement <16 x i8> poison, i8 %8, i64 0
  %broadcast.splat51 = shufflevector <16 x i8> %broadcast.splatinsert50, <16 x i8> poison, <16 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <16 x i64> [ <i64 0, i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10, i64 11, i64 12, i64 13, i64 14, i64 15>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %9 = icmp ult <16 x i64> %vec.ind, %broadcast.splat
  %10 = select <16 x i1> %9, <16 x i8> zeroinitializer, <16 x i8> %broadcast.splat51
  %11 = getelementptr inbounds i8, ptr %1, i64 %index
  store <16 x i8> %10, ptr %11, align 1, !tbaa !5
  %index.next = add nuw i64 %index, 16
  %vec.ind.next = add <16 x i64> %vec.ind, <i64 16, i64 16, i64 16, i64 16, i64 16, i64 16, i64 16, i64 16, i64 16, i64 16, i64 16, i64 16, i64 16, i64 16, i64 16, i64 16>
  %12 = icmp eq i64 %index.next, 256
  br i1 %12, label %.split43.us, label %vector.body, !llvm.loop !8

._crit_edge:                                      ; preds = %._crit_edge, %2
  %.02840 = phi i32 [ 0, %2 ], [ %24, %._crit_edge ]
  %.02939 = phi i32 [ 0, %2 ], [ %.1.1, %._crit_edge ]
  %.03038 = phi i64 [ 0, %2 ], [ %26, %._crit_edge ]
  %.03137 = phi i32 [ 0, %2 ], [ %.132.1, %._crit_edge ]
  %13 = icmp eq i32 %.02840, 0
  %14 = getelementptr inbounds i32, ptr %0, i64 %.03038
  %15 = load i32, ptr %14, align 4, !tbaa !12
  %.not35 = icmp eq i32 %15, 0
  %16 = trunc nuw nsw i64 %.03038 to i32
  %spec.select = select i1 %.not35, i32 %.03137, i32 %16
  %spec.select36 = select i1 %.not35, i32 %.02939, i32 %15
  %.132 = select i1 %13, i32 %spec.select, i32 %.03137
  %.1 = select i1 %13, i32 %spec.select36, i32 %.02939
  %17 = add i32 %15, %.02840
  %18 = getelementptr inbounds [256 x i32], ptr %3, i64 0, i64 %.03038
  store i32 %17, ptr %18, align 8, !tbaa !12
  %19 = or disjoint i64 %.03038, 1
  %20 = icmp eq i32 %17, 0
  %21 = getelementptr inbounds i32, ptr %0, i64 %19
  %22 = load i32, ptr %21, align 4, !tbaa !12
  %.not35.1 = icmp eq i32 %22, 0
  %23 = trunc nuw nsw i64 %19 to i32
  %spec.select.1 = select i1 %.not35.1, i32 %.132, i32 %23
  %spec.select36.1 = select i1 %.not35.1, i32 %.1, i32 %22
  %.132.1 = select i1 %20, i32 %spec.select.1, i32 %.132
  %.1.1 = select i1 %20, i32 %spec.select36.1, i32 %.1
  %24 = add i32 %22, %17
  %25 = getelementptr inbounds [256 x i32], ptr %3, i64 0, i64 %19
  store i32 %24, ptr %25, align 4, !tbaa !12
  %26 = add nuw nsw i64 %.03038, 2
  %exitcond.not.1 = icmp eq i64 %26, 256
  br i1 %exitcond.not.1, label %4, label %._crit_edge, !llvm.loop !14

.split43.us:                                      ; preds = %35, %vector.body
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %3) #2
  ret void

.split:                                           ; preds = %4, %35
  %.041 = phi i64 [ %39, %35 ], [ 0, %4 ]
  %27 = icmp ult i64 %.041, %7
  br i1 %27, label %35, label %28

28:                                               ; preds = %.split
  %29 = getelementptr inbounds [256 x i32], ptr %3, i64 0, i64 %.041
  %30 = load i32, ptr %29, align 4, !tbaa !12
  %31 = sub i32 %30, %.1.1
  %32 = mul i32 %31, 255
  %33 = add i32 %32, %6
  %34 = udiv i32 %33, %5
  br label %35

35:                                               ; preds = %28, %.split
  %36 = phi i32 [ 0, %.split ], [ %34, %28 ]
  %37 = trunc i32 %36 to i8
  %38 = getelementptr inbounds i8, ptr %1, i64 %.041
  store i8 %37, ptr %38, align 1, !tbaa !5
  %39 = add nuw nsw i64 %.041, 1
  %exitcond46.not = icmp eq i64 %39, 256
  br i1 %exitcond46.not, label %.split43.us, label %.split, !llvm.loop !15
}

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9, !10, !11}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !6, i64 0}
!14 = distinct !{!14, !9}
!15 = distinct !{!15, !9}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass