; LLVM IR from 0intro/wmii :: lib/libutf (runestrncpy)

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

; Function Attrs: nounwind uwtable
define dso_local ptr @runestrncpy(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  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
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %11, ptr %9, align 8, !tbaa !5
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %12

12:                                               ; preds = %37, %3
  %13 = load i32, ptr %8, align 4, !tbaa !11
  %14 = sext i32 %13 to i64
  %15 = load i64, ptr %7, align 8, !tbaa !9
  %16 = icmp slt i64 %14, %15
  br i1 %16, label %17, label %40

17:                                               ; preds = %12
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds i32, ptr %18, i32 1
  store ptr %19, ptr %6, align 8, !tbaa !5
  %20 = load i32, ptr %18, align 4, !tbaa !11
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds i32, ptr %21, i32 1
  store ptr %22, ptr %5, align 8, !tbaa !5
  store i32 %20, ptr %21, align 4, !tbaa !11
  %23 = icmp eq i32 %20, 0
  br i1 %23, label %24, label %36

24:                                               ; preds = %17
  br label %25

25:                                               ; preds = %31, %24
  %26 = load i32, ptr %8, align 4, !tbaa !11
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %8, align 4, !tbaa !11
  %28 = sext i32 %27 to i64
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = icmp slt i64 %28, %29
  br i1 %30, label %31, label %34

31:                                               ; preds = %25
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds i32, ptr %32, i32 1
  store ptr %33, ptr %5, align 8, !tbaa !5
  store i32 0, ptr %32, align 4, !tbaa !11
  br label %25, !llvm.loop !13

34:                                               ; preds = %25
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %35, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %42

36:                                               ; preds = %17
  br label %37

37:                                               ; preds = %36
  %38 = load i32, ptr %8, align 4, !tbaa !11
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %8, align 4, !tbaa !11
  br label %12, !llvm.loop !15

40:                                               ; preds = %12
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %41, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %42

42:                                               ; preds = %40, %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %43 = load ptr, ptr %4, align 8
  ret ptr %43
}

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

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

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 = { 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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @runestrncpy(ptr noundef returned writeonly %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i64 %2, 0
  br i1 %4, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %3, %15
  %indvars.iv = phi i64 [ %indvars.iv.next, %15 ], [ 0, %3 ]
  %.01218 = phi ptr [ %6, %15 ], [ %0, %3 ]
  %.01417 = phi ptr [ %16, %15 ], [ %1, %3 ]
  %5 = load i32, ptr %.01417, align 4, !tbaa !5
  %6 = getelementptr i8, ptr %.01218, i64 4
  store i32 %5, ptr %.01218, align 4, !tbaa !5
  %7 = icmp eq i32 %5, 0
  br i1 %7, label %.preheader, label %15

.preheader:                                       ; preds = %.lr.ph
  %8 = add nuw i64 %indvars.iv, 1
  %9 = and i64 %8, 4294967295
  %10 = icmp ult i64 %9, %2
  br i1 %10, label %.lr.ph21.preheader, label %.loopexit

.lr.ph21.preheader:                               ; preds = %.preheader
  %11 = shl i64 %2, 2
  %12 = add i64 %11, -4
  %13 = shl i64 %indvars.iv, 2
  %14 = sub i64 %12, %13
  tail call void @llvm.memset.p0.i64(ptr align 4 %6, i8 0, i64 %14, i1 false), !tbaa !5
  br label %.loopexit

15:                                               ; preds = %.lr.ph
  %16 = getelementptr inbounds i8, ptr %.01417, i64 4
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %2
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !9

.loopexit:                                        ; preds = %15, %.lr.ph21.preheader, %3, %.preheader
  ret ptr %0
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 nounwind willreturn memory(argmem: write) }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass