; 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: 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: nounwind willreturn memory(none)
declare ptr @__errno_location() #2

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @insert_raw_vector(ptr noundef %0, i64 noundef %1, i32 noundef %2, i32 noundef %3) #4 {
  %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 i32, align 4
  %13 = alloca ptr, align 8
  %14 = 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) #5
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %15, ptr %10, align 8, !tbaa !5
  %16 = load i32, ptr %8, align 4, !tbaa !11
  %17 = icmp slt i32 %16, 0
  br i1 %17, label %18, label %25

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

24:                                               ; preds = %18
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %122

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

28:                                               ; preds = %25
  store i32 1, ptr %9, align 4, !tbaa !11
  br label %29

29:                                               ; preds = %28, %25
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.raw_vector, ptr %30, i32 0, i32 1
  %32 = load i32, ptr %31, align 4, !tbaa !13
  %33 = load i32, ptr %9, align 4, !tbaa !11
  %34 = add i32 %32, %33
  %35 = icmp ugt i32 %34, 2147483647
  br i1 %35, label %36, label %38

36:                                               ; preds = %29
  %37 = call ptr @__errno_location() #6
  store i32 34, ptr %37, align 4, !tbaa !11
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %122

38:                                               ; preds = %29
  %39 = load ptr, ptr %10, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.raw_vector, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !13
  %42 = load i32, ptr %9, align 4, !tbaa !11
  %43 = add i32 %41, %42
  %44 = load ptr, ptr %10, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.raw_vector, ptr %44, i32 0, i32 0
  %46 = load i32, ptr %45, align 8, !tbaa !15
  %47 = icmp sgt i32 %43, %46
  br i1 %47, label %48, label %86

48:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.raw_vector, ptr %49, i32 0, i32 1
  %51 = load i32, ptr %50, align 4, !tbaa !13
  %52 = load i32, ptr %9, align 4, !tbaa !11
  %53 = add i32 %51, %52
  store i32 %53, ptr %12, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %54 = load i32, ptr %12, align 4, !tbaa !11
  %55 = load ptr, ptr %10, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.raw_vector, ptr %55, i32 0, i32 0
  %57 = load i32, ptr %56, align 8, !tbaa !15
  %58 = mul nsw i32 %57, 2
  %59 = icmp slt i32 %54, %58
  br i1 %59, label %60, label %65

60:                                               ; preds = %48
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.raw_vector, ptr %61, i32 0, i32 0
  %63 = load i32, ptr %62, align 8, !tbaa !15
  %64 = mul nsw i32 %63, 2
  store i32 %64, ptr %12, align 4, !tbaa !11
  br label %65

65:                                               ; preds = %60, %48
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.raw_vector, ptr %66, i32 0, i32 2
  %68 = load ptr, ptr %67, align 8, !tbaa !16
  %69 = load i64, ptr %7, align 8, !tbaa !9
  %70 = load i32, ptr %12, align 4, !tbaa !11
  %71 = sext i32 %70 to i64
  %72 = mul i64 %69, %71
  %73 = call ptr @realloc(ptr noundef %68, i64 noundef %72) #7
  store ptr %73, ptr %13, align 8, !tbaa !5
  %74 = icmp ne ptr %73, null
  br i1 %74, label %76, label %75

75:                                               ; preds = %65
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %83

76:                                               ; preds = %65
  %77 = load i32, ptr %12, align 4, !tbaa !11
  %78 = load ptr, ptr %10, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.raw_vector, ptr %78, i32 0, i32 0
  store i32 %77, ptr %79, align 8, !tbaa !15
  %80 = load ptr, ptr %13, align 8, !tbaa !5
  %81 = load ptr, ptr %10, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.raw_vector, ptr %81, i32 0, i32 2
  store ptr %80, ptr %82, align 8, !tbaa !16
  store i32 0, ptr %11, align 4
  br label %83

83:                                               ; preds = %76, %75
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  %84 = load i32, ptr %11, align 4
  switch i32 %84, label %122 [
    i32 0, label %85
  ]

85:                                               ; preds = %83
  br label %86

86:                                               ; preds = %85, %38
  %87 = load i32, ptr %8, align 4, !tbaa !11
  %88 = load ptr, ptr %10, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.raw_vector, ptr %88, i32 0, i32 1
  %90 = load i32, ptr %89, align 4, !tbaa !13
  %91 = icmp slt i32 %87, %90
  br i1 %91, label %92, label %116

92:                                               ; preds = %86
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %93 = load ptr, ptr %10, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.raw_vector, ptr %93, i32 0, i32 2
  %95 = load ptr, ptr %94, align 8, !tbaa !16
  %96 = load i32, ptr %8, align 4, !tbaa !11
  %97 = sext i32 %96 to i64
  %98 = load i64, ptr %7, align 8, !tbaa !9
  %99 = mul i64 %97, %98
  %100 = getelementptr inbounds i8, ptr %95, i64 %99
  store ptr %100, ptr %14, align 8, !tbaa !5
  %101 = load ptr, ptr %14, align 8, !tbaa !5
  %102 = load i32, ptr %9, align 4, !tbaa !11
  %103 = zext i32 %102 to i64
  %104 = load i64, ptr %7, align 8, !tbaa !9
  %105 = mul i64 %103, %104
  %106 = getelementptr inbounds i8, ptr %101, i64 %105
  %107 = load ptr, ptr %14, align 8, !tbaa !5
  %108 = load i64, ptr %7, align 8, !tbaa !9
  %109 = load ptr, ptr %10, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.raw_vector, ptr %109, i32 0, i32 1
  %111 = load i32, ptr %110, align 4, !tbaa !13
  %112 = load i32, ptr %8, align 4, !tbaa !11
  %113 = sub nsw i32 %111, %112
  %114 = sext i32 %113 to i64
  %115 = mul i64 %108, %114
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %106, ptr align 1 %107, i64 %115, i1 false)
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  br label %116

116:                                              ; preds = %92, %86
  %117 = load i32, ptr %9, align 4, !tbaa !11
  %118 = load ptr, ptr %10, align 8, !tbaa !5
  %119 = getelementptr inbounds %struct.raw_vector, ptr %118, i32 0, i32 1
  %120 = load i32, ptr %119, align 4, !tbaa !13
  %121 = add i32 %120, %117
  store i32 %121, ptr %119, align 4, !tbaa !13
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %122

122:                                              ; preds = %116, %83, %36, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  %123 = load i32, ptr %5, align 4
  ret i32 %123
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind willreturn memory(none) "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 #3 = { nounwind allocsize(1) "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 #4 = { 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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(none) }
attributes #7 = { nounwind allocsize(1) }

!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, !12, i64 0}
!16 = !{!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 nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #0

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn uwtable
define dso_local range(i32 -1, 1) i32 @insert_raw_vector(ptr nocapture noundef %0, i64 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #3 {
  %5 = icmp slt i32 %2, 0
  %6 = getelementptr inbounds i8, ptr %0, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = icmp slt i32 %7, %2
  %or.cond = select i1 %5, i1 %8, i1 false
  br i1 %or.cond, label %.critedge, label %._crit_edge

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

12:                                               ; preds = %._crit_edge
  %13 = tail call ptr @__errno_location() #5
  store i32 34, ptr %13, align 4, !tbaa !11
  br label %.critedge

14:                                               ; preds = %._crit_edge
  %15 = load i32, ptr %0, align 8, !tbaa !12
  %16 = icmp sgt i32 %10, %15
  br i1 %16, label %17, label %25

17:                                               ; preds = %14
  %18 = shl nsw i32 %15, 1
  %spec.select = tail call i32 @llvm.smax.i32(i32 %10, i32 %18)
  %19 = getelementptr inbounds i8, ptr %0, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = zext nneg i32 %spec.select to i64
  %22 = mul i64 %21, %1
  %23 = tail call ptr @realloc(ptr noundef %20, i64 noundef %22) #6
  %.not.not = icmp eq ptr %23, null
  br i1 %.not.not, label %.critedge, label %24

24:                                               ; preds = %17
  store i32 %spec.select, ptr %0, align 8, !tbaa !12
  store ptr %23, ptr %19, align 8, !tbaa !13
  %.pre45 = load i32, ptr %9, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %24, %14
  %26 = phi i32 [ %.pre45, %24 ], [ %7, %14 ]
  %27 = icmp sgt i32 %26, %2
  br i1 %27, label %28, label %40

28:                                               ; preds = %25
  %29 = getelementptr inbounds i8, ptr %0, i64 8
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = sext i32 %2 to i64
  %32 = mul i64 %31, %1
  %33 = getelementptr inbounds i8, ptr %30, i64 %32
  %34 = zext i32 %spec.store.select to i64
  %35 = mul i64 %34, %1
  %36 = getelementptr inbounds i8, ptr %33, i64 %35
  %37 = sub nsw i32 %26, %2
  %38 = sext i32 %37 to i64
  %39 = mul i64 %38, %1
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %36, ptr align 1 %33, i64 %39, i1 false)
  %.pre46 = load i32, ptr %9, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %28, %25
  %41 = phi i32 [ %.pre46, %28 ], [ %26, %25 ]
  %42 = add i32 %41, %spec.store.select
  store i32 %42, ptr %9, align 4, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %4, %17, %40, %12
  %.1 = phi i32 [ -1, %12 ], [ 0, %40 ], [ -1, %17 ], [ -1, %4 ]
  ret i32 %.1
}

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

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #3 = { mustprogress nounwind willreturn 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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nounwind willreturn memory(none) }
attributes #6 = { nounwind allocsize(1) }

!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 = !{!7, !7, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 8}


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