; 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_gray_asimd(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 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) #2
  %17 = load i64, ptr %9, align 8, !tbaa !9
  %18 = load i64, ptr %10, align 8, !tbaa !9
  %19 = mul i64 %17, %18
  store i64 %19, ptr %15, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #2
  store i64 0, ptr %16, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %46, %7
  %21 = load i64, ptr %16, align 8, !tbaa !9
  %22 = load i64, ptr %15, align 8, !tbaa !9
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %24, label %49

24:                                               ; preds = %20
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i64 0
  %27 = load i8, ptr %26, align 1, !tbaa !11
  %28 = zext i8 %27 to i32
  %29 = load i8, ptr %12, align 1, !tbaa !11
  %30 = zext i8 %29 to i32
  %31 = icmp sgt i32 %28, %30
  br i1 %31, label %32, label %35

32:                                               ; preds = %24
  %33 = load i8, ptr %14, align 1, !tbaa !11
  %34 = zext i8 %33 to i32
  br label %38

35:                                               ; preds = %24
  %36 = load i8, ptr %13, align 1, !tbaa !11
  %37 = zext i8 %36 to i32
  br label %38

38:                                               ; preds = %35, %32
  %39 = phi i32 [ %34, %32 ], [ %37, %35 ]
  %40 = trunc i32 %39 to i8
  %41 = load ptr, ptr %11, align 8, !tbaa !5
  store i8 %40, ptr %41, align 1, !tbaa !11
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %42, i32 1
  store ptr %43, ptr %8, align 8, !tbaa !5
  %44 = load ptr, ptr %11, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %44, i32 1
  store ptr %45, ptr %11, align 8, !tbaa !5
  br label %46

46:                                               ; preds = %38
  %47 = load i64, ptr %16, align 8, !tbaa !9
  %48 = add i64 %47, 1
  store i64 %48, ptr %16, align 8, !tbaa !9
  br label %20, !llvm.loop !12

49:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  ret void
}

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 = { 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_gray_asimd(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
  %9 = ptrtoint ptr %3 to i64
  %10 = ptrtoint ptr %0 to i64
  %min.iters.check = icmp ult i64 %8, 8
  %11 = sub i64 %9, %10
  %diff.check = icmp ult i64 %11, 32
  %or.cond = or i1 %min.iters.check, %diff.check
  br i1 %or.cond, label %.lr.ph.preheader, label %vector.main.loop.iter.check

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

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %8, -32
  %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.splatinsert21 = insertelement <16 x i8> poison, i8 %6, i64 0
  %broadcast.splat22 = shufflevector <16 x i8> %broadcast.splatinsert21, <16 x i8> poison, <16 x i32> zeroinitializer
  %broadcast.splatinsert23 = insertelement <16 x i8> poison, i8 %5, i64 0
  %broadcast.splat24 = shufflevector <16 x i8> %broadcast.splatinsert23, <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 ]
  %next.gep = getelementptr i8, ptr %0, i64 %index
  %next.gep18 = getelementptr i8, ptr %3, i64 %index
  %12 = getelementptr i8, ptr %next.gep, i64 16
  %wide.load = load <16 x i8>, ptr %next.gep, align 1, !tbaa !5
  %wide.load20 = load <16 x i8>, ptr %12, align 1, !tbaa !5
  %13 = icmp ugt <16 x i8> %wide.load, %broadcast.splat
  %14 = icmp ugt <16 x i8> %wide.load20, %broadcast.splat
  %15 = select <16 x i1> %13, <16 x i8> %broadcast.splat22, <16 x i8> %broadcast.splat24
  %16 = select <16 x i1> %14, <16 x i8> %broadcast.splat22, <16 x i8> %broadcast.splat24
  %17 = getelementptr i8, ptr %next.gep18, i64 16
  store <16 x i8> %15, ptr %next.gep18, align 1, !tbaa !5
  store <16 x i8> %16, ptr %17, align 1, !tbaa !5
  %index.next = add nuw i64 %index, 32
  %18 = icmp eq i64 %index.next, %n.vec
  br i1 %18, label %middle.block, label %vector.body, !llvm.loop !8

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.end30 = getelementptr i8, ptr %3, i64 %n.vec
  %ind.end27 = getelementptr i8, ptr %0, i64 %n.vec
  %n.vec.remaining = and i64 %8, 24
  %min.epilog.iters.check = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check, label %.lr.ph.preheader, label %vec.epilog.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.vec26 = and i64 %8, -8
  %ind.end = getelementptr i8, ptr %0, i64 %n.vec26
  %ind.end29 = getelementptr i8, ptr %3, i64 %n.vec26
  %broadcast.splatinsert36 = insertelement <8 x i8> poison, i8 %4, i64 0
  %broadcast.splat37 = shufflevector <8 x i8> %broadcast.splatinsert36, <8 x i8> poison, <8 x i32> zeroinitializer
  %broadcast.splatinsert38 = insertelement <8 x i8> poison, i8 %6, i64 0
  %broadcast.splat39 = shufflevector <8 x i8> %broadcast.splatinsert38, <8 x i8> poison, <8 x i32> zeroinitializer
  %broadcast.splatinsert40 = insertelement <8 x i8> poison, i8 %5, i64 0
  %broadcast.splat41 = shufflevector <8 x i8> %broadcast.splatinsert40, <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
  %index32 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next42, %vec.epilog.vector.body ]
  %next.gep33 = getelementptr i8, ptr %0, i64 %index32
  %next.gep34 = getelementptr i8, ptr %3, i64 %index32
  %wide.load35 = load <8 x i8>, ptr %next.gep33, align 1, !tbaa !5
  %19 = icmp ugt <8 x i8> %wide.load35, %broadcast.splat37
  %20 = select <8 x i1> %19, <8 x i8> %broadcast.splat39, <8 x i8> %broadcast.splat41
  store <8 x i8> %20, ptr %next.gep34, align 1, !tbaa !5
  %index.next42 = add nuw i64 %index32, 8
  %21 = icmp eq i64 %index.next42, %n.vec26
  br i1 %21, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !12

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

.lr.ph.preheader:                                 ; preds = %vec.epilog.middle.block, %iter.check, %vec.epilog.iter.check
  %.015.ph = phi i64 [ 0, %iter.check ], [ %n.vec, %vec.epilog.iter.check ], [ %n.vec26, %vec.epilog.middle.block ]
  %.01114.ph = phi ptr [ %0, %iter.check ], [ %ind.end27, %vec.epilog.iter.check ], [ %ind.end, %vec.epilog.middle.block ]
  %.01213.ph = phi ptr [ %3, %iter.check ], [ %ind.end30, %vec.epilog.iter.check ], [ %ind.end29, %vec.epilog.middle.block ]
  %xtraiter = and i64 %8, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %.015.prol = phi i64 [ %26, %.lr.ph.prol ], [ %.015.ph, %.lr.ph.preheader ]
  %.01114.prol = phi ptr [ %24, %.lr.ph.prol ], [ %.01114.ph, %.lr.ph.preheader ]
  %.01213.prol = phi ptr [ %25, %.lr.ph.prol ], [ %.01213.ph, %.lr.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %22 = load i8, ptr %.01114.prol, align 1, !tbaa !5
  %23 = icmp ugt i8 %22, %4
  %.v.prol = select i1 %23, i8 %6, i8 %5
  store i8 %.v.prol, ptr %.01213.prol, align 1, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %.01114.prol, i64 1
  %25 = getelementptr inbounds i8, ptr %.01213.prol, i64 1
  %26 = add nuw i64 %.015.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !13

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.015.unr = phi i64 [ %.015.ph, %.lr.ph.preheader ], [ %26, %.lr.ph.prol ]
  %.01114.unr = phi ptr [ %.01114.ph, %.lr.ph.preheader ], [ %24, %.lr.ph.prol ]
  %.01213.unr = phi ptr [ %.01213.ph, %.lr.ph.preheader ], [ %25, %.lr.ph.prol ]
  %27 = sub i64 %.015.ph, %8
  %28 = icmp ugt i64 %27, -4
  br i1 %28, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.015 = phi i64 [ %45, %.lr.ph ], [ %.015.unr, %.lr.ph.prol.loopexit ]
  %.01114 = phi ptr [ %43, %.lr.ph ], [ %.01114.unr, %.lr.ph.prol.loopexit ]
  %.01213 = phi ptr [ %44, %.lr.ph ], [ %.01213.unr, %.lr.ph.prol.loopexit ]
  %29 = load i8, ptr %.01114, align 1, !tbaa !5
  %30 = icmp ugt i8 %29, %4
  %.v = select i1 %30, i8 %6, i8 %5
  store i8 %.v, ptr %.01213, align 1, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %.01114, i64 1
  %32 = getelementptr inbounds i8, ptr %.01213, i64 1
  %33 = load i8, ptr %31, align 1, !tbaa !5
  %34 = icmp ugt i8 %33, %4
  %.v.1 = select i1 %34, i8 %6, i8 %5
  store i8 %.v.1, ptr %32, align 1, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %.01114, i64 2
  %36 = getelementptr inbounds i8, ptr %.01213, i64 2
  %37 = load i8, ptr %35, align 1, !tbaa !5
  %38 = icmp ugt i8 %37, %4
  %.v.2 = select i1 %38, i8 %6, i8 %5
  store i8 %.v.2, ptr %36, align 1, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %.01114, i64 3
  %40 = getelementptr inbounds i8, ptr %.01213, i64 3
  %41 = load i8, ptr %39, align 1, !tbaa !5
  %42 = icmp ugt i8 %41, %4
  %.v.3 = select i1 %42, i8 %6, i8 %5
  store i8 %.v.3, ptr %40, align 1, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %.01114, i64 4
  %44 = getelementptr inbounds i8, ptr %.01213, i64 4
  %45 = add nuw i64 %.015, 4
  %exitcond.not.3 = icmp eq i64 %45, %8
  br i1 %exitcond.not.3, label %._crit_edge, label %.lr.ph, !llvm.loop !15

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

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

!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 = distinct !{!8, !9, !10, !11}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !9, !10, !11}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.unroll.disable"}
!15 = distinct !{!15, !9, !10}


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