; LLVM IR from clunietp/libgmsd :: bin (gmsd)

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

; Function Attrs: nounwind uwtable
define dso_local ptr @rgb2gray(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca double, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store ptr null, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store ptr null, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store i64 0, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store double 0.000000e+00, ptr %8, align 8, !tbaa !11
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %27

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.bmpread_t, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %14, align 8, !tbaa !13
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %27

17:                                               ; preds = %12
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.bmpread_t, ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 4, !tbaa !16
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %27

22:                                               ; preds = %17
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.bmpread_t, ptr %23, i32 0, i32 3
  %25 = load ptr, ptr %24, align 8, !tbaa !17
  %26 = icmp ne ptr %25, null
  br i1 %26, label %28, label %27

27:                                               ; preds = %22, %17, %12, %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %86

28:                                               ; preds = %22
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.bmpread_t, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !17
  store ptr %31, ptr %5, align 8, !tbaa !5
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.bmpread_t, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 8, !tbaa !13
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.bmpread_t, ptr %35, i32 0, i32 1
  %37 = load i32, ptr %36, align 4, !tbaa !16
  %38 = mul nsw i32 %34, %37
  %39 = sext i32 %38 to i64
  %40 = mul i64 %39, 4
  %41 = call noalias ptr @malloc(i64 noundef %40) #5
  store ptr %41, ptr %4, align 8, !tbaa !5
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = icmp ne ptr %42, null
  br i1 %43, label %45, label %44

44:                                               ; preds = %28
  store ptr null, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %86

45:                                               ; preds = %28
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %46, ptr %6, align 8, !tbaa !5
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %47

47:                                               ; preds = %81, %45
  %48 = load i64, ptr %7, align 8, !tbaa !9
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.bmpread_t, ptr %49, i32 0, i32 0
  %51 = load i32, ptr %50, align 8, !tbaa !13
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.bmpread_t, ptr %52, i32 0, i32 1
  %54 = load i32, ptr %53, align 4, !tbaa !16
  %55 = mul nsw i32 %51, %54
  %56 = sext i32 %55 to i64
  %57 = icmp ult i64 %48, %56
  br i1 %57, label %58, label %84

58:                                               ; preds = %47
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds i8, ptr %59, i32 1
  store ptr %60, ptr %5, align 8, !tbaa !5
  %61 = load i8, ptr %59, align 1, !tbaa !18
  %62 = uitofp i8 %61 to double
  %63 = fmul double 2.990000e-01, %62
  store double %63, ptr %8, align 8, !tbaa !11
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = getelementptr inbounds i8, ptr %64, i32 1
  store ptr %65, ptr %5, align 8, !tbaa !5
  %66 = load i8, ptr %64, align 1, !tbaa !18
  %67 = uitofp i8 %66 to double
  %68 = load double, ptr %8, align 8, !tbaa !11
  %69 = call double @llvm.fmuladd.f64(double 5.870000e-01, double %67, double %68)
  store double %69, ptr %8, align 8, !tbaa !11
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds i8, ptr %70, i32 1
  store ptr %71, ptr %5, align 8, !tbaa !5
  %72 = load i8, ptr %70, align 1, !tbaa !18
  %73 = uitofp i8 %72 to double
  %74 = load double, ptr %8, align 8, !tbaa !11
  %75 = call double @llvm.fmuladd.f64(double 1.140000e-01, double %73, double %74)
  store double %75, ptr %8, align 8, !tbaa !11
  %76 = load double, ptr %8, align 8, !tbaa !11
  %77 = call double @llvm.round.f64(double %76)
  %78 = fptrunc double %77 to float
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = getelementptr inbounds float, ptr %79, i32 1
  store ptr %80, ptr %6, align 8, !tbaa !5
  store float %78, ptr %79, align 4, !tbaa !19
  br label %81

81:                                               ; preds = %58
  %82 = load i64, ptr %7, align 8, !tbaa !9
  %83 = add i64 %82, 1
  store i64 %83, ptr %7, align 8, !tbaa !9
  br label %47, !llvm.loop !21

84:                                               ; preds = %47
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %85, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %86

86:                                               ; preds = %84, %44, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %87 = load ptr, ptr %2, align 8
  ret ptr %87
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.round.f64(double) #3

; 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 = { nounwind allocsize(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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

!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 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!14, !15, i64 0}
!14 = !{!"bmpread_t", !15, i64 0, !15, i64 4, !15, i64 8, !6, i64 16}
!15 = !{!"int", !7, i64 0}
!16 = !{!14, !15, i64 4}
!17 = !{!14, !6, i64 16}
!18 = !{!7, !7, i64 0}
!19 = !{!20, !20, i64 0}
!20 = !{!"float", !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"

; Function Attrs: nofree nounwind uwtable
define dso_local noalias noundef ptr @rgb2gray(ptr noundef readonly %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %2

2:                                                ; preds = %1
  %3 = load i32, ptr %0, align 8, !tbaa !5
  %.not29 = icmp eq i32 %3, 0
  br i1 %.not29, label %.loopexit, label %4

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  %6 = load i32, ptr %5, align 4, !tbaa !11
  %.not30 = icmp eq i32 %6, 0
  br i1 %.not30, label %.loopexit, label %7

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load ptr, ptr %8, align 8, !tbaa !12
  %.not31 = icmp eq ptr %9, null
  br i1 %.not31, label %.loopexit, label %10

10:                                               ; preds = %7
  %11 = mul nsw i32 %6, %3
  %12 = sext i32 %11 to i64
  %13 = shl nsw i64 %12, 2
  %14 = tail call noalias ptr @malloc(i64 noundef %13) #4
  %.not32 = icmp eq ptr %14, null
  br i1 %.not32, label %.loopexit, label %.preheader.preheader

.preheader.preheader:                             ; preds = %10
  %umax = tail call i64 @llvm.umax.i64(i64 %12, i64 1)
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %.035 = phi i64 [ %30, %.preheader ], [ 0, %.preheader.preheader ]
  %.02134 = phi ptr [ %29, %.preheader ], [ %14, %.preheader.preheader ]
  %.02233 = phi ptr [ %23, %.preheader ], [ %9, %.preheader.preheader ]
  %15 = getelementptr inbounds i8, ptr %.02233, i64 1
  %16 = load i8, ptr %.02233, align 1, !tbaa !13
  %17 = uitofp i8 %16 to double
  %18 = fmul double %17, 2.990000e-01
  %19 = getelementptr inbounds i8, ptr %.02233, i64 2
  %20 = load i8, ptr %15, align 1, !tbaa !13
  %21 = uitofp i8 %20 to double
  %22 = tail call double @llvm.fmuladd.f64(double %21, double 5.870000e-01, double %18)
  %23 = getelementptr inbounds i8, ptr %.02233, i64 3
  %24 = load i8, ptr %19, align 1, !tbaa !13
  %25 = uitofp i8 %24 to double
  %26 = tail call double @llvm.fmuladd.f64(double %25, double 1.140000e-01, double %22)
  %27 = tail call double @llvm.round.f64(double %26)
  %28 = fptrunc double %27 to float
  %29 = getelementptr inbounds i8, ptr %.02134, i64 4
  store float %28, ptr %.02134, align 4, !tbaa !14
  %30 = add nuw i64 %.035, 1
  %exitcond.not = icmp eq i64 %30, %umax
  br i1 %exitcond.not, label %.loopexit, label %.preheader, !llvm.loop !16

.loopexit:                                        ; preds = %.preheader, %10, %1, %2, %4, %7
  %.023 = phi ptr [ null, %7 ], [ null, %4 ], [ null, %2 ], [ null, %1 ], [ null, %10 ], [ %14, %.preheader ]
  ret ptr %.023
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

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

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

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

attributes #0 = { nofree 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind allocsize(0) }

!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 = !{!"bmpread_t", !7, i64 0, !7, i64 4, !7, i64 8, !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 4}
!12 = !{!6, !10, i64 16}
!13 = !{!8, !8, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"float", !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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