; 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 }

@.str = external hidden unnamed_addr constant [11 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [77 x i8], align 1
@__PRETTY_FUNCTION__.strsto_find_prefix = external hidden unnamed_addr constant [66 x i8], align 1

; 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: nounwind uwtable
define dso_local i32 @strsto_find_prefix(ptr noundef %0, ptr %1, i64 %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca %struct.StrSlice, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  %10 = alloca i8, align 1
  %11 = alloca i32, align 4
  %12 = alloca %struct.StrSlice, align 8
  %13 = alloca i64, align 8
  %14 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 0
  store ptr %1, ptr %14, align 8
  %15 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 1
  store i64 %2, ptr %15, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.StringStore, ptr %16, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  %19 = call i32 @dynbuf_count(ptr noundef %18)
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %22, label %21

21:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  br label %121

22:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #5
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8
  %26 = getelementptr inbounds { ptr, i64 }, ptr %5, i32 0, i32 1
  %27 = load i64, ptr %26, align 8
  %28 = call zeroext i1 @strsto_iidx_find(ptr noundef %23, ptr %25, i64 %27, ptr noundef %7)
  %29 = zext i1 %28 to i8
  store i8 %29, ptr %8, align 1, !tbaa !13
  %30 = load i8, ptr %8, align 1, !tbaa !13, !range !15, !noundef !16
  %31 = trunc i8 %30 to i1
  br i1 %31, label %32, label %39

32:                                               ; preds = %22
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.StringStore, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !9
  %36 = load i64, ptr %7, align 8, !tbaa !17
  %37 = getelementptr inbounds i32, ptr %35, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !18
  store i32 %38, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %120

39:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #5
  store i8 0, ptr %10, align 1, !tbaa !13
  br label %40

40:                                               ; preds = %118, %39
  br label %41

41:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.StringStore, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !9
  %45 = load i64, ptr %7, align 8, !tbaa !17
  %46 = getelementptr inbounds i32, ptr %44, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !18
  store i32 %47, ptr %11, align 4, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 16, ptr %12) #5
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.StringStore, ptr %48, i32 0, i32 0
  %50 = load ptr, ptr %49, align 8, !tbaa !20
  %51 = load i32, ptr %11, align 4, !tbaa !18
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds %struct.StrSlice, ptr %50, i64 %52
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %12, ptr align 8 %53, i64 16, i1 false), !tbaa.struct !21
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  store i64 0, ptr %13, align 8, !tbaa !17
  br label %54

54:                                               ; preds = %80, %41
  %55 = load i64, ptr %13, align 8, !tbaa !17
  %56 = getelementptr inbounds %struct.StrSlice, ptr %12, i32 0, i32 1
  %57 = load i64, ptr %56, align 8, !tbaa !22
  %58 = icmp ult i64 %55, %57
  br i1 %58, label %59, label %78

59:                                               ; preds = %54
  %60 = load i64, ptr %13, align 8, !tbaa !17
  %61 = getelementptr inbounds %struct.StrSlice, ptr %5, i32 0, i32 1
  %62 = load i64, ptr %61, align 8, !tbaa !22
  %63 = icmp ult i64 %60, %62
  br i1 %63, label %64, label %78

64:                                               ; preds = %59
  %65 = getelementptr inbounds %struct.StrSlice, ptr %12, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !24
  %67 = load i64, ptr %13, align 8, !tbaa !17
  %68 = getelementptr inbounds i8, ptr %66, i64 %67
  %69 = load i8, ptr %68, align 1, !tbaa !25
  %70 = sext i8 %69 to i32
  %71 = getelementptr inbounds %struct.StrSlice, ptr %5, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !24
  %73 = load i64, ptr %13, align 8, !tbaa !17
  %74 = getelementptr inbounds i8, ptr %72, i64 %73
  %75 = load i8, ptr %74, align 1, !tbaa !25
  %76 = sext i8 %75 to i32
  %77 = icmp eq i32 %70, %76
  br label %78

78:                                               ; preds = %64, %59, %54
  %79 = phi i1 [ false, %59 ], [ false, %54 ], [ %77, %64 ]
  br i1 %79, label %80, label %83

80:                                               ; preds = %78
  %81 = load i64, ptr %13, align 8, !tbaa !17
  %82 = add i64 %81, 1
  store i64 %82, ptr %13, align 8, !tbaa !17
  br label %54, !llvm.loop !26

83:                                               ; preds = %78
  %84 = load i64, ptr %13, align 8, !tbaa !17
  %85 = getelementptr inbounds %struct.StrSlice, ptr %12, i32 0, i32 1
  %86 = load i64, ptr %85, align 8, !tbaa !22
  %87 = icmp ne i64 %84, %86
  br i1 %87, label %88, label %106

88:                                               ; preds = %83
  %89 = load i64, ptr %7, align 8, !tbaa !17
  %90 = icmp ne i64 %89, 0
  br i1 %90, label %92, label %91

91:                                               ; preds = %88
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %116

92:                                               ; preds = %88
  %93 = load i64, ptr %13, align 8, !tbaa !17
  %94 = icmp ne i64 %93, 0
  br i1 %94, label %99, label %95

95:                                               ; preds = %92
  %96 = load i8, ptr %10, align 1, !tbaa !13, !range !15, !noundef !16
  %97 = trunc i8 %96 to i1
  br i1 %97, label %98, label %99

98:                                               ; preds = %95
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %116

99:                                               ; preds = %95, %92
  %100 = load i64, ptr %13, align 8, !tbaa !17
  %101 = icmp ne i64 %100, 0
  %102 = xor i1 %101, true
  %103 = zext i1 %102 to i8
  store i8 %103, ptr %10, align 1, !tbaa !13
  %104 = load i64, ptr %7, align 8, !tbaa !17
  %105 = add i64 %104, -1
  store i64 %105, ptr %7, align 8, !tbaa !17
  br label %115

106:                                              ; preds = %83
  %107 = load i64, ptr %13, align 8, !tbaa !17
  %108 = getelementptr inbounds %struct.StrSlice, ptr %5, i32 0, i32 1
  %109 = load i64, ptr %108, align 8, !tbaa !22
  %110 = icmp ne i64 %107, %109
  br i1 %110, label %111, label %112

111:                                              ; preds = %106
  br label %113

112:                                              ; preds = %106
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 52, ptr noundef @__PRETTY_FUNCTION__.strsto_find_prefix) #6
  unreachable

113:                                              ; preds = %111
  %114 = load i32, ptr %11, align 4, !tbaa !18
  store i32 %114, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %116

115:                                              ; preds = %99
  store i32 0, ptr %9, align 4
  br label %116

116:                                              ; preds = %115, %113, %98, %91
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  %117 = load i32, ptr %9, align 4
  switch i32 %117, label %119 [
    i32 0, label %118
  ]

118:                                              ; preds = %116
  br label %40

119:                                              ; preds = %116
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #5
  br label %120

120:                                              ; preds = %119, %32
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %121

121:                                              ; preds = %120, %21
  %122 = load i32, ptr %4, align 4
  ret i32 %122
}

; 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: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #4

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 = { noreturn nounwind "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 }
attributes #6 = { noreturn 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, !6, i64 8}
!10 = !{!"StringStore", !6, i64 0, !6, i64 8, !11, i64 16}
!11 = !{!"AllocatorArena", !12, i64 0, !6, i64 8, !6, i64 16}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!12, !12, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"int", !7, i64 0}
!20 = !{!10, !6, i64 0}
!21 = !{i64 0, i64 8, !5, i64 8, i64 8, !17}
!22 = !{!23, !12, i64 8}
!23 = !{!"StrSlice", !6, i64 0, !12, i64 8}
!24 = !{!23, !6, i64 0}
!25 = !{!7, !7, i64 0}
!26 = distinct !{!26, !27}
!27 = !{!"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"

%struct.StrSlice = type { ptr, i64 }

@.str = external hidden unnamed_addr constant [11 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [77 x i8], align 1
@__PRETTY_FUNCTION__.strsto_find_prefix = external hidden unnamed_addr constant [66 x i8], align 1

; 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: nounwind uwtable
define dso_local i32 @strsto_find_prefix(ptr noundef %0, ptr %1, i64 %2) local_unnamed_addr #0 {
  %4 = alloca i64, align 8
  %.fr = freeze i64 %2
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = tail call i32 @dynbuf_count(ptr noundef %6)
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %36, label %8

8:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #5
  %9 = call zeroext i1 @strsto_iidx_find(ptr noundef nonnull %0, ptr %1, i64 %.fr, ptr noundef nonnull %4)
  br i1 %9, label %10, label %thread-pre-split

10:                                               ; preds = %8
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = load i64, ptr %4, align 8, !tbaa !12
  %13 = getelementptr inbounds i32, ptr %11, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !13
  br label %.thread

thread-pre-split:                                 ; preds = %8
  %.pr = load i64, ptr %4, align 8, !tbaa !12
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load ptr, ptr %0, align 8, !tbaa !15
  br label %17

17:                                               ; preds = %thread-pre-split, %34
  %18 = phi i64 [ %.pr, %thread-pre-split ], [ %35, %34 ]
  %.026 = phi i1 [ false, %thread-pre-split ], [ %.not31, %34 ]
  %19 = getelementptr inbounds i32, ptr %15, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !13
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds %struct.StrSlice, ptr %16, i64 %21
  %.sroa.0.0.copyload = load ptr, ptr %22, align 8, !tbaa !16
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %22, i64 8
  %.sroa.4.0.copyload = load i64, ptr %.sroa.4.0..sroa_idx, align 8, !tbaa !12
  %invariant.umin = call i64 @llvm.umin.i64(i64 %.sroa.4.0.copyload, i64 %.fr)
  %or.cond37.not = icmp eq i64 %invariant.umin, 0
  br i1 %or.cond37.not, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %17, %28
  %.038 = phi i64 [ %29, %28 ], [ 0, %17 ]
  %23 = getelementptr inbounds i8, ptr %.sroa.0.0.copyload, i64 %.038
  %24 = load i8, ptr %23, align 1, !tbaa !17
  %25 = getelementptr inbounds i8, ptr %1, i64 %.038
  %26 = load i8, ptr %25, align 1, !tbaa !17
  %27 = icmp eq i8 %24, %26
  br i1 %27, label %28, label %.critedge

28:                                               ; preds = %.lr.ph
  %29 = add nuw i64 %.038, 1
  %exitcond.not = icmp eq i64 %29, %invariant.umin
  br i1 %exitcond.not, label %.critedge, label %.lr.ph, !llvm.loop !18

.critedge:                                        ; preds = %.lr.ph, %28, %17
  %.0.lcssa = phi i64 [ 0, %17 ], [ %invariant.umin, %28 ], [ %.038, %.lr.ph ]
  %.not28 = icmp eq i64 %.0.lcssa, %.sroa.4.0.copyload
  br i1 %.not28, label %32, label %30

30:                                               ; preds = %.critedge
  %.not30 = icmp eq i64 %18, 0
  br i1 %.not30, label %.thread, label %31

31:                                               ; preds = %30
  %.not31 = icmp eq i64 %.0.lcssa, 0
  %brmerge.demorgan = and i1 %.026, %.not31
  br i1 %brmerge.demorgan, label %.thread, label %34

32:                                               ; preds = %.critedge
  %.not29 = icmp eq i64 %.sroa.4.0.copyload, %.fr
  br i1 %.not29, label %33, label %.thread

33:                                               ; preds = %32
  call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 52, ptr noundef nonnull @__PRETTY_FUNCTION__.strsto_find_prefix) #6
  unreachable

34:                                               ; preds = %31
  %35 = add i64 %18, -1
  store i64 %35, ptr %4, align 8, !tbaa !12
  br label %17

.thread:                                          ; preds = %31, %30, %32, %10
  %.2 = phi i32 [ %14, %10 ], [ %20, %32 ], [ -1, %30 ], [ -1, %31 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #5
  br label %36

36:                                               ; preds = %3, %.thread
  %.3 = phi i32 [ %.2, %.thread ], [ -1, %3 ]
  ret i32 %.3
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #3

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

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 = { noreturn nounwind "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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nounwind }
attributes #6 = { noreturn 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, !7, i64 8}
!6 = !{!"StringStore", !7, i64 0, !7, i64 8, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"AllocatorArena", !11, i64 0, !7, i64 8, !7, i64 16}
!11 = !{!"long", !8, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !8, i64 0}
!15 = !{!6, !7, i64 0}
!16 = !{!7, !7, i64 0}
!17 = !{!8, !8, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass