; LLVM IR from nde-jesu/Doom_Nukem :: libft/srcs/memory (ft_memccpy)

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

16:                                               ; preds = %51, %4
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %27

19:                                               ; preds = %16
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %27

22:                                               ; preds = %19
  %23 = load i64, ptr %11, align 8, !tbaa !11
  %24 = add i64 %23, 1
  store i64 %24, ptr %11, align 8, !tbaa !11
  %25 = load i64, ptr %9, align 8, !tbaa !11
  %26 = icmp ult i64 %24, %25
  br label %27

27:                                               ; preds = %22, %19, %16
  %28 = phi i1 [ false, %19 ], [ false, %16 ], [ %26, %22 ]
  br i1 %28, label %29, label %52

29:                                               ; preds = %27
  %30 = load ptr, ptr %12, align 8, !tbaa !5
  %31 = load i64, ptr %11, align 8, !tbaa !11
  %32 = getelementptr inbounds i8, ptr %30, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !13
  %34 = load ptr, ptr %10, align 8, !tbaa !5
  %35 = load i64, ptr %11, align 8, !tbaa !11
  %36 = getelementptr inbounds i8, ptr %34, i64 %35
  store i8 %33, ptr %36, align 1, !tbaa !13
  %37 = load ptr, ptr %12, align 8, !tbaa !5
  %38 = load i64, ptr %11, align 8, !tbaa !11
  %39 = getelementptr inbounds i8, ptr %37, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !13
  %41 = zext i8 %40 to i32
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = trunc i32 %42 to i8
  %44 = zext i8 %43 to i32
  %45 = icmp eq i32 %41, %44
  br i1 %45, label %46, label %51

46:                                               ; preds = %29
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  %48 = load i64, ptr %11, align 8, !tbaa !11
  %49 = add i64 %48, 1
  %50 = getelementptr inbounds i8, ptr %47, i64 %49
  store ptr %50, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %53

51:                                               ; preds = %29
  br label %16, !llvm.loop !14

52:                                               ; preds = %27
  store ptr null, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %53

53:                                               ; preds = %52, %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %54 = load ptr, ptr %5, align 8
  ret ptr %54
}

; 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !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(argmem: readwrite) uwtable
define dso_local ptr @ft_memccpy(ptr noundef writeonly %0, ptr noundef readonly %1, i32 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = icmp ne ptr %0, null
  %6 = icmp ne ptr %1, null
  %or.cond = and i1 %5, %6
  %7 = trunc i32 %2 to i8
  br i1 %or.cond, label %.split.preheader, label %.critedge

.split.preheader:                                 ; preds = %4
  %8 = add i64 %3, -1
  br label %.split

.split:                                           ; preds = %.split.preheader, %9
  %.016 = phi i64 [ %10, %9 ], [ -1, %.split.preheader ]
  %exitcond.not = icmp eq i64 %.016, %8
  br i1 %exitcond.not, label %.critedge, label %9

9:                                                ; preds = %.split
  %10 = add i64 %.016, 1
  %11 = getelementptr inbounds i8, ptr %1, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %0, i64 %10
  store i8 %12, ptr %13, align 1, !tbaa !5
  %14 = icmp eq i8 %12, %7
  br i1 %14, label %15, label %.split, !llvm.loop !8

15:                                               ; preds = %9
  %16 = getelementptr i8, ptr %0, i64 %.016
  %17 = getelementptr i8, ptr %16, i64 2
  br label %.critedge

.critedge:                                        ; preds = %.split, %4, %15
  %.0 = phi ptr [ %17, %15 ], [ null, %4 ], [ null, %.split ]
  ret ptr %.0
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: 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" }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass