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

; 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::CharDecoder" = type { %"class.vl::Object", %"class.vl::stream::IDecoder", ptr, [4 x i8], i64 }
%"class.vl::Object" = type { ptr }
%"class.vl::stream::IDecoder" = type { %"class.vl::Interface" }
%"class.vl::Interface" = 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 i64 @_ZN2vl6stream12Utf16Decoder10ReadStringEPwl(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, i64 noundef %2) unnamed_addr #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  %10 = 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
  %11 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %12, ptr %7, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %84, %3
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = ptrtoint ptr %14 to i64
  %17 = ptrtoint ptr %15 to i64
  %18 = sub i64 %16, %17
  %19 = sdiv exact i64 %18, 4
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = icmp slt i64 %19, %20
  br i1 %21, label %22, label %85

22:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #2
  store i16 0, ptr %8, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #2
  store i16 0, ptr %9, align 2, !tbaa !11
  %23 = getelementptr inbounds %"class.vl::stream::CharDecoder", ptr %11, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  %25 = load ptr, ptr %24, align 8, !tbaa !18
  %26 = getelementptr inbounds ptr, ptr %25, i64 14
  %27 = load ptr, ptr %26, align 8
  %28 = call noundef i64 %27(ptr noundef nonnull align 8 dereferenceable(8) %24, ptr noundef %8, i64 noundef 2)
  %29 = icmp ne i64 %28, 2
  br i1 %29, label %30, label %31

30:                                               ; preds = %22
  store i32 3, ptr %10, align 4
  br label %82

31:                                               ; preds = %22
  %32 = load i16, ptr %8, align 2, !tbaa !11
  %33 = zext i16 %32 to i32
  %34 = icmp slt i32 %33, 55296
  br i1 %34, label %39, label %35

35:                                               ; preds = %31
  %36 = load i16, ptr %8, align 2, !tbaa !11
  %37 = zext i16 %36 to i32
  %38 = icmp sgt i32 %37, 57343
  br i1 %38, label %39, label %44

39:                                               ; preds = %35, %31
  %40 = load i16, ptr %8, align 2, !tbaa !11
  %41 = zext i16 %40 to i32
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = getelementptr inbounds i32, ptr %42, i32 1
  store ptr %43, ptr %7, align 8, !tbaa !5
  store i32 %41, ptr %42, align 4, !tbaa !20
  br label %81

44:                                               ; preds = %35
  %45 = load i16, ptr %8, align 2, !tbaa !11
  %46 = zext i16 %45 to i32
  %47 = icmp slt i32 %46, 56320
  br i1 %47, label %48, label %79

48:                                               ; preds = %44
  %49 = getelementptr inbounds %"class.vl::stream::CharDecoder", ptr %11, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !13
  %51 = load ptr, ptr %50, align 8, !tbaa !18
  %52 = getelementptr inbounds ptr, ptr %51, i64 14
  %53 = load ptr, ptr %52, align 8
  %54 = call noundef i64 %53(ptr noundef nonnull align 8 dereferenceable(8) %50, ptr noundef %9, i64 noundef 2)
  %55 = icmp ne i64 %54, 2
  br i1 %55, label %56, label %57

56:                                               ; preds = %48
  store i32 3, ptr %10, align 4
  br label %82

57:                                               ; preds = %48
  %58 = load i16, ptr %9, align 2, !tbaa !11
  %59 = zext i16 %58 to i32
  %60 = icmp sle i32 56320, %59
  br i1 %60, label %61, label %77

61:                                               ; preds = %57
  %62 = load i16, ptr %9, align 2, !tbaa !11
  %63 = zext i16 %62 to i32
  %64 = icmp sle i32 %63, 57343
  br i1 %64, label %65, label %77

65:                                               ; preds = %61
  %66 = load i16, ptr %8, align 2, !tbaa !11
  %67 = zext i16 %66 to i32
  %68 = sub nsw i32 %67, 55296
  %69 = mul nsw i32 %68, 1024
  %70 = load i16, ptr %9, align 2, !tbaa !11
  %71 = zext i16 %70 to i32
  %72 = sub nsw i32 %71, 56320
  %73 = add nsw i32 %69, %72
  %74 = add nsw i32 %73, 65536
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  %76 = getelementptr inbounds i32, ptr %75, i32 1
  store ptr %76, ptr %7, align 8, !tbaa !5
  store i32 %74, ptr %75, align 4, !tbaa !20
  br label %78

77:                                               ; preds = %61, %57
  store i32 3, ptr %10, align 4
  br label %82

78:                                               ; preds = %65
  br label %80

79:                                               ; preds = %44
  store i32 3, ptr %10, align 4
  br label %82

80:                                               ; preds = %78
  br label %81

81:                                               ; preds = %80, %39
  store i32 0, ptr %10, align 4
  br label %82

82:                                               ; preds = %81, %79, %77, %56, %30
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #2
  %83 = load i32, ptr %10, align 4
  switch i32 %83, label %92 [
    i32 0, label %84
    i32 3, label %85
  ]

84:                                               ; preds = %82
  br label %13, !llvm.loop !22

85:                                               ; preds = %82, %13
  %86 = load ptr, ptr %7, align 8, !tbaa !5
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  %88 = ptrtoint ptr %86 to i64
  %89 = ptrtoint ptr %87 to i64
  %90 = sub i64 %88, %89
  %91 = sdiv exact i64 %90, 4
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %91

92:                                               ; preds = %82
  unreachable
}

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 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !6, i64 16}
!14 = !{!"_ZTSN2vl6stream11CharDecoderE", !15, i64 0, !16, i64 8, !6, i64 16, !7, i64 24, !10, i64 32}
!15 = !{!"_ZTSN2vl6ObjectE"}
!16 = !{!"_ZTSN2vl6stream8IDecoderE", !17, i64 0}
!17 = !{!"_ZTSN2vl9InterfaceE"}
!18 = !{!19, !19, i64 0}
!19 = !{!"vtable pointer", !8, i64 0}
!20 = !{!21, !21, i64 0}
!21 = !{!"wchar_t", !7, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


; 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress 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 range(i64 -2305843009213693952, 2305843009213693952) i64 @_ZN2vl6stream12Utf16Decoder10ReadStringEPwl(ptr nocapture noundef nonnull readonly align 8 dereferenceable(40) %0, ptr noundef %1, i64 noundef %2) unnamed_addr #1 align 2 {
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = ptrtoint ptr %1 to i64
  %7 = icmp sgt i64 %2, 0
  br i1 %7, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  br label %9

9:                                                ; preds = %.lr.ph, %38
  %.01424 = phi ptr [ %1, %.lr.ph ], [ %.1, %38 ]
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %4) #2
  store i16 0, ptr %4, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %5) #2
  store i16 0, ptr %5, align 2, !tbaa !5
  %10 = load ptr, ptr %8, align 8, !tbaa !9
  %11 = load ptr, ptr %10, align 8, !tbaa !16
  %12 = getelementptr inbounds i8, ptr %11, i64 112
  %13 = load ptr, ptr %12, align 8
  %14 = call noundef i64 %13(ptr noundef nonnull align 8 dereferenceable(8) %10, ptr noundef nonnull %4, i64 noundef 2)
  %.not = icmp eq i64 %14, 2
  br i1 %.not, label %15, label %.thread

15:                                               ; preds = %9
  %16 = load i16, ptr %4, align 2
  %17 = add i16 %16, 8192
  %or.cond = icmp ult i16 %17, -2048
  br i1 %or.cond, label %18, label %20

18:                                               ; preds = %15
  %19 = zext i16 %16 to i32
  br label %38

20:                                               ; preds = %15
  %21 = icmp ult i16 %16, -9216
  br i1 %21, label %22, label %.thread

22:                                               ; preds = %20
  %23 = load ptr, ptr %8, align 8, !tbaa !9
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = getelementptr inbounds i8, ptr %24, i64 112
  %26 = load ptr, ptr %25, align 8
  %27 = call noundef i64 %26(ptr noundef nonnull align 8 dereferenceable(8) %23, ptr noundef nonnull %5, i64 noundef 2)
  %.not17 = icmp eq i64 %27, 2
  br i1 %.not17, label %28, label %.thread

28:                                               ; preds = %22
  %29 = load i16, ptr %5, align 2
  %30 = and i16 %29, -1024
  %or.cond5 = icmp eq i16 %30, -9216
  br i1 %or.cond5, label %31, label %.thread

31:                                               ; preds = %28
  %32 = zext i16 %29 to i32
  %33 = load i16, ptr %4, align 2, !tbaa !5
  %34 = zext i16 %33 to i32
  %35 = shl nuw nsw i32 %34, 10
  %36 = add nuw nsw i32 %32, -56613888
  %37 = add nsw i32 %36, %35
  br label %38

.thread:                                          ; preds = %9, %22, %28, %20
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %5) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %4) #2
  %.pre = ptrtoint ptr %.01424 to i64
  br label %.loopexit

38:                                               ; preds = %18, %31
  %storemerge = phi i32 [ %37, %31 ], [ %19, %18 ]
  %.1 = getelementptr inbounds i8, ptr %.01424, i64 4
  store i32 %storemerge, ptr %.01424, align 4, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %5) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %4) #2
  %39 = ptrtoint ptr %.1 to i64
  %40 = sub i64 %39, %6
  %41 = ashr exact i64 %40, 2
  %42 = icmp slt i64 %41, %2
  br i1 %42, label %9, label %.loopexit

.loopexit:                                        ; preds = %38, %3, %.thread
  %.pre-phi = phi i64 [ %6, %3 ], [ %.pre, %.thread ], [ %39, %38 ]
  %43 = sub i64 %.pre-phi, %6
  %44 = ashr exact i64 %43, 2
  ret i64 %44
}

attributes #0 = { mustprogress 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 = { 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !14, i64 16}
!10 = !{!"_ZTSN2vl6stream11CharDecoderE", !11, i64 0, !12, i64 8, !14, i64 16, !7, i64 24, !15, i64 32}
!11 = !{!"_ZTSN2vl6ObjectE"}
!12 = !{!"_ZTSN2vl6stream8IDecoderE", !13, i64 0}
!13 = !{!"_ZTSN2vl9InterfaceE"}
!14 = !{!"any pointer", !7, i64 0}
!15 = !{!"long", !7, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"vtable pointer", !8, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"wchar_t", !7, i64 0}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass