; LLVM IR from mncoppola/rpef :: utilities_src/squashfs4.2/lzma465/CPP/7zip/Common (LimitedStreams)

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

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

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

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

15:                                               ; preds = %4
  %16 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %15, %4
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = zext i32 %18 to i64
  %20 = getelementptr inbounds %class.CLimitedSequentialOutStream, ptr %12, i32 0, i32 4
  %21 = load i64, ptr %20, align 8, !tbaa !11
  %22 = icmp ugt i64 %19, %21
  br i1 %22, label %23, label %32

23:                                               ; preds = %17
  %24 = getelementptr inbounds %class.CLimitedSequentialOutStream, ptr %12, i32 0, i32 4
  %25 = load i64, ptr %24, align 8, !tbaa !11
  %26 = trunc i64 %25 to i32
  store i32 %26, ptr %8, align 4, !tbaa !9
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %31

29:                                               ; preds = %23
  %30 = getelementptr inbounds %class.CLimitedSequentialOutStream, ptr %12, i32 0, i32 5
  store i8 1, ptr %30, align 8, !tbaa !19
  store i32 -2147467259, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %58

31:                                               ; preds = %23
  br label %32

32:                                               ; preds = %31, %17
  %33 = getelementptr inbounds %class.CLimitedSequentialOutStream, ptr %12, i32 0, i32 3
  %34 = call noundef ptr @_ZNK9CMyComPtrI20ISequentialOutStreamEcvPS0_Ev(ptr noundef nonnull align 8 dereferenceable(8) %33)
  %35 = icmp ne ptr %34, null
  br i1 %35, label %36, label %45

36:                                               ; preds = %32
  %37 = getelementptr inbounds %class.CLimitedSequentialOutStream, ptr %12, i32 0, i32 3
  %38 = call noundef ptr @_ZNK9CMyComPtrI20ISequentialOutStreamEptEv(ptr noundef nonnull align 8 dereferenceable(8) %37)
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = load ptr, ptr %38, align 8, !tbaa !20
  %42 = getelementptr inbounds ptr, ptr %41, i64 5
  %43 = load ptr, ptr %42, align 8
  %44 = call noundef i32 %43(ptr noundef nonnull align 8 dereferenceable(8) %38, ptr noundef %39, i32 noundef %40, ptr noundef %8)
  store i32 %44, ptr %10, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %36, %32
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds %class.CLimitedSequentialOutStream, ptr %12, i32 0, i32 4
  %49 = load i64, ptr %48, align 8, !tbaa !11
  %50 = sub i64 %49, %47
  store i64 %50, ptr %48, align 8, !tbaa !11
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = icmp ne ptr %51, null
  br i1 %52, label %53, label %56

53:                                               ; preds = %45
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %54, ptr %55, align 4, !tbaa !9
  br label %56

56:                                               ; preds = %53, %45
  %57 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %57, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %58

58:                                               ; preds = %56, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %59 = load i32, ptr %5, align 4
  ret i32 %59
}

; 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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 }

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


; 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 @_ZN27CLimitedSequentialOutStream5WriteEPKvjPj(ptr noundef nonnull align 8 dereferenceable(33) %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
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %7, label %6

6:                                                ; preds = %4
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %6, %4
  %8 = zext i32 %2 to i64
  %9 = getelementptr inbounds i8, ptr %0, i64 24
  %10 = load i64, ptr %9, align 8, !tbaa !9
  %11 = icmp ult i64 %10, %8
  br i1 %11, label %12, label %17

12:                                               ; preds = %7
  %13 = trunc nuw i64 %10 to i32
  store i32 %13, ptr %5, align 4, !tbaa !5
  %14 = icmp eq i64 %10, 0
  br i1 %14, label %15, label %17

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %0, i64 32
  store i8 1, ptr %16, align 8, !tbaa !18
  br label %33

17:                                               ; preds = %12, %7
  %18 = phi i32 [ %13, %12 ], [ %2, %7 ]
  %19 = getelementptr inbounds i8, ptr %0, i64 16
  %20 = tail call noundef ptr @_ZNK9CMyComPtrI20ISequentialOutStreamEcvPS0_Ev(ptr noundef nonnull align 8 dereferenceable(8) %19)
  %.not10 = icmp eq ptr %20, null
  br i1 %.not10, label %27, label %21

21:                                               ; preds = %17
  %22 = tail call noundef ptr @_ZNK9CMyComPtrI20ISequentialOutStreamEptEv(ptr noundef nonnull align 8 dereferenceable(8) %19)
  %23 = load ptr, ptr %22, align 8, !tbaa !19
  %24 = getelementptr inbounds i8, ptr %23, i64 40
  %25 = load ptr, ptr %24, align 8
  %26 = call noundef i32 %25(ptr noundef nonnull align 8 dereferenceable(8) %22, ptr noundef %1, i32 noundef %18, ptr noundef nonnull %5)
  %.pre = load i32, ptr %5, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %21, %17
  %28 = phi i32 [ %.pre, %21 ], [ %18, %17 ]
  %.0 = phi i32 [ %26, %21 ], [ 0, %17 ]
  %29 = zext i32 %28 to i64
  %30 = load i64, ptr %9, align 8, !tbaa !9
  %31 = sub i64 %30, %29
  store i64 %31, ptr %9, align 8, !tbaa !9
  br i1 %.not, label %33, label %32

32:                                               ; preds = %27
  store i32 %28, ptr %3, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %27, %32, %15
  %.07 = phi i32 [ -2147467259, %15 ], [ %.0, %32 ], [ %.0, %27 ]
  ret i32 %.07
}

; 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

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

!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, !16, i64 24}
!10 = !{!"_ZTS27CLimitedSequentialOutStream", !11, i64 0, !13, i64 8, !14, i64 16, !16, i64 24, !17, i64 32}
!11 = !{!"_ZTS20ISequentialOutStream", !12, i64 0}
!12 = !{!"_ZTS8IUnknown"}
!13 = !{!"_ZTS13CMyUnknownImp", !6, i64 0}
!14 = !{!"_ZTS9CMyComPtrI20ISequentialOutStreamE", !15, i64 0}
!15 = !{!"any pointer", !7, i64 0}
!16 = !{!"long long", !7, i64 0}
!17 = !{!"bool", !7, i64 0}
!18 = !{!10, !17, i64 32}
!19 = !{!20, !20, i64 0}
!20 = !{!"vtable pointer", !8, i64 0}


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