; LLVM IR from humz2k/sugaryc :: ext/bdwgc (reclaim)

; 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.obj_kind = type { ptr, ptr, i64, i32, i32 }
%struct.hblkhdr = type { ptr, ptr, ptr, i8, i8, i16, i64, i64, ptr, i64, [5 x i64] }

@GC_obj_kinds = external global [16 x %struct.obj_kind], align 16

; 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

declare ptr @GC_find_header(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @GC_continue_reclaim(i64 noundef %0, i32 noundef %1) #2 {
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [16 x %struct.obj_kind], ptr @GC_obj_kinds, i64 0, i64 %12
  store ptr %13, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %14 = load ptr, ptr %6, align 8, !tbaa !11
  %15 = getelementptr inbounds %struct.obj_kind, ptr %14, i32 0, i32 1
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  store ptr %16, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %17 = load ptr, ptr %6, align 8, !tbaa !11
  %18 = getelementptr inbounds %struct.obj_kind, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  %20 = load i64, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds ptr, ptr %19, i64 %20
  store ptr %21, ptr %8, align 8, !tbaa !11
  %22 = load ptr, ptr %7, align 8, !tbaa !11
  %23 = icmp eq ptr null, %22
  br i1 %23, label %24, label %25

24:                                               ; preds = %2
  store i32 1, ptr %9, align 4
  br label %53

25:                                               ; preds = %2
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = load ptr, ptr %7, align 8, !tbaa !11
  %28 = getelementptr inbounds ptr, ptr %27, i64 %26
  store ptr %28, ptr %7, align 8, !tbaa !11
  br label %29

29:                                               ; preds = %51, %25
  %30 = load ptr, ptr %7, align 8, !tbaa !11
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  store ptr %31, ptr %5, align 8, !tbaa !11
  %32 = icmp ne ptr %31, null
  br i1 %32, label %33, label %52

33:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %34 = load ptr, ptr %5, align 8, !tbaa !11
  %35 = call ptr @GC_find_header(ptr noundef %34)
  store ptr %35, ptr %10, align 8, !tbaa !11
  %36 = load ptr, ptr %10, align 8, !tbaa !11
  %37 = getelementptr inbounds %struct.hblkhdr, ptr %36, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !16
  %39 = load ptr, ptr %7, align 8, !tbaa !11
  store ptr %38, ptr %39, align 8, !tbaa !11
  %40 = load ptr, ptr %5, align 8, !tbaa !11
  %41 = load ptr, ptr %10, align 8, !tbaa !11
  %42 = getelementptr inbounds %struct.hblkhdr, ptr %41, i32 0, i32 6
  %43 = load i64, ptr %42, align 8, !tbaa !19
  call void @GC_reclaim_small_nonempty_block(ptr noundef %40, i64 noundef %43, i32 noundef 0)
  %44 = load ptr, ptr %8, align 8, !tbaa !11
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  %46 = icmp ne ptr %45, null
  br i1 %46, label %47, label %48

47:                                               ; preds = %33
  store i32 2, ptr %9, align 4
  br label %49

48:                                               ; preds = %33
  store i32 0, ptr %9, align 4
  br label %49

49:                                               ; preds = %48, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %50 = load i32, ptr %9, align 4
  switch i32 %50, label %56 [
    i32 0, label %51
    i32 2, label %52
  ]

51:                                               ; preds = %49
  br label %29, !llvm.loop !20

52:                                               ; preds = %49, %29
  store i32 0, ptr %9, align 4
  br label %53

53:                                               ; preds = %52, %24
  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
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %54 = load i32, ptr %9, align 4
  switch i32 %54, label %56 [
    i32 0, label %55
    i32 1, label %55
  ]

55:                                               ; preds = %53, %53
  ret void

56:                                               ; preds = %53, %49
  unreachable
}

; Function Attrs: nounwind uwtable
declare hidden void @GC_reclaim_small_nonempty_block(ptr noundef, i64 noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #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 #2 = { 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 #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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 8}
!14 = !{!"obj_kind", !12, i64 0, !12, i64 8, !6, i64 16, !10, i64 24, !10, i64 28}
!15 = !{!14, !12, i64 0}
!16 = !{!17, !12, i64 0}
!17 = !{!"hblkhdr", !12, i64 0, !12, i64 8, !12, i64 16, !7, i64 24, !7, i64 25, !18, i64 26, !6, i64 32, !6, i64 40, !12, i64 48, !6, i64 56, !7, i64 64}
!18 = !{!"short", !7, i64 0}
!19 = !{!17, !6, i64 32}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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

%struct.obj_kind = type { ptr, ptr, i64, i32, i32 }

@GC_obj_kinds = external local_unnamed_addr global [16 x %struct.obj_kind], align 16

declare ptr @GC_find_header(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @GC_continue_reclaim(i64 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = sext i32 %1 to i64
  %4 = getelementptr inbounds [16 x %struct.obj_kind], ptr @GC_obj_kinds, i64 0, i64 %3
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = load ptr, ptr %4, align 16, !tbaa !12
  %8 = getelementptr inbounds ptr, ptr %7, i64 %0
  %9 = icmp eq ptr %6, null
  br i1 %9, label %.loopexit, label %10

10:                                               ; preds = %2
  %11 = getelementptr inbounds ptr, ptr %6, i64 %0
  br label %12

12:                                               ; preds = %14, %10
  %13 = load ptr, ptr %11, align 8, !tbaa !13
  %.not = icmp eq ptr %13, null
  br i1 %.not, label %.loopexit, label %14

14:                                               ; preds = %12
  %15 = tail call ptr @GC_find_header(ptr noundef nonnull %13) #2
  %16 = load ptr, ptr %15, align 8, !tbaa !14
  store ptr %16, ptr %11, align 8, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %15, i64 32
  %18 = load i64, ptr %17, align 8, !tbaa !17
  tail call void @GC_reclaim_small_nonempty_block(ptr noundef nonnull %13, i64 noundef %18, i32 noundef 0)
  %19 = load ptr, ptr %8, align 8, !tbaa !13
  %.not16 = icmp eq ptr %19, null
  br i1 %.not16, label %12, label %.loopexit

.loopexit:                                        ; preds = %14, %12, %2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @GC_reclaim_small_nonempty_block(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

attributes #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 = { 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, !7, i64 8}
!6 = !{!"obj_kind", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 24, !11, i64 28}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !7, i64 0}
!15 = !{!"hblkhdr", !7, i64 0, !7, i64 8, !7, i64 16, !8, i64 24, !8, i64 25, !16, i64 26, !10, i64 32, !10, i64 40, !7, i64 48, !10, i64 56, !8, i64 64}
!16 = !{!"short", !8, i64 0}
!17 = !{!15, !10, i64 32}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass