; LLVM IR from arasgungore/filters-and-fractals :: main (weightedGrayscaleFilter)

; 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.PPMImage = type { i32, i32, ptr }
%struct.PPMPixel = type { i8, i8, i8 }

; 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

; Function Attrs: nounwind uwtable
define dso_local void @weightedGrayscaleFilter(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca double, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %72, %1
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.PPMImage, ptr %7, i32 0, i32 0
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.PPMImage, ptr %10, i32 0, i32 1
  %12 = load i32, ptr %11, align 4, !tbaa !13
  %13 = mul i32 %9, %12
  %14 = icmp ult i32 %6, %13
  br i1 %14, label %15, label %75

15:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.PPMImage, ptr %16, i32 0, i32 2
  %18 = load ptr, ptr %17, align 8, !tbaa !14
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = zext i32 %19 to i64
  %21 = getelementptr inbounds %struct.PPMPixel, ptr %18, i64 %20
  %22 = getelementptr inbounds %struct.PPMPixel, ptr %21, i32 0, i32 0
  %23 = load i8, ptr %22, align 1, !tbaa !15
  %24 = zext i8 %23 to i32
  %25 = sitofp i32 %24 to double
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.PPMImage, ptr %26, i32 0, i32 2
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %29 = load i32, ptr %3, align 4, !tbaa !9
  %30 = zext i32 %29 to i64
  %31 = getelementptr inbounds %struct.PPMPixel, ptr %28, i64 %30
  %32 = getelementptr inbounds %struct.PPMPixel, ptr %31, i32 0, i32 1
  %33 = load i8, ptr %32, align 1, !tbaa !17
  %34 = zext i8 %33 to i32
  %35 = sitofp i32 %34 to double
  %36 = fmul double 5.870000e-01, %35
  %37 = call double @llvm.fmuladd.f64(double 2.990000e-01, double %25, double %36)
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.PPMImage, ptr %38, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  %41 = load i32, ptr %3, align 4, !tbaa !9
  %42 = zext i32 %41 to i64
  %43 = getelementptr inbounds %struct.PPMPixel, ptr %40, i64 %42
  %44 = getelementptr inbounds %struct.PPMPixel, ptr %43, i32 0, i32 2
  %45 = load i8, ptr %44, align 1, !tbaa !18
  %46 = zext i8 %45 to i32
  %47 = sitofp i32 %46 to double
  %48 = call double @llvm.fmuladd.f64(double 1.140000e-01, double %47, double %37)
  store double %48, ptr %4, align 8, !tbaa !19
  %49 = load double, ptr %4, align 8, !tbaa !19
  %50 = fptoui double %49 to i8
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.PPMImage, ptr %51, i32 0, i32 2
  %53 = load ptr, ptr %52, align 8, !tbaa !14
  %54 = load i32, ptr %3, align 4, !tbaa !9
  %55 = zext i32 %54 to i64
  %56 = getelementptr inbounds %struct.PPMPixel, ptr %53, i64 %55
  %57 = getelementptr inbounds %struct.PPMPixel, ptr %56, i32 0, i32 2
  store i8 %50, ptr %57, align 1, !tbaa !18
  %58 = load ptr, ptr %2, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.PPMImage, ptr %58, i32 0, i32 2
  %60 = load ptr, ptr %59, align 8, !tbaa !14
  %61 = load i32, ptr %3, align 4, !tbaa !9
  %62 = zext i32 %61 to i64
  %63 = getelementptr inbounds %struct.PPMPixel, ptr %60, i64 %62
  %64 = getelementptr inbounds %struct.PPMPixel, ptr %63, i32 0, i32 1
  store i8 %50, ptr %64, align 1, !tbaa !17
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.PPMImage, ptr %65, i32 0, i32 2
  %67 = load ptr, ptr %66, align 8, !tbaa !14
  %68 = load i32, ptr %3, align 4, !tbaa !9
  %69 = zext i32 %68 to i64
  %70 = getelementptr inbounds %struct.PPMPixel, ptr %67, i64 %69
  %71 = getelementptr inbounds %struct.PPMPixel, ptr %70, i32 0, i32 0
  store i8 %50, ptr %71, align 1, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %72

72:                                               ; preds = %15
  %73 = load i32, ptr %3, align 4, !tbaa !9
  %74 = add i32 %73, 1
  store i32 %74, ptr %3, align 4, !tbaa !9
  br label %5, !llvm.loop !21

75:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 #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, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8}
!13 = !{!12, !10, i64 4}
!14 = !{!12, !6, i64 8}
!15 = !{!16, !7, i64 0}
!16 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2}
!17 = !{!16, !7, i64 1}
!18 = !{!16, !7, i64 2}
!19 = !{!20, !20, i64 0}
!20 = !{!"double", !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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"

%struct.PPMPixel = type { i8, i8, i8 }

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

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @weightedGrayscaleFilter(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = load i32, ptr %0, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %0, i64 4
  %4 = load i32, ptr %3, align 4, !tbaa !11
  %5 = mul i32 %4, %2
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %wide.trip.count = zext i32 %5 to i64
  br label %8

8:                                                ; preds = %.lr.ph, %8
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %8 ]
  %9 = getelementptr inbounds %struct.PPMPixel, ptr %7, i64 %indvars.iv
  %10 = load i8, ptr %9, align 1, !tbaa !13
  %11 = uitofp i8 %10 to double
  %12 = getelementptr inbounds i8, ptr %9, i64 1
  %13 = load i8, ptr %12, align 1, !tbaa !15
  %14 = uitofp i8 %13 to double
  %15 = fmul double %14, 5.870000e-01
  %16 = tail call double @llvm.fmuladd.f64(double %11, double 2.990000e-01, double %15)
  %17 = getelementptr inbounds i8, ptr %9, i64 2
  %18 = load i8, ptr %17, align 1, !tbaa !16
  %19 = uitofp i8 %18 to double
  %20 = tail call double @llvm.fmuladd.f64(double %19, double 1.140000e-01, double %16)
  %21 = fptoui double %20 to i8
  store i8 %21, ptr %17, align 1, !tbaa !16
  %22 = getelementptr inbounds %struct.PPMPixel, ptr %7, i64 %indvars.iv, i32 1
  store i8 %21, ptr %22, align 1, !tbaa !15
  store i8 %21, ptr %9, align 1, !tbaa !13
  %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, label %8, !llvm.loop !17

._crit_edge:                                      ; preds = %8, %1
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !10, i64 8}
!13 = !{!14, !8, i64 0}
!14 = !{!"", !8, i64 0, !8, i64 1, !8, i64 2}
!15 = !{!14, !8, i64 1}
!16 = !{!14, !8, i64 2}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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