; LLVM IR from masyagin1998/GCI :: src/garbage-collector (garbage-collector)

; 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.GARBAGE_COLLECTOR = type { ptr, ptr, %struct.ALLOCATOR, %struct.ALLOCATOR, %struct.PTRS_MAP, i32 }
%struct.ALLOCATOR = type { ptr, i64, %struct.ALLOCATOR_LIST, %struct.ALLOCATOR_LIST }
%struct.ALLOCATOR_LIST = type { ptr, ptr, i64, i64 }
%struct.PTRS_MAP = type { ptr, i64, i64 }

; 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
declare hidden void @run_gc(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @garbage_collector_realloc_arr(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %11 = load i64, ptr %7, align 8, !tbaa !9
  %12 = mul i64 %11, 2
  store i64 %12, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %13 = load i64, ptr %8, align 8, !tbaa !9
  %14 = mul i64 24, %13
  %15 = add i64 16, %14
  store i64 %15, ptr %9, align 8, !tbaa !9
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.GARBAGE_COLLECTOR, ptr %16, i32 0, i32 2
  %18 = getelementptr inbounds %struct.ALLOCATOR, ptr %17, i32 0, i32 2
  %19 = getelementptr inbounds %struct.ALLOCATOR_LIST, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %39

22:                                               ; preds = %3
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.GARBAGE_COLLECTOR, ptr %23, i32 0, i32 2
  %25 = getelementptr inbounds %struct.ALLOCATOR, ptr %24, i32 0, i32 2
  %26 = getelementptr inbounds %struct.ALLOCATOR_LIST, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !11
  %28 = getelementptr inbounds i8, ptr %27, i64 1
  %29 = load i64, ptr %28, align 8, !tbaa !9
  %30 = load i64, ptr %9, align 8, !tbaa !9
  %31 = add i64 %30, 34
  %32 = icmp uge i64 %29, %31
  br i1 %32, label %33, label %39

33:                                               ; preds = %22
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = load i64, ptr %8, align 8, !tbaa !9
  %37 = load i64, ptr %9, align 8, !tbaa !9
  %38 = call ptr @realloc_arr_force(ptr noundef %34, ptr noundef %35, i64 noundef %36, i64 noundef %37)
  store ptr %38, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %47

39:                                               ; preds = %22, %3
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load i64, ptr %9, align 8, !tbaa !9
  call void @run_gc(ptr noundef %40, ptr noundef %6, i64 noundef %41)
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = load i64, ptr %8, align 8, !tbaa !9
  %45 = load i64, ptr %9, align 8, !tbaa !9
  %46 = call ptr @realloc_arr_force(ptr noundef %42, ptr noundef %43, i64 noundef %44, i64 noundef %45)
  store ptr %46, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %47

47:                                               ; preds = %39, %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %48 = load ptr, ptr %4, align 8
  ret ptr %48
}

; Function Attrs: nounwind uwtable
declare hidden ptr @realloc_arr_force(ptr noundef, ptr noundef, i64 noundef, i64 noundef) #1

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 = { 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, !6, i64 40}
!12 = !{!"GARBAGE_COLLECTOR", !6, i64 0, !6, i64 8, !13, i64 16, !13, i64 96, !15, i64 176, !16, i64 200}
!13 = !{!"ALLOCATOR", !6, i64 0, !10, i64 8, !14, i64 16, !14, i64 48}
!14 = !{!"ALLOCATOR_LIST", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 24}
!15 = !{!"PTRS_MAP", !6, i64 0, !10, i64 8, !10, i64 16}
!16 = !{!"int", !7, 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"

; Function Attrs: nounwind uwtable
declare hidden void @run_gc(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @garbage_collector_realloc_arr(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = alloca ptr, align 8
  store ptr %1, ptr %4, align 8, !tbaa !5
  %5 = shl i64 %2, 1
  %6 = mul i64 %2, 48
  %7 = add i64 %6, 16
  %8 = getelementptr inbounds i8, ptr %0, i64 40
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %.not = icmp eq ptr %9, null
  br i1 %.not, label %16, label %10

10:                                               ; preds = %3
  %11 = getelementptr inbounds i8, ptr %9, i64 1
  %12 = load i64, ptr %11, align 8, !tbaa !16
  %13 = add i64 %6, 50
  %.not14 = icmp ult i64 %12, %13
  br i1 %.not14, label %16, label %14

14:                                               ; preds = %10
  %15 = tail call ptr @realloc_arr_force(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %5, i64 noundef %7)
  br label %19

16:                                               ; preds = %10, %3
  call void @run_gc(ptr noundef nonnull %0, ptr noundef nonnull %4, i64 noundef %7)
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = call ptr @realloc_arr_force(ptr noundef nonnull %0, ptr noundef %17, i64 noundef %5, i64 noundef %7)
  br label %19

19:                                               ; preds = %16, %14
  %.0 = phi ptr [ %15, %14 ], [ %18, %16 ]
  ret ptr %.0
}

; Function Attrs: nounwind uwtable
declare hidden ptr @realloc_arr_force(ptr noundef, ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #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" }

!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, !6, i64 40}
!10 = !{!"GARBAGE_COLLECTOR", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 96, !14, i64 176, !15, i64 200}
!11 = !{!"ALLOCATOR", !6, i64 0, !12, i64 8, !13, i64 16, !13, i64 48}
!12 = !{!"long", !7, i64 0}
!13 = !{!"ALLOCATOR_LIST", !6, i64 0, !6, i64 8, !12, i64 16, !12, i64 24}
!14 = !{!"PTRS_MAP", !6, i64 0, !12, i64 8, !12, i64 16}
!15 = !{!"int", !7, i64 0}
!16 = !{!12, !12, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,TailCallElimPass