; LLVM IR from yudi-matsuzake/libgenerics :: src (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.vector_t = type { ptr, i64, i64, i64 }

@vector_min_siz = external dso_local global i64, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @vector_resize_buffer(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %13, label %12

12:                                               ; preds = %2
  store i32 1, ptr %3, align 4
  br label %84

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.vector_t, ptr %15, i32 0, i32 3
  %17 = load i64, ptr %16, align 8, !tbaa !11
  %18 = mul i64 %14, %17
  store i64 %18, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %19 = load i64, ptr %5, align 8, !tbaa !9
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.vector_t, ptr %20, i32 0, i32 1
  %22 = load i64, ptr %21, align 8, !tbaa !13
  %23 = icmp ult i64 %19, %22
  br i1 %23, label %24, label %28

24:                                               ; preds = %13
  %25 = load i64, ptr %5, align 8, !tbaa !9
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.vector_t, ptr %26, i32 0, i32 1
  store i64 %25, ptr %27, align 8, !tbaa !13
  br label %28

28:                                               ; preds = %24, %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %29 = load i64, ptr @vector_min_siz, align 8, !tbaa !9
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.vector_t, ptr %30, i32 0, i32 3
  %32 = load i64, ptr %31, align 8, !tbaa !11
  %33 = mul i64 %29, %32
  store i64 %33, ptr %8, align 8, !tbaa !9
  %34 = load i64, ptr %6, align 8, !tbaa !9
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.vector_t, ptr %35, i32 0, i32 2
  %37 = load i64, ptr %36, align 8, !tbaa !14
  %38 = icmp uge i64 %34, %37
  br i1 %38, label %39, label %57

39:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %40 = load i64, ptr %6, align 8, !tbaa !9
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.vector_t, ptr %41, i32 0, i32 2
  %43 = load i64, ptr %42, align 8, !tbaa !14
  %44 = sub i64 %40, %43
  store i64 %44, ptr %9, align 8, !tbaa !9
  %45 = load i64, ptr %9, align 8, !tbaa !9
  %46 = load i64, ptr %8, align 8, !tbaa !9
  %47 = icmp ult i64 %45, %46
  br i1 %47, label %48, label %54

48:                                               ; preds = %39
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.vector_t, ptr %49, i32 0, i32 2
  %51 = load i64, ptr %50, align 8, !tbaa !14
  %52 = load i64, ptr %8, align 8, !tbaa !9
  %53 = add i64 %51, %52
  store i64 %53, ptr %7, align 8, !tbaa !9
  br label %56

54:                                               ; preds = %39
  %55 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %55, ptr %7, align 8, !tbaa !9
  br label %56

56:                                               ; preds = %54, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %66

57:                                               ; preds = %28
  %58 = load i64, ptr %6, align 8, !tbaa !9
  %59 = load i64, ptr %8, align 8, !tbaa !9
  %60 = icmp ult i64 %58, %59
  br i1 %60, label %61, label %63

61:                                               ; preds = %57
  %62 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %62, ptr %7, align 8, !tbaa !9
  br label %65

63:                                               ; preds = %57
  %64 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %64, ptr %7, align 8, !tbaa !9
  br label %65

65:                                               ; preds = %63, %61
  br label %66

66:                                               ; preds = %65, %56
  %67 = load i64, ptr %7, align 8, !tbaa !9
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.vector_t, ptr %68, i32 0, i32 2
  %70 = load i64, ptr %69, align 8, !tbaa !14
  %71 = icmp ne i64 %67, %70
  br i1 %71, label %72, label %83

72:                                               ; preds = %66
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.vector_t, ptr %73, i32 0, i32 0
  %75 = load ptr, ptr %74, align 8, !tbaa !15
  %76 = load i64, ptr %7, align 8, !tbaa !9
  %77 = call ptr @realloc(ptr noundef %75, i64 noundef %76) #4
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.vector_t, ptr %78, i32 0, i32 0
  store ptr %77, ptr %79, align 8, !tbaa !15
  %80 = load i64, ptr %7, align 8, !tbaa !9
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.vector_t, ptr %81, i32 0, i32 2
  store i64 %80, ptr %82, align 8, !tbaa !14
  br label %83

83:                                               ; preds = %72, %66
  store i32 0, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %84

84:                                               ; preds = %83, %12
  %85 = load i32, ptr %3, align 4
  ret i32 %85
}

; 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

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

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 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 #3 = { nounwind }
attributes #4 = { 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, !10, i64 24}
!12 = !{!"vector_t", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !6, i64 0}


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

@vector_min_siz = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nounwind willreturn uwtable
define dso_local range(i32 0, 2) i32 @vector_resize_buffer(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %25, label %3

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

10:                                               ; preds = %3
  store i64 %1, ptr %7, align 8, !tbaa !11
  br label %11

11:                                               ; preds = %10, %3
  %12 = load i64, ptr @vector_min_siz, align 8, !tbaa !12
  %13 = mul i64 %12, %5
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load i64, ptr %14, align 8, !tbaa !13
  %.not36 = icmp ult i64 %6, %15
  br i1 %.not36, label %20, label %16

16:                                               ; preds = %11
  %17 = sub i64 %6, %15
  %18 = icmp ult i64 %17, %13
  %19 = add i64 %15, %13
  %.028 = select i1 %18, i64 %19, i64 %6
  br label %21

20:                                               ; preds = %11
  %. = tail call i64 @llvm.umax.i64(i64 %6, i64 %13)
  br label %21

21:                                               ; preds = %20, %16
  %.1 = phi i64 [ %.028, %16 ], [ %., %20 ]
  %.not37 = icmp eq i64 %.1, %15
  br i1 %.not37, label %25, label %22

22:                                               ; preds = %21
  %23 = load ptr, ptr %0, align 8, !tbaa !14
  %24 = tail call ptr @realloc(ptr noundef %23, i64 noundef %.1) #3
  store ptr %24, ptr %0, align 8, !tbaa !14
  store i64 %.1, ptr %14, align 8, !tbaa !13
  br label %25

25:                                               ; preds = %21, %22, %2
  %.0 = phi i32 [ 1, %2 ], [ 0, %22 ], [ 0, %21 ]
  ret i32 %.0
}

; 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 #1

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

attributes #0 = { 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 #1 = { 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 #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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, !10, i64 24}
!6 = !{!"vector_t", !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, !10, i64 8}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !10, i64 16}
!14 = !{!6, !7, i64 0}


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