; 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.16 = external hidden unnamed_addr constant [35 x i8], align 1
@__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer20write_segment_headerENS_16jpeg_marker_codeEm = external hidden unnamed_addr constant [92 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
define dso_local void @_ZN6charls18jpeg_stream_writer20write_segment_headerENS_16jpeg_marker_codeEm(ptr noundef nonnull align 8 dereferenceable(56) %0, i8 noundef zeroext %1, i64 noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !11
  %9 = load ptr, ptr %4, align 8
  %10 = load i64, ptr %6, align 8, !tbaa !11
  %11 = icmp ule i64 %10, 65533
  br i1 %11, label %12, label %13

12:                                               ; preds = %3
  br label %15

13:                                               ; preds = %3
  call void @__assert_fail(ptr noundef @.str.16, ptr noundef @.str.1, i32 noundef 232, ptr noundef @__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer20write_segment_headerENS_16jpeg_marker_codeEm) #6
  unreachable

14:                                               ; No predecessors!
  br label %15

15:                                               ; preds = %14, %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #7
  store i64 2, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #7
  %16 = load i64, ptr %6, align 8, !tbaa !11
  %17 = add i64 4, %16
  store i64 %17, ptr %8, align 8, !tbaa !11
  %18 = getelementptr inbounds %"class.charls::jpeg_stream_writer", ptr %9, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !13
  %20 = load i64, ptr %8, align 8, !tbaa !11
  %21 = add i64 %19, %20
  %22 = getelementptr inbounds %"class.charls::jpeg_stream_writer", ptr %9, i32 0, i32 0
  %23 = call noundef i64 @_ZNK6charls4spanISt4byteE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %22) #7
  %24 = icmp ugt i64 %21, %23
  %25 = xor i1 %24, true
  %26 = xor i1 %25, true
  %27 = zext i1 %26 to i64
  %28 = call i64 @llvm.expect.i64(i64 %27, i64 0)
  %29 = icmp ne i64 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %15
  call void @_ZN6charls4impl18throw_jpegls_errorENS_11jpegls_errcE(i32 noundef 3) #8
  unreachable

31:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #7
  %32 = load i8, ptr %5, align 1, !tbaa !9
  call void @_ZN6charls18jpeg_stream_writer12write_markerENS_16jpeg_marker_codeE(ptr noundef nonnull align 8 dereferenceable(56) %9, i8 noundef zeroext %32) #7
  %33 = load i64, ptr %6, align 8, !tbaa !11
  %34 = add i64 2, %33
  %35 = trunc i64 %34 to i16
  call void @_ZN6charls18jpeg_stream_writer12write_uint16Et(ptr noundef nonnull align 8 dereferenceable(56) %9, i16 noundef zeroext %35) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #7
  ret void
}

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK6charls4spanISt4byteE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16)) #3 align 2

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

; Function Attrs: mustprogress noinline noreturn uwtable
declare dso_local void @_ZN6charls4impl18throw_jpegls_errorENS_11jpegls_errcE(i32 noundef) #5

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6charls18jpeg_stream_writer12write_uint16Et(ptr noundef nonnull align 8 dereferenceable(56), i16 noundef zeroext) #3 align 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #5 = { mustprogress noinline noreturn 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 #6 = { noreturn nounwind }
attributes #7 = { nounwind }
attributes #8 = { noreturn }

!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 = !{!"_ZTSN6charls16jpeg_marker_codeE", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !12, i64 16}
!14 = !{!"_ZTSN6charls18jpeg_stream_writerE", !15, i64 0, !12, i64 16, !7, i64 24, !16, i64 32}
!15 = !{!"_ZTSN6charls4spanISt4byteEE", !6, i64 0, !12, i64 8}
!16 = !{!"_ZTSSt6vectorIhSaIhEE", !17, i64 0}
!17 = !{!"_ZTSSt12_Vector_baseIhSaIhEE", !18, i64 0}
!18 = !{!"_ZTSNSt12_Vector_baseIhSaIhEE12_Vector_implE", !19, i64 0}
!19 = !{!"_ZTSNSt12_Vector_baseIhSaIhEE17_Vector_impl_dataE", !6, i64 0, !6, i64 8, !6, i64 16}


; 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.16 = external hidden unnamed_addr constant [35 x i8], align 1
@__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer20write_segment_headerENS_16jpeg_marker_codeEm = external hidden unnamed_addr constant [92 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
define dso_local void @_ZN6charls18jpeg_stream_writer20write_segment_headerENS_16jpeg_marker_codeEm(ptr noundef nonnull align 8 dereferenceable(56) %0, i8 noundef zeroext %1, i64 noundef %2) local_unnamed_addr #1 align 2 {
  %4 = icmp ult i64 %2, 65534
  br i1 %4, label %6, label %5

5:                                                ; preds = %3
  tail call void @__assert_fail(ptr noundef nonnull @.str.16, ptr noundef nonnull @.str.1, i32 noundef 232, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN6charls18jpeg_stream_writer20write_segment_headerENS_16jpeg_marker_codeEm) #4
  unreachable

6:                                                ; preds = %3
  %7 = add nuw nsw i64 %2, 4
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load i64, ptr %8, align 8, !tbaa !5
  %10 = add i64 %7, %9
  %11 = tail call noundef i64 @_ZNK6charls4spanISt4byteE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %0) #5
  %12 = icmp ugt i64 %10, %11
  br i1 %12, label %13, label %14, !prof !16

13:                                               ; preds = %6
  tail call void @_ZN6charls4impl18throw_jpegls_errorENS_11jpegls_errcE(i32 noundef 3) #6
  unreachable

14:                                               ; preds = %6
  tail call void @_ZN6charls18jpeg_stream_writer12write_markerENS_16jpeg_marker_codeE(ptr noundef nonnull align 8 dereferenceable(56) %0, i8 noundef zeroext %1) #5
  %15 = trunc nuw i64 %2 to i16
  %16 = add nuw i16 %15, 2
  tail call void @_ZN6charls18jpeg_stream_writer12write_uint16Et(ptr noundef nonnull align 8 dereferenceable(56) %0, i16 noundef zeroext %16) #5
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK6charls4spanISt4byteE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress noinline noreturn uwtable
declare dso_local void @_ZN6charls4impl18throw_jpegls_errorENS_11jpegls_errcE(i32 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN6charls18jpeg_stream_writer12write_markerENS_16jpeg_marker_codeE(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_writer12write_uint16Et(ptr noundef nonnull align 8 dereferenceable(56), i16 noundef zeroext) local_unnamed_addr #2 align 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 = { mustprogress noinline noreturn 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 = { noreturn nounwind }
attributes #5 = { nounwind }
attributes #6 = { noreturn }

!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 16}
!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 = !{!"branch_weights", !"expected", i32 1, i32 2000}


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