; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @XString_Add(ptr noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  %9 = load i64, ptr %7, align 8, !tbaa !9
  %10 = icmp ne i64 %9, 0
  br i1 %10, label %16, label %11

11:                                               ; preds = %3
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.XString, ptr %12, i32 0, i32 1
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %4, align 4
  br label %71

16:                                               ; preds = %3
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = add i64 %18, 1
  %20 = call i32 @XString_Increase(ptr noundef %17, i64 noundef %19)
  %21 = icmp sle i32 %20, 0
  br i1 %21, label %22, label %23

22:                                               ; preds = %16
  store i32 -1, ptr %4, align 4
  br label %71

23:                                               ; preds = %16
  br label %24

24:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %25 = load i64, ptr %7, align 8, !tbaa !9
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.XString, ptr %26, i32 0, i32 2
  %28 = load i64, ptr %27, align 8, !tbaa !14
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.XString, ptr %29, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = sub i64 %28, %31
  %33 = icmp ult i64 %25, %32
  br i1 %33, label %34, label %36

34:                                               ; preds = %24
  %35 = load i64, ptr %7, align 8, !tbaa !9
  br label %44

36:                                               ; preds = %24
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.XString, ptr %37, i32 0, i32 2
  %39 = load i64, ptr %38, align 8, !tbaa !14
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.XString, ptr %40, i32 0, i32 1
  %42 = load i64, ptr %41, align 8, !tbaa !11
  %43 = sub i64 %39, %42
  br label %44

44:                                               ; preds = %36, %34
  %45 = phi i64 [ %35, %34 ], [ %43, %36 ]
  store i64 %45, ptr %8, align 8, !tbaa !9
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.XString, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !15
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.XString, ptr %49, i32 0, i32 1
  %51 = load i64, ptr %50, align 8, !tbaa !11
  %52 = getelementptr inbounds i8, ptr %48, i64 %51
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %52, ptr align 1 %53, i64 %54, i1 false)
  %55 = load i64, ptr %8, align 8, !tbaa !9
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.XString, ptr %56, i32 0, i32 1
  %58 = load i64, ptr %57, align 8, !tbaa !11
  %59 = add i64 %58, %55
  store i64 %59, ptr %57, align 8, !tbaa !11
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.XString, ptr %60, i32 0, i32 0
  %62 = load ptr, ptr %61, align 8, !tbaa !15
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.XString, ptr %63, i32 0, i32 1
  %65 = load i64, ptr %64, align 8, !tbaa !11
  %66 = getelementptr inbounds i8, ptr %62, i64 %65
  store i8 0, ptr %66, align 1, !tbaa !16
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.XString, ptr %67, i32 0, i32 1
  %69 = load i64, ptr %68, align 8, !tbaa !11
  %70 = trunc i64 %69 to i32
  store i32 %70, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %71

71:                                               ; preds = %44, %22, %11
  %72 = load i32, ptr %4, align 4
  ret i32 %72
}

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 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 = !{!12, !10, i64 16}
!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.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @XString_Add(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #1 {
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %4, label %6

4:                                                ; preds = %3
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  br label %.sink.split

6:                                                ; preds = %3
  %7 = add i64 %2, 1
  %8 = tail call i32 @XString_Increase(ptr noundef %0, i64 noundef %7)
  %9 = icmp slt i32 %8, 1
  br i1 %9, label %24, label %10

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !12
  %15 = sub i64 %12, %14
  %. = tail call i64 @llvm.umin.i64(i64 %15, i64 %2)
  %16 = load ptr, ptr %0, align 8, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %16, i64 %14
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %17, ptr align 1 %1, i64 %., i1 false)
  %18 = load i64, ptr %13, align 8, !tbaa !12
  %19 = add i64 %18, %.
  store i64 %19, ptr %13, align 8, !tbaa !12
  %20 = load ptr, ptr %0, align 8, !tbaa !13
  %21 = getelementptr inbounds i8, ptr %20, i64 %19
  store i8 0, ptr %21, align 1, !tbaa !14
  br label %.sink.split

.sink.split:                                      ; preds = %4, %10
  %.sink21 = phi ptr [ %13, %10 ], [ %5, %4 ]
  %22 = load i64, ptr %.sink21, align 8, !tbaa !12
  %23 = trunc i64 %22 to i32
  br label %24

24:                                               ; preds = %.sink.split, %6
  %.0 = phi i32 [ -1, %6 ], [ %23, %.sink.split ]
  ret i32 %.0
}

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"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, !10, i64 8}
!13 = !{!6, !7, i64 0}
!14 = !{!8, !8, i64 0}


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