; LLVM IR from aagdev/mlimgsynth :: src/ccommon (stringstore)

; 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.StrSlice = type { ptr, i64 }
%struct.StringStore = type { ptr, ptr, %struct.AllocatorArena }
%struct.AllocatorArena = type { i64, ptr, ptr }

@g_allocator_dopt = external global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @strsto_iidx_find(ptr noundef, ptr, i64, ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @dynbuf_count(ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @strsto_add2(ptr noundef %0, ptr %1, i64 %2, i32 noundef %3, i1 noundef zeroext %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca %struct.StrSlice, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i8, align 1
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca %struct.StrSlice, align 8
  %16 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 0
  store ptr %1, ptr %16, align 8
  %17 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 1
  store i64 %2, ptr %17, align 8
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !9
  %18 = zext i1 %4 to i8
  store i8 %18, ptr %10, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8
  %22 = getelementptr inbounds { ptr, i64 }, ptr %7, i32 0, i32 1
  %23 = load i64, ptr %22, align 8
  %24 = call zeroext i1 @strsto_iidx_find(ptr noundef %19, ptr %21, i64 %23, ptr noundef %11)
  br i1 %24, label %25, label %45

25:                                               ; preds = %5
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = icmp sge i32 %26, 0
  br i1 %27, label %28, label %38

28:                                               ; preds = %25
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.StringStore, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  %33 = load i64, ptr %11, align 8, !tbaa !17
  %34 = getelementptr inbounds i32, ptr %32, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = icmp ne i32 %29, %35
  br i1 %36, label %37, label %38

37:                                               ; preds = %28
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %12, align 4
  br label %125

38:                                               ; preds = %28, %25
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.StringStore, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !13
  %42 = load i64, ptr %11, align 8, !tbaa !17
  %43 = getelementptr inbounds i32, ptr %41, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !9
  store i32 %44, ptr %6, align 4
  store i32 1, ptr %12, align 4
  br label %125

45:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.StringStore, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !18
  %49 = call i32 @dynbuf_count(ptr noundef %48)
  store i32 %49, ptr %13, align 4, !tbaa !9
  %50 = load i32, ptr %9, align 4, !tbaa !9
  %51 = icmp slt i32 %50, 0
  br i1 %51, label %52, label %54

52:                                               ; preds = %45
  %53 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %53, ptr %9, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %52, %45
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = load i32, ptr %13, align 4, !tbaa !9
  %57 = icmp ult i32 %55, %56
  br i1 %57, label %58, label %70

58:                                               ; preds = %54
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.StringStore, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !18
  %62 = load i32, ptr %9, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds %struct.StrSlice, ptr %61, i64 %63
  %65 = getelementptr inbounds %struct.StrSlice, ptr %64, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !19
  %67 = icmp ne ptr %66, null
  br i1 %67, label %68, label %69

68:                                               ; preds = %58
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %12, align 4
  br label %124

69:                                               ; preds = %58
  br label %79

70:                                               ; preds = %54
  %71 = load ptr, ptr %8, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.StringStore, ptr %71, i32 0, i32 0
  %73 = load i32, ptr %9, align 4, !tbaa !9
  %74 = load i32, ptr %13, align 4, !tbaa !9
  %75 = sub i32 %73, %74
  %76 = add i32 %75, 1
  %77 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !5
  %78 = call ptr @dynbuf_append(ptr noundef %72, i32 noundef %76, ptr noundef null, i64 noundef 16, i32 noundef 2, ptr noundef %77)
  br label %79

79:                                               ; preds = %70, %69
  %80 = load i8, ptr %10, align 1, !tbaa !11, !range !21, !noundef !22
  %81 = trunc i8 %80 to i1
  br i1 %81, label %82, label %89

82:                                               ; preds = %79
  %83 = load ptr, ptr %8, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.StringStore, ptr %83, i32 0, i32 0
  %85 = load ptr, ptr %84, align 8, !tbaa !18
  %86 = load i32, ptr %9, align 4, !tbaa !9
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds %struct.StrSlice, ptr %85, i64 %87
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %88, ptr align 8 %7, i64 16, i1 false), !tbaa.struct !23
  br label %116

89:                                               ; preds = %79
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %90 = load ptr, ptr %8, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.StringStore, ptr %90, i32 0, i32 2
  %92 = getelementptr inbounds %struct.StrSlice, ptr %7, i32 0, i32 1
  %93 = load i64, ptr %92, align 8, !tbaa !24
  %94 = add i64 %93, 1
  %95 = call ptr @alloc_arena_alloc(ptr noundef %91, i64 noundef %94)
  store ptr %95, ptr %14, align 8, !tbaa !5
  %96 = load ptr, ptr %14, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.StrSlice, ptr %7, i32 0, i32 0
  %98 = load ptr, ptr %97, align 8, !tbaa !19
  %99 = getelementptr inbounds %struct.StrSlice, ptr %7, i32 0, i32 1
  %100 = load i64, ptr %99, align 8, !tbaa !24
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %96, ptr align 1 %98, i64 %100, i1 false)
  %101 = load ptr, ptr %14, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.StrSlice, ptr %7, i32 0, i32 1
  %103 = load i64, ptr %102, align 8, !tbaa !24
  %104 = getelementptr inbounds i8, ptr %101, i64 %103
  store i8 0, ptr %104, align 1, !tbaa !25
  %105 = load ptr, ptr %8, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.StringStore, ptr %105, i32 0, i32 0
  %107 = load ptr, ptr %106, align 8, !tbaa !18
  %108 = load i32, ptr %9, align 4, !tbaa !9
  %109 = sext i32 %108 to i64
  %110 = getelementptr inbounds %struct.StrSlice, ptr %107, i64 %109
  %111 = getelementptr inbounds %struct.StrSlice, ptr %15, i32 0, i32 0
  %112 = load ptr, ptr %14, align 8, !tbaa !5
  store ptr %112, ptr %111, align 8, !tbaa !19
  %113 = getelementptr inbounds %struct.StrSlice, ptr %15, i32 0, i32 1
  %114 = getelementptr inbounds %struct.StrSlice, ptr %7, i32 0, i32 1
  %115 = load i64, ptr %114, align 8, !tbaa !24
  store i64 %115, ptr %113, align 8, !tbaa !24
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %110, ptr align 8 %15, i64 16, i1 false), !tbaa.struct !23
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  br label %116

116:                                              ; preds = %89, %82
  %117 = load ptr, ptr %8, align 8, !tbaa !5
  %118 = getelementptr inbounds %struct.StringStore, ptr %117, i32 0, i32 1
  %119 = load i64, ptr %11, align 8, !tbaa !17
  %120 = trunc i64 %119 to i32
  %121 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !5
  %122 = call ptr @dynbuf_insert(ptr noundef %118, i32 noundef %120, i32 noundef 1, ptr noundef %9, i64 noundef 4, i32 noundef 0, ptr noundef %121)
  %123 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %123, ptr %6, align 4
  store i32 1, ptr %12, align 4
  br label %124

124:                                              ; preds = %116, %68
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  br label %125

125:                                              ; preds = %124, %38, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  %126 = load i32, ptr %6, align 4
  ret i32 %126
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @dynbuf_append(ptr noundef, i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef) #2

declare ptr @alloc_arena_alloc(ptr noundef, i64 noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @dynbuf_insert(ptr noundef, i32 noundef, i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef) #2

attributes #0 = { 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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 #5 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"StringStore", !6, i64 0, !6, i64 8, !15, i64 16}
!15 = !{!"AllocatorArena", !16, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!"long", !7, i64 0}
!17 = !{!16, !16, i64 0}
!18 = !{!14, !6, i64 0}
!19 = !{!20, !6, i64 0}
!20 = !{!"StrSlice", !6, i64 0, !16, i64 8}
!21 = !{i8 0, i8 2}
!22 = !{}
!23 = !{i64 0, i64 8, !5, i64 8, i64 8, !17}
!24 = !{!20, !16, i64 8}
!25 = !{!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"

%struct.StrSlice = type { ptr, i64 }

@g_allocator_dopt = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @strsto_iidx_find(ptr noundef, ptr, i64, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @dynbuf_count(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @strsto_add2(ptr noundef %0, ptr %1, i64 %2, i32 noundef %3, i1 noundef zeroext %4) local_unnamed_addr #0 {
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  store i32 %3, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #5
  %8 = call zeroext i1 @strsto_iidx_find(ptr noundef %0, ptr %1, i64 %2, ptr noundef nonnull %7)
  br i1 %8, label %9, label %17

9:                                                ; preds = %5
  %10 = icmp sgt i32 %3, -1
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = load i64, ptr %7, align 8, !tbaa !14
  %14 = getelementptr inbounds i32, ptr %12, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  br i1 %10, label %16, label %._crit_edge

16:                                               ; preds = %9
  %.not27 = icmp eq i32 %15, %3
  %spec.select = select i1 %.not27, i32 %3, i32 -1
  br label %._crit_edge

17:                                               ; preds = %5
  %18 = load ptr, ptr %0, align 8, !tbaa !15
  %19 = call i32 @dynbuf_count(ptr noundef %18)
  %20 = icmp slt i32 %3, 0
  br i1 %20, label %.thread, label %21

.thread:                                          ; preds = %17
  store i32 %19, ptr %6, align 4, !tbaa !5
  br label %28

21:                                               ; preds = %17
  %22 = icmp ugt i32 %19, %3
  br i1 %22, label %23, label %28

23:                                               ; preds = %21
  %24 = load ptr, ptr %0, align 8, !tbaa !15
  %25 = zext nneg i32 %3 to i64
  %26 = getelementptr inbounds %struct.StrSlice, ptr %24, i64 %25
  %27 = load ptr, ptr %26, align 8, !tbaa !16
  %.not = icmp eq ptr %27, null
  br i1 %.not, label %33, label %._crit_edge

28:                                               ; preds = %.thread, %21
  %29 = phi i32 [ %19, %.thread ], [ %3, %21 ]
  %reass.sub = sub i32 %29, %19
  %30 = add i32 %reass.sub, 1
  %31 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !18
  %32 = call ptr @dynbuf_append(ptr noundef nonnull %0, i32 noundef %30, ptr noundef null, i64 noundef 16, i32 noundef 2, ptr noundef %31)
  br label %33

33:                                               ; preds = %23, %28
  %34 = phi i32 [ %3, %23 ], [ %29, %28 ]
  br i1 %4, label %40, label %35

35:                                               ; preds = %33
  %36 = getelementptr inbounds i8, ptr %0, i64 16
  %37 = add i64 %2, 1
  %38 = call ptr @alloc_arena_alloc(ptr noundef nonnull %36, i64 noundef %37) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %38, ptr align 1 %1, i64 %2, i1 false)
  %39 = getelementptr inbounds i8, ptr %38, i64 %2
  store i8 0, ptr %39, align 1, !tbaa !19
  br label %40

40:                                               ; preds = %33, %35
  %.sink31 = phi ptr [ %38, %35 ], [ %1, %33 ]
  %41 = load ptr, ptr %0, align 8, !tbaa !15
  %42 = sext i32 %34 to i64
  %43 = getelementptr inbounds %struct.StrSlice, ptr %41, i64 %42
  store ptr %.sink31, ptr %43, align 8, !tbaa !18
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %43, i64 8
  store i64 %2, ptr %.sroa.2.0..sroa_idx, align 8, !tbaa !14
  %44 = getelementptr inbounds i8, ptr %0, i64 8
  %45 = load i64, ptr %7, align 8, !tbaa !14
  %46 = trunc i64 %45 to i32
  %47 = load ptr, ptr @g_allocator_dopt, align 8, !tbaa !18
  %48 = call ptr @dynbuf_insert(ptr noundef nonnull %44, i32 noundef %46, i32 noundef 1, ptr noundef nonnull %6, i64 noundef 4, i32 noundef 0, ptr noundef %47)
  %49 = load i32, ptr %6, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %16, %9, %40, %23
  %.1 = phi i32 [ %49, %40 ], [ -1, %23 ], [ %15, %9 ], [ %spec.select, %16 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #5
  ret i32 %.1
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @dynbuf_append(ptr noundef, i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

declare ptr @alloc_arena_alloc(ptr noundef, i64 noundef) local_unnamed_addr #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @dynbuf_insert(ptr noundef, i32 noundef, i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { 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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 #5 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"StringStore", !11, i64 0, !11, i64 8, !12, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"AllocatorArena", !13, i64 0, !11, i64 8, !11, i64 16}
!13 = !{!"long", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!10, !11, i64 0}
!16 = !{!17, !11, i64 0}
!17 = !{!"StrSlice", !11, i64 0, !13, i64 8}
!18 = !{!11, !11, i64 0}
!19 = !{!7, !7, i64 0}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,ReassociatePass,SROAPass,SimplifyCFGPass