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

; 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.600 = external hidden unnamed_addr constant [75 x i32], align 4
@.str.601 = external hidden unnamed_addr constant [66 x i32], align 4

; 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

declare i32 @__gxx_personality_v0(...)

declare ptr @__cxa_allocate_exception(i64)

declare void @__cxa_throw(ptr, ptr, ptr)

declare void @__cxa_free_exception(ptr)

; 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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN2vl6stream12MemoryStream4PeekEPvl(ptr noundef nonnull align 8 dereferenceable(48) %0, ptr noundef %1, i64 noundef %2) unnamed_addr #2 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
  %9 = alloca i64, align 8
  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
  %10 = load ptr, ptr %4, align 8
  br label %11

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

15:                                               ; preds = %11
  %16 = call ptr @__cxa_allocate_exception(i64 8) #3
  invoke void @_ZN2vl5ErrorC1EPKw(ptr noundef nonnull align 8 dereferenceable(8) %16, ptr noundef @.str.600)
          to label %17 unwind label %18

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

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

22:                                               ; preds = %11
  br label %23

23:                                               ; preds = %22
  br label %24

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

27:                                               ; preds = %24
  %28 = call ptr @__cxa_allocate_exception(i64 8) #3
  invoke void @_ZN2vl5ErrorC1EPKw(ptr noundef nonnull align 8 dereferenceable(8) %28, ptr noundef @.str.601)
          to label %29 unwind label %30

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

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

34:                                               ; preds = %24
  br label %35

35:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %36 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %10, i32 0, i32 3
  %37 = load i64, ptr %36, align 8, !tbaa !14
  %38 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %10, i32 0, i32 4
  %39 = load i64, ptr %38, align 8, !tbaa !15
  %40 = sub nsw i64 %37, %39
  store i64 %40, ptr %9, align 8, !tbaa !9
  %41 = load i64, ptr %6, align 8, !tbaa !9
  %42 = load i64, ptr %9, align 8, !tbaa !9
  %43 = icmp sgt i64 %41, %42
  br i1 %43, label %44, label %46

44:                                               ; preds = %35
  %45 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %45, ptr %6, align 8, !tbaa !9
  br label %46

46:                                               ; preds = %44, %35
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %10, i32 0, i32 2
  %49 = load ptr, ptr %48, align 8, !tbaa !16
  %50 = getelementptr inbounds %"class.vl::stream::MemoryStream", ptr %10, i32 0, i32 4
  %51 = load i64, ptr %50, align 8, !tbaa !15
  %52 = getelementptr inbounds i8, ptr %49, i64 %51
  %53 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %47, ptr align 1 %52, i64 %53, i1 false)
  %54 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret i64 %54

55:                                               ; preds = %30, %18
  %56 = load ptr, ptr %7, align 8
  %57 = load i32, ptr %8, align 4
  %58 = insertvalue { ptr, i32 } poison, ptr %56, 0
  %59 = insertvalue { ptr, i32 } %58, i32 %57, 1
  resume { ptr, i32 } %59
}

declare void @_ZN2vl5ErrorC1EPKw(ptr, ptr)

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nounwind }
attributes #4 = { 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, !10, i64 32}
!16 = !{!12, !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"

@_ZTIN2vl5ErrorE = external dso_local constant { ptr, ptr }, align 8
@.str.600 = external hidden unnamed_addr constant [75 x i32], align 4
@.str.601 = external hidden unnamed_addr constant [66 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

declare void @__cxa_free_exception(ptr) local_unnamed_addr

; 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) #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN2vl6stream12MemoryStream4PeekEPvl(ptr nocapture noundef nonnull readonly align 8 dereferenceable(48) %0, ptr nocapture noundef writeonly %1, i64 noundef %2) unnamed_addr #1 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) #3
  invoke void @_ZN2vl5ErrorC1EPKw(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull @.str.600)
          to label %8 unwind label %9

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

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

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) #3
  invoke void @_ZN2vl5ErrorC1EPKw(ptr noundef nonnull align 8 dereferenceable(8) %14, ptr noundef nonnull @.str.601)
          to label %15 unwind label %16

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

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

18:                                               ; preds = %11
  %19 = getelementptr inbounds i8, ptr %0, i64 24
  %20 = load i64, ptr %19, align 8, !tbaa !12
  %21 = getelementptr inbounds i8, ptr %0, i64 32
  %22 = load i64, ptr %21, align 8, !tbaa !13
  %23 = sub nsw i64 %20, %22
  %spec.select = tail call i64 @llvm.smin.i64(i64 %23, i64 %2)
  %24 = getelementptr inbounds i8, ptr %0, i64 16
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  %26 = getelementptr inbounds i8, ptr %25, i64 %22
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %1, ptr align 1 %26, i64 %spec.select, i1 false)
  ret i64 %spec.select

27:                                               ; 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) #3
  resume { ptr, i32 } %.pn
}

declare void @_ZN2vl5ErrorC1EPKw(ptr, ptr) local_unnamed_addr

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

attributes #0 = { mustprogress nocallback nofree 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nounwind }
attributes #4 = { 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, !8, i64 32}
!14 = !{!6, !11, i64 16}


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