; 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 i64 @xstrncpys(ptr noundef, i64 noundef, ptr 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_Cut(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %4) #2 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !5
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %22, label %17

17:                                               ; preds = %5
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.XString, ptr %18, i32 0, i32 1
  %20 = load i64, ptr %19, align 8, !tbaa !11
  %21 = icmp ne i64 %20, 0
  br i1 %21, label %23, label %22

22:                                               ; preds = %17, %5
  store i32 -1, ptr %6, align 4
  br label %78

23:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = load ptr, ptr %10, align 8, !tbaa !5
  %26 = call i32 @XString_Search(ptr noundef %24, i64 noundef 0, ptr noundef %25)
  store i32 %26, ptr %12, align 4, !tbaa !14
  %27 = load i32, ptr %12, align 4, !tbaa !14
  %28 = icmp slt i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %23
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %77

30:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 0, ptr %14, align 4, !tbaa !14
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = call i64 @strlen(ptr noundef %31) #4
  %33 = trunc i64 %32 to i32
  %34 = load i32, ptr %12, align 4, !tbaa !14
  %35 = add nsw i32 %34, %33
  store i32 %35, ptr %12, align 4, !tbaa !14
  %36 = load i32, ptr %12, align 4, !tbaa !14
  %37 = sext i32 %36 to i64
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.XString, ptr %38, i32 0, i32 1
  %40 = load i64, ptr %39, align 8, !tbaa !11
  %41 = icmp uge i64 %37, %40
  br i1 %41, label %42, label %43

42:                                               ; preds = %30
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %76

43:                                               ; preds = %30
  %44 = load ptr, ptr %11, align 8, !tbaa !5
  %45 = icmp eq ptr %44, null
  br i1 %45, label %46, label %53

46:                                               ; preds = %43
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.XString, ptr %47, i32 0, i32 1
  %49 = load i64, ptr %48, align 8, !tbaa !11
  %50 = trunc i64 %49 to i32
  %51 = load i32, ptr %12, align 4, !tbaa !14
  %52 = sub nsw i32 %50, %51
  store i32 %52, ptr %14, align 4, !tbaa !14
  br label %59

53:                                               ; preds = %43
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = load i32, ptr %12, align 4, !tbaa !14
  %56 = sext i32 %55 to i64
  %57 = load ptr, ptr %11, align 8, !tbaa !5
  %58 = call i32 @XString_Search(ptr noundef %54, i64 noundef %56, ptr noundef %57)
  store i32 %58, ptr %14, align 4, !tbaa !14
  br label %59

59:                                               ; preds = %53, %46
  %60 = load i32, ptr %14, align 4, !tbaa !14
  %61 = icmp slt i32 %60, 0
  br i1 %61, label %62, label %63

62:                                               ; preds = %59
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %76

63:                                               ; preds = %59
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  %65 = load i64, ptr %9, align 8, !tbaa !9
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.XString, ptr %66, i32 0, i32 0
  %68 = load ptr, ptr %67, align 8, !tbaa !16
  %69 = load i32, ptr %12, align 4, !tbaa !14
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds i8, ptr %68, i64 %70
  %72 = load i32, ptr %14, align 4, !tbaa !14
  %73 = sext i32 %72 to i64
  %74 = call i64 @xstrncpys(ptr noundef %64, i64 noundef %65, ptr noundef %71, i64 noundef %73)
  %75 = load i32, ptr %14, align 4, !tbaa !14
  store i32 %75, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %76

76:                                               ; preds = %63, %62, %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %77

77:                                               ; preds = %76, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %78

78:                                               ; preds = %77, %22
  %79 = load i32, ptr %6, align 4
  ret i32 %79
}

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @xstrncpys(ptr noundef, i64 noundef, ptr 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 range(i32 -1, -2147483648) i32 @XString_Cut(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %4) local_unnamed_addr #1 {
  %6 = icmp eq ptr %0, null
  br i1 %6, label %33, label %7

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

10:                                               ; preds = %7
  %11 = tail call i32 @XString_Search(ptr noundef nonnull %0, i64 noundef 0, ptr noundef %3)
  %12 = icmp slt i32 %11, 0
  br i1 %12, label %33, label %13

13:                                               ; preds = %10
  %14 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %3) #2
  %15 = trunc i64 %14 to i32
  %16 = add nsw i32 %11, %15
  %17 = sext i32 %16 to i64
  %18 = load i64, ptr %8, align 8, !tbaa !5
  %.not28 = icmp ugt i64 %18, %17
  br i1 %.not28, label %19, label %33

19:                                               ; preds = %13
  %20 = icmp eq ptr %4, null
  br i1 %20, label %21, label %24

21:                                               ; preds = %19
  %22 = trunc i64 %18 to i32
  %23 = sub nsw i32 %22, %16
  br label %26

24:                                               ; preds = %19
  %25 = tail call i32 @XString_Search(ptr noundef nonnull %0, i64 noundef %17, ptr noundef nonnull %4)
  br label %26

26:                                               ; preds = %24, %21
  %.0 = phi i32 [ %23, %21 ], [ %25, %24 ]
  %27 = icmp slt i32 %.0, 0
  br i1 %27, label %33, label %28

28:                                               ; preds = %26
  %29 = load ptr, ptr %0, align 8, !tbaa !12
  %30 = getelementptr inbounds i8, ptr %29, i64 %17
  %31 = zext nneg i32 %.0 to i64
  %32 = tail call i64 @xstrncpys(ptr noundef %1, i64 noundef %2, ptr noundef %30, i64 noundef %31)
  br label %33

33:                                               ; preds = %10, %26, %13, %28, %5, %7
  %.2 = phi i32 [ -1, %7 ], [ -1, %5 ], [ -1, %10 ], [ %.0, %28 ], [ -1, %13 ], [ -1, %26 ]
  ret i32 %.2
}

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 = !{!6, !7, i64 0}


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