; LLVM IR from zchrissirhcz/pixel :: legacy/zcnn (zcnn_cvtformat)

; 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.px_image_t = type { ptr, i32, i32, i32, i32 }

; 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 @bgr_to_bgr565(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i8, align 1
  %13 = alloca i8, align 1
  %14 = alloca i8, align 1
  %15 = alloca i16, align 2
  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 8, ptr %5) #2
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %16, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.px_image_t, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 8, !tbaa !9
  store i32 %19, ptr %6, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.px_image_t, ptr %20, i32 0, i32 2
  %22 = load i32, ptr %21, align 4, !tbaa !13
  store i32 %22, ptr %7, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %23 = load i32, ptr %6, align 4, !tbaa !12
  %24 = mul nsw i32 %23, 24
  %25 = add nsw i32 %24, 31
  %26 = sdiv i32 %25, 32
  %27 = mul nsw i32 %26, 4
  store i32 %27, ptr %8, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %28 = load i32, ptr %6, align 4, !tbaa !12
  %29 = mul nsw i32 %28, 16
  %30 = add nsw i32 %29, 31
  %31 = sdiv i32 %30, 32
  %32 = mul nsw i32 %31, 4
  %33 = sdiv i32 %32, 2
  store i32 %33, ptr %9, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %10, align 4, !tbaa !12
  br label %34

34:                                               ; preds = %109, %2
  %35 = load i32, ptr %10, align 4, !tbaa !12
  %36 = load i32, ptr %7, align 4, !tbaa !12
  %37 = icmp slt i32 %35, %36
  br i1 %37, label %38, label %112

38:                                               ; preds = %34
  store i32 0, ptr %11, align 4, !tbaa !12
  br label %39

39:                                               ; preds = %105, %38
  %40 = load i32, ptr %11, align 4, !tbaa !12
  %41 = load i32, ptr %6, align 4, !tbaa !12
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %43, label %108

43:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #2
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.px_image_t, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !14
  %47 = load i32, ptr %10, align 4, !tbaa !12
  %48 = load i32, ptr %8, align 4, !tbaa !12
  %49 = mul nsw i32 %47, %48
  %50 = load i32, ptr %11, align 4, !tbaa !12
  %51 = mul nsw i32 %50, 3
  %52 = add nsw i32 %49, %51
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i8, ptr %46, i64 %53
  %55 = load i8, ptr %54, align 1, !tbaa !15
  store i8 %55, ptr %12, align 1, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 1, ptr %13) #2
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.px_image_t, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !14
  %59 = load i32, ptr %10, align 4, !tbaa !12
  %60 = load i32, ptr %8, align 4, !tbaa !12
  %61 = mul nsw i32 %59, %60
  %62 = load i32, ptr %11, align 4, !tbaa !12
  %63 = mul nsw i32 %62, 3
  %64 = add nsw i32 %61, %63
  %65 = add nsw i32 %64, 1
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds i8, ptr %58, i64 %66
  %68 = load i8, ptr %67, align 1, !tbaa !15
  store i8 %68, ptr %13, align 1, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #2
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.px_image_t, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !14
  %72 = load i32, ptr %10, align 4, !tbaa !12
  %73 = load i32, ptr %8, align 4, !tbaa !12
  %74 = mul nsw i32 %72, %73
  %75 = load i32, ptr %11, align 4, !tbaa !12
  %76 = mul nsw i32 %75, 3
  %77 = add nsw i32 %74, %76
  %78 = add nsw i32 %77, 2
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds i8, ptr %71, i64 %79
  %81 = load i8, ptr %80, align 1, !tbaa !15
  store i8 %81, ptr %14, align 1, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 2, ptr %15) #2
  %82 = load i8, ptr %14, align 1, !tbaa !15
  %83 = zext i8 %82 to i32
  %84 = ashr i32 %83, 3
  %85 = shl i32 %84, 11
  %86 = load i8, ptr %13, align 1, !tbaa !15
  %87 = zext i8 %86 to i32
  %88 = ashr i32 %87, 2
  %89 = shl i32 %88, 5
  %90 = or i32 %85, %89
  %91 = load i8, ptr %12, align 1, !tbaa !15
  %92 = zext i8 %91 to i32
  %93 = ashr i32 %92, 3
  %94 = or i32 %90, %93
  %95 = trunc i32 %94 to i16
  store i16 %95, ptr %15, align 2, !tbaa !16
  %96 = load i16, ptr %15, align 2, !tbaa !16
  %97 = load ptr, ptr %5, align 8, !tbaa !5
  %98 = load i32, ptr %10, align 4, !tbaa !12
  %99 = load i32, ptr %9, align 4, !tbaa !12
  %100 = mul nsw i32 %98, %99
  %101 = load i32, ptr %11, align 4, !tbaa !12
  %102 = add nsw i32 %100, %101
  %103 = sext i32 %102 to i64
  %104 = getelementptr inbounds i16, ptr %97, i64 %103
  store i16 %96, ptr %104, align 2, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 2, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #2
  br label %105

105:                                              ; preds = %43
  %106 = load i32, ptr %11, align 4, !tbaa !12
  %107 = add nsw i32 %106, 1
  store i32 %107, ptr %11, align 4, !tbaa !12
  br label %39, !llvm.loop !18

108:                                              ; preds = %39
  br label %109

109:                                              ; preds = %108
  %110 = load i32, ptr %10, align 4, !tbaa !12
  %111 = add nsw i32 %110, 1
  store i32 %111, ptr %10, align 4, !tbaa !12
  br label %34, !llvm.loop !20

112:                                              ; preds = %34
  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 %9) #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 8, 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, !11, i64 8}
!10 = !{!"_ZTS10px_image_t", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !11, i64 12}
!14 = !{!10, !6, i64 0}
!15 = !{!7, !7, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"short", !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


; 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 nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @bgr_to_bgr565(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 12
  %6 = load i32, ptr %5, align 4, !tbaa !11
  %7 = icmp sgt i32 %6, 0
  %8 = icmp sgt i32 %4, 0
  %or.cond = select i1 %7, i1 %8, i1 false
  br i1 %or.cond, label %.preheader.lr.ph.split.us, label %._crit_edge33

.preheader.lr.ph.split.us:                        ; preds = %2
  %9 = add nuw i32 %4, 1
  %10 = and i32 %9, 268435454
  %11 = mul nuw nsw i32 %4, 24
  %12 = add nuw nsw i32 %11, 31
  %13 = lshr i32 %12, 3
  %14 = and i32 %13, 268435452
  %15 = load ptr, ptr %0, align 8, !tbaa !12
  %16 = zext nneg i32 %14 to i64
  %17 = zext nneg i32 %10 to i64
  %wide.trip.count39 = zext nneg i32 %6 to i64
  %wide.trip.count = zext nneg i32 %4 to i64
  %18 = add nsw i64 %wide.trip.count39, -1
  %19 = mul nsw i64 %18, %17
  %20 = add nsw i64 %19, %wide.trip.count
  %21 = shl nsw i64 %20, 1
  %scevgep = getelementptr i8, ptr %1, i64 %21
  %22 = mul nsw i64 %18, %16
  %23 = mul nuw nsw i64 %wide.trip.count, 3
  %24 = getelementptr i8, ptr %15, i64 %22
  %scevgep43 = getelementptr i8, ptr %24, i64 %23
  %min.iters.check = icmp ult i32 %4, 8
  %bound0 = icmp ugt ptr %scevgep43, %1
  %bound1 = icmp ult ptr %15, %scevgep
  %found.conflict = and i1 %bound0, %bound1
  %n.vec = and i64 %wide.trip.count, 2147483640
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br label %.preheader.us

.preheader.us:                                    ; preds = %._crit_edge.us, %.preheader.lr.ph.split.us
  %indvars.iv36 = phi i64 [ %indvars.iv.next37, %._crit_edge.us ], [ 0, %.preheader.lr.ph.split.us ]
  %25 = mul nuw nsw i64 %indvars.iv36, %16
  %26 = mul nuw nsw i64 %indvars.iv36, %17
  %invariant.gep = getelementptr inbounds i8, ptr %15, i64 %25
  %invariant.gep41 = getelementptr inbounds i16, ptr %1, i64 %26
  %brmerge = select i1 %min.iters.check, i1 true, i1 %found.conflict
  br i1 %brmerge, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %.preheader.us, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.preheader.us ]
  %27 = mul nuw nsw i64 %index, 3
  %28 = getelementptr inbounds i8, ptr %invariant.gep, i64 %27
  %wide.vec = load <24 x i8>, ptr %28, align 1, !tbaa !13
  %strided.vec = shufflevector <24 x i8> %wide.vec, <24 x i8> poison, <8 x i32> <i32 0, i32 3, i32 6, i32 9, i32 12, i32 15, i32 18, i32 21>
  %strided.vec44 = shufflevector <24 x i8> %wide.vec, <24 x i8> poison, <8 x i32> <i32 1, i32 4, i32 7, i32 10, i32 13, i32 16, i32 19, i32 22>
  %strided.vec45 = shufflevector <24 x i8> %wide.vec, <24 x i8> poison, <8 x i32> <i32 2, i32 5, i32 8, i32 11, i32 14, i32 17, i32 20, i32 23>
  %29 = lshr <8 x i8> %strided.vec45, <i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3>
  %30 = zext nneg <8 x i8> %29 to <8 x i16>
  %31 = shl nuw <8 x i16> %30, <i16 11, i16 11, i16 11, i16 11, i16 11, i16 11, i16 11, i16 11>
  %32 = lshr <8 x i8> %strided.vec44, <i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2>
  %33 = zext nneg <8 x i8> %32 to <8 x i16>
  %34 = shl nuw nsw <8 x i16> %33, <i16 5, i16 5, i16 5, i16 5, i16 5, i16 5, i16 5, i16 5>
  %35 = or disjoint <8 x i16> %31, %34
  %36 = lshr <8 x i8> %strided.vec, <i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3>
  %37 = zext nneg <8 x i8> %36 to <8 x i16>
  %38 = or disjoint <8 x i16> %35, %37
  %39 = getelementptr inbounds i16, ptr %invariant.gep41, i64 %index
  store <8 x i16> %38, ptr %39, align 2, !tbaa !14, !alias.scope !16, !noalias !19
  %index.next = add nuw i64 %index, 8
  %40 = icmp eq i64 %index.next, %n.vec
  br i1 %40, label %middle.block, label %vector.body, !llvm.loop !21

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %.preheader.us, %middle.block
  %indvars.iv.ph = phi i64 [ 0, %.preheader.us ], [ %n.vec, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %41 = mul nuw nsw i64 %indvars.iv, 3
  %gep = getelementptr inbounds i8, ptr %invariant.gep, i64 %41
  %42 = load i8, ptr %gep, align 1, !tbaa !13
  %43 = getelementptr i8, ptr %gep, i64 1
  %44 = load i8, ptr %43, align 1, !tbaa !13
  %45 = getelementptr i8, ptr %gep, i64 2
  %46 = load i8, ptr %45, align 1, !tbaa !13
  %47 = lshr i8 %46, 3
  %48 = zext nneg i8 %47 to i16
  %49 = shl nuw i16 %48, 11
  %50 = lshr i8 %44, 2
  %51 = zext nneg i8 %50 to i16
  %52 = shl nuw nsw i16 %51, 5
  %53 = or disjoint i16 %49, %52
  %54 = lshr i8 %42, 3
  %55 = zext nneg i8 %54 to i16
  %56 = or disjoint i16 %53, %55
  %gep42 = getelementptr inbounds i16, ptr %invariant.gep41, i64 %indvars.iv
  store i16 %56, ptr %gep42, align 2, !tbaa !14
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.us, label %scalar.ph, !llvm.loop !25

._crit_edge.us:                                   ; preds = %scalar.ph, %middle.block
  %indvars.iv.next37 = add nuw nsw i64 %indvars.iv36, 1
  %exitcond40.not = icmp eq i64 %indvars.iv.next37, %wide.trip.count39
  br i1 %exitcond40.not, label %._crit_edge33, label %.preheader.us, !llvm.loop !26

._crit_edge33:                                    ; preds = %._crit_edge.us, %2
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: 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, !10, i64 8}
!6 = !{!"_ZTS10px_image_t", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 12}
!12 = !{!6, !7, i64 0}
!13 = !{!8, !8, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !8, i64 0}
!16 = !{!17}
!17 = distinct !{!17, !18}
!18 = distinct !{!18, !"LVerDomain"}
!19 = !{!20}
!20 = distinct !{!20, !18}
!21 = distinct !{!21, !22, !23, !24}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!"llvm.loop.isvectorized", i32 1}
!24 = !{!"llvm.loop.unroll.runtime.disable"}
!25 = distinct !{!25, !22, !23}
!26 = distinct !{!26, !22}


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