; LLVM IR from SSBMTonberry/pocketlzma :: ref_spec/lzma1900/CPP/7zip/Archive/Common (OutStreamWithCRC)

; 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.COutStreamWithCRC = type <{ %struct.ISequentialOutStream, %class.CMyUnknownImp, [4 x i8], %class.CMyComPtr, i64, i32, i8, [3 x i8] }>
%struct.ISequentialOutStream = type { %struct.IUnknown }
%struct.IUnknown = type { ptr }
%class.CMyUnknownImp = type { i32 }
%class.CMyComPtr = type { ptr }

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN17COutStreamWithCRC5WriteEPKvjPj(ptr noundef nonnull align 8 dereferenceable(37) %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) unnamed_addr #0 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  %10 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !9
  %11 = getelementptr inbounds %class.COutStreamWithCRC, ptr %10, i32 0, i32 3
  %12 = call noundef ptr @_ZNK9CMyComPtrI20ISequentialOutStreamEcvPS0_Ev(ptr noundef nonnull align 8 dereferenceable(8) %11)
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %23

14:                                               ; preds = %4
  %15 = getelementptr inbounds %class.COutStreamWithCRC, ptr %10, i32 0, i32 3
  %16 = call noundef ptr @_ZNK9CMyComPtrI20ISequentialOutStreamEptEv(ptr noundef nonnull align 8 dereferenceable(8) %15)
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = load ptr, ptr %16, align 8, !tbaa !11
  %20 = getelementptr inbounds ptr, ptr %19, i64 5
  %21 = load ptr, ptr %20, align 8
  %22 = call noundef i32 %21(ptr noundef nonnull align 8 dereferenceable(8) %16, ptr noundef %17, i32 noundef %18, ptr noundef %7)
  store i32 %22, ptr %9, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %14, %4
  %24 = getelementptr inbounds %class.COutStreamWithCRC, ptr %10, i32 0, i32 6
  %25 = load i8, ptr %24, align 4, !tbaa !13, !range !21, !noundef !22
  %26 = trunc i8 %25 to i1
  br i1 %26, label %27, label %35

27:                                               ; preds = %23
  %28 = getelementptr inbounds %class.COutStreamWithCRC, ptr %10, i32 0, i32 5
  %29 = load i32, ptr %28, align 8, !tbaa !23
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = zext i32 %31 to i64
  %33 = call i32 @CrcUpdate(i32 noundef %29, ptr noundef %30, i64 noundef %32)
  %34 = getelementptr inbounds %class.COutStreamWithCRC, ptr %10, i32 0, i32 5
  store i32 %33, ptr %34, align 8, !tbaa !23
  br label %35

35:                                               ; preds = %27, %23
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = zext i32 %36 to i64
  %38 = getelementptr inbounds %class.COutStreamWithCRC, ptr %10, i32 0, i32 4
  %39 = load i64, ptr %38, align 8, !tbaa !24
  %40 = add i64 %39, %37
  store i64 %40, ptr %38, align 8, !tbaa !24
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = icmp ne ptr %41, null
  br i1 %42, label %43, label %46

43:                                               ; preds = %35
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  store i32 %44, ptr %45, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %43, %35
  %47 = load i32, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret i32 %47
}

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

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

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

declare i32 @CrcUpdate(i32 noundef, ptr noundef, i64 noundef) #3

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

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 = { 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 = { "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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"vtable pointer", !8, i64 0}
!13 = !{!14, !20, i64 36}
!14 = !{!"_ZTS17COutStreamWithCRC", !15, i64 0, !17, i64 8, !18, i64 16, !19, i64 24, !10, i64 32, !20, i64 36}
!15 = !{!"_ZTS20ISequentialOutStream", !16, i64 0}
!16 = !{!"_ZTS8IUnknown"}
!17 = !{!"_ZTS13CMyUnknownImp", !10, i64 0}
!18 = !{!"_ZTS9CMyComPtrI20ISequentialOutStreamE", !6, i64 0}
!19 = !{!"long long", !7, i64 0}
!20 = !{!"bool", !7, i64 0}
!21 = !{i8 0, i8 2}
!22 = !{}
!23 = !{!14, !10, i64 32}
!24 = !{!14, !19, i64 24}


; 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
define dso_local noundef i32 @_ZN17COutStreamWithCRC5WriteEPKvjPj(ptr noundef nonnull align 8 dereferenceable(37) %0, ptr noundef %1, i32 noundef %2, ptr noundef writeonly %3) unnamed_addr #0 align 2 {
  %5 = alloca i32, align 4
  store i32 %2, ptr %5, align 4, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = tail call noundef ptr @_ZNK9CMyComPtrI20ISequentialOutStreamEcvPS0_Ev(ptr noundef nonnull align 8 dereferenceable(8) %6)
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %14, label %8

8:                                                ; preds = %4
  %9 = tail call noundef ptr @_ZNK9CMyComPtrI20ISequentialOutStreamEptEv(ptr noundef nonnull align 8 dereferenceable(8) %6)
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %10, i64 40
  %12 = load ptr, ptr %11, align 8
  %13 = call noundef i32 %12(ptr noundef nonnull align 8 dereferenceable(8) %9, ptr noundef %1, i32 noundef %2, ptr noundef nonnull %5)
  %.pre7.pre = load i32, ptr %5, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %8, %4
  %.pre7 = phi i32 [ %.pre7.pre, %8 ], [ %2, %4 ]
  %.0 = phi i32 [ %13, %8 ], [ 0, %4 ]
  %15 = getelementptr inbounds i8, ptr %0, i64 36
  %16 = load i8, ptr %15, align 4, !tbaa !11, !range !20, !noundef !21
  %17 = trunc nuw i8 %16 to i1
  br i1 %17, label %18, label %23

18:                                               ; preds = %14
  %19 = getelementptr inbounds i8, ptr %0, i64 32
  %20 = load i32, ptr %19, align 8, !tbaa !22
  %21 = zext i32 %.pre7 to i64
  %22 = call i32 @CrcUpdate(i32 noundef %20, ptr noundef %1, i64 noundef %21)
  store i32 %22, ptr %19, align 8, !tbaa !22
  %.pre = load i32, ptr %5, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %18, %14
  %24 = phi i32 [ %.pre, %18 ], [ %.pre7, %14 ]
  %25 = zext i32 %24 to i64
  %26 = getelementptr inbounds i8, ptr %0, i64 24
  %27 = load i64, ptr %26, align 8, !tbaa !23
  %28 = add i64 %27, %25
  store i64 %28, ptr %26, align 8, !tbaa !23
  %.not6 = icmp eq ptr %3, null
  br i1 %.not6, label %30, label %29

29:                                               ; preds = %23
  store i32 %24, ptr %3, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %29, %23
  ret i32 %.0
}

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

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

declare i32 @CrcUpdate(i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

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 = { 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 #2 = { "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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"vtable pointer", !8, i64 0}
!11 = !{!12, !19, i64 36}
!12 = !{!"_ZTS17COutStreamWithCRC", !13, i64 0, !15, i64 8, !16, i64 16, !18, i64 24, !6, i64 32, !19, i64 36}
!13 = !{!"_ZTS20ISequentialOutStream", !14, i64 0}
!14 = !{!"_ZTS8IUnknown"}
!15 = !{!"_ZTS13CMyUnknownImp", !6, i64 0}
!16 = !{!"_ZTS9CMyComPtrI20ISequentialOutStreamE", !17, i64 0}
!17 = !{!"any pointer", !7, i64 0}
!18 = !{!"long long", !7, i64 0}
!19 = !{!"bool", !7, i64 0}
!20 = !{i8 0, i8 2}
!21 = !{}
!22 = !{!12, !6, i64 32}
!23 = !{!12, !18, i64 24}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass