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

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

32:                                               ; preds = %59, %4
  %33 = load i64, ptr %18, align 8, !tbaa !9
  %34 = load i64, ptr %17, align 8, !tbaa !9
  %35 = icmp ult i64 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #2
  br label %62

37:                                               ; preds = %32
  %38 = load ptr, ptr %11, align 8, !tbaa !5
  %39 = load i8, ptr %38, align 1, !tbaa !11
  %40 = load ptr, ptr %16, align 8, !tbaa !5
  store i8 %39, ptr %40, align 1, !tbaa !11
  %41 = load ptr, ptr %16, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i64 3
  store ptr %42, ptr %16, align 8, !tbaa !5
  %43 = load ptr, ptr %11, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i64 3
  store ptr %44, ptr %11, align 8, !tbaa !5
  %45 = load ptr, ptr %12, align 8, !tbaa !5
  %46 = load i8, ptr %45, align 1, !tbaa !11
  %47 = load ptr, ptr %15, align 8, !tbaa !5
  store i8 %46, ptr %47, align 1, !tbaa !11
  %48 = load ptr, ptr %15, align 8, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %48, i64 3
  store ptr %49, ptr %15, align 8, !tbaa !5
  %50 = load ptr, ptr %12, align 8, !tbaa !5
  %51 = getelementptr inbounds i8, ptr %50, i64 3
  store ptr %51, ptr %12, align 8, !tbaa !5
  %52 = load ptr, ptr %13, align 8, !tbaa !5
  %53 = load i8, ptr %52, align 1, !tbaa !11
  %54 = load ptr, ptr %14, align 8, !tbaa !5
  store i8 %53, ptr %54, align 1, !tbaa !11
  %55 = load ptr, ptr %14, align 8, !tbaa !5
  %56 = getelementptr inbounds i8, ptr %55, i64 3
  store ptr %56, ptr %14, align 8, !tbaa !5
  %57 = load ptr, ptr %13, align 8, !tbaa !5
  %58 = getelementptr inbounds i8, ptr %57, i64 3
  store ptr %58, ptr %13, align 8, !tbaa !5
  br label %59

59:                                               ; preds = %37
  %60 = load i64, ptr %18, align 8, !tbaa !9
  %61 = add i64 %60, 1
  store i64 %61, ptr %18, align 8, !tbaa !9
  br label %32, !llvm.loop !12

62:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #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 @rgb2bgr_idxopt(ptr nocapture noundef readonly %0, i64 noundef %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = mul i64 %2, %1
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %6 = getelementptr inbounds i8, ptr %3, i64 2
  %7 = getelementptr inbounds i8, ptr %3, i64 1
  %8 = getelementptr inbounds i8, ptr %0, i64 2
  %9 = getelementptr inbounds i8, ptr %0, i64 1
  %xtraiter = and i64 %5, 1
  %10 = icmp eq i64 %5, 1
  br i1 %10, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %5, -2
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.02233.unr = phi ptr [ %0, %.lr.ph.preheader ], [ %25, %.lr.ph ]
  %.02332.unr = phi ptr [ %6, %.lr.ph.preheader ], [ %24, %.lr.ph ]
  %.02431.unr = phi ptr [ %7, %.lr.ph.preheader ], [ %27, %.lr.ph ]
  %.02530.unr = phi ptr [ %3, %.lr.ph.preheader ], [ %30, %.lr.ph ]
  %.02629.unr = phi ptr [ %8, %.lr.ph.preheader ], [ %31, %.lr.ph ]
  %.02728.unr = phi ptr [ %9, %.lr.ph.preheader ], [ %28, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %11 = load i8, ptr %.02233.unr, align 1, !tbaa !5
  store i8 %11, ptr %.02332.unr, align 1, !tbaa !5
  %12 = load i8, ptr %.02728.unr, align 1, !tbaa !5
  store i8 %12, ptr %.02431.unr, align 1, !tbaa !5
  %13 = load i8, ptr %.02629.unr, align 1, !tbaa !5
  store i8 %13, ptr %.02530.unr, align 1, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %4
  ret void

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.02233 = phi ptr [ %0, %.lr.ph.preheader.new ], [ %25, %.lr.ph ]
  %.02332 = phi ptr [ %6, %.lr.ph.preheader.new ], [ %24, %.lr.ph ]
  %.02431 = phi ptr [ %7, %.lr.ph.preheader.new ], [ %27, %.lr.ph ]
  %.02530 = phi ptr [ %3, %.lr.ph.preheader.new ], [ %30, %.lr.ph ]
  %.02629 = phi ptr [ %8, %.lr.ph.preheader.new ], [ %31, %.lr.ph ]
  %.02728 = phi ptr [ %9, %.lr.ph.preheader.new ], [ %28, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %14 = load i8, ptr %.02233, align 1, !tbaa !5
  store i8 %14, ptr %.02332, align 1, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %.02332, i64 3
  %16 = getelementptr inbounds i8, ptr %.02233, i64 3
  %17 = load i8, ptr %.02728, align 1, !tbaa !5
  store i8 %17, ptr %.02431, align 1, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %.02431, i64 3
  %19 = getelementptr inbounds i8, ptr %.02728, i64 3
  %20 = load i8, ptr %.02629, align 1, !tbaa !5
  store i8 %20, ptr %.02530, align 1, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %.02530, i64 3
  %22 = getelementptr inbounds i8, ptr %.02629, i64 3
  %23 = load i8, ptr %16, align 1, !tbaa !5
  store i8 %23, ptr %15, align 1, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %.02332, i64 6
  %25 = getelementptr inbounds i8, ptr %.02233, i64 6
  %26 = load i8, ptr %19, align 1, !tbaa !5
  store i8 %26, ptr %18, align 1, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %.02431, i64 6
  %28 = getelementptr inbounds i8, ptr %.02728, i64 6
  %29 = load i8, ptr %22, align 1, !tbaa !5
  store i8 %29, ptr %21, align 1, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %.02530, i64 6
  %31 = getelementptr inbounds i8, ptr %.02629, i64 6
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !8
}

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}
!9 = !{!"llvm.loop.mustprogress"}


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