; LLVM IR from casensiom/fast-image-processing :: src (image)

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

; Function Attrs: nounwind uwtable
declare dso_local void @create_image(ptr dead_on_unwind noalias writable sret(%struct.SImage) align 8, i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @reset_image(ptr noundef) #0

; 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 willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local void @convertToGray(ptr dead_on_unwind noalias writable sret(%struct.SImage) align 8 %0, ptr noundef byval(%struct.SImage) align 8 %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %struct.SImage, align 8
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  %10 = alloca i8, align 1
  %11 = alloca i8, align 1
  %12 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %13 = getelementptr inbounds %struct.SImage, ptr %1, i32 0, i32 4
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %14, ptr %3, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %15 = getelementptr inbounds %struct.SImage, ptr %1, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !12
  %17 = getelementptr inbounds %struct.SImage, ptr %1, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !13
  %19 = mul i32 %16, %18
  store i32 %19, ptr %5, align 4, !tbaa !14
  call void @reset_image(ptr noundef %0)
  call void @llvm.lifetime.start.p0(i64 24, ptr %6) #4
  %20 = getelementptr inbounds %struct.SImage, ptr %1, i32 0, i32 0
  %21 = load i32, ptr %20, align 8, !tbaa !12
  %22 = getelementptr inbounds %struct.SImage, ptr %1, i32 0, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !13
  call void @create_image(ptr dead_on_unwind writable sret(%struct.SImage) align 8 %6, i32 noundef %21, i32 noundef %23, i32 noundef 1)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 8 %6, i64 24, i1 false), !tbaa.struct !15
  call void @llvm.lifetime.end.p0(i64 24, ptr %6) #4
  %24 = getelementptr inbounds %struct.SImage, ptr %1, i32 0, i32 3
  %25 = load i32, ptr %24, align 4, !tbaa !16
  %26 = icmp eq i32 %25, 1
  br i1 %26, label %27, label %33

27:                                               ; preds = %2
  %28 = getelementptr inbounds %struct.SImage, ptr %0, i32 0, i32 4
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  %30 = load ptr, ptr %3, align 8, !tbaa !11
  %31 = load i32, ptr %5, align 4, !tbaa !14
  %32 = zext i32 %31 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %29, ptr align 1 %30, i64 %32, i1 false)
  br label %116

33:                                               ; preds = %2
  %34 = getelementptr inbounds %struct.SImage, ptr %1, i32 0, i32 3
  %35 = load i32, ptr %34, align 4, !tbaa !16
  %36 = icmp eq i32 %35, 4
  br i1 %36, label %37, label %73

37:                                               ; preds = %33
  store i32 0, ptr %4, align 4, !tbaa !14
  br label %38

38:                                               ; preds = %69, %37
  %39 = load i32, ptr %4, align 4, !tbaa !14
  %40 = load i32, ptr %5, align 4, !tbaa !14
  %41 = icmp ult i32 %39, %40
  br i1 %41, label %42, label %72

42:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #4
  %43 = load ptr, ptr %3, align 8, !tbaa !11
  %44 = getelementptr inbounds i8, ptr %43, i64 0
  %45 = load i8, ptr %44, align 1, !tbaa !17
  store i8 %45, ptr %7, align 1, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #4
  %46 = load ptr, ptr %3, align 8, !tbaa !11
  %47 = getelementptr inbounds i8, ptr %46, i64 1
  %48 = load i8, ptr %47, align 1, !tbaa !17
  store i8 %48, ptr %8, align 1, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #4
  %49 = load ptr, ptr %3, align 8, !tbaa !11
  %50 = getelementptr inbounds i8, ptr %49, i64 2
  %51 = load i8, ptr %50, align 1, !tbaa !17
  store i8 %51, ptr %9, align 1, !tbaa !17
  %52 = load i8, ptr %9, align 1, !tbaa !17
  %53 = uitofp i8 %52 to float
  %54 = load i8, ptr %8, align 1, !tbaa !17
  %55 = uitofp i8 %54 to float
  %56 = fmul float 0x3FE2C08320000000, %55
  %57 = call float @llvm.fmuladd.f32(float 0x3FD3126EA0000000, float %53, float %56)
  %58 = load i8, ptr %7, align 1, !tbaa !17
  %59 = uitofp i8 %58 to float
  %60 = call float @llvm.fmuladd.f32(float 0x3FBD2F1AA0000000, float %59, float %57)
  %61 = fptoui float %60 to i8
  %62 = getelementptr inbounds %struct.SImage, ptr %0, i32 0, i32 4
  %63 = load ptr, ptr %62, align 8, !tbaa !5
  %64 = load i32, ptr %4, align 4, !tbaa !14
  %65 = zext i32 %64 to i64
  %66 = getelementptr inbounds i8, ptr %63, i64 %65
  store i8 %61, ptr %66, align 1, !tbaa !17
  %67 = load ptr, ptr %3, align 8, !tbaa !11
  %68 = getelementptr inbounds i8, ptr %67, i64 4
  store ptr %68, ptr %3, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #4
  br label %69

69:                                               ; preds = %42
  %70 = load i32, ptr %4, align 4, !tbaa !14
  %71 = add i32 %70, 1
  store i32 %71, ptr %4, align 4, !tbaa !14
  br label %38, !llvm.loop !18

72:                                               ; preds = %38
  br label %115

73:                                               ; preds = %33
  %74 = getelementptr inbounds %struct.SImage, ptr %1, i32 0, i32 3
  %75 = load i32, ptr %74, align 4, !tbaa !16
  %76 = icmp eq i32 %75, 3
  br i1 %76, label %77, label %113

77:                                               ; preds = %73
  store i32 0, ptr %4, align 4, !tbaa !14
  br label %78

78:                                               ; preds = %109, %77
  %79 = load i32, ptr %4, align 4, !tbaa !14
  %80 = load i32, ptr %5, align 4, !tbaa !14
  %81 = icmp ult i32 %79, %80
  br i1 %81, label %82, label %112

82:                                               ; preds = %78
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #4
  %83 = load ptr, ptr %3, align 8, !tbaa !11
  %84 = getelementptr inbounds i8, ptr %83, i64 0
  %85 = load i8, ptr %84, align 1, !tbaa !17
  store i8 %85, ptr %10, align 1, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #4
  %86 = load ptr, ptr %3, align 8, !tbaa !11
  %87 = getelementptr inbounds i8, ptr %86, i64 1
  %88 = load i8, ptr %87, align 1, !tbaa !17
  store i8 %88, ptr %11, align 1, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #4
  %89 = load ptr, ptr %3, align 8, !tbaa !11
  %90 = getelementptr inbounds i8, ptr %89, i64 2
  %91 = load i8, ptr %90, align 1, !tbaa !17
  store i8 %91, ptr %12, align 1, !tbaa !17
  %92 = load i8, ptr %10, align 1, !tbaa !17
  %93 = uitofp i8 %92 to float
  %94 = load i8, ptr %11, align 1, !tbaa !17
  %95 = uitofp i8 %94 to float
  %96 = fmul float 0x3FE2C08320000000, %95
  %97 = call float @llvm.fmuladd.f32(float 0x3FD3126EA0000000, float %93, float %96)
  %98 = load i8, ptr %12, align 1, !tbaa !17
  %99 = uitofp i8 %98 to float
  %100 = call float @llvm.fmuladd.f32(float 0x3FBD2F1AA0000000, float %99, float %97)
  %101 = fptoui float %100 to i8
  %102 = getelementptr inbounds %struct.SImage, ptr %0, i32 0, i32 4
  %103 = load ptr, ptr %102, align 8, !tbaa !5
  %104 = load i32, ptr %4, align 4, !tbaa !14
  %105 = zext i32 %104 to i64
  %106 = getelementptr inbounds i8, ptr %103, i64 %105
  store i8 %101, ptr %106, align 1, !tbaa !17
  %107 = load ptr, ptr %3, align 8, !tbaa !11
  %108 = getelementptr inbounds i8, ptr %107, i64 3
  store ptr %108, ptr %3, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #4
  br label %109

109:                                              ; preds = %82
  %110 = load i32, ptr %4, align 4, !tbaa !14
  %111 = add i32 %110, 1
  store i32 %111, ptr %4, align 4, !tbaa !14
  br label %78, !llvm.loop !20

112:                                              ; preds = %78
  br label %114

113:                                              ; preds = %73
  br label %114

114:                                              ; preds = %113, %112
  br label %115

115:                                              ; preds = %114, %72
  br label %116

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

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

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 nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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, !10, i64 16}
!6 = !{!"SImage", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !7, i64 4}
!14 = !{!7, !7, i64 0}
!15 = !{i64 0, i64 4, !14, i64 4, i64 4, !14, i64 8, i64 4, !14, i64 12, i64 4, !14, i64 16, i64 8, !11}
!16 = !{!6, !7, i64 12}
!17 = !{!8, !8, 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"

%struct.SImage = type { i32, i32, i32, i32, ptr }

; Function Attrs: nounwind uwtable
declare dso_local void @create_image(ptr dead_on_unwind noalias writable sret(%struct.SImage) align 8, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @reset_image(ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local void @convertToGray(ptr dead_on_unwind noalias writable sret(%struct.SImage) align 8 %0, ptr nocapture noundef readonly byval(%struct.SImage) align 8 %1) local_unnamed_addr #0 {
  %3 = alloca %struct.SImage, align 8
  %4 = getelementptr inbounds i8, ptr %1, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = load i32, ptr %1, align 8, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %1, i64 4
  %8 = load i32, ptr %7, align 4, !tbaa !12
  %9 = mul i32 %8, %6
  tail call void @reset_image(ptr noundef %0)
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #4
  call void @create_image(ptr dead_on_unwind nonnull writable sret(%struct.SImage) align 8 %3, i32 noundef %6, i32 noundef %8, i32 noundef 1)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef nonnull align 8 dereferenceable(24) %3, i64 24, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #4
  %10 = getelementptr inbounds i8, ptr %1, i64 12
  %11 = load i32, ptr %10, align 4, !tbaa !16
  switch i32 %11, label %.loopexit [
    i32 1, label %14
    i32 4, label %.preheader
    i32 3, label %.preheader26
  ]

.preheader26:                                     ; preds = %2
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader26
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %wide.trip.count = zext i32 %9 to i64
  br label %34

.preheader:                                       ; preds = %2
  %.not33 = icmp eq i32 %9, 0
  br i1 %.not33, label %.loopexit, label %.lr.ph32

.lr.ph32:                                         ; preds = %.preheader
  %13 = getelementptr inbounds i8, ptr %0, i64 16
  %wide.trip.count39 = zext i32 %9 to i64
  br label %18

14:                                               ; preds = %2
  %15 = getelementptr inbounds i8, ptr %0, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !5
  %17 = zext i32 %9 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %16, ptr align 1 %5, i64 %17, i1 false)
  br label %.loopexit

18:                                               ; preds = %.lr.ph32, %18
  %indvars.iv36 = phi i64 [ 0, %.lr.ph32 ], [ %indvars.iv.next37, %18 ]
  %.031 = phi ptr [ %5, %.lr.ph32 ], [ %33, %18 ]
  %19 = load i8, ptr %.031, align 1, !tbaa !17
  %20 = getelementptr inbounds i8, ptr %.031, i64 1
  %21 = load i8, ptr %20, align 1, !tbaa !17
  %22 = getelementptr inbounds i8, ptr %.031, i64 2
  %23 = load i8, ptr %22, align 1, !tbaa !17
  %24 = uitofp i8 %23 to float
  %25 = uitofp i8 %21 to float
  %26 = fmul float %25, 0x3FE2C08320000000
  %27 = call float @llvm.fmuladd.f32(float %24, float 0x3FD3126EA0000000, float %26)
  %28 = uitofp i8 %19 to float
  %29 = call float @llvm.fmuladd.f32(float %28, float 0x3FBD2F1AA0000000, float %27)
  %30 = fptoui float %29 to i8
  %31 = load ptr, ptr %13, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i64 %indvars.iv36
  store i8 %30, ptr %32, align 1, !tbaa !17
  %33 = getelementptr inbounds i8, ptr %.031, i64 4
  %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 %.loopexit, label %18, !llvm.loop !18

34:                                               ; preds = %.lr.ph, %34
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %34 ]
  %.129 = phi ptr [ %5, %.lr.ph ], [ %49, %34 ]
  %35 = load i8, ptr %.129, align 1, !tbaa !17
  %36 = getelementptr inbounds i8, ptr %.129, i64 1
  %37 = load i8, ptr %36, align 1, !tbaa !17
  %38 = getelementptr inbounds i8, ptr %.129, i64 2
  %39 = load i8, ptr %38, align 1, !tbaa !17
  %40 = uitofp i8 %35 to float
  %41 = uitofp i8 %37 to float
  %42 = fmul float %41, 0x3FE2C08320000000
  %43 = call float @llvm.fmuladd.f32(float %40, float 0x3FD3126EA0000000, float %42)
  %44 = uitofp i8 %39 to float
  %45 = call float @llvm.fmuladd.f32(float %44, float 0x3FBD2F1AA0000000, float %43)
  %46 = fptoui float %45 to i8
  %47 = load ptr, ptr %12, align 8, !tbaa !5
  %48 = getelementptr inbounds i8, ptr %47, i64 %indvars.iv
  store i8 %46, ptr %48, align 1, !tbaa !17
  %49 = getelementptr inbounds i8, ptr %.129, i64 3
  %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 %.loopexit, label %34, !llvm.loop !20

.loopexit:                                        ; preds = %34, %18, %.preheader26, %.preheader, %2, %14
  ret void
}

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

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


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