; LLVM IR from wrnlb666/yet-another-string-library-in-c :: yasli (str_remove)

; 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.string_t = type { i64, i64, [1 x i8] }

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @str_resize(ptr noundef, i64 noundef) #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 ptr @str_remove(ptr noundef %0, i64 noundef %1, i64 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, 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
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %3
  store ptr null, ptr %4, align 8
  br label %65

12:                                               ; preds = %3
  %13 = load i64, ptr %6, align 8, !tbaa !9
  %14 = load i64, ptr %7, align 8, !tbaa !9
  %15 = add i64 %13, %14
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.string_t, ptr %17, i32 0, i32 0
  %19 = load i64, ptr %18, align 8, !tbaa !11
  %20 = icmp ugt i64 %15, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %12
  store ptr null, ptr %4, align 8
  br label %65

22:                                               ; preds = %12
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.string_t, ptr %24, i32 0, i32 2
  %26 = getelementptr inbounds [1 x i8], ptr %25, i64 0, i64 0
  %27 = load i64, ptr %6, align 8, !tbaa !9
  %28 = getelementptr inbounds i8, ptr %26, i64 %27
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = load ptr, ptr %29, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.string_t, ptr %30, i32 0, i32 2
  %32 = getelementptr inbounds [1 x i8], ptr %31, i64 0, i64 0
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = getelementptr inbounds i8, ptr %32, i64 %33
  %35 = load i64, ptr %7, align 8, !tbaa !9
  %36 = getelementptr inbounds i8, ptr %34, i64 %35
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.string_t, ptr %38, i32 0, i32 0
  %40 = load i64, ptr %39, align 8, !tbaa !11
  %41 = load i64, ptr %6, align 8, !tbaa !9
  %42 = sub i64 %40, %41
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = sub i64 %42, %43
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %28, ptr align 1 %36, i64 %44, i1 false)
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = load ptr, ptr %46, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.string_t, ptr %47, i32 0, i32 0
  %49 = load i64, ptr %48, align 8, !tbaa !11
  %50 = load i64, ptr %7, align 8, !tbaa !9
  %51 = sub i64 %49, %50
  %52 = call zeroext i1 @str_resize(ptr noundef %45, i64 noundef %51)
  br i1 %52, label %53, label %64

53:                                               ; preds = %22
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = load ptr, ptr %54, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.string_t, ptr %55, i32 0, i32 2
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = load ptr, ptr %57, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.string_t, ptr %58, i32 0, i32 0
  %60 = load i64, ptr %59, align 8, !tbaa !11
  %61 = getelementptr inbounds [1 x i8], ptr %56, i64 0, i64 %60
  store i8 0, ptr %61, align 1, !tbaa !13
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = load ptr, ptr %62, align 8, !tbaa !5
  store ptr %63, ptr %4, align 8
  br label %65

64:                                               ; preds = %22
  store ptr null, ptr %4, align 8
  br label %65

65:                                               ; preds = %64, %53, %21, %11
  %66 = load ptr, ptr %4, align 8
  ret ptr %66
}

attributes #0 = { inlinehint 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 #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" }

!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 0}
!12 = !{!"string_t", !10, i64 0, !10, i64 8, !7, i64 16}
!13 = !{!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: inlinehint nounwind uwtable
declare hidden zeroext i1 @str_resize(ptr noundef, i64 noundef) local_unnamed_addr #0

; 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) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @str_remove(ptr noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %25, label %6

6:                                                ; preds = %3
  %7 = add i64 %2, %1
  %8 = load i64, ptr %4, align 8, !tbaa !9
  %9 = icmp ugt i64 %7, %8
  br i1 %9, label %25, label %10

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %4, i64 16
  %12 = getelementptr inbounds i8, ptr %11, i64 %1
  %13 = getelementptr inbounds i8, ptr %12, i64 %2
  %14 = sub i64 %8, %7
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 1 %12, ptr nonnull align 1 %13, i64 %14, i1 false)
  %15 = load ptr, ptr %0, align 8, !tbaa !5
  %16 = load i64, ptr %15, align 8, !tbaa !9
  %17 = sub i64 %16, %2
  %18 = tail call zeroext i1 @str_resize(ptr noundef nonnull %0, i64 noundef %17)
  br i1 %18, label %19, label %25

19:                                               ; preds = %10
  %20 = load ptr, ptr %0, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  %22 = load i64, ptr %20, align 8, !tbaa !9
  %23 = getelementptr inbounds [1 x i8], ptr %21, i64 0, i64 %22
  store i8 0, ptr %23, align 1, !tbaa !12
  %24 = load ptr, ptr %0, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %10, %6, %3, %19
  %.0 = phi ptr [ %24, %19 ], [ null, %3 ], [ null, %6 ], [ null, %10 ]
  ret ptr %.0
}

attributes #0 = { inlinehint 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 #1 = { mustprogress 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" }

!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 0}
!10 = !{!"string_t", !11, i64 0, !11, i64 8, !7, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!7, !7, i64 0}


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