; LLVM IR from thepowersgang/mrustc :: src/rc_string.ll (ZN8RcStringC2EPKcm)

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

%class.RcString = type { ptr }
%"struct.RcString::Inner" = type { i32, i32, i32, [1 x i32] }

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN8RcStringC2EPKcm(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef %1, i64 noundef %2) unnamed_addr #0 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8
  %11 = getelementptr inbounds %class.RcString, ptr %10, i32 0, i32 0
  store ptr null, ptr %11, align 8, !tbaa !11
  %12 = load i64, ptr %6, align 8, !tbaa !9
  %13 = icmp ugt i64 %12, 0
  br i1 %13, label %14, label %64

14:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %15 = load i64, ptr %6, align 8, !tbaa !9
  %16 = add i64 %15, 1
  %17 = add i64 %16, 4
  %18 = sub i64 %17, 1
  %19 = udiv i64 %18, 4
  store i64 %19, ptr %7, align 8, !tbaa !9
  %20 = load i64, ptr %7, align 8, !tbaa !9
  %21 = sub i64 %20, 1
  %22 = mul i64 %21, 4
  %23 = add i64 16, %22
  %24 = call noalias ptr @malloc(i64 noundef %23) #4
  %25 = getelementptr inbounds %class.RcString, ptr %10, i32 0, i32 0
  store ptr %24, ptr %25, align 8, !tbaa !11
  %26 = getelementptr inbounds %class.RcString, ptr %10, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  %28 = getelementptr inbounds %"struct.RcString::Inner", ptr %27, i32 0, i32 0
  store i32 1, ptr %28, align 4, !tbaa !13
  %29 = load i64, ptr %6, align 8, !tbaa !9
  %30 = trunc i64 %29 to i32
  %31 = getelementptr inbounds %class.RcString, ptr %10, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  %33 = getelementptr inbounds %"struct.RcString::Inner", ptr %32, i32 0, i32 1
  store i32 %30, ptr %33, align 4, !tbaa !16
  %34 = getelementptr inbounds %class.RcString, ptr %10, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = getelementptr inbounds %"struct.RcString::Inner", ptr %35, i32 0, i32 2
  store i32 0, ptr %36, align 4, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %37 = getelementptr inbounds %class.RcString, ptr %10, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  %39 = getelementptr inbounds %"struct.RcString::Inner", ptr %38, i32 0, i32 3
  %40 = getelementptr inbounds [1 x i32], ptr %39, i64 0, i64 0
  store ptr %40, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !18
  br label %41

41:                                               ; preds = %57, %14
  %42 = load i32, ptr %9, align 4, !tbaa !18
  %43 = zext i32 %42 to i64
  %44 = load i64, ptr %6, align 8, !tbaa !9
  %45 = icmp ult i64 %43, %44
  br i1 %45, label %47, label %46

46:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %60

47:                                               ; preds = %41
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = load i32, ptr %9, align 4, !tbaa !18
  %50 = zext i32 %49 to i64
  %51 = getelementptr inbounds i8, ptr %48, i64 %50
  %52 = load i8, ptr %51, align 1, !tbaa !19
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = load i32, ptr %9, align 4, !tbaa !18
  %55 = zext i32 %54 to i64
  %56 = getelementptr inbounds i8, ptr %53, i64 %55
  store i8 %52, ptr %56, align 1, !tbaa !19
  br label %57

57:                                               ; preds = %47
  %58 = load i32, ptr %9, align 4, !tbaa !18
  %59 = add i32 %58, 1
  store i32 %59, ptr %9, align 4, !tbaa !18
  br label %41, !llvm.loop !20

60:                                               ; preds = %46
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  %62 = load i64, ptr %6, align 8, !tbaa !9
  %63 = getelementptr inbounds i8, ptr %61, i64 %62
  store i8 0, ptr %63, align 1, !tbaa !19
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %64

64:                                               ; preds = %60, %3
  ret void
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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

attributes #0 = { mustprogress 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 = { nounwind allocsize(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 allocsize(0) }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS8RcString", !6, i64 0}
!13 = !{!14, !15, i64 0}
!14 = !{!"_ZTSN8RcString5InnerE", !15, i64 0, !15, i64 4, !15, i64 8, !7, i64 12}
!15 = !{!"int", !7, i64 0}
!16 = !{!14, !15, i64 4}
!17 = !{!14, !15, i64 8}
!18 = !{!15, !15, i64 0}
!19 = !{!7, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"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"

; Function Attrs: mustprogress nofree nounwind memory(write, argmem: readwrite, inaccessiblemem: readwrite) uwtable
define dso_local void @_ZN8RcStringC2EPKcm(ptr nocapture noundef nonnull writeonly align 8 dereferenceable(8) %0, ptr nocapture noundef readonly %1, i64 noundef %2) unnamed_addr #0 align 2 {
  store ptr null, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %32, label %iter.check

iter.check:                                       ; preds = %3
  %4 = and i64 %2, -4
  %5 = add i64 %4, 16
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #2
  store ptr %6, ptr %0, align 8, !tbaa !5
  store i32 1, ptr %6, align 4, !tbaa !10
  %7 = trunc i64 %2 to i32
  %8 = getelementptr inbounds i8, ptr %6, i64 4
  store i32 %7, ptr %8, align 4, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %6, i64 8
  store i32 0, ptr %9, align 4, !tbaa !14
  %10 = getelementptr inbounds i8, ptr %6, i64 12
  %min.iters.check = icmp ult i64 %2, 8
  br i1 %min.iters.check, label %vec.epilog.scalar.ph.preheader, label %vector.scevcheck

vector.scevcheck:                                 ; preds = %iter.check
  %11 = add i64 %2, -1
  %12 = and i64 %11, 4294967295
  %13 = icmp eq i64 %12, 4294967295
  %14 = icmp ugt i64 %11, 4294967295
  %15 = or i1 %13, %14
  br i1 %15, label %vec.epilog.scalar.ph.preheader, label %vector.main.loop.iter.check

vector.main.loop.iter.check:                      ; preds = %vector.scevcheck
  %min.iters.check16 = icmp ult i64 %2, 32
  br i1 %min.iters.check16, label %vec.epilog.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %2, 8589934560
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %16 = getelementptr inbounds i8, ptr %1, i64 %index
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %wide.load = load <16 x i8>, ptr %16, align 1, !tbaa !15
  %wide.load17 = load <16 x i8>, ptr %17, align 1, !tbaa !15
  %18 = getelementptr inbounds i8, ptr %10, i64 %index
  %19 = getelementptr inbounds i8, ptr %18, i64 16
  store <16 x i8> %wide.load, ptr %18, align 1, !tbaa !15
  store <16 x i8> %wide.load17, ptr %19, align 1, !tbaa !15
  %index.next = add nuw i64 %index, 32
  %20 = icmp eq i64 %index.next, %n.vec
  br i1 %20, label %middle.block, label %vector.body, !llvm.loop !16

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %2
  br i1 %cmp.n, label %.loopexit, label %vec.epilog.iter.check

vec.epilog.iter.check:                            ; preds = %middle.block
  %ind.end20 = trunc i64 %n.vec to i32
  %n.vec.remaining = and i64 %2, 24
  %min.epilog.iters.check = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check, label %vec.epilog.scalar.ph.preheader, label %vec.epilog.ph

vec.epilog.scalar.ph.preheader:                   ; preds = %vec.epilog.middle.block, %vector.scevcheck, %iter.check, %vec.epilog.iter.check
  %.ph = phi i64 [ 0, %iter.check ], [ 0, %vector.scevcheck ], [ %n.vec, %vec.epilog.iter.check ], [ %n.vec19, %vec.epilog.middle.block ]
  %.015.ph = phi i32 [ 0, %iter.check ], [ 0, %vector.scevcheck ], [ %ind.end20, %vec.epilog.iter.check ], [ %ind.end, %vec.epilog.middle.block ]
  br label %vec.epilog.scalar.ph

vec.epilog.ph:                                    ; preds = %vector.main.loop.iter.check, %vec.epilog.iter.check
  %vec.epilog.resume.val = phi i64 [ %n.vec, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %n.vec19 = and i64 %2, 8589934584
  %ind.end = trunc i64 %n.vec19 to i32
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index22 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next24, %vec.epilog.vector.body ]
  %21 = getelementptr inbounds i8, ptr %1, i64 %index22
  %wide.load23 = load <8 x i8>, ptr %21, align 1, !tbaa !15
  %22 = getelementptr inbounds i8, ptr %10, i64 %index22
  store <8 x i8> %wide.load23, ptr %22, align 1, !tbaa !15
  %index.next24 = add nuw i64 %index22, 8
  %23 = icmp eq i64 %index.next24, %n.vec19
  br i1 %23, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !20

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %cmp.n25 = icmp eq i64 %n.vec19, %2
  br i1 %cmp.n25, label %.loopexit, label %vec.epilog.scalar.ph.preheader

.loopexit:                                        ; preds = %vec.epilog.scalar.ph, %vec.epilog.middle.block, %middle.block
  %24 = getelementptr inbounds i8, ptr %10, i64 %2
  store i8 0, ptr %24, align 1, !tbaa !15
  br label %32

vec.epilog.scalar.ph:                             ; preds = %vec.epilog.scalar.ph.preheader, %vec.epilog.scalar.ph
  %25 = phi i64 [ %30, %vec.epilog.scalar.ph ], [ %.ph, %vec.epilog.scalar.ph.preheader ]
  %.015 = phi i32 [ %29, %vec.epilog.scalar.ph ], [ %.015.ph, %vec.epilog.scalar.ph.preheader ]
  %26 = getelementptr inbounds i8, ptr %1, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !15
  %28 = getelementptr inbounds i8, ptr %10, i64 %25
  store i8 %27, ptr %28, align 1, !tbaa !15
  %29 = add i32 %.015, 1
  %30 = zext i32 %29 to i64
  %31 = icmp ult i64 %30, %2
  br i1 %31, label %vec.epilog.scalar.ph, label %.loopexit, !llvm.loop !21

32:                                               ; preds = %.loopexit, %3
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nofree nounwind memory(write, argmem: readwrite, inaccessiblemem: readwrite) 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 = { nounwind allocsize(0) }

!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 0}
!6 = !{!"_ZTS8RcString", !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !12, i64 0}
!11 = !{!"_ZTSN8RcString5InnerE", !12, i64 0, !12, i64 4, !12, i64 8, !8, i64 12}
!12 = !{!"int", !8, i64 0}
!13 = !{!11, !12, i64 4}
!14 = !{!11, !12, i64 8}
!15 = !{!8, !8, i64 0}
!16 = distinct !{!16, !17, !18, !19}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!"llvm.loop.isvectorized", i32 1}
!19 = !{!"llvm.loop.unroll.runtime.disable"}
!20 = distinct !{!20, !17, !18, !19}
!21 = distinct !{!21, !17, !18}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LoopLoadEliminationPass,LoopRotatePass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass