; LLVM IR from SSBMTonberry/pocketlzma :: ref_spec/lzma1900/CPP/7zip/Common/FileStreams.ll (ZN14COutFileStream5WriteEPKvjPj)

; 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.COutFileStream = type { %struct.IOutStream, %class.CMyUnknownImp, %"class.NC::NFile::NIO::COutFile", i64 }
%struct.IOutStream = type { %struct.ISequentialOutStream }
%struct.ISequentialOutStream = type { %struct.IUnknown }
%struct.IUnknown = type { ptr }
%class.CMyUnknownImp = type { i32 }
%"class.NC::NFile::NIO::COutFile" = type { %"class.NC::NFile::NIO::CFileBase" }
%"class.NC::NFile::NIO::CFileBase" = type { i32 }

; 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 @_ZN14COutFileStream5WriteEPKvjPj(ptr noundef nonnull align 8 dereferenceable(24) %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 i64, align 8
  %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
  %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
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %18 = getelementptr inbounds %class.COutFileStream, ptr %12, i32 0, i32 2
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = zext i32 %20 to i64
  %22 = call noundef i64 @_ZN2NC5NFile3NIO8COutFile5WriteEPKvm(ptr noundef nonnull align 4 dereferenceable(4) %18, ptr noundef %19, i64 noundef %21)
  store i64 %22, ptr %10, align 8, !tbaa !11
  %23 = load i64, ptr %10, align 8, !tbaa !11
  %24 = icmp eq i64 %23, -1
  br i1 %24, label %25, label %26

25:                                               ; preds = %17
  store i32 -2147467259, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %38

26:                                               ; preds = %17
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %33

29:                                               ; preds = %26
  %30 = load i64, ptr %10, align 8, !tbaa !11
  %31 = trunc i64 %30 to i32
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %31, ptr %32, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %29, %26
  %34 = load i64, ptr %10, align 8, !tbaa !11
  %35 = getelementptr inbounds %class.COutFileStream, ptr %12, i32 0, i32 3
  %36 = load i64, ptr %35, align 8, !tbaa !13
  %37 = add i64 %36, %34
  store i64 %37, ptr %35, align 8, !tbaa !13
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %38

38:                                               ; preds = %33, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %39 = load i32, ptr %5, align 4
  ret i32 %39
}

declare noundef i64 @_ZN2NC5NFile3NIO8COutFile5WriteEPKvm(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef, i64 noundef) #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 = { "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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !21, i64 16}
!14 = !{!"_ZTS14COutFileStream", !15, i64 0, !18, i64 8, !19, i64 12, !21, i64 16}
!15 = !{!"_ZTS10IOutStream", !16, i64 0}
!16 = !{!"_ZTS20ISequentialOutStream", !17, i64 0}
!17 = !{!"_ZTS8IUnknown"}
!18 = !{!"_ZTS13CMyUnknownImp", !10, i64 0}
!19 = !{!"_ZTSN2NC5NFile3NIO8COutFileE", !20, i64 0}
!20 = !{!"_ZTSN2NC5NFile3NIO9CFileBaseE", !10, i64 0}
!21 = !{!"long long", !7, 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 range(i32 -2147467259, 1) i32 @_ZN14COutFileStream5WriteEPKvjPj(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef %1, i32 noundef %2, ptr noundef writeonly %3) unnamed_addr #0 align 2 {
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %5, label %.thread

5:                                                ; preds = %4
  %6 = getelementptr inbounds i8, ptr %0, i64 12
  %7 = zext i32 %2 to i64
  %8 = tail call noundef i64 @_ZN2NC5NFile3NIO8COutFile5WriteEPKvm(ptr noundef nonnull align 4 dereferenceable(4) %6, ptr noundef %1, i64 noundef %7)
  %9 = icmp eq i64 %8, -1
  br i1 %9, label %21, label %16

.thread:                                          ; preds = %4
  store i32 0, ptr %3, align 4, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %0, i64 12
  %11 = zext i32 %2 to i64
  %12 = tail call noundef i64 @_ZN2NC5NFile3NIO8COutFile5WriteEPKvm(ptr noundef nonnull align 4 dereferenceable(4) %10, ptr noundef %1, i64 noundef %11)
  %13 = icmp eq i64 %12, -1
  br i1 %13, label %21, label %14

14:                                               ; preds = %.thread
  %15 = trunc i64 %12 to i32
  store i32 %15, ptr %3, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %5, %14
  %17 = phi i64 [ %12, %14 ], [ %8, %5 ]
  %18 = getelementptr inbounds i8, ptr %0, i64 16
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %20 = add i64 %19, %17
  store i64 %20, ptr %18, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %.thread, %5, %16
  %.0 = phi i32 [ 0, %16 ], [ -2147467259, %5 ], [ -2147467259, %.thread ]
  ret i32 %.0
}

declare noundef i64 @_ZN2NC5NFile3NIO8COutFile5WriteEPKvm(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef, i64 noundef) local_unnamed_addr #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 = { "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, !17, i64 16}
!10 = !{!"_ZTS14COutFileStream", !11, i64 0, !14, i64 8, !15, i64 12, !17, i64 16}
!11 = !{!"_ZTS10IOutStream", !12, i64 0}
!12 = !{!"_ZTS20ISequentialOutStream", !13, i64 0}
!13 = !{!"_ZTS8IUnknown"}
!14 = !{!"_ZTS13CMyUnknownImp", !6, i64 0}
!15 = !{!"_ZTSN2NC5NFile3NIO8COutFileE", !16, i64 0}
!16 = !{!"_ZTSN2NC5NFile3NIO9CFileBaseE", !6, i64 0}
!17 = !{!"long long", !7, i64 0}


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