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

@GC_debugging_started = external global i32, align 4

; 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
define dso_local ptr @GC_reclaim_generic(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3, ptr noundef %4, ptr noundef %5) #1 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store i32 %3, ptr %10, align 4, !tbaa !11
  store ptr %4, ptr %11, align 8, !tbaa !5
  store ptr %5, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = load ptr, ptr %8, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.hblkhdr, ptr %15, i32 0, i32 7
  %17 = load i64, ptr %16, align 8, !tbaa !13
  %18 = icmp eq i64 %17, 0
  %19 = zext i1 %18 to i32
  call void @GC_remove_protection(ptr noundef %14, i64 noundef 1, i32 noundef %19)
  %20 = load i32, ptr %10, align 4, !tbaa !11
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %25, label %22

22:                                               ; preds = %6
  %23 = load i32, ptr @GC_debugging_started, align 4, !tbaa !11
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %32

25:                                               ; preds = %22, %6
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = load i64, ptr %9, align 8, !tbaa !9
  %29 = load ptr, ptr %11, align 8, !tbaa !5
  %30 = load ptr, ptr %12, align 8, !tbaa !5
  %31 = call ptr @GC_reclaim_clear(ptr noundef %26, ptr noundef %27, i64 noundef %28, ptr noundef %29, ptr noundef %30)
  store ptr %31, ptr %13, align 8, !tbaa !5
  br label %39

32:                                               ; preds = %22
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = load i64, ptr %9, align 8, !tbaa !9
  %36 = load ptr, ptr %11, align 8, !tbaa !5
  %37 = load ptr, ptr %12, align 8, !tbaa !5
  %38 = call ptr @GC_reclaim_uninit(ptr noundef %33, ptr noundef %34, i64 noundef %35, ptr noundef %36, ptr noundef %37)
  store ptr %38, ptr %13, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %32, %25
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.hblkhdr, ptr %40, i32 0, i32 3
  %42 = load i8, ptr %41, align 8, !tbaa !16
  %43 = zext i8 %42 to i32
  %44 = icmp eq i32 %43, 2
  br i1 %44, label %45, label %47

45:                                               ; preds = %39
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  call void @GC_set_hdr_marks(ptr noundef %46)
  br label %47

47:                                               ; preds = %45, %39
  %48 = load ptr, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  ret ptr %48
}

declare void @GC_remove_protection(ptr noundef, i64 noundef, i32 noundef) #2

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

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

declare void @GC_set_hdr_marks(ptr noundef) #2

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 = { "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 = !{!"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, !10, i64 40}
!14 = !{!"hblkhdr", !6, i64 0, !6, i64 8, !6, i64 16, !7, i64 24, !7, i64 25, !15, i64 26, !10, i64 32, !10, i64 40, !6, i64 48, !10, i64 56, !7, i64 64}
!15 = !{!"short", !7, i64 0}
!16 = !{!14, !7, i64 24}


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

@GC_debugging_started = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local ptr @GC_reclaim_generic(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3, ptr noundef %4, ptr noundef %5) local_unnamed_addr #0 {
  %7 = getelementptr inbounds i8, ptr %1, i64 40
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = icmp eq i64 %8, 0
  %10 = zext i1 %9 to i32
  tail call void @GC_remove_protection(ptr noundef %0, i64 noundef 1, i32 noundef %10) #2
  %11 = icmp ne i32 %3, 0
  %12 = load i32, ptr @GC_debugging_started, align 4
  %13 = icmp ne i32 %12, 0
  %or.cond = select i1 %11, i1 true, i1 %13
  br i1 %or.cond, label %14, label %16

14:                                               ; preds = %6
  %15 = tail call ptr @GC_reclaim_clear(ptr noundef %0, ptr noundef nonnull %1, i64 noundef %2, ptr noundef %4, ptr noundef %5)
  br label %18

16:                                               ; preds = %6
  %17 = tail call ptr @GC_reclaim_uninit(ptr noundef %0, ptr noundef nonnull %1, i64 noundef %2, ptr noundef %4, ptr noundef %5)
  br label %18

18:                                               ; preds = %16, %14
  %.0 = phi ptr [ %15, %14 ], [ %17, %16 ]
  %19 = getelementptr inbounds i8, ptr %1, i64 24
  %20 = load i8, ptr %19, align 8, !tbaa !12
  %21 = icmp eq i8 %20, 2
  br i1 %21, label %22, label %23

22:                                               ; preds = %18
  tail call void @GC_set_hdr_marks(ptr noundef nonnull %1) #2
  br label %23

23:                                               ; preds = %22, %18
  ret ptr %.0
}

declare void @GC_remove_protection(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

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

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

declare void @GC_set_hdr_marks(ptr noundef) local_unnamed_addr #1

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


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