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

; 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.XString = type { ptr, i64, i64, i16, i8, i8 }

; 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: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @XString_Status(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @XString_Insert(ptr noundef, i64 noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @XString_Remove(ptr noundef, i64 noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @XString_Search(ptr noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @XString_Replace(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, 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 ptr %2, ptr %7, align 8, !tbaa !5
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %18, label %13

13:                                               ; preds = %3
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.XString, ptr %14, i32 0, i32 1
  %16 = load i64, ptr %15, align 8, !tbaa !9
  %17 = icmp ne i64 %16, 0
  br i1 %17, label %19, label %18

18:                                               ; preds = %13, %3
  store i32 -1, ptr %4, align 4
  br label %50

19:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = call i64 @strlen(ptr noundef %20) #4
  store i64 %21, ptr %8, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = call i64 @strlen(ptr noundef %22) #4
  store i64 %23, ptr %9, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !14
  br label %24

24:                                               ; preds = %31, %19
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load i32, ptr %10, align 4, !tbaa !14
  %27 = sext i32 %26 to i64
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = call i32 @XString_Search(ptr noundef %25, i64 noundef %27, ptr noundef %28)
  store i32 %29, ptr %10, align 4, !tbaa !14
  %30 = icmp sge i32 %29, 0
  br i1 %30, label %31, label %47

31:                                               ; preds = %24
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load i32, ptr %10, align 4, !tbaa !14
  %34 = sext i32 %33 to i64
  %35 = load i64, ptr %9, align 8, !tbaa !13
  %36 = call i32 @XString_Remove(ptr noundef %32, i64 noundef %34, i64 noundef %35)
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = load i32, ptr %10, align 4, !tbaa !14
  %39 = sext i32 %38 to i64
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = load i64, ptr %8, align 8, !tbaa !13
  %42 = call i32 @XString_Insert(ptr noundef %37, i64 noundef %39, ptr noundef %40, i64 noundef %41)
  %43 = load i64, ptr %8, align 8, !tbaa !13
  %44 = trunc i64 %43 to i32
  %45 = load i32, ptr %10, align 4, !tbaa !14
  %46 = add nsw i32 %45, %44
  store i32 %46, ptr %10, align 4, !tbaa !14
  br label %24, !llvm.loop !16

47:                                               ; preds = %24
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = call i32 @XString_Status(ptr noundef %48)
  store i32 %49, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %50

50:                                               ; preds = %47, %18
  %51 = load i32, ptr %4, align 4
  ret i32 %51
}

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

!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, !11, i64 8}
!10 = !{!"XString", !6, i64 0, !11, i64 8, !11, i64 16, !12, i64 24, !7, i64 26, !7, i64 27}
!11 = !{!"long", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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 nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @XString_Status(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @XString_Insert(ptr noundef, i64 noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @XString_Remove(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @XString_Search(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XString_Replace(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = icmp eq ptr %0, null
  br i1 %4, label %24, label %5

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %.not = icmp eq i64 %7, 0
  br i1 %.not, label %24, label %8

8:                                                ; preds = %5
  %9 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %2) #2
  %10 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #2
  %11 = tail call i32 @XString_Search(ptr noundef nonnull %0, i64 noundef 0, ptr noundef %1)
  %12 = icmp sgt i32 %11, -1
  br i1 %12, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %8
  %13 = trunc i64 %9 to i32
  br label %14

14:                                               ; preds = %.lr.ph, %14
  %15 = phi i32 [ %11, %.lr.ph ], [ %21, %14 ]
  %16 = zext nneg i32 %15 to i64
  %17 = tail call i32 @XString_Remove(ptr noundef nonnull %0, i64 noundef %16, i64 noundef %10)
  %18 = tail call i32 @XString_Insert(ptr noundef nonnull %0, i64 noundef %16, ptr noundef %2, i64 noundef %9)
  %19 = add nsw i32 %15, %13
  %20 = sext i32 %19 to i64
  %21 = tail call i32 @XString_Search(ptr noundef nonnull %0, i64 noundef %20, ptr noundef %1)
  %22 = icmp sgt i32 %21, -1
  br i1 %22, label %14, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %14, %8
  %23 = tail call i32 @XString_Status(ptr noundef nonnull %0)
  br label %24

24:                                               ; preds = %3, %5, %._crit_edge
  %.017 = phi i32 [ %23, %._crit_edge ], [ -1, %5 ], [ -1, %3 ]
  ret i32 %.017
}

attributes #0 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { 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 #2 = { nounwind willreturn memory(read) }

!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 8}
!6 = !{!"XString", !7, i64 0, !10, i64 8, !10, i64 16, !11, i64 24, !8, i64 26, !8, i64 27}
!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 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass