; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/ShellPkg/Library (UnicodeToAscii)

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

8:                                                ; preds = %19, %3
  %9 = load i64, ptr %7, align 8, !tbaa !9
  %10 = load i64, ptr %5, align 8, !tbaa !9
  %11 = icmp ult i64 %9, %10
  br i1 %11, label %12, label %22

12:                                               ; preds = %8
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds i16, ptr %13, i32 1
  store ptr %14, ptr %4, align 8, !tbaa !5
  %15 = load i16, ptr %13, align 2, !tbaa !11
  %16 = trunc i16 %15 to i8
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i32 1
  store ptr %18, ptr %6, align 8, !tbaa !5
  store i8 %16, ptr %17, align 1, !tbaa !13
  br label %19

19:                                               ; preds = %12
  %20 = load i64, ptr %7, align 8, !tbaa !9
  %21 = add i64 %20, 1
  store i64 %21, ptr %7, align 8, !tbaa !9
  br label %8, !llvm.loop !14

22:                                               ; preds = %8
  %23 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %23
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef i64 @UnicodeToAscii(ptr nocapture noundef readonly %0, i64 noundef returned %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %min.iters.check = icmp ult i64 %1, 16
  br i1 %min.iters.check, label %.lr.ph.preheader18, label %vector.memcheck

vector.memcheck:                                  ; preds = %.lr.ph.preheader
  %scevgep = getelementptr i8, ptr %2, i64 %1
  %4 = shl i64 %1, 1
  %scevgep10 = getelementptr i8, ptr %0, i64 %4
  %bound0 = icmp ugt ptr %scevgep10, %2
  %bound1 = icmp ugt ptr %scevgep, %0
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph.preheader18, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %1, -16
  %ind.end = getelementptr i8, ptr %2, i64 %n.vec
  %5 = shl i64 %n.vec, 1
  %ind.end12 = getelementptr i8, ptr %0, i64 %5
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %next.gep = getelementptr i8, ptr %2, i64 %index
  %offset.idx = shl i64 %index, 1
  %next.gep15 = getelementptr i8, ptr %0, i64 %offset.idx
  %6 = getelementptr i8, ptr %next.gep15, i64 16
  %wide.load = load <8 x i16>, ptr %next.gep15, align 2, !tbaa !5, !alias.scope !9
  %wide.load17 = load <8 x i16>, ptr %6, align 2, !tbaa !5, !alias.scope !9
  %7 = trunc <8 x i16> %wide.load to <8 x i8>
  %8 = trunc <8 x i16> %wide.load17 to <8 x i8>
  %9 = getelementptr i8, ptr %next.gep, i64 8
  store <8 x i8> %7, ptr %next.gep, align 1, !tbaa !12, !alias.scope !13, !noalias !9
  store <8 x i8> %8, ptr %9, align 1, !tbaa !12, !alias.scope !13, !noalias !9
  %index.next = add nuw i64 %index, 16
  %10 = icmp eq i64 %index.next, %n.vec
  br i1 %10, label %middle.block, label %vector.body, !llvm.loop !15

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %1
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader18

.lr.ph.preheader18:                               ; preds = %middle.block, %vector.memcheck, %.lr.ph.preheader
  %.09.ph = phi i64 [ 0, %vector.memcheck ], [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.058.ph = phi ptr [ %2, %vector.memcheck ], [ %2, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.067.ph = phi ptr [ %0, %vector.memcheck ], [ %0, %.lr.ph.preheader ], [ %ind.end12, %middle.block ]
  %xtraiter = and i64 %1, 7
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader18, %.lr.ph.prol
  %.09.prol = phi i64 [ %15, %.lr.ph.prol ], [ %.09.ph, %.lr.ph.preheader18 ]
  %.058.prol = phi ptr [ %14, %.lr.ph.prol ], [ %.058.ph, %.lr.ph.preheader18 ]
  %.067.prol = phi ptr [ %11, %.lr.ph.prol ], [ %.067.ph, %.lr.ph.preheader18 ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader18 ]
  %11 = getelementptr inbounds i8, ptr %.067.prol, i64 2
  %12 = load i16, ptr %.067.prol, align 2, !tbaa !5
  %13 = trunc i16 %12 to i8
  %14 = getelementptr inbounds i8, ptr %.058.prol, i64 1
  store i8 %13, ptr %.058.prol, align 1, !tbaa !12
  %15 = add nuw i64 %.09.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !19

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader18
  %.09.unr = phi i64 [ %.09.ph, %.lr.ph.preheader18 ], [ %15, %.lr.ph.prol ]
  %.058.unr = phi ptr [ %.058.ph, %.lr.ph.preheader18 ], [ %14, %.lr.ph.prol ]
  %.067.unr = phi ptr [ %.067.ph, %.lr.ph.preheader18 ], [ %11, %.lr.ph.prol ]
  %16 = sub i64 %.09.ph, %1
  %17 = icmp ugt i64 %16, -8
  br i1 %17, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.09 = phi i64 [ %50, %.lr.ph ], [ %.09.unr, %.lr.ph.prol.loopexit ]
  %.058 = phi ptr [ %49, %.lr.ph ], [ %.058.unr, %.lr.ph.prol.loopexit ]
  %.067 = phi ptr [ %46, %.lr.ph ], [ %.067.unr, %.lr.ph.prol.loopexit ]
  %18 = getelementptr inbounds i8, ptr %.067, i64 2
  %19 = load i16, ptr %.067, align 2, !tbaa !5
  %20 = trunc i16 %19 to i8
  %21 = getelementptr inbounds i8, ptr %.058, i64 1
  store i8 %20, ptr %.058, align 1, !tbaa !12
  %22 = getelementptr inbounds i8, ptr %.067, i64 4
  %23 = load i16, ptr %18, align 2, !tbaa !5
  %24 = trunc i16 %23 to i8
  %25 = getelementptr inbounds i8, ptr %.058, i64 2
  store i8 %24, ptr %21, align 1, !tbaa !12
  %26 = getelementptr inbounds i8, ptr %.067, i64 6
  %27 = load i16, ptr %22, align 2, !tbaa !5
  %28 = trunc i16 %27 to i8
  %29 = getelementptr inbounds i8, ptr %.058, i64 3
  store i8 %28, ptr %25, align 1, !tbaa !12
  %30 = getelementptr inbounds i8, ptr %.067, i64 8
  %31 = load i16, ptr %26, align 2, !tbaa !5
  %32 = trunc i16 %31 to i8
  %33 = getelementptr inbounds i8, ptr %.058, i64 4
  store i8 %32, ptr %29, align 1, !tbaa !12
  %34 = getelementptr inbounds i8, ptr %.067, i64 10
  %35 = load i16, ptr %30, align 2, !tbaa !5
  %36 = trunc i16 %35 to i8
  %37 = getelementptr inbounds i8, ptr %.058, i64 5
  store i8 %36, ptr %33, align 1, !tbaa !12
  %38 = getelementptr inbounds i8, ptr %.067, i64 12
  %39 = load i16, ptr %34, align 2, !tbaa !5
  %40 = trunc i16 %39 to i8
  %41 = getelementptr inbounds i8, ptr %.058, i64 6
  store i8 %40, ptr %37, align 1, !tbaa !12
  %42 = getelementptr inbounds i8, ptr %.067, i64 14
  %43 = load i16, ptr %38, align 2, !tbaa !5
  %44 = trunc i16 %43 to i8
  %45 = getelementptr inbounds i8, ptr %.058, i64 7
  store i8 %44, ptr %41, align 1, !tbaa !12
  %46 = getelementptr inbounds i8, ptr %.067, i64 16
  %47 = load i16, ptr %42, align 2, !tbaa !5
  %48 = trunc i16 %47 to i8
  %49 = getelementptr inbounds i8, ptr %.058, i64 8
  store i8 %48, ptr %45, align 1, !tbaa !12
  %50 = add nuw i64 %.09, 8
  %exitcond.not.7 = icmp eq i64 %50, %1
  br i1 %exitcond.not.7, label %._crit_edge, label %.lr.ph, !llvm.loop !21

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %3
  ret i64 %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" }

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


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass