; LLVM IR from BrunoLevy/learn-fpga :: FemtoRV/TUTORIALS/FROM_BLINKER_TO_RISCV/FIRMWARE (memcpy)

; 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 @memcpy(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  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
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %11, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %12, ptr %8, align 8, !tbaa !5
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = ptrtoint ptr %13 to i32
  %15 = and i32 %14, 3
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %35, label %17

17:                                               ; preds = %3
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = ptrtoint ptr %18 to i32
  %20 = and i32 %19, 3
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %35, label %22

22:                                               ; preds = %17
  br label %23

23:                                               ; preds = %26, %22
  %24 = load i64, ptr %6, align 8, !tbaa !9
  %25 = icmp uge i64 %24, 4
  br i1 %25, label %26, label %34

26:                                               ; preds = %23
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = getelementptr inbounds i32, ptr %27, i32 1
  store ptr %28, ptr %8, align 8, !tbaa !5
  %29 = load i32, ptr %27, align 4, !tbaa !11
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds i32, ptr %30, i32 1
  store ptr %31, ptr %7, align 8, !tbaa !5
  store i32 %29, ptr %30, align 4, !tbaa !11
  %32 = load i64, ptr %6, align 8, !tbaa !9
  %33 = sub i64 %32, 4
  store i64 %33, ptr %6, align 8, !tbaa !9
  br label %23, !llvm.loop !13

34:                                               ; preds = %23
  br label %35

35:                                               ; preds = %34, %17, %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %36, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %37, ptr %10, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %42, %35
  %39 = load i64, ptr %6, align 8, !tbaa !9
  %40 = add i64 %39, -1
  store i64 %40, ptr %6, align 8, !tbaa !9
  %41 = icmp ne i64 %39, 0
  br i1 %41, label %42, label %48

42:                                               ; preds = %38
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i32 1
  store ptr %44, ptr %10, align 8, !tbaa !5
  %45 = load i8, ptr %43, align 1, !tbaa !15
  %46 = load ptr, ptr %9, align 8, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %46, i32 1
  store ptr %47, ptr %9, align 8, !tbaa !5
  store i8 %45, ptr %46, align 1, !tbaa !15
  br label %38, !llvm.loop !16

48:                                               ; preds = %38
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret ptr %49
}

; 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 = !{!7, !7, i64 0}
!16 = distinct !{!16, !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 @memcpy(ptr noundef returned %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = ptrtoint ptr %1 to i64
  %5 = and i64 %4, 3
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %6, label %.loopexit

6:                                                ; preds = %3
  %7 = ptrtoint ptr %0 to i64
  %8 = and i64 %7, 3
  %9 = icmp eq i64 %8, 0
  %10 = icmp ugt i64 %2, 3
  %or.cond = and i1 %9, %10
  br i1 %or.cond, label %.preheader.preheader, label %.loopexit

.preheader.preheader:                             ; preds = %6
  %11 = add i64 %2, -4
  %12 = lshr i64 %11, 2
  %13 = add nuw nsw i64 %12, 1
  %min.iters.check = icmp ult i64 %11, 28
  %14 = sub i64 %7, %4
  %diff.check = icmp ult i64 %14, 32
  %or.cond85 = or i1 %min.iters.check, %diff.check
  br i1 %or.cond85, label %.preheader.preheader87, label %vector.ph

vector.ph:                                        ; preds = %.preheader.preheader
  %n.vec = and i64 %13, 9223372036854775800
  %15 = shl i64 %n.vec, 2
  %ind.end = sub i64 %2, %15
  %16 = shl i64 %n.vec, 2
  %ind.end36 = getelementptr i8, ptr %0, i64 %16
  %17 = shl i64 %n.vec, 2
  %ind.end38 = getelementptr i8, ptr %1, i64 %17
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx40 = shl i64 %index, 2
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx40
  %offset.idx42 = shl i64 %index, 2
  %next.gep43 = getelementptr i8, ptr %1, i64 %offset.idx42
  %18 = getelementptr i8, ptr %next.gep43, i64 16
  %wide.load = load <4 x i32>, ptr %next.gep43, align 4, !tbaa !5
  %wide.load45 = load <4 x i32>, ptr %18, align 4, !tbaa !5
  %19 = getelementptr i8, ptr %next.gep, i64 16
  store <4 x i32> %wide.load, ptr %next.gep, align 4, !tbaa !5
  store <4 x i32> %wide.load45, ptr %19, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %20 = icmp eq i64 %index.next, %n.vec
  br i1 %20, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %13, %n.vec
  br i1 %cmp.n, label %.loopexit, label %.preheader.preheader87

.preheader.preheader87:                           ; preds = %middle.block, %.preheader.preheader
  %.019.ph = phi i64 [ %2, %.preheader.preheader ], [ %ind.end, %middle.block ]
  %.017.ph = phi ptr [ %0, %.preheader.preheader ], [ %ind.end36, %middle.block ]
  %.016.ph = phi ptr [ %1, %.preheader.preheader ], [ %ind.end38, %middle.block ]
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader87, %.preheader
  %.019 = phi i64 [ %24, %.preheader ], [ %.019.ph, %.preheader.preheader87 ]
  %.017 = phi ptr [ %23, %.preheader ], [ %.017.ph, %.preheader.preheader87 ]
  %.016 = phi ptr [ %21, %.preheader ], [ %.016.ph, %.preheader.preheader87 ]
  %21 = getelementptr inbounds i8, ptr %.016, i64 4
  %22 = load i32, ptr %.016, align 4, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %.017, i64 4
  store i32 %22, ptr %.017, align 4, !tbaa !5
  %24 = add i64 %.019, -4
  %.old1 = icmp ugt i64 %24, 3
  br i1 %.old1, label %.preheader, label %.loopexit, !llvm.loop !12

.loopexit:                                        ; preds = %.preheader, %middle.block, %6, %3
  %.120 = phi i64 [ %2, %3 ], [ %2, %6 ], [ %ind.end, %middle.block ], [ %24, %.preheader ]
  %.118 = phi ptr [ %0, %3 ], [ %0, %6 ], [ %ind.end36, %middle.block ], [ %23, %.preheader ]
  %.1 = phi ptr [ %1, %3 ], [ %1, %6 ], [ %ind.end38, %middle.block ], [ %21, %.preheader ]
  %.not2326 = icmp eq i64 %.120, 0
  br i1 %.not2326, label %._crit_edge, label %iter.check

iter.check:                                       ; preds = %.loopexit
  %.148 = ptrtoint ptr %.1 to i64
  %.11847 = ptrtoint ptr %.118 to i64
  %min.iters.check52 = icmp ult i64 %.120, 8
  %25 = sub i64 %.11847, %.148
  %diff.check49 = icmp ult i64 %25, 32
  %or.cond86 = select i1 %min.iters.check52, i1 true, i1 %diff.check49
  br i1 %or.cond86, label %.lr.ph.preheader, label %vector.main.loop.iter.check

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

vector.ph55:                                      ; preds = %vector.main.loop.iter.check
  %n.vec57 = and i64 %.120, -32
  br label %vector.body58

vector.body58:                                    ; preds = %vector.body58, %vector.ph55
  %index59 = phi i64 [ 0, %vector.ph55 ], [ %index.next66, %vector.body58 ]
  %next.gep60 = getelementptr i8, ptr %.1, i64 %index59
  %next.gep62 = getelementptr i8, ptr %.118, i64 %index59
  %26 = getelementptr i8, ptr %next.gep60, i64 16
  %wide.load64 = load <16 x i8>, ptr %next.gep60, align 1, !tbaa !13
  %wide.load65 = load <16 x i8>, ptr %26, align 1, !tbaa !13
  %27 = getelementptr i8, ptr %next.gep62, i64 16
  store <16 x i8> %wide.load64, ptr %next.gep62, align 1, !tbaa !13
  store <16 x i8> %wide.load65, ptr %27, align 1, !tbaa !13
  %index.next66 = add nuw i64 %index59, 32
  %28 = icmp eq i64 %index.next66, %n.vec57
  br i1 %28, label %middle.block50, label %vector.body58, !llvm.loop !14

middle.block50:                                   ; preds = %vector.body58
  %cmp.n67 = icmp eq i64 %.120, %n.vec57
  br i1 %cmp.n67, label %._crit_edge, label %vec.epilog.iter.check

vec.epilog.iter.check:                            ; preds = %middle.block50
  %ind.end77 = and i64 %.120, 31
  %ind.end74 = getelementptr i8, ptr %.118, i64 %n.vec57
  %ind.end71 = getelementptr i8, ptr %.1, i64 %n.vec57
  %n.vec.remaining = and i64 %.120, 24
  %min.epilog.iters.check = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check, label %.lr.ph.preheader, label %vec.epilog.ph

vec.epilog.ph:                                    ; preds = %vector.main.loop.iter.check, %vec.epilog.iter.check
  %vec.epilog.resume.val = phi i64 [ %n.vec57, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %n.vec69 = and i64 %.120, -8
  %ind.end70 = getelementptr i8, ptr %.1, i64 %n.vec69
  %ind.end73 = getelementptr i8, ptr %.118, i64 %n.vec69
  %ind.end76 = and i64 %.120, 7
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index79 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next83, %vec.epilog.vector.body ]
  %next.gep80 = getelementptr i8, ptr %.1, i64 %index79
  %next.gep81 = getelementptr i8, ptr %.118, i64 %index79
  %wide.load82 = load <8 x i8>, ptr %next.gep80, align 1, !tbaa !13
  store <8 x i8> %wide.load82, ptr %next.gep81, align 1, !tbaa !13
  %index.next83 = add nuw i64 %index79, 8
  %29 = icmp eq i64 %index.next83, %n.vec69
  br i1 %29, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !16

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %cmp.n84 = icmp eq i64 %.120, %n.vec69
  br i1 %cmp.n84, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %vec.epilog.middle.block, %iter.check, %vec.epilog.iter.check
  %.029.ph = phi ptr [ %.1, %iter.check ], [ %ind.end71, %vec.epilog.iter.check ], [ %ind.end70, %vec.epilog.middle.block ]
  %.01528.ph = phi ptr [ %.118, %iter.check ], [ %ind.end74, %vec.epilog.iter.check ], [ %ind.end73, %vec.epilog.middle.block ]
  %.227.ph = phi i64 [ %.120, %iter.check ], [ %ind.end77, %vec.epilog.iter.check ], [ %ind.end76, %vec.epilog.middle.block ]
  %30 = add i64 %.227.ph, -1
  %xtraiter = and i64 %.227.ph, 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.preheader, %.lr.ph.prol
  %.029.prol = phi ptr [ %32, %.lr.ph.prol ], [ %.029.ph, %.lr.ph.preheader ]
  %.01528.prol = phi ptr [ %34, %.lr.ph.prol ], [ %.01528.ph, %.lr.ph.preheader ]
  %.227.prol = phi i64 [ %31, %.lr.ph.prol ], [ %.227.ph, %.lr.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %31 = add i64 %.227.prol, -1
  %32 = getelementptr inbounds i8, ptr %.029.prol, i64 1
  %33 = load i8, ptr %.029.prol, align 1, !tbaa !13
  %34 = getelementptr inbounds i8, ptr %.01528.prol, i64 1
  store i8 %33, ptr %.01528.prol, align 1, !tbaa !13
  %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 !17

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.029.unr = phi ptr [ %.029.ph, %.lr.ph.preheader ], [ %32, %.lr.ph.prol ]
  %.01528.unr = phi ptr [ %.01528.ph, %.lr.ph.preheader ], [ %34, %.lr.ph.prol ]
  %.227.unr = phi i64 [ %.227.ph, %.lr.ph.preheader ], [ %31, %.lr.ph.prol ]
  %35 = icmp ult i64 %30, 7
  br i1 %35, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.029 = phi ptr [ %58, %.lr.ph ], [ %.029.unr, %.lr.ph.prol.loopexit ]
  %.01528 = phi ptr [ %60, %.lr.ph ], [ %.01528.unr, %.lr.ph.prol.loopexit ]
  %.227 = phi i64 [ %57, %.lr.ph ], [ %.227.unr, %.lr.ph.prol.loopexit ]
  %36 = getelementptr inbounds i8, ptr %.029, i64 1
  %37 = load i8, ptr %.029, align 1, !tbaa !13
  %38 = getelementptr inbounds i8, ptr %.01528, i64 1
  store i8 %37, ptr %.01528, align 1, !tbaa !13
  %39 = getelementptr inbounds i8, ptr %.029, i64 2
  %40 = load i8, ptr %36, align 1, !tbaa !13
  %41 = getelementptr inbounds i8, ptr %.01528, i64 2
  store i8 %40, ptr %38, align 1, !tbaa !13
  %42 = getelementptr inbounds i8, ptr %.029, i64 3
  %43 = load i8, ptr %39, align 1, !tbaa !13
  %44 = getelementptr inbounds i8, ptr %.01528, i64 3
  store i8 %43, ptr %41, align 1, !tbaa !13
  %45 = getelementptr inbounds i8, ptr %.029, i64 4
  %46 = load i8, ptr %42, align 1, !tbaa !13
  %47 = getelementptr inbounds i8, ptr %.01528, i64 4
  store i8 %46, ptr %44, align 1, !tbaa !13
  %48 = getelementptr inbounds i8, ptr %.029, i64 5
  %49 = load i8, ptr %45, align 1, !tbaa !13
  %50 = getelementptr inbounds i8, ptr %.01528, i64 5
  store i8 %49, ptr %47, align 1, !tbaa !13
  %51 = getelementptr inbounds i8, ptr %.029, i64 6
  %52 = load i8, ptr %48, align 1, !tbaa !13
  %53 = getelementptr inbounds i8, ptr %.01528, i64 6
  store i8 %52, ptr %50, align 1, !tbaa !13
  %54 = getelementptr inbounds i8, ptr %.029, i64 7
  %55 = load i8, ptr %51, align 1, !tbaa !13
  %56 = getelementptr inbounds i8, ptr %.01528, i64 7
  store i8 %55, ptr %53, align 1, !tbaa !13
  %57 = add i64 %.227, -8
  %58 = getelementptr inbounds i8, ptr %.029, i64 8
  %59 = load i8, ptr %54, align 1, !tbaa !13
  %60 = getelementptr inbounds i8, ptr %.01528, i64 8
  store i8 %59, ptr %56, align 1, !tbaa !13
  %.not23.7 = icmp eq i64 %57, 0
  br i1 %.not23.7, label %._crit_edge, label %.lr.ph, !llvm.loop !19

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block50, %vec.epilog.middle.block, %.loopexit
  ret ptr %0
}

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


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