; LLVM IR from ermig1979/Simd :: src/Simd/SimdBaseImageSavePpm.ll (ZN4Simd4Base16ImagePgmBinSaver8ToStreamEPKhm)

; 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"

%"class.Simd::ImageSaver" = type { ptr, %"struct.Simd::ImageSaverParam", %"class.Simd::OutputMemoryStream" }
%"struct.Simd::ImageSaverParam" = type { i64, i64, i32, i32, i32, i32 }
%"class.Simd::OutputMemoryStream" = type { i64, ptr, i64, i64, i64, i64, i64 }
%"class.Simd::Base::ImagePxmSaver" = type { %"class.Simd::ImageSaver", ptr, %"struct.Simd::Array", i64, i64 }
%"struct.Simd::Array" = type { ptr, i64 }

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN4Simd4Base13ImagePxmSaver11WriteHeaderEm(ptr noundef nonnull align 8 dereferenceable(136), i64 noundef) #0 align 2

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

; Function Attrs: alwaysinline mustprogress uwtable
declare dso_local void @_ZN4Simd18OutputMemoryStream5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(56), ptr noundef, i64 noundef) #2 align 2

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

; Function Attrs: alwaysinline mustprogress uwtable
declare dso_local void @_ZN4Simd18OutputMemoryStream7ReserveEm(ptr noundef nonnull align 8 dereferenceable(56), i64 noundef) #2 align 2

; Function Attrs: alwaysinline mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN4Simd3MinImEET_S1_S1_(i64 noundef, i64 noundef) #3

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN4Simd4Base16ImagePgmBinSaver8ToStreamEPKhm(ptr noundef nonnull align 8 dereferenceable(136) %0, ptr noundef %1, i64 noundef %2) unnamed_addr #0 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  %13 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %14 = getelementptr inbounds %"class.Simd::ImageSaver", ptr %13, i32 0, i32 1
  %15 = getelementptr inbounds %"struct.Simd::ImageSaverParam", ptr %14, i32 0, i32 2
  %16 = load i32, ptr %15, align 8, !tbaa !11
  %17 = icmp eq i32 %16, 1
  br i1 %17, label %18, label %20

18:                                               ; preds = %3
  %19 = load i64, ptr %6, align 8, !tbaa !9
  br label %23

20:                                               ; preds = %3
  %21 = getelementptr inbounds %"class.Simd::Base::ImagePxmSaver", ptr %13, i32 0, i32 4
  %22 = load i64, ptr %21, align 8, !tbaa !19
  br label %23

23:                                               ; preds = %20, %18
  %24 = phi i64 [ %19, %18 ], [ %22, %20 ]
  store i64 %24, ptr %7, align 8, !tbaa !9
  %25 = getelementptr inbounds %"class.Simd::ImageSaver", ptr %13, i32 0, i32 2
  %26 = getelementptr inbounds %"class.Simd::ImageSaver", ptr %13, i32 0, i32 1
  %27 = getelementptr inbounds %"struct.Simd::ImageSaverParam", ptr %26, i32 0, i32 1
  %28 = load i64, ptr %27, align 8, !tbaa !22
  %29 = getelementptr inbounds %"class.Simd::Base::ImagePxmSaver", ptr %13, i32 0, i32 4
  %30 = load i64, ptr %29, align 8, !tbaa !19
  %31 = mul i64 %28, %30
  %32 = add i64 32, %31
  call void @_ZN4Simd18OutputMemoryStream7ReserveEm(ptr noundef nonnull align 8 dereferenceable(56) %25, i64 noundef %32)
  call void @_ZN4Simd4Base13ImagePxmSaver11WriteHeaderEm(ptr noundef nonnull align 8 dereferenceable(136) %13, i64 noundef 5)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %89, %23
  %34 = load i64, ptr %8, align 8, !tbaa !9
  %35 = getelementptr inbounds %"class.Simd::ImageSaver", ptr %13, i32 0, i32 1
  %36 = getelementptr inbounds %"struct.Simd::ImageSaverParam", ptr %35, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !22
  %38 = icmp ult i64 %34, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %33
  store i32 2, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %98

40:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %41 = load i64, ptr %8, align 8, !tbaa !9
  %42 = getelementptr inbounds %"class.Simd::Base::ImagePxmSaver", ptr %13, i32 0, i32 3
  %43 = load i64, ptr %42, align 8, !tbaa !23
  %44 = add i64 %41, %43
  %45 = getelementptr inbounds %"class.Simd::ImageSaver", ptr %13, i32 0, i32 1
  %46 = getelementptr inbounds %"struct.Simd::ImageSaverParam", ptr %45, i32 0, i32 1
  %47 = load i64, ptr %46, align 8, !tbaa !22
  %48 = call noundef i64 @_ZN4Simd3MinImEET_S1_S1_(i64 noundef %44, i64 noundef %47)
  %49 = load i64, ptr %8, align 8, !tbaa !9
  %50 = sub i64 %48, %49
  store i64 %50, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %51, ptr %11, align 8, !tbaa !5
  %52 = getelementptr inbounds %"class.Simd::ImageSaver", ptr %13, i32 0, i32 1
  %53 = getelementptr inbounds %"struct.Simd::ImageSaverParam", ptr %52, i32 0, i32 2
  %54 = load i32, ptr %53, align 8, !tbaa !11
  %55 = icmp ne i32 %54, 1
  br i1 %55, label %56, label %72

56:                                               ; preds = %40
  %57 = getelementptr inbounds %"class.Simd::Base::ImagePxmSaver", ptr %13, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !24
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %"class.Simd::ImageSaver", ptr %13, i32 0, i32 1
  %61 = getelementptr inbounds %"struct.Simd::ImageSaverParam", ptr %60, i32 0, i32 0
  %62 = load i64, ptr %61, align 8, !tbaa !25
  %63 = load i64, ptr %10, align 8, !tbaa !9
  %64 = load i64, ptr %6, align 8, !tbaa !9
  %65 = getelementptr inbounds %"class.Simd::Base::ImagePxmSaver", ptr %13, i32 0, i32 2
  %66 = getelementptr inbounds %"struct.Simd::Array", ptr %65, i32 0, i32 0
  %67 = load ptr, ptr %66, align 8, !tbaa !26
  %68 = load i64, ptr %7, align 8, !tbaa !9
  call void %58(ptr noundef %59, i64 noundef %62, i64 noundef %63, i64 noundef %64, ptr noundef %67, i64 noundef %68)
  %69 = getelementptr inbounds %"class.Simd::Base::ImagePxmSaver", ptr %13, i32 0, i32 2
  %70 = getelementptr inbounds %"struct.Simd::Array", ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !26
  store ptr %71, ptr %11, align 8, !tbaa !5
  br label %72

72:                                               ; preds = %56, %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %73

73:                                               ; preds = %86, %72
  %74 = load i64, ptr %12, align 8, !tbaa !9
  %75 = load i64, ptr %10, align 8, !tbaa !9
  %76 = icmp ult i64 %74, %75
  br i1 %76, label %78, label %77

77:                                               ; preds = %73
  store i32 4, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %89

78:                                               ; preds = %73
  %79 = getelementptr inbounds %"class.Simd::ImageSaver", ptr %13, i32 0, i32 2
  %80 = load ptr, ptr %11, align 8, !tbaa !5
  %81 = getelementptr inbounds %"class.Simd::Base::ImagePxmSaver", ptr %13, i32 0, i32 4
  %82 = load i64, ptr %81, align 8, !tbaa !19
  call void @_ZN4Simd18OutputMemoryStream5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(56) %79, ptr noundef %80, i64 noundef %82)
  %83 = load i64, ptr %7, align 8, !tbaa !9
  %84 = load ptr, ptr %11, align 8, !tbaa !5
  %85 = getelementptr inbounds i8, ptr %84, i64 %83
  store ptr %85, ptr %11, align 8, !tbaa !5
  br label %86

86:                                               ; preds = %78
  %87 = load i64, ptr %12, align 8, !tbaa !9
  %88 = add i64 %87, 1
  store i64 %88, ptr %12, align 8, !tbaa !9
  br label %73, !llvm.loop !27

89:                                               ; preds = %77
  %90 = load i64, ptr %6, align 8, !tbaa !9
  %91 = load i64, ptr %10, align 8, !tbaa !9
  %92 = mul i64 %90, %91
  %93 = load ptr, ptr %5, align 8, !tbaa !5
  %94 = getelementptr inbounds i8, ptr %93, i64 %92
  store ptr %94, ptr %5, align 8, !tbaa !5
  %95 = load i64, ptr %10, align 8, !tbaa !9
  %96 = load i64, ptr %8, align 8, !tbaa !9
  %97 = add i64 %96, %95
  store i64 %97, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %33, !llvm.loop !29

98:                                               ; preds = %39
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret i1 true
}

attributes #0 = { mustprogress 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 = { alwaysinline mustprogress 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 = { alwaysinline mustprogress 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 #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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !14, i64 24}
!12 = !{!"_ZTSN4Simd10ImageSaverE", !13, i64 8, !18, i64 40}
!13 = !{!"_ZTSN4Simd15ImageSaverParamE", !10, i64 0, !10, i64 8, !14, i64 16, !15, i64 20, !16, i64 24, !17, i64 28}
!14 = !{!"_ZTS19SimdPixelFormatType", !7, i64 0}
!15 = !{!"_ZTS17SimdImageFileType", !7, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!"_ZTS11SimdYuvType", !7, i64 0}
!18 = !{!"_ZTSN4Simd18OutputMemoryStreamE", !10, i64 0, !6, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!19 = !{!20, !10, i64 128}
!20 = !{!"_ZTSN4Simd4Base13ImagePxmSaverE", !12, i64 0, !6, i64 96, !21, i64 104, !10, i64 120, !10, i64 128}
!21 = !{!"_ZTSN4Simd5ArrayIhEE", !6, i64 0, !10, i64 8}
!22 = !{!12, !10, i64 16}
!23 = !{!20, !10, i64 120}
!24 = !{!20, !6, i64 96}
!25 = !{!12, !10, i64 8}
!26 = !{!20, !6, i64 104}
!27 = distinct !{!27, !28}
!28 = !{!"llvm.loop.mustprogress"}
!29 = distinct !{!29, !28}


; 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: mustprogress uwtable
declare dso_local void @_ZN4Simd4Base13ImagePxmSaver11WriteHeaderEm(ptr noundef nonnull align 8 dereferenceable(136), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: alwaysinline mustprogress uwtable
declare dso_local void @_ZN4Simd18OutputMemoryStream5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(56), ptr noundef, i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: alwaysinline mustprogress uwtable
declare dso_local void @_ZN4Simd18OutputMemoryStream7ReserveEm(ptr noundef nonnull align 8 dereferenceable(56), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: alwaysinline mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN4Simd3MinImEET_S1_S1_(i64 noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN4Simd4Base16ImagePgmBinSaver8ToStreamEPKhm(ptr noundef nonnull align 8 dereferenceable(136) %0, ptr noundef %1, i64 noundef %2) unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = icmp eq i32 %6, 1
  %8 = getelementptr inbounds i8, ptr %0, i64 128
  %9 = load i64, ptr %8, align 8
  %10 = select i1 %7, i64 %2, i64 %9
  %11 = getelementptr inbounds i8, ptr %0, i64 40
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load i64, ptr %12, align 8, !tbaa !17
  %14 = mul i64 %13, %9
  %15 = add i64 %14, 32
  tail call void @_ZN4Simd18OutputMemoryStream7ReserveEm(ptr noundef nonnull align 8 dereferenceable(56) %11, i64 noundef %15)
  tail call void @_ZN4Simd4Base13ImagePxmSaver11WriteHeaderEm(ptr noundef nonnull align 8 dereferenceable(136) %0, i64 noundef 5)
  %16 = load i64, ptr %12, align 8, !tbaa !17
  %.not32 = icmp eq i64 %16, 0
  br i1 %.not32, label %._crit_edge31, label %.lr.ph30

.lr.ph30:                                         ; preds = %3
  %17 = getelementptr inbounds i8, ptr %0, i64 120
  %18 = getelementptr inbounds i8, ptr %0, i64 96
  %19 = getelementptr inbounds i8, ptr %0, i64 104
  br label %20

._crit_edge31:                                    ; preds = %._crit_edge, %3
  ret i1 true

20:                                               ; preds = %.lr.ph30, %._crit_edge
  %21 = phi i64 [ %16, %.lr.ph30 ], [ %35, %._crit_edge ]
  %.02128 = phi ptr [ %1, %.lr.ph30 ], [ %34, %._crit_edge ]
  %.02227 = phi i64 [ 0, %.lr.ph30 ], [ %24, %._crit_edge ]
  %22 = load i64, ptr %17, align 8, !tbaa !18
  %23 = add i64 %22, %.02227
  %24 = tail call noundef i64 @_ZN4Simd3MinImEET_S1_S1_(i64 noundef %23, i64 noundef %21)
  %25 = sub i64 %24, %.02227
  %26 = load i32, ptr %5, align 8, !tbaa !5
  %.not = icmp eq i32 %26, 1
  br i1 %.not, label %32, label %27

27:                                               ; preds = %20
  %28 = load ptr, ptr %18, align 8, !tbaa !21
  %29 = load i64, ptr %4, align 8, !tbaa !22
  %30 = load ptr, ptr %19, align 8, !tbaa !23
  tail call void %28(ptr noundef %.02128, i64 noundef %29, i64 noundef %25, i64 noundef %2, ptr noundef %30, i64 noundef %10)
  %31 = load ptr, ptr %19, align 8, !tbaa !23
  br label %32

32:                                               ; preds = %27, %20
  %.020 = phi ptr [ %31, %27 ], [ %.02128, %20 ]
  %.not33 = icmp eq i64 %24, %.02227
  br i1 %.not33, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %32
  %33 = mul i64 %25, %2
  %34 = getelementptr inbounds i8, ptr %.02128, i64 %33
  %35 = load i64, ptr %12, align 8, !tbaa !17
  %36 = icmp ult i64 %24, %35
  br i1 %36, label %20, label %._crit_edge31, !llvm.loop !24

.lr.ph:                                           ; preds = %32, %.lr.ph
  %.026 = phi i64 [ %39, %.lr.ph ], [ 0, %32 ]
  %.125 = phi ptr [ %38, %.lr.ph ], [ %.020, %32 ]
  %37 = load i64, ptr %8, align 8, !tbaa !26
  tail call void @_ZN4Simd18OutputMemoryStream5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(56) %11, ptr noundef %.125, i64 noundef %37)
  %38 = getelementptr inbounds i8, ptr %.125, i64 %10
  %39 = add nuw i64 %.026, 1
  %exitcond.not = icmp eq i64 %39, %25
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !27
}

attributes #0 = { mustprogress 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 = { alwaysinline mustprogress 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 = { alwaysinline mustprogress 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" }

!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, !11, i64 24}
!6 = !{!"_ZTSN4Simd10ImageSaverE", !7, i64 8, !15, i64 40}
!7 = !{!"_ZTSN4Simd15ImageSaverParamE", !8, i64 0, !8, i64 8, !11, i64 16, !12, i64 20, !13, i64 24, !14, i64 28}
!8 = !{!"long", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"_ZTS19SimdPixelFormatType", !9, i64 0}
!12 = !{!"_ZTS17SimdImageFileType", !9, i64 0}
!13 = !{!"int", !9, i64 0}
!14 = !{!"_ZTS11SimdYuvType", !9, i64 0}
!15 = !{!"_ZTSN4Simd18OutputMemoryStreamE", !8, i64 0, !16, i64 8, !8, i64 16, !8, i64 24, !8, i64 32, !8, i64 40, !8, i64 48}
!16 = !{!"any pointer", !9, i64 0}
!17 = !{!6, !8, i64 16}
!18 = !{!19, !8, i64 120}
!19 = !{!"_ZTSN4Simd4Base13ImagePxmSaverE", !6, i64 0, !16, i64 96, !20, i64 104, !8, i64 120, !8, i64 128}
!20 = !{!"_ZTSN4Simd5ArrayIhEE", !16, i64 0, !8, i64 8}
!21 = !{!19, !16, i64 96}
!22 = !{!6, !8, i64 8}
!23 = !{!19, !16, i64 104}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!19, !8, i64 128}
!27 = distinct !{!27, !25}


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