; LLVM IR from team-charls/charls :: src (jpeg_stream_writer)

; 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.charls::jpeg_stream_writer" = type { %"class.charls::span", i64, i8, %"class.std::vector" }
%"class.charls::span" = type { ptr, i64 }
%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<unsigned char, std::allocator<unsigned char>>::_Vector_impl" }
%"struct.std::_Vector_base<unsigned char, std::allocator<unsigned char>>::_Vector_impl" = type { %"struct.std::_Vector_base<unsigned char, std::allocator<unsigned char>>::_Vector_impl_data" }
%"struct.std::_Vector_base<unsigned char, std::allocator<unsigned char>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@.str.1 = external hidden unnamed_addr constant [79 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [73 x i8], align 1
@__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer27write_start_of_scan_segmentEiiNS_15interleave_modeE = external hidden unnamed_addr constant [114 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [70 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [131 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #0

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6charls18jpeg_stream_writer11write_uint8Eh(ptr noundef nonnull align 8 dereferenceable(56), i8 noundef zeroext) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56), i32 noundef) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i8 @_ZNSt14numeric_limitsIhE3maxEv() #2 align 2

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6charls18jpeg_stream_writer27write_start_of_scan_segmentEiiNS_15interleave_modeE(ptr noundef nonnull align 8 dereferenceable(56) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !11
  %10 = load ptr, ptr %5, align 8
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %13, label %18

13:                                               ; preds = %4
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = call noundef zeroext i8 @_ZNSt14numeric_limitsIhE3maxEv() #4
  %16 = zext i8 %15 to i32
  %17 = icmp sle i32 %14, %16
  br label %18

18:                                               ; preds = %13, %4
  %19 = phi i1 [ false, %4 ], [ %17, %13 ]
  br i1 %19, label %20, label %21

20:                                               ; preds = %18
  br label %23

21:                                               ; preds = %18
  call void @__assert_fail(ptr noundef @.str.8, ptr noundef @.str.1, i32 noundef 188, ptr noundef @__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer27write_start_of_scan_segmentEiiNS_15interleave_modeE) #5
  unreachable

22:                                               ; No predecessors!
  br label %23

23:                                               ; preds = %22, %20
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = icmp sge i32 %24, 0
  br i1 %25, label %26, label %31

26:                                               ; preds = %23
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = call noundef zeroext i8 @_ZNSt14numeric_limitsIhE3maxEv() #4
  %29 = zext i8 %28 to i32
  %30 = icmp sle i32 %27, %29
  br label %31

31:                                               ; preds = %26, %23
  %32 = phi i1 [ false, %23 ], [ %30, %26 ]
  br i1 %32, label %33, label %34

33:                                               ; preds = %31
  br label %36

34:                                               ; preds = %31
  call void @__assert_fail(ptr noundef @.str.9, ptr noundef @.str.1, i32 noundef 189, ptr noundef @__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer27write_start_of_scan_segmentEiiNS_15interleave_modeE) #5
  unreachable

35:                                               ; No predecessors!
  br label %36

36:                                               ; preds = %35, %33
  %37 = load i32, ptr %8, align 4, !tbaa !11
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %45, label %39

39:                                               ; preds = %36
  %40 = load i32, ptr %8, align 4, !tbaa !11
  %41 = icmp eq i32 %40, 1
  br i1 %41, label %45, label %42

42:                                               ; preds = %39
  %43 = load i32, ptr %8, align 4, !tbaa !11
  %44 = icmp eq i32 %43, 2
  br label %45

45:                                               ; preds = %42, %39, %36
  %46 = phi i1 [ true, %39 ], [ true, %36 ], [ %44, %42 ]
  br i1 %46, label %47, label %48

47:                                               ; preds = %45
  br label %50

48:                                               ; preds = %45
  call void @__assert_fail(ptr noundef @.str.10, ptr noundef @.str.1, i32 noundef 191, ptr noundef @__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer27write_start_of_scan_segmentEiiNS_15interleave_modeE) #5
  unreachable

49:                                               ; No predecessors!
  br label %50

50:                                               ; preds = %49, %47
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = mul i64 %52, 2
  %54 = add i64 1, %53
  %55 = add i64 %54, 3
  call void @_ZN6charls18jpeg_stream_writer20write_segment_headerENS_16jpeg_marker_codeEm(ptr noundef nonnull align 8 dereferenceable(56) %10, i8 noundef zeroext -38, i64 noundef %55)
  %56 = load i32, ptr %6, align 4, !tbaa !9
  call void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56) %10, i32 noundef %56) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %57

57:                                               ; preds = %71, %50
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = load i32, ptr %6, align 4, !tbaa !9
  %60 = icmp ne i32 %58, %59
  br i1 %60, label %62, label %61

61:                                               ; preds = %57
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %74

62:                                               ; preds = %57
  %63 = getelementptr inbounds %"class.charls::jpeg_stream_writer", ptr %10, i32 0, i32 2
  %64 = load i8, ptr %63, align 8, !tbaa !13
  %65 = zext i8 %64 to i32
  %66 = add nsw i32 %65, 1
  call void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56) %10, i32 noundef %66) #4
  %67 = call noundef zeroext i8 @_ZNK6charls18jpeg_stream_writer22mapping_table_selectorEv(ptr noundef nonnull align 8 dereferenceable(56) %10) #4
  call void @_ZN6charls18jpeg_stream_writer11write_uint8Eh(ptr noundef nonnull align 8 dereferenceable(56) %10, i8 noundef zeroext %67) #4
  %68 = getelementptr inbounds %"class.charls::jpeg_stream_writer", ptr %10, i32 0, i32 2
  %69 = load i8, ptr %68, align 8, !tbaa !13
  %70 = add i8 %69, 1
  store i8 %70, ptr %68, align 8, !tbaa !13
  br label %71

71:                                               ; preds = %62
  %72 = load i32, ptr %9, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %9, align 4, !tbaa !9
  br label %57, !llvm.loop !21

74:                                               ; preds = %61
  %75 = load i32, ptr %7, align 4, !tbaa !9
  call void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56) %10, i32 noundef %75) #4
  %76 = load i32, ptr %8, align 4, !tbaa !11
  %77 = call noundef i32 @_ZN6charls18to_underlying_typeINS_15interleave_modeEEEDaT_(i32 noundef %76) #4
  call void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56) %10, i32 noundef %77) #4
  call void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56) %10, i32 noundef 0) #4
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i8 @_ZNK6charls18jpeg_stream_writer22mapping_table_selectorEv(ptr noundef nonnull align 8 dereferenceable(56)) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN6charls18to_underlying_typeINS_15interleave_modeEEEDaT_(i32 noundef) #2

attributes #0 = { noreturn nounwind "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 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 = { 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 #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind }
attributes #5 = { noreturn 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_ZTSN6charls15interleave_modeE", !7, i64 0}
!13 = !{!14, !7, i64 24}
!14 = !{!"_ZTSN6charls18jpeg_stream_writerE", !15, i64 0, !16, i64 16, !7, i64 24, !17, i64 32}
!15 = !{!"_ZTSN6charls4spanISt4byteEE", !6, i64 0, !16, i64 8}
!16 = !{!"long", !7, i64 0}
!17 = !{!"_ZTSSt6vectorIhSaIhEE", !18, i64 0}
!18 = !{!"_ZTSSt12_Vector_baseIhSaIhEE", !19, i64 0}
!19 = !{!"_ZTSNSt12_Vector_baseIhSaIhEE12_Vector_implE", !20, i64 0}
!20 = !{!"_ZTSNSt12_Vector_baseIhSaIhEE17_Vector_impl_dataE", !6, i64 0, !6, i64 8, !6, i64 16}
!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"

@.str.1 = external hidden unnamed_addr constant [79 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [73 x i8], align 1
@__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer27write_start_of_scan_segmentEiiNS_15interleave_modeE = external hidden unnamed_addr constant [114 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [70 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [131 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6charls18jpeg_stream_writer11write_uint8Eh(ptr noundef nonnull align 8 dereferenceable(56), i8 noundef zeroext) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56), i32 noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i8 @_ZNSt14numeric_limitsIhE3maxEv() local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6charls18jpeg_stream_writer27write_start_of_scan_segmentEiiNS_15interleave_modeE(ptr noundef nonnull align 8 dereferenceable(56) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #1 align 2 {
  %5 = icmp sgt i32 %1, 0
  br i1 %5, label %6, label %.critedge

6:                                                ; preds = %4
  %7 = tail call noundef zeroext i8 @_ZNSt14numeric_limitsIhE3maxEv() #3
  %8 = zext i8 %7 to i32
  %.not = icmp ult i32 %8, %1
  br i1 %.not, label %.critedge, label %9

.critedge:                                        ; preds = %4, %6
  tail call void @__assert_fail(ptr noundef nonnull @.str.8, ptr noundef nonnull @.str.1, i32 noundef 188, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer27write_start_of_scan_segmentEiiNS_15interleave_modeE) #4
  unreachable

9:                                                ; preds = %6
  %10 = icmp sgt i32 %2, -1
  br i1 %10, label %11, label %.critedge2

11:                                               ; preds = %9
  %12 = tail call noundef zeroext i8 @_ZNSt14numeric_limitsIhE3maxEv() #3
  %13 = zext i8 %12 to i32
  %.not23 = icmp ult i32 %13, %2
  br i1 %.not23, label %.critedge2, label %14

.critedge2:                                       ; preds = %9, %11
  tail call void @__assert_fail(ptr noundef nonnull @.str.9, ptr noundef nonnull @.str.1, i32 noundef 189, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer27write_start_of_scan_segmentEiiNS_15interleave_modeE) #4
  unreachable

14:                                               ; preds = %11
  %or.cond7 = icmp ult i32 %3, 3
  br i1 %or.cond7, label %.critedge5, label %15

15:                                               ; preds = %14
  tail call void @__assert_fail(ptr noundef nonnull @.str.10, ptr noundef nonnull @.str.1, i32 noundef 191, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer27write_start_of_scan_segmentEiiNS_15interleave_modeE) #4
  unreachable

.critedge5:                                       ; preds = %14
  %16 = shl nuw nsw i32 %1, 1
  %narrow = add nuw nsw i32 %16, 4
  %17 = zext nneg i32 %narrow to i64
  tail call void @_ZN6charls18jpeg_stream_writer20write_segment_headerENS_16jpeg_marker_codeEm(ptr noundef nonnull align 8 dereferenceable(56) %0, i8 noundef zeroext -38, i64 noundef %17)
  tail call void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56) %0, i32 noundef %1) #3
  %18 = getelementptr inbounds i8, ptr %0, i64 24
  %.pre = load i8, ptr %18, align 8, !tbaa !5
  br label %21

19:                                               ; preds = %21
  tail call void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56) %0, i32 noundef %2) #3
  %20 = tail call noundef i32 @_ZN6charls18to_underlying_typeINS_15interleave_modeEEEDaT_(i32 noundef %3) #3
  tail call void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56) %0, i32 noundef %20) #3
  tail call void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56) %0, i32 noundef 0) #3
  ret void

21:                                               ; preds = %.critedge5, %21
  %22 = phi i8 [ %.pre, %.critedge5 ], [ %27, %21 ]
  %.025 = phi i32 [ 0, %.critedge5 ], [ %28, %21 ]
  %23 = zext i8 %22 to i32
  %24 = add nuw nsw i32 %23, 1
  tail call void @_ZN6charls18jpeg_stream_writer11write_uint8Ei(ptr noundef nonnull align 8 dereferenceable(56) %0, i32 noundef %24) #3
  %25 = tail call noundef zeroext i8 @_ZNK6charls18jpeg_stream_writer22mapping_table_selectorEv(ptr noundef nonnull align 8 dereferenceable(56) %0) #3
  tail call void @_ZN6charls18jpeg_stream_writer11write_uint8Eh(ptr noundef nonnull align 8 dereferenceable(56) %0, i8 noundef zeroext %25) #3
  %26 = load i8, ptr %18, align 8, !tbaa !5
  %27 = add i8 %26, 1
  store i8 %27, ptr %18, align 8, !tbaa !5
  %28 = add nuw nsw i32 %.025, 1
  %.not24 = icmp eq i32 %28, %1
  br i1 %.not24, label %19, label %21, !llvm.loop !16
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i8 @_ZNK6charls18jpeg_stream_writer22mapping_table_selectorEv(ptr noundef nonnull align 8 dereferenceable(56)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN6charls18to_underlying_typeINS_15interleave_modeEEEDaT_(i32 noundef) local_unnamed_addr #2

attributes #0 = { noreturn nounwind "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 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 = { 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 #3 = { nounwind }
attributes #4 = { noreturn 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, !9, i64 24}
!6 = !{!"_ZTSN6charls18jpeg_stream_writerE", !7, i64 0, !11, i64 16, !9, i64 24, !12, i64 32}
!7 = !{!"_ZTSN6charls4spanISt4byteEE", !8, i64 0, !11, i64 8}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"_ZTSSt6vectorIhSaIhEE", !13, i64 0}
!13 = !{!"_ZTSSt12_Vector_baseIhSaIhEE", !14, i64 0}
!14 = !{!"_ZTSNSt12_Vector_baseIhSaIhEE12_Vector_implE", !15, i64 0}
!15 = !{!"_ZTSNSt12_Vector_baseIhSaIhEE17_Vector_impl_dataE", !8, i64 0, !8, i64 8, !8, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass