; LLVM IR from vczh/herodb :: Kernel/Source/Vlpp.ll (ZN2vl6stream12MemoryStream5WriteEPvl)

; 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.vl::stream::MemoryStream" = type { %"class.vl::Object", i64, ptr, i64, i64, i64, %"class.vl::stream::IStream" }
%"class.vl::Object" = type { ptr }
%"class.vl::stream::IStream" = type { %"class.vl::Interface" }
%"class.vl::Interface" = type { ptr }

@_ZTIN2vl5ErrorE = external dso_local constant { ptr, ptr }, align 8
@.str.598 = external hidden unnamed_addr constant [76 x i32], align 4
@.str.599 = external hidden unnamed_addr constant [67 x i32], align 4

declare i32 @__gxx_personality_v0(...)

declare ptr @__cxa_allocate_exception(i64)

declare void @__cxa_throw(ptr, ptr, ptr)

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN2vl6stream12MemoryStream5WriteEPvl(ptr noundef nonnull align 8 dereferenceable(48) %0, ptr noundef %1, i64 noundef %2) unnamed_addr #0 align 2 personality ptr @__gxx_personality_v0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  %9 = load ptr, ptr %4, align 8
  br label %10

10:                                               ; preds = %3
  %11 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %9, i32 0, i32 1
  %12 = load i64, ptr %11, align 8, !tbaa !11
  %13 = icmp ne i64 %12, 0
  br i1 %13, label %21, label %14

14:                                               ; preds = %10
  %15 = call ptr @__cxa_allocate_exception(i64 8) #2
  invoke void @_ZN2vl5ErrorC1EPKw(ptr noundef nonnull align 8 dereferenceable(8) %15, ptr noundef @.str.598)
          to label %16 unwind label %17

16:                                               ; preds = %14
  call void @__cxa_throw(ptr %15, ptr @_ZTIN2vl5ErrorE, ptr null) #3
  unreachable

17:                                               ; preds = %14
  %18 = landingpad { ptr, i32 }
          cleanup
  %19 = extractvalue { ptr, i32 } %18, 0
  store ptr %19, ptr %7, align 8
  %20 = extractvalue { ptr, i32 } %18, 1
  store i32 %20, ptr %8, align 4
  call void @__cxa_free_exception(ptr %15) #2
  br label %61

21:                                               ; preds = %10
  br label %22

22:                                               ; preds = %21
  br label %23

23:                                               ; preds = %22
  %24 = load i64, ptr %6, align 8, !tbaa !9
  %25 = icmp sge i64 %24, 0
  br i1 %25, label %33, label %26

26:                                               ; preds = %23
  %27 = call ptr @__cxa_allocate_exception(i64 8) #2
  invoke void @_ZN2vl5ErrorC1EPKw(ptr noundef nonnull align 8 dereferenceable(8) %27, ptr noundef @.str.599)
          to label %28 unwind label %29

28:                                               ; preds = %26
  call void @__cxa_throw(ptr %27, ptr @_ZTIN2vl5ErrorE, ptr null) #3
  unreachable

29:                                               ; preds = %26
  %30 = landingpad { ptr, i32 }
          cleanup
  %31 = extractvalue { ptr, i32 } %30, 0
  store ptr %31, ptr %7, align 8
  %32 = extractvalue { ptr, i32 } %30, 1
  store i32 %32, ptr %8, align 4
  call void @__cxa_free_exception(ptr %27) #2
  br label %61

33:                                               ; preds = %23
  br label %34

34:                                               ; preds = %33
  %35 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %9, i32 0, i32 3
  %36 = load i64, ptr %35, align 8, !tbaa !14
  %37 = load i64, ptr %6, align 8, !tbaa !9
  %38 = add nsw i64 %36, %37
  call void @_ZN2vl6stream12MemoryStream12PrepareSpaceEl(ptr noundef nonnull align 8 dereferenceable(48) %9, i64 noundef %38)
  %39 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %9, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !15
  %41 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %9, i32 0, i32 4
  %42 = load i64, ptr %41, align 8, !tbaa !16
  %43 = getelementptr inbounds i8, ptr %40, i64 %42
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %43, ptr align 1 %44, i64 %45, i1 false)
  %46 = load i64, ptr %6, align 8, !tbaa !9
  %47 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %9, i32 0, i32 4
  %48 = load i64, ptr %47, align 8, !tbaa !16
  %49 = add nsw i64 %48, %46
  store i64 %49, ptr %47, align 8, !tbaa !16
  %50 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %9, i32 0, i32 3
  %51 = load i64, ptr %50, align 8, !tbaa !14
  %52 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %9, i32 0, i32 4
  %53 = load i64, ptr %52, align 8, !tbaa !16
  %54 = icmp slt i64 %51, %53
  br i1 %54, label %55, label %59

55:                                               ; preds = %34
  %56 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %9, i32 0, i32 4
  %57 = load i64, ptr %56, align 8, !tbaa !16
  %58 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %9, i32 0, i32 3
  store i64 %57, ptr %58, align 8, !tbaa !14
  br label %59

59:                                               ; preds = %55, %34
  %60 = load i64, ptr %6, align 8, !tbaa !9
  ret i64 %60

61:                                               ; preds = %29, %17
  %62 = load ptr, ptr %7, align 8
  %63 = load i32, ptr %8, align 4
  %64 = insertvalue { ptr, i32 } poison, ptr %62, 0
  %65 = insertvalue { ptr, i32 } %64, i32 %63, 1
  resume { ptr, i32 } %65
}

declare void @__cxa_free_exception(ptr)

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2vl6stream12MemoryStream12PrepareSpaceEl(ptr noundef nonnull align 8 dereferenceable(48), i64 noundef) #0 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #1

declare void @_ZN2vl5ErrorC1EPKw(ptr, ptr)

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 nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }
attributes #3 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTSN2vl6stream12MemoryStreamE", !13, i64 0, !10, i64 8, !6, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}
!13 = !{!"_ZTSN2vl6ObjectE"}
!14 = !{!12, !10, i64 24}
!15 = !{!12, !6, i64 16}
!16 = !{!12, !10, i64 32}


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

@_ZTIN2vl5ErrorE = external dso_local constant { ptr, ptr }, align 8
@.str.598 = external hidden unnamed_addr constant [76 x i32], align 4
@.str.599 = external hidden unnamed_addr constant [67 x i32], align 4

declare i32 @__gxx_personality_v0(...)

declare ptr @__cxa_allocate_exception(i64) local_unnamed_addr

declare void @__cxa_throw(ptr, ptr, ptr) local_unnamed_addr

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i64 0, -9223372036854775808) i64 @_ZN2vl6stream12MemoryStream5WriteEPvl(ptr noundef nonnull align 8 dereferenceable(48) %0, ptr nocapture noundef readonly %1, i64 noundef returned %2) unnamed_addr #0 align 2 personality ptr @__gxx_personality_v0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %6, label %11

6:                                                ; preds = %3
  %7 = tail call ptr @__cxa_allocate_exception(i64 8) #2
  invoke void @_ZN2vl5ErrorC1EPKw(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull @.str.598)
          to label %8 unwind label %9

8:                                                ; preds = %6
  tail call void @__cxa_throw(ptr %7, ptr nonnull @_ZTIN2vl5ErrorE, ptr null) #3
  unreachable

9:                                                ; preds = %6
  %10 = landingpad { ptr, i32 }
          cleanup
  br label %33

11:                                               ; preds = %3
  %12 = icmp sgt i64 %2, -1
  br i1 %12, label %18, label %13

13:                                               ; preds = %11
  %14 = tail call ptr @__cxa_allocate_exception(i64 8) #2
  invoke void @_ZN2vl5ErrorC1EPKw(ptr noundef nonnull align 8 dereferenceable(8) %14, ptr noundef nonnull @.str.599)
          to label %15 unwind label %16

15:                                               ; preds = %13
  tail call void @__cxa_throw(ptr %14, ptr nonnull @_ZTIN2vl5ErrorE, ptr null) #3
  unreachable

16:                                               ; preds = %13
  %17 = landingpad { ptr, i32 }
          cleanup
  br label %33

18:                                               ; preds = %11
  %19 = getelementptr inbounds i8, ptr %0, i64 24
  %20 = load i64, ptr %19, align 8, !tbaa !12
  %21 = add nsw i64 %20, %2
  tail call void @_ZN2vl6stream12MemoryStream12PrepareSpaceEl(ptr noundef nonnull align 8 dereferenceable(48) %0, i64 noundef %21)
  %22 = getelementptr inbounds i8, ptr %0, i64 16
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  %24 = getelementptr inbounds i8, ptr %0, i64 32
  %25 = load i64, ptr %24, align 8, !tbaa !14
  %26 = getelementptr inbounds i8, ptr %23, i64 %25
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %26, ptr align 1 %1, i64 %2, i1 false)
  %27 = load i64, ptr %24, align 8, !tbaa !14
  %28 = add nsw i64 %27, %2
  store i64 %28, ptr %24, align 8, !tbaa !14
  %29 = load i64, ptr %19, align 8, !tbaa !12
  %30 = icmp slt i64 %29, %28
  br i1 %30, label %31, label %32

31:                                               ; preds = %18
  store i64 %28, ptr %19, align 8, !tbaa !12
  br label %32

32:                                               ; preds = %31, %18
  ret i64 %2

33:                                               ; preds = %16, %9
  %.sink = phi ptr [ %14, %16 ], [ %7, %9 ]
  %.pn = phi { ptr, i32 } [ %17, %16 ], [ %10, %9 ]
  tail call void @__cxa_free_exception(ptr %.sink) #2
  resume { ptr, i32 } %.pn
}

declare void @__cxa_free_exception(ptr) local_unnamed_addr

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2vl6stream12MemoryStream12PrepareSpaceEl(ptr noundef nonnull align 8 dereferenceable(48), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #1

declare void @_ZN2vl5ErrorC1EPKw(ptr, ptr) local_unnamed_addr

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 nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }
attributes #3 = { 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, !8, i64 8}
!6 = !{!"_ZTSN2vl6stream12MemoryStreamE", !7, i64 0, !8, i64 8, !11, i64 16, !8, i64 24, !8, i64 32, !8, i64 40}
!7 = !{!"_ZTSN2vl6ObjectE"}
!8 = !{!"long", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!6, !8, i64 24}
!13 = !{!6, !11, i64 16}
!14 = !{!6, !8, i64 32}


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