; LLVM IR from Softmotions/iowow :: src/utils (iwxstr)

; 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.iwxstr = type { ptr, i64, i64, ptr, ptr }

; 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 allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #2

; 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 i64 @iwxstr_insert(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3) #3 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.iwxstr, ptr %14, i32 0, i32 1
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = icmp ugt i64 %13, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %4
  store i64 70011, ptr %5, align 8
  br label %107

19:                                               ; preds = %4
  %20 = load i64, ptr %9, align 8, !tbaa !9
  %21 = icmp eq i64 %20, 0
  br i1 %21, label %22, label %23

22:                                               ; preds = %19
  store i64 0, ptr %5, align 8
  br label %107

23:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.iwxstr, ptr %24, i32 0, i32 1
  %26 = load i64, ptr %25, align 8, !tbaa !11
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = add i64 %26, %27
  %29 = add i64 %28, 1
  store i64 %29, ptr %10, align 8, !tbaa !9
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.iwxstr, ptr %30, i32 0, i32 2
  %32 = load i64, ptr %31, align 8, !tbaa !13
  %33 = load i64, ptr %10, align 8, !tbaa !9
  %34 = icmp ult i64 %32, %33
  br i1 %34, label %35, label %75

35:                                               ; preds = %23
  br label %36

36:                                               ; preds = %56, %35
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.iwxstr, ptr %37, i32 0, i32 2
  %39 = load i64, ptr %38, align 8, !tbaa !13
  %40 = load i64, ptr %10, align 8, !tbaa !9
  %41 = icmp ult i64 %39, %40
  br i1 %41, label %42, label %57

42:                                               ; preds = %36
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.iwxstr, ptr %43, i32 0, i32 2
  %45 = load i64, ptr %44, align 8, !tbaa !13
  %46 = shl i64 %45, 1
  store i64 %46, ptr %44, align 8, !tbaa !13
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.iwxstr, ptr %47, i32 0, i32 2
  %49 = load i64, ptr %48, align 8, !tbaa !13
  %50 = load i64, ptr %10, align 8, !tbaa !9
  %51 = icmp ult i64 %49, %50
  br i1 %51, label %52, label %56

52:                                               ; preds = %42
  %53 = load i64, ptr %10, align 8, !tbaa !9
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.iwxstr, ptr %54, i32 0, i32 2
  store i64 %53, ptr %55, align 8, !tbaa !13
  br label %56

56:                                               ; preds = %52, %42
  br label %36, !llvm.loop !14

57:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.iwxstr, ptr %58, i32 0, i32 0
  %60 = load ptr, ptr %59, align 8, !tbaa !16
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.iwxstr, ptr %61, i32 0, i32 2
  %63 = load i64, ptr %62, align 8, !tbaa !13
  %64 = call ptr @realloc(ptr noundef %60, i64 noundef %63) #5
  store ptr %64, ptr %11, align 8, !tbaa !5
  %65 = load ptr, ptr %11, align 8, !tbaa !5
  %66 = icmp ne ptr %65, null
  br i1 %66, label %68, label %67

67:                                               ; preds = %57
  store i64 70013, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %72

68:                                               ; preds = %57
  %69 = load ptr, ptr %11, align 8, !tbaa !5
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.iwxstr, ptr %70, i32 0, i32 0
  store ptr %69, ptr %71, align 8, !tbaa !16
  store i32 0, ptr %12, align 4
  br label %72

72:                                               ; preds = %68, %67
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  %73 = load i32, ptr %12, align 4
  switch i32 %73, label %106 [
    i32 0, label %74
  ]

74:                                               ; preds = %72
  br label %75

75:                                               ; preds = %74, %23
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.iwxstr, ptr %76, i32 0, i32 0
  %78 = load ptr, ptr %77, align 8, !tbaa !16
  %79 = load i64, ptr %7, align 8, !tbaa !9
  %80 = getelementptr inbounds i8, ptr %78, i64 %79
  %81 = load i64, ptr %9, align 8, !tbaa !9
  %82 = getelementptr inbounds i8, ptr %80, i64 %81
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.iwxstr, ptr %83, i32 0, i32 0
  %85 = load ptr, ptr %84, align 8, !tbaa !16
  %86 = load i64, ptr %7, align 8, !tbaa !9
  %87 = getelementptr inbounds i8, ptr %85, i64 %86
  %88 = load ptr, ptr %6, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.iwxstr, ptr %88, i32 0, i32 1
  %90 = load i64, ptr %89, align 8, !tbaa !11
  %91 = load i64, ptr %7, align 8, !tbaa !9
  %92 = sub i64 %90, %91
  %93 = add i64 %92, 1
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %82, ptr align 1 %87, i64 %93, i1 false)
  %94 = load ptr, ptr %6, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.iwxstr, ptr %94, i32 0, i32 0
  %96 = load ptr, ptr %95, align 8, !tbaa !16
  %97 = load i64, ptr %7, align 8, !tbaa !9
  %98 = getelementptr inbounds i8, ptr %96, i64 %97
  %99 = load ptr, ptr %8, align 8, !tbaa !5
  %100 = load i64, ptr %9, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %98, ptr align 1 %99, i64 %100, i1 false)
  %101 = load i64, ptr %9, align 8, !tbaa !9
  %102 = load ptr, ptr %6, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.iwxstr, ptr %102, i32 0, i32 1
  %104 = load i64, ptr %103, align 8, !tbaa !11
  %105 = add i64 %104, %101
  store i64 %105, ptr %103, align 8, !tbaa !11
  store i64 0, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %106

106:                                              ; preds = %75, %72
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %107

107:                                              ; preds = %106, %22, %18
  %108 = load i64, ptr %5, align 8
  ret i64 %108
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind allocsize(1) "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 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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(1) }

!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 = !{!"iwxstr", !6, i64 0, !10, i64 8, !10, i64 16, !6, i64 24, !6, i64 32}
!13 = !{!12, !10, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!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 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: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress nounwind willreturn uwtable
define dso_local range(i64 0, 70014) i64 @iwxstr_insert(ptr nocapture noundef %0, i64 noundef %1, ptr nocapture noundef readonly %2, i64 noundef %3) local_unnamed_addr #2 {
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = icmp ult i64 %6, %1
  br i1 %7, label %.critedge, label %8

8:                                                ; preds = %4
  %9 = icmp eq i64 %3, 0
  br i1 %9, label %.critedge, label %10

10:                                               ; preds = %8
  %11 = add i64 %3, 1
  %12 = add i64 %11, %6
  %13 = getelementptr inbounds i8, ptr %0, i64 16
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = icmp ult i64 %14, %12
  br i1 %15, label %16, label %._crit_edge

._crit_edge:                                      ; preds = %10
  %.pre = load ptr, ptr %0, align 8, !tbaa !12
  br label %21

16:                                               ; preds = %10
  %17 = shl i64 %14, 1
  %spec.select = tail call i64 @llvm.umax.i64(i64 %17, i64 %12)
  store i64 %spec.select, ptr %13, align 8, !tbaa !11
  %18 = load ptr, ptr %0, align 8, !tbaa !12
  %19 = tail call ptr @realloc(ptr noundef %18, i64 noundef %spec.select) #4
  %.not.not = icmp eq ptr %19, null
  br i1 %.not.not, label %.critedge, label %20

20:                                               ; preds = %16
  store ptr %19, ptr %0, align 8, !tbaa !12
  %.pre43 = load i64, ptr %5, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %._crit_edge, %20
  %22 = phi i64 [ %6, %._crit_edge ], [ %.pre43, %20 ]
  %23 = phi ptr [ %.pre, %._crit_edge ], [ %19, %20 ]
  %24 = getelementptr inbounds i8, ptr %23, i64 %1
  %25 = getelementptr inbounds i8, ptr %24, i64 %3
  %reass.sub = sub i64 %22, %1
  %26 = add i64 %reass.sub, 1
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 1 %25, ptr align 1 %24, i64 %26, i1 false)
  %27 = load ptr, ptr %0, align 8, !tbaa !12
  %28 = getelementptr inbounds i8, ptr %27, i64 %1
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %28, ptr align 1 %2, i64 %3, i1 false)
  %29 = load i64, ptr %5, align 8, !tbaa !5
  %30 = add i64 %29, %3
  store i64 %30, ptr %5, align 8, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %21, %16, %8, %4
  %.2 = phi i64 [ 70011, %4 ], [ 0, %8 ], [ 0, %21 ], [ 70013, %16 ]
  ret i64 %.2
}

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn 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 nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind allocsize(1) }

!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 = !{!"iwxstr", !7, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !7, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 16}
!12 = !{!6, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass