; LLVM IR from p4lang/p4c :: backends/tofino/bf-asm (vector)

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

%struct.raw_vector = type { i32, i32, ptr }

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

16:                                               ; preds = %4
  %17 = load i32, ptr %8, align 4, !tbaa !11
  %18 = load ptr, ptr %10, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.raw_vector, ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 4, !tbaa !13
  %21 = icmp sge i32 %17, %20
  br i1 %21, label %22, label %23

22:                                               ; preds = %16
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %71

23:                                               ; preds = %16, %4
  %24 = load i32, ptr %9, align 4, !tbaa !11
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %23
  store i32 1, ptr %9, align 4, !tbaa !11
  br label %27

27:                                               ; preds = %26, %23
  %28 = load i32, ptr %8, align 4, !tbaa !11
  %29 = load i32, ptr %9, align 4, !tbaa !11
  %30 = add i32 %28, %29
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.raw_vector, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 4, !tbaa !13
  %34 = icmp uge i32 %30, %33
  br i1 %34, label %35, label %39

35:                                               ; preds = %27
  %36 = load i32, ptr %8, align 4, !tbaa !11
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.raw_vector, ptr %37, i32 0, i32 1
  store i32 %36, ptr %38, align 4, !tbaa !13
  br label %70

39:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %40 = load ptr, ptr %10, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.raw_vector, ptr %40, i32 0, i32 2
  %42 = load ptr, ptr %41, align 8, !tbaa !15
  %43 = load i32, ptr %8, align 4, !tbaa !11
  %44 = sext i32 %43 to i64
  %45 = load i64, ptr %7, align 8, !tbaa !9
  %46 = mul i64 %44, %45
  %47 = getelementptr inbounds i8, ptr %42, i64 %46
  store ptr %47, ptr %12, align 8, !tbaa !5
  %48 = load ptr, ptr %12, align 8, !tbaa !5
  %49 = load ptr, ptr %12, align 8, !tbaa !5
  %50 = load i64, ptr %7, align 8, !tbaa !9
  %51 = load i32, ptr %9, align 4, !tbaa !11
  %52 = zext i32 %51 to i64
  %53 = mul i64 %50, %52
  %54 = getelementptr inbounds i8, ptr %49, i64 %53
  %55 = load i64, ptr %7, align 8, !tbaa !9
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.raw_vector, ptr %56, i32 0, i32 1
  %58 = load i32, ptr %57, align 4, !tbaa !13
  %59 = load i32, ptr %8, align 4, !tbaa !11
  %60 = sub nsw i32 %58, %59
  %61 = load i32, ptr %9, align 4, !tbaa !11
  %62 = sub i32 %60, %61
  %63 = zext i32 %62 to i64
  %64 = mul i64 %55, %63
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %48, ptr align 1 %54, i64 %64, i1 false)
  %65 = load i32, ptr %9, align 4, !tbaa !11
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.raw_vector, ptr %66, i32 0, i32 1
  %68 = load i32, ptr %67, align 4, !tbaa !13
  %69 = sub i32 %68, %65
  store i32 %69, ptr %67, align 4, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %70

70:                                               ; preds = %39, %35
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %71

71:                                               ; preds = %70, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %72 = load i32, ptr %5, align 4
  ret i32 %72
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = !{!14, !12, i64 4}
!14 = !{!"raw_vector", !12, i64 0, !12, i64 4, !6, i64 8}
!15 = !{!14, !6, i64 8}


; 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 norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 -1, 1) i32 @erase_raw_vector(ptr nocapture noundef %0, i64 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp sgt i32 %2, -1
  %6 = getelementptr inbounds i8, ptr %0, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %.not = icmp sgt i32 %7, %2
  %or.cond = select i1 %5, i1 true, i1 %.not
  br i1 %or.cond, label %._crit_edge, label %25

._crit_edge:                                      ; preds = %4
  %spec.store.select = tail call i32 @llvm.umax.i32(i32 %3, i32 1)
  %8 = add i32 %spec.store.select, %2
  %9 = getelementptr inbounds i8, ptr %0, i64 4
  %.not25 = icmp ult i32 %8, %7
  br i1 %.not25, label %10, label %24

10:                                               ; preds = %._crit_edge
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = sext i32 %2 to i64
  %14 = mul i64 %13, %1
  %15 = getelementptr inbounds i8, ptr %12, i64 %14
  %16 = zext i32 %spec.store.select to i64
  %17 = mul i64 %16, %1
  %18 = getelementptr inbounds i8, ptr %15, i64 %17
  %19 = sub i32 %7, %8
  %20 = zext i32 %19 to i64
  %21 = mul i64 %20, %1
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %15, ptr align 1 %18, i64 %21, i1 false)
  %22 = load i32, ptr %9, align 4, !tbaa !5
  %23 = sub i32 %22, %spec.store.select
  br label %24

24:                                               ; preds = %._crit_edge, %10
  %storemerge = phi i32 [ %23, %10 ], [ %2, %._crit_edge ]
  store i32 %storemerge, ptr %9, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %4, %24
  %.0 = phi i32 [ 0, %24 ], [ -1, %4 ]
  ret i32 %.0
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #2

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 4}
!6 = !{!"raw_vector", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass