; LLVM IR from kala13x/libxutils :: src/data (buf)

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

%struct.XByteBuffer = type { ptr, i64, i64, i16, i16, 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: 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: nounwind uwtable
define dso_local i32 @XByteBuffer_Remove(ptr noundef %0, i64 noundef %1, i64 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  %12 = load i64, ptr %7, align 8, !tbaa !9
  %13 = icmp ne i64 %12, 0
  br i1 %13, label %14, label %20

14:                                               ; preds = %3
  %15 = load i64, ptr %6, align 8, !tbaa !9
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.XByteBuffer, ptr %16, i32 0, i32 2
  %18 = load i64, ptr %17, align 8, !tbaa !11
  %19 = icmp uge i64 %15, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %14, %3
  store i32 0, ptr %4, align 4
  br label %98

21:                                               ; preds = %14
  %22 = load i64, ptr %6, align 8, !tbaa !9
  %23 = load i64, ptr %7, align 8, !tbaa !9
  %24 = add i64 %22, %23
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.XByteBuffer, ptr %25, i32 0, i32 2
  %27 = load i64, ptr %26, align 8, !tbaa !11
  %28 = icmp ugt i64 %24, %27
  br i1 %28, label %29, label %35

29:                                               ; preds = %21
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.XByteBuffer, ptr %30, i32 0, i32 2
  %32 = load i64, ptr %31, align 8, !tbaa !11
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = sub i64 %32, %33
  br label %37

35:                                               ; preds = %21
  %36 = load i64, ptr %7, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %35, %29
  %38 = phi i64 [ %34, %29 ], [ %36, %35 ]
  store i64 %38, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %39 = load i64, ptr %6, align 8, !tbaa !9
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = add i64 %39, %40
  store i64 %41, ptr %8, align 8, !tbaa !9
  %42 = load i64, ptr %8, align 8, !tbaa !9
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.XByteBuffer, ptr %43, i32 0, i32 2
  %45 = load i64, ptr %44, align 8, !tbaa !11
  %46 = icmp uge i64 %42, %45
  br i1 %46, label %47, label %55

47:                                               ; preds = %37
  %48 = load i64, ptr %6, align 8, !tbaa !9
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.XByteBuffer, ptr %49, i32 0, i32 2
  store i64 %48, ptr %50, align 8, !tbaa !11
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.XByteBuffer, ptr %51, i32 0, i32 2
  %53 = load i64, ptr %52, align 8, !tbaa !11
  %54 = trunc i64 %53 to i32
  store i32 %54, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %97

55:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.XByteBuffer, ptr %56, i32 0, i32 2
  %58 = load i64, ptr %57, align 8, !tbaa !11
  %59 = load i64, ptr %8, align 8, !tbaa !9
  %60 = sub i64 %58, %59
  store i64 %60, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.XByteBuffer, ptr %61, i32 0, i32 0
  %63 = load ptr, ptr %62, align 8, !tbaa !15
  %64 = load i64, ptr %8, align 8, !tbaa !9
  %65 = getelementptr inbounds i8, ptr %63, i64 %64
  store ptr %65, ptr %11, align 8, !tbaa !5
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.XByteBuffer, ptr %66, i32 0, i32 0
  %68 = load ptr, ptr %67, align 8, !tbaa !15
  %69 = load i64, ptr %6, align 8, !tbaa !9
  %70 = getelementptr inbounds i8, ptr %68, i64 %69
  %71 = load ptr, ptr %11, align 8, !tbaa !5
  %72 = load i64, ptr %10, align 8, !tbaa !9
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %70, ptr align 1 %71, i64 %72, i1 false)
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.XByteBuffer, ptr %73, i32 0, i32 2
  %75 = load i64, ptr %74, align 8, !tbaa !11
  %76 = load i64, ptr %7, align 8, !tbaa !9
  %77 = icmp ult i64 %75, %76
  br i1 %77, label %78, label %81

78:                                               ; preds = %55
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.XByteBuffer, ptr %79, i32 0, i32 2
  store i64 0, ptr %80, align 8, !tbaa !11
  br label %87

81:                                               ; preds = %55
  %82 = load i64, ptr %7, align 8, !tbaa !9
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.XByteBuffer, ptr %83, i32 0, i32 2
  %85 = load i64, ptr %84, align 8, !tbaa !11
  %86 = sub i64 %85, %82
  store i64 %86, ptr %84, align 8, !tbaa !11
  br label %87

87:                                               ; preds = %81, %78
  %88 = load ptr, ptr %5, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.XByteBuffer, ptr %88, i32 0, i32 0
  %90 = load ptr, ptr %89, align 8, !tbaa !15
  %91 = load ptr, ptr %5, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.XByteBuffer, ptr %91, i32 0, i32 2
  %93 = load i64, ptr %92, align 8, !tbaa !11
  %94 = getelementptr inbounds i8, ptr %90, i64 %93
  store i8 0, ptr %94, align 1, !tbaa !16
  %95 = load i64, ptr %7, align 8, !tbaa !9
  %96 = trunc i64 %95 to i32
  store i32 %96, ptr %4, align 4
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %97

97:                                               ; preds = %87, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %98

98:                                               ; preds = %97, %20
  %99 = load i32, ptr %4, align 4
  ret i32 %99
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind 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 }

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"XByteBuffer", !6, i64 0, !10, i64 8, !10, i64 16, !13, i64 24, !13, i64 26, !14, i64 28}
!13 = !{!"short", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !6, i64 0}
!16 = !{!7, !7, i64 0}


; 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 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 nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local i32 @XByteBuffer_Remove(ptr nocapture noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #1 {
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %23, label %4

4:                                                ; preds = %3
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %.not39 = icmp ugt i64 %6, %1
  br i1 %.not39, label %7, label %23

7:                                                ; preds = %4
  %8 = add i64 %2, %1
  %9 = icmp ugt i64 %8, %6
  %10 = sub i64 %6, %1
  %11 = select i1 %9, i64 %10, i64 %2
  %12 = add i64 %11, %1
  %.not40 = icmp ult i64 %12, %6
  br i1 %.not40, label %14, label %13

13:                                               ; preds = %7
  store i64 %1, ptr %5, align 8, !tbaa !5
  br label %22

14:                                               ; preds = %7
  %15 = sub i64 %6, %12
  %16 = load ptr, ptr %0, align 8, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %16, i64 %12
  %18 = getelementptr inbounds i8, ptr %16, i64 %1
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %18, ptr align 1 %17, i64 %15, i1 false)
  %19 = load i64, ptr %5, align 8, !tbaa !5
  %storemerge = tail call i64 @llvm.usub.sat.i64(i64 %19, i64 %11)
  store i64 %storemerge, ptr %5, align 8, !tbaa !5
  %20 = load ptr, ptr %0, align 8, !tbaa !13
  %21 = getelementptr inbounds i8, ptr %20, i64 %storemerge
  store i8 0, ptr %21, align 1, !tbaa !14
  br label %22

22:                                               ; preds = %14, %13
  %.0.in = phi i64 [ %1, %13 ], [ %11, %14 ]
  %.0 = trunc i64 %.0.in to i32
  br label %23

23:                                               ; preds = %3, %4, %22
  %.1 = phi i32 [ %.0, %22 ], [ 0, %4 ], [ 0, %3 ]
  ret i32 %.1
}

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) 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) }

!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, !10, i64 16}
!6 = !{!"XByteBuffer", !7, i64 0, !10, i64 8, !10, i64 16, !11, i64 24, !11, i64 26, !12, i64 28}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !7, i64 0}
!14 = !{!8, !8, i64 0}


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