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

; 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 @_ZN13CInBufferBase4SkipEm(ptr noundef nonnull align 8 dereferenceable(56) %0, i64 noundef %1) #1 align 2 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %9 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %10

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

21:                                               ; preds = %10
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = getelementptr inbounds %class.CInBufferBase, ptr %9, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = getelementptr inbounds i8, ptr %24, i64 %22
  store ptr %25, ptr %23, align 8, !tbaa !16
  %26 = load i64, ptr %6, align 8, !tbaa !9
  %27 = load i64, ptr %5, align 8, !tbaa !9
  %28 = add i64 %26, %27
  store i64 %28, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %44

29:                                               ; preds = %10
  %30 = load i64, ptr %7, align 8, !tbaa !9
  %31 = getelementptr inbounds %class.CInBufferBase, ptr %9, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !16
  %33 = getelementptr inbounds i8, ptr %32, i64 %30
  store ptr %33, ptr %31, align 8, !tbaa !16
  %34 = load i64, ptr %7, align 8, !tbaa !9
  %35 = load i64, ptr %6, align 8, !tbaa !9
  %36 = add i64 %35, %34
  store i64 %36, ptr %6, align 8, !tbaa !9
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = load i64, ptr %5, align 8, !tbaa !9
  %39 = sub i64 %38, %37
  store i64 %39, ptr %5, align 8, !tbaa !9
  %40 = call noundef zeroext i1 @_ZN13CInBufferBase9ReadBlockEv(ptr noundef nonnull align 8 dereferenceable(56) %9)
  br i1 %40, label %43, label %41

41:                                               ; preds = %29
  %42 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %42, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %44

43:                                               ; preds = %29
  store i32 0, ptr %8, align 4
  br label %44

44:                                               ; preds = %43, %41, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  %45 = load i32, ptr %8, align 4
  switch i32 %45, label %47 [
    i32 0, label %46
  ]

46:                                               ; preds = %44
  br label %10, !llvm.loop !17

47:                                               ; preds = %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %48 = load i64, ptr %3, align 8
  ret i64 %48
}

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, !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 @_ZN13CInBufferBase4SkipEm(ptr noundef nonnull align 8 dereferenceable(56) %0, i64 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  br label %4

4:                                                ; preds = %12, %2
  %.016 = phi i64 [ %1, %2 ], [ %15, %12 ]
  %.014 = phi i64 [ 0, %2 ], [ %14, %12 ]
  %5 = load ptr, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %0, align 8, !tbaa !14
  %7 = ptrtoint ptr %5 to i64
  %8 = ptrtoint ptr %6 to i64
  %9 = sub i64 %7, %8
  %.not = icmp ult i64 %9, %.016
  br i1 %.not, label %12, label %.thread

.thread:                                          ; preds = %4
  %10 = getelementptr inbounds i8, ptr %6, i64 %.016
  store ptr %10, ptr %0, align 8, !tbaa !14
  %11 = add i64 %.014, %.016
  br label %.loopexit

12:                                               ; preds = %4
  %13 = getelementptr inbounds i8, ptr %6, i64 %9
  store ptr %13, ptr %0, align 8, !tbaa !14
  %14 = add i64 %9, %.014
  %15 = sub i64 %.016, %9
  %16 = tail call noundef zeroext i1 @_ZN13CInBufferBase9ReadBlockEv(ptr noundef nonnull align 8 dereferenceable(56) %0)
  br i1 %16, label %4, label %.loopexit, !llvm.loop !15

.loopexit:                                        ; preds = %12, %.thread
  %.123 = phi i64 [ %11, %.thread ], [ %14, %12 ]
  ret i64 %.123
}

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

!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,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass