; LLVM IR from zchrissirhcz/pixel :: perfs/threshold (threshold)

; 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: nounwind uwtable
define dso_local void @threshold_rgb_naive(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3, i8 noundef zeroext %4, i8 noundef zeroext %5, i8 noundef zeroext %6) #1 {
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i8, align 1
  %13 = alloca i8, align 1
  %14 = alloca i8, align 1
  %15 = alloca i64, align 8
  %16 = alloca i8, align 1
  %17 = alloca i64, align 8
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i64 %1, ptr %9, align 8, !tbaa !9
  store i64 %2, ptr %10, align 8, !tbaa !9
  store ptr %3, ptr %11, align 8, !tbaa !5
  store i8 %4, ptr %12, align 1, !tbaa !11
  store i8 %5, ptr %13, align 1, !tbaa !11
  store i8 %6, ptr %14, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  %18 = load i64, ptr %9, align 8, !tbaa !9
  %19 = load i64, ptr %10, align 8, !tbaa !9
  %20 = mul i64 %18, %19
  store i64 %20, ptr %15, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #3
  store i64 0, ptr %17, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %65, %7
  %22 = load i64, ptr %17, align 8, !tbaa !9
  %23 = load i64, ptr %15, align 8, !tbaa !9
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #3
  br label %68

26:                                               ; preds = %21
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %27, i64 0
  %29 = load i8, ptr %28, align 1, !tbaa !11
  %30 = zext i8 %29 to i32
  %31 = sitofp i32 %30 to double
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 1
  %34 = load i8, ptr %33, align 1, !tbaa !11
  %35 = zext i8 %34 to i32
  %36 = sitofp i32 %35 to double
  %37 = fmul double 5.870000e-01, %36
  %38 = call double @llvm.fmuladd.f64(double 2.990000e-01, double %31, double %37)
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds i8, ptr %39, i64 2
  %41 = load i8, ptr %40, align 1, !tbaa !11
  %42 = zext i8 %41 to i32
  %43 = sitofp i32 %42 to double
  %44 = call double @llvm.fmuladd.f64(double 1.140000e-01, double %43, double %38)
  %45 = fptoui double %44 to i8
  store i8 %45, ptr %16, align 1, !tbaa !11
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %46, i64 3
  store ptr %47, ptr %8, align 8, !tbaa !5
  %48 = load i8, ptr %16, align 1, !tbaa !11
  %49 = zext i8 %48 to i32
  %50 = load i8, ptr %12, align 1, !tbaa !11
  %51 = zext i8 %50 to i32
  %52 = icmp sgt i32 %49, %51
  br i1 %52, label %53, label %56

53:                                               ; preds = %26
  %54 = load i8, ptr %14, align 1, !tbaa !11
  %55 = zext i8 %54 to i32
  br label %59

56:                                               ; preds = %26
  %57 = load i8, ptr %13, align 1, !tbaa !11
  %58 = zext i8 %57 to i32
  br label %59

59:                                               ; preds = %56, %53
  %60 = phi i32 [ %55, %53 ], [ %58, %56 ]
  %61 = trunc i32 %60 to i8
  %62 = load ptr, ptr %11, align 8, !tbaa !5
  store i8 %61, ptr %62, align 1, !tbaa !11
  %63 = load ptr, ptr %11, align 8, !tbaa !5
  %64 = getelementptr inbounds i8, ptr %63, i32 1
  store ptr %64, ptr %11, align 8, !tbaa !5
  br label %65

65:                                               ; preds = %59
  %66 = load i64, ptr %17, align 8, !tbaa !9
  %67 = add i64 %66, 1
  store i64 %67, ptr %17, align 8, !tbaa !9
  br label %21, !llvm.loop !12

68:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #2

attributes #0 = { nocallback nofree nosync 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 speculatable willreturn memory(none) }
attributes #3 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @threshold_rgb_naive(ptr nocapture noundef readonly %0, i64 noundef %1, i64 noundef %2, ptr nocapture noundef writeonly %3, i8 noundef zeroext %4, i8 noundef zeroext %5, i8 noundef zeroext %6) local_unnamed_addr #0 {
  %8 = mul i64 %2, %1
  %.not = icmp eq i64 %8, 0
  br i1 %.not, label %._crit_edge, label %iter.check

iter.check:                                       ; preds = %7
  %min.iters.check = icmp ult i64 %8, 8
  br i1 %min.iters.check, label %.lr.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %iter.check
  %scevgep = getelementptr i8, ptr %3, i64 %8
  %9 = mul i64 %2, %1
  %10 = mul i64 %9, 3
  %scevgep19 = getelementptr i8, ptr %0, i64 %10
  %bound0 = icmp ugt ptr %scevgep19, %3
  %bound1 = icmp ugt ptr %scevgep, %0
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph.preheader, label %vector.main.loop.iter.check

vector.main.loop.iter.check:                      ; preds = %vector.memcheck
  %min.iters.check20 = icmp ult i64 %8, 16
  br i1 %min.iters.check20, label %vec.epilog.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %8, -16
  %broadcast.splatinsert = insertelement <16 x i8> poison, i8 %4, i64 0
  %broadcast.splat = shufflevector <16 x i8> %broadcast.splatinsert, <16 x i8> poison, <16 x i32> zeroinitializer
  %broadcast.splatinsert24 = insertelement <16 x i8> poison, i8 %6, i64 0
  %broadcast.splat25 = shufflevector <16 x i8> %broadcast.splatinsert24, <16 x i8> poison, <16 x i32> zeroinitializer
  %broadcast.splatinsert26 = insertelement <16 x i8> poison, i8 %5, i64 0
  %broadcast.splat27 = shufflevector <16 x i8> %broadcast.splatinsert26, <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 ]
  %offset.idx = mul i64 %index, 3
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %next.gep21 = getelementptr i8, ptr %3, i64 %index
  %wide.vec = load <48 x i8>, ptr %next.gep, align 1, !tbaa !5
  %strided.vec = shufflevector <48 x i8> %wide.vec, <48 x i8> poison, <16 x i32> <i32 0, i32 3, i32 6, i32 9, i32 12, i32 15, i32 18, i32 21, i32 24, i32 27, i32 30, i32 33, i32 36, i32 39, i32 42, i32 45>
  %strided.vec22 = shufflevector <48 x i8> %wide.vec, <48 x i8> poison, <16 x i32> <i32 1, i32 4, i32 7, i32 10, i32 13, i32 16, i32 19, i32 22, i32 25, i32 28, i32 31, i32 34, i32 37, i32 40, i32 43, i32 46>
  %strided.vec23 = shufflevector <48 x i8> %wide.vec, <48 x i8> poison, <16 x i32> <i32 2, i32 5, i32 8, i32 11, i32 14, i32 17, i32 20, i32 23, i32 26, i32 29, i32 32, i32 35, i32 38, i32 41, i32 44, i32 47>
  %11 = uitofp <16 x i8> %strided.vec to <16 x double>
  %12 = uitofp <16 x i8> %strided.vec22 to <16 x double>
  %13 = fmul <16 x double> %12, <double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01>
  %14 = tail call <16 x double> @llvm.fmuladd.v16f64(<16 x double> %11, <16 x double> <double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01>, <16 x double> %13)
  %15 = uitofp <16 x i8> %strided.vec23 to <16 x double>
  %16 = tail call <16 x double> @llvm.fmuladd.v16f64(<16 x double> %15, <16 x double> <double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01>, <16 x double> %14)
  %17 = fptoui <16 x double> %16 to <16 x i8>
  %18 = icmp ult <16 x i8> %broadcast.splat, %17
  %19 = select <16 x i1> %18, <16 x i8> %broadcast.splat25, <16 x i8> %broadcast.splat27
  store <16 x i8> %19, ptr %next.gep21, align 1, !tbaa !5, !alias.scope !8, !noalias !11
  %index.next = add nuw i64 %index, 16
  %20 = icmp eq i64 %index.next, %n.vec
  br i1 %20, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %8, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %vec.epilog.iter.check

vec.epilog.iter.check:                            ; preds = %middle.block
  %ind.end33 = getelementptr i8, ptr %3, i64 %n.vec
  %21 = mul i64 %n.vec, 3
  %ind.end30 = getelementptr i8, ptr %0, i64 %21
  %n.vec.remaining = and i64 %8, 8
  %min.epilog.iters.check.not.not = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check.not.not, label %.lr.ph.preheader, label %vec.epilog.ph

.lr.ph.preheader:                                 ; preds = %vec.epilog.middle.block, %vector.memcheck, %iter.check, %vec.epilog.iter.check
  %.018.ph = phi i64 [ 0, %iter.check ], [ 0, %vector.memcheck ], [ %n.vec, %vec.epilog.iter.check ], [ %n.vec29, %vec.epilog.middle.block ]
  %.01417.ph = phi ptr [ %0, %iter.check ], [ %0, %vector.memcheck ], [ %ind.end30, %vec.epilog.iter.check ], [ %ind.end, %vec.epilog.middle.block ]
  %.01516.ph = phi ptr [ %3, %iter.check ], [ %3, %vector.memcheck ], [ %ind.end33, %vec.epilog.iter.check ], [ %ind.end32, %vec.epilog.middle.block ]
  br label %.lr.ph

vec.epilog.ph:                                    ; preds = %vector.main.loop.iter.check, %vec.epilog.iter.check
  %vec.epilog.resume.val = phi i64 [ %n.vec, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %n.vec29 = and i64 %8, -8
  %22 = mul i64 %n.vec29, 3
  %ind.end = getelementptr i8, ptr %0, i64 %22
  %ind.end32 = getelementptr i8, ptr %3, i64 %n.vec29
  %broadcast.splatinsert43 = insertelement <8 x i8> poison, i8 %4, i64 0
  %broadcast.splat44 = shufflevector <8 x i8> %broadcast.splatinsert43, <8 x i8> poison, <8 x i32> zeroinitializer
  %broadcast.splatinsert45 = insertelement <8 x i8> poison, i8 %6, i64 0
  %broadcast.splat46 = shufflevector <8 x i8> %broadcast.splatinsert45, <8 x i8> poison, <8 x i32> zeroinitializer
  %broadcast.splatinsert47 = insertelement <8 x i8> poison, i8 %5, i64 0
  %broadcast.splat48 = shufflevector <8 x i8> %broadcast.splatinsert47, <8 x i8> poison, <8 x i32> zeroinitializer
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index35 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next49, %vec.epilog.vector.body ]
  %offset.idx36 = mul i64 %index35, 3
  %next.gep37 = getelementptr i8, ptr %0, i64 %offset.idx36
  %next.gep38 = getelementptr i8, ptr %3, i64 %index35
  %wide.vec39 = load <24 x i8>, ptr %next.gep37, align 1, !tbaa !5
  %strided.vec40 = shufflevector <24 x i8> %wide.vec39, <24 x i8> poison, <8 x i32> <i32 0, i32 3, i32 6, i32 9, i32 12, i32 15, i32 18, i32 21>
  %strided.vec41 = shufflevector <24 x i8> %wide.vec39, <24 x i8> poison, <8 x i32> <i32 1, i32 4, i32 7, i32 10, i32 13, i32 16, i32 19, i32 22>
  %strided.vec42 = shufflevector <24 x i8> %wide.vec39, <24 x i8> poison, <8 x i32> <i32 2, i32 5, i32 8, i32 11, i32 14, i32 17, i32 20, i32 23>
  %23 = uitofp <8 x i8> %strided.vec40 to <8 x double>
  %24 = uitofp <8 x i8> %strided.vec41 to <8 x double>
  %25 = fmul <8 x double> %24, <double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01, double 5.870000e-01>
  %26 = tail call <8 x double> @llvm.fmuladd.v8f64(<8 x double> %23, <8 x double> <double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01, double 2.990000e-01>, <8 x double> %25)
  %27 = uitofp <8 x i8> %strided.vec42 to <8 x double>
  %28 = tail call <8 x double> @llvm.fmuladd.v8f64(<8 x double> %27, <8 x double> <double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01, double 1.140000e-01>, <8 x double> %26)
  %29 = fptoui <8 x double> %28 to <8 x i8>
  %30 = icmp ult <8 x i8> %broadcast.splat44, %29
  %31 = select <8 x i1> %30, <8 x i8> %broadcast.splat46, <8 x i8> %broadcast.splat48
  store <8 x i8> %31, ptr %next.gep38, align 1, !tbaa !5, !alias.scope !17, !noalias !20
  %index.next49 = add nuw i64 %index35, 8
  %32 = icmp eq i64 %index.next49, %n.vec29
  br i1 %32, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !22

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %cmp.n50 = icmp eq i64 %8, %n.vec29
  br i1 %cmp.n50, label %._crit_edge, label %.lr.ph.preheader

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %vec.epilog.middle.block, %7
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.018 = phi i64 [ %48, %.lr.ph ], [ %.018.ph, %.lr.ph.preheader ]
  %.01417 = phi ptr [ %45, %.lr.ph ], [ %.01417.ph, %.lr.ph.preheader ]
  %.01516 = phi ptr [ %47, %.lr.ph ], [ %.01516.ph, %.lr.ph.preheader ]
  %33 = load i8, ptr %.01417, align 1, !tbaa !5
  %34 = uitofp i8 %33 to double
  %35 = getelementptr inbounds i8, ptr %.01417, i64 1
  %36 = load i8, ptr %35, align 1, !tbaa !5
  %37 = uitofp i8 %36 to double
  %38 = fmul double %37, 5.870000e-01
  %39 = tail call double @llvm.fmuladd.f64(double %34, double 2.990000e-01, double %38)
  %40 = getelementptr inbounds i8, ptr %.01417, i64 2
  %41 = load i8, ptr %40, align 1, !tbaa !5
  %42 = uitofp i8 %41 to double
  %43 = tail call double @llvm.fmuladd.f64(double %42, double 1.140000e-01, double %39)
  %44 = fptoui double %43 to i8
  %45 = getelementptr inbounds i8, ptr %.01417, i64 3
  %46 = icmp ugt i8 %44, %4
  %.v = select i1 %46, i8 %6, i8 %5
  store i8 %.v, ptr %.01516, align 1, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %.01516, i64 1
  %48 = add nuw i64 %.018, 1
  %exitcond.not = icmp eq i64 %48, %8
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !23
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <16 x double> @llvm.fmuladd.v16f64(<16 x double>, <16 x double>, <16 x double>) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <8 x double> @llvm.fmuladd.v8f64(<8 x double>, <8 x double>, <8 x double>) #2

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9}
!9 = distinct !{!9, !10}
!10 = distinct !{!10, !"LVerDomain"}
!11 = !{!12}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !14, !15, !16}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = !{!18}
!18 = distinct !{!18, !19}
!19 = distinct !{!19, !"LVerDomain"}
!20 = !{!21}
!21 = distinct !{!21, !19}
!22 = distinct !{!22, !14, !15, !16}
!23 = distinct !{!23, !14, !15}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass