; LLVM IR from parallella/pal :: src/image (p_grayscale)

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

15:                                               ; preds = %45, %4
  %16 = load i32, ptr %9, align 4, !tbaa !9
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = mul nsw i32 %17, %18
  %20 = icmp slt i32 %16, %19
  br i1 %20, label %21, label %48

21:                                               ; preds = %15
  %22 = load ptr, ptr %10, align 8, !tbaa !5
  %23 = getelementptr inbounds float, ptr %22, i32 1
  store ptr %23, ptr %10, align 8, !tbaa !5
  %24 = load float, ptr %22, align 4, !tbaa !11
  %25 = fmul float %24, 0x3FD322D0E0000000
  store float %25, ptr %12, align 4, !tbaa !11
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  %27 = getelementptr inbounds float, ptr %26, i32 1
  store ptr %27, ptr %10, align 8, !tbaa !5
  %28 = load float, ptr %26, align 4, !tbaa !11
  %29 = load float, ptr %12, align 4, !tbaa !11
  %30 = call float @llvm.fma.f32(float 0x3FE2C8B440000000, float %28, float %29)
  store float %30, ptr %12, align 4, !tbaa !11
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = getelementptr inbounds float, ptr %31, i32 1
  store ptr %32, ptr %10, align 8, !tbaa !5
  %33 = load float, ptr %31, align 4, !tbaa !11
  %34 = load float, ptr %12, align 4, !tbaa !11
  %35 = call float @llvm.fma.f32(float 0x3FBD2F1AA0000000, float %33, float %34)
  store float %35, ptr %12, align 4, !tbaa !11
  %36 = load float, ptr %12, align 4, !tbaa !11
  %37 = load ptr, ptr %11, align 8, !tbaa !5
  %38 = getelementptr inbounds float, ptr %37, i32 1
  store ptr %38, ptr %11, align 8, !tbaa !5
  store float %36, ptr %37, align 4, !tbaa !11
  %39 = load float, ptr %12, align 4, !tbaa !11
  %40 = load ptr, ptr %11, align 8, !tbaa !5
  %41 = getelementptr inbounds float, ptr %40, i32 1
  store ptr %41, ptr %11, align 8, !tbaa !5
  store float %39, ptr %40, align 4, !tbaa !11
  %42 = load float, ptr %12, align 4, !tbaa !11
  %43 = load ptr, ptr %11, align 8, !tbaa !5
  %44 = getelementptr inbounds float, ptr %43, i32 1
  store ptr %44, ptr %11, align 8, !tbaa !5
  store float %42, ptr %43, align 4, !tbaa !11
  br label %45

45:                                               ; preds = %21
  %46 = load i32, ptr %9, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %9, align 4, !tbaa !9
  br label %15, !llvm.loop !13

48:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret void
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fma.f32(float, float, float) #2

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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 @p_rgb2grayscale_f32(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = mul nsw i32 %3, %2
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %4
  %xtraiter = and i32 %5, 1
  %7 = icmp eq i32 %5, 1
  br i1 %7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %5, 2147483646
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.01619 = phi ptr [ %1, %.lr.ph.preheader.new ], [ %31, %.lr.ph ]
  %.01718 = phi ptr [ %0, %.lr.ph.preheader.new ], [ %26, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %8 = getelementptr inbounds i8, ptr %.01718, i64 4
  %9 = load float, ptr %.01718, align 4, !tbaa !5
  %10 = fmul float %9, 0x3FD322D0E0000000
  %11 = getelementptr inbounds i8, ptr %.01718, i64 8
  %12 = load float, ptr %8, align 4, !tbaa !5
  %13 = tail call float @llvm.fma.f32(float %12, float 0x3FE2C8B440000000, float %10)
  %14 = getelementptr inbounds i8, ptr %.01718, i64 12
  %15 = load float, ptr %11, align 4, !tbaa !5
  %16 = tail call float @llvm.fma.f32(float %15, float 0x3FBD2F1AA0000000, float %13)
  %17 = getelementptr inbounds i8, ptr %.01619, i64 4
  store float %16, ptr %.01619, align 4, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %.01619, i64 8
  store float %16, ptr %17, align 4, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %.01619, i64 12
  store float %16, ptr %18, align 4, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %.01718, i64 16
  %21 = load float, ptr %14, align 4, !tbaa !5
  %22 = fmul float %21, 0x3FD322D0E0000000
  %23 = getelementptr inbounds i8, ptr %.01718, i64 20
  %24 = load float, ptr %20, align 4, !tbaa !5
  %25 = tail call float @llvm.fma.f32(float %24, float 0x3FE2C8B440000000, float %22)
  %26 = getelementptr inbounds i8, ptr %.01718, i64 24
  %27 = load float, ptr %23, align 4, !tbaa !5
  %28 = tail call float @llvm.fma.f32(float %27, float 0x3FBD2F1AA0000000, float %25)
  %29 = getelementptr inbounds i8, ptr %.01619, i64 16
  store float %28, ptr %19, align 4, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %.01619, i64 20
  store float %28, ptr %29, align 4, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %.01619, i64 24
  store float %28, ptr %30, align 4, !tbaa !5
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.01619.unr = phi ptr [ %1, %.lr.ph.preheader ], [ %31, %.lr.ph ]
  %.01718.unr = phi ptr [ %0, %.lr.ph.preheader ], [ %26, %.lr.ph ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %32 = getelementptr inbounds i8, ptr %.01718.unr, i64 4
  %33 = load float, ptr %.01718.unr, align 4, !tbaa !5
  %34 = fmul float %33, 0x3FD322D0E0000000
  %35 = getelementptr inbounds i8, ptr %.01718.unr, i64 8
  %36 = load float, ptr %32, align 4, !tbaa !5
  %37 = tail call float @llvm.fma.f32(float %36, float 0x3FE2C8B440000000, float %34)
  %38 = load float, ptr %35, align 4, !tbaa !5
  %39 = tail call float @llvm.fma.f32(float %38, float 0x3FBD2F1AA0000000, float %37)
  %40 = getelementptr inbounds i8, ptr %.01619.unr, i64 4
  store float %39, ptr %.01619.unr, align 4, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %.01619.unr, i64 8
  store float %39, ptr %40, align 4, !tbaa !5
  store float %39, ptr %41, align 4, !tbaa !5
  br label %._crit_edge

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fma.f32(float, float, float) #1

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

!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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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