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

; 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.CInBufferBase = type { ptr, ptr, ptr, ptr, i64, i64, i8, 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
declare dso_local noundef zeroext i1 @_ZN13CInBufferBase9ReadBlockEv(ptr noundef nonnull align 8 dereferenceable(56)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN13CInBufferBase9ReadBytesEPhm(ptr noundef nonnull align 8 dereferenceable(56) %0, ptr noundef %1, i64 noundef %2) #1 align 2 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  %11 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %68, %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %13 = getelementptr inbounds %class.CInBufferBase, ptr %11, i32 0, i32 1
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = getelementptr inbounds %class.CInBufferBase, ptr %11, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !16
  %17 = ptrtoint ptr %14 to i64
  %18 = ptrtoint ptr %16 to i64
  %19 = sub i64 %17, %18
  store i64 %19, ptr %9, align 8, !tbaa !9
  %20 = load i64, ptr %7, align 8, !tbaa !9
  %21 = load i64, ptr %9, align 8, !tbaa !9
  %22 = icmp ule i64 %20, %21
  br i1 %22, label %23, label %40

23:                                               ; preds = %12
  %24 = load i64, ptr %7, align 8, !tbaa !9
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %26, label %38

26:                                               ; preds = %23
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %class.CInBufferBase, ptr %11, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !16
  %30 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %27, ptr align 1 %29, i64 %30, i1 false)
  %31 = load i64, ptr %7, align 8, !tbaa !9
  %32 = getelementptr inbounds %class.CInBufferBase, ptr %11, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !16
  %34 = getelementptr inbounds i8, ptr %33, i64 %31
  store ptr %34, ptr %32, align 8, !tbaa !16
  %35 = load i64, ptr %7, align 8, !tbaa !9
  %36 = load i64, ptr %8, align 8, !tbaa !9
  %37 = add i64 %36, %35
  store i64 %37, ptr %8, align 8, !tbaa !9
  br label %38

38:                                               ; preds = %26, %23
  %39 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %39, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %66

40:                                               ; preds = %12
  %41 = load i64, ptr %9, align 8, !tbaa !9
  %42 = icmp ne i64 %41, 0
  br i1 %42, label %43, label %61

43:                                               ; preds = %40
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %class.CInBufferBase, ptr %11, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !16
  %47 = load i64, ptr %9, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %44, ptr align 1 %46, i64 %47, i1 false)
  %48 = load i64, ptr %9, align 8, !tbaa !9
  %49 = getelementptr inbounds %class.CInBufferBase, ptr %11, i32 0, i32 0
  %50 = load ptr, ptr %49, align 8, !tbaa !16
  %51 = getelementptr inbounds i8, ptr %50, i64 %48
  store ptr %51, ptr %49, align 8, !tbaa !16
  %52 = load i64, ptr %9, align 8, !tbaa !9
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds i8, ptr %53, i64 %52
  store ptr %54, ptr %6, align 8, !tbaa !5
  %55 = load i64, ptr %9, align 8, !tbaa !9
  %56 = load i64, ptr %8, align 8, !tbaa !9
  %57 = add i64 %56, %55
  store i64 %57, ptr %8, align 8, !tbaa !9
  %58 = load i64, ptr %9, align 8, !tbaa !9
  %59 = load i64, ptr %7, align 8, !tbaa !9
  %60 = sub i64 %59, %58
  store i64 %60, ptr %7, align 8, !tbaa !9
  br label %61

61:                                               ; preds = %43, %40
  %62 = call noundef zeroext i1 @_ZN13CInBufferBase9ReadBlockEv(ptr noundef nonnull align 8 dereferenceable(56) %11)
  br i1 %62, label %65, label %63

63:                                               ; preds = %61
  %64 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %64, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %66

65:                                               ; preds = %61
  store i32 0, ptr %10, align 4
  br label %66

66:                                               ; preds = %65, %63, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  %67 = load i32, ptr %10, align 4
  switch i32 %67, label %69 [
    i32 0, label %68
  ]

68:                                               ; preds = %66
  br label %12, !llvm.loop !17

69:                                               ; preds = %66
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %70 = load i64, ptr %4, align 8
  ret i64 %70
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS13CInBufferBase", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !13, i64 32, !10, i64 40, !14, i64 48, !15, i64 52}
!13 = !{!"long long", !7, i64 0}
!14 = !{!"bool", !7, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!12, !6, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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 uwtable
declare dso_local noundef zeroext i1 @_ZN13CInBufferBase9ReadBlockEv(ptr noundef nonnull align 8 dereferenceable(56)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN13CInBufferBase9ReadBytesEPhm(ptr noundef nonnull align 8 dereferenceable(56) %0, ptr nocapture noundef writeonly %1, i64 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  br label %5

5:                                                ; preds = %23, %3
  %.028 = phi ptr [ %1, %3 ], [ %.129, %23 ]
  %.025 = phi i64 [ %2, %3 ], [ %.126, %23 ]
  %.023 = phi i64 [ 0, %3 ], [ %.2, %23 ]
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = load ptr, ptr %0, align 8, !tbaa !14
  %8 = ptrtoint ptr %6 to i64
  %9 = ptrtoint ptr %7 to i64
  %10 = sub i64 %8, %9
  %.not = icmp ugt i64 %.025, %10
  br i1 %.not, label %16, label %11

11:                                               ; preds = %5
  %.not33 = icmp eq i64 %.025, 0
  br i1 %.not33, label %.thread, label %12

12:                                               ; preds = %11
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.028, ptr align 1 %7, i64 %.025, i1 false)
  %13 = load ptr, ptr %0, align 8, !tbaa !14
  %14 = getelementptr inbounds i8, ptr %13, i64 %.025
  store ptr %14, ptr %0, align 8, !tbaa !14
  %15 = add i64 %.023, %.025
  br label %.thread

16:                                               ; preds = %5
  %.not32 = icmp eq ptr %6, %7
  br i1 %.not32, label %23, label %17

17:                                               ; preds = %16
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.028, ptr align 1 %7, i64 %10, i1 false)
  %18 = load ptr, ptr %0, align 8, !tbaa !14
  %19 = getelementptr inbounds i8, ptr %18, i64 %10
  store ptr %19, ptr %0, align 8, !tbaa !14
  %20 = getelementptr inbounds i8, ptr %.028, i64 %10
  %21 = add i64 %10, %.023
  %22 = sub i64 %.025, %10
  br label %23

23:                                               ; preds = %16, %17
  %.129 = phi ptr [ %20, %17 ], [ %.028, %16 ]
  %.126 = phi i64 [ %22, %17 ], [ %.025, %16 ]
  %.2 = phi i64 [ %21, %17 ], [ %.023, %16 ]
  %24 = tail call noundef zeroext i1 @_ZN13CInBufferBase9ReadBlockEv(ptr noundef nonnull align 8 dereferenceable(56) %0)
  br i1 %24, label %5, label %.thread, !llvm.loop !15

.thread:                                          ; preds = %23, %11, %12
  %.139 = phi i64 [ %.023, %11 ], [ %15, %12 ], [ %.2, %23 ]
  ret i64 %.139
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !7, i64 8}
!6 = !{!"_ZTS13CInBufferBase", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !11, i64 40, !12, i64 48, !13, i64 52}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"bool", !8, i64 0}
!13 = !{!"int", !8, i64 0}
!14 = !{!6, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass