; LLVM IR from juliuskoskela/c-vec :: vec (vec_remove)

; 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.s_vec = type { ptr, i64, i64, i64 }

; Function Attrs: nounwind uwtable
declare dso_local ptr @vec_get(ptr noundef, i64 noundef) #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 uwtable
define dso_local i32 @vec_remove(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %8, label %14

8:                                                ; preds = %2
  %9 = load i64, ptr %5, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.s_vec, ptr %10, i32 0, i32 3
  %12 = load i64, ptr %11, align 8, !tbaa !11
  %13 = icmp ugt i64 %9, %12
  br i1 %13, label %14, label %15

14:                                               ; preds = %8, %2
  store i32 -1, ptr %3, align 4
  br label %54

15:                                               ; preds = %8
  %16 = load i64, ptr %5, align 8, !tbaa !9
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.s_vec, ptr %17, i32 0, i32 3
  %19 = load i64, ptr %18, align 8, !tbaa !11
  %20 = icmp eq i64 %16, %19
  br i1 %20, label %21, label %26

21:                                               ; preds = %15
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.s_vec, ptr %22, i32 0, i32 3
  %24 = load i64, ptr %23, align 8, !tbaa !11
  %25 = add i64 %24, -1
  store i64 %25, ptr %23, align 8, !tbaa !11
  store i32 1, ptr %3, align 4
  br label %54

26:                                               ; preds = %15
  br label %27

27:                                               ; preds = %26
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = load i64, ptr %5, align 8, !tbaa !9
  %30 = call ptr @vec_get(ptr noundef %28, i64 noundef %29)
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.s_vec, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.s_vec, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !14
  %37 = load i64, ptr %5, align 8, !tbaa !9
  %38 = add i64 %37, 1
  %39 = mul i64 %36, %38
  %40 = getelementptr inbounds i8, ptr %33, i64 %39
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.s_vec, ptr %41, i32 0, i32 3
  %43 = load i64, ptr %42, align 8, !tbaa !11
  %44 = load i64, ptr %5, align 8, !tbaa !9
  %45 = sub i64 %43, %44
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.s_vec, ptr %46, i32 0, i32 1
  %48 = load i64, ptr %47, align 8, !tbaa !14
  %49 = mul i64 %45, %48
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %30, ptr align 1 %40, i64 %49, i1 false)
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.s_vec, ptr %50, i32 0, i32 3
  %52 = load i64, ptr %51, align 8, !tbaa !11
  %53 = add i64 %52, -1
  store i64 %53, ptr %51, align 8, !tbaa !11
  store i32 1, ptr %3, align 4
  br label %54

54:                                               ; preds = %27, %21, %14
  %55 = load i32, ptr %3, align 4
  ret i32 %55
}

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 nounwind willreturn memory(argmem: readwrite) }

!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, !10, i64 24}
!12 = !{!"s_vec", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!12, !6, i64 0}
!14 = !{!12, !10, 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: nounwind uwtable
declare dso_local ptr @vec_get(ptr noundef, i64 noundef) local_unnamed_addr #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: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @vec_remove(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %22, label %3

3:                                                ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp ult i64 %5, %1
  br i1 %6, label %22, label %7

7:                                                ; preds = %3
  %8 = icmp eq i64 %5, %1
  br i1 %8, label %.sink.split, label %9

9:                                                ; preds = %7
  %10 = tail call ptr @vec_get(ptr noundef nonnull %0, i64 noundef %1)
  %11 = load ptr, ptr %0, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load i64, ptr %12, align 8, !tbaa !12
  %14 = add i64 %1, 1
  %15 = mul i64 %13, %14
  %16 = getelementptr inbounds i8, ptr %11, i64 %15
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = sub i64 %17, %1
  %19 = mul i64 %18, %13
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %10, ptr align 1 %16, i64 %19, i1 false)
  %20 = load i64, ptr %4, align 8, !tbaa !5
  br label %.sink.split

.sink.split:                                      ; preds = %7, %9
  %.sink18 = phi i64 [ %20, %9 ], [ %1, %7 ]
  %21 = add i64 %.sink18, -1
  store i64 %21, ptr %4, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %.sink.split, %2, %3
  %.0 = phi i32 [ -1, %3 ], [ -1, %2 ], [ 1, %.sink.split ]
  ret i32 %.0
}

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !10, i64 24}
!6 = !{!"s_vec", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!6, !10, i64 8}


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