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

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

@GC_print_stats = external global i32, align 4
@GC_page_size = external global i64, align 8
@.str.7 = external hidden unnamed_addr constant [47 x i8], align 1

; 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: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #1

declare void @GC_log_printf(ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
define dso_local void @GC_scratch_recycle_inner(ptr noundef %0, i64 noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 0, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp eq ptr null, %9
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store i32 1, ptr %8, align 4
  br label %62

12:                                               ; preds = %2
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = ptrtoint ptr %13 to i64
  %15 = load i64, ptr @GC_page_size, align 8, !tbaa !9
  %16 = sub i64 %15, 1
  %17 = and i64 %14, %16
  store i64 %17, ptr %5, align 8, !tbaa !9
  %18 = load i64, ptr %5, align 8, !tbaa !9
  %19 = icmp ne i64 %18, 0
  br i1 %19, label %20, label %24

20:                                               ; preds = %12
  %21 = load i64, ptr @GC_page_size, align 8, !tbaa !9
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = sub i64 %21, %22
  store i64 %23, ptr %6, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %20, %12
  %25 = load i64, ptr %4, align 8, !tbaa !9
  %26 = load i64, ptr %6, align 8, !tbaa !9
  %27 = icmp ugt i64 %25, %26
  br i1 %27, label %28, label %36

28:                                               ; preds = %24
  %29 = load i64, ptr %4, align 8, !tbaa !9
  %30 = load i64, ptr %6, align 8, !tbaa !9
  %31 = sub i64 %29, %30
  %32 = load i64, ptr @GC_page_size, align 8, !tbaa !9
  %33 = sub i64 %32, 1
  %34 = xor i64 %33, -1
  %35 = and i64 %31, %34
  br label %37

36:                                               ; preds = %24
  br label %37

37:                                               ; preds = %36, %28
  %38 = phi i64 [ %35, %28 ], [ 0, %36 ]
  store i64 %38, ptr %7, align 8, !tbaa !9
  %39 = load i32, ptr @GC_print_stats, align 4, !tbaa !11
  %40 = icmp ne i32 %39, 0
  %41 = xor i1 %40, true
  %42 = zext i1 %41 to i32
  %43 = sext i32 %42 to i64
  %44 = call i64 @llvm.expect.i64(i64 %43, i64 1)
  %45 = icmp ne i64 %44, 0
  br i1 %45, label %46, label %47

46:                                               ; preds = %37
  br label %51

47:                                               ; preds = %37
  %48 = load i64, ptr %7, align 8, !tbaa !9
  %49 = load i64, ptr %4, align 8, !tbaa !9
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  call void (ptr, ...) @GC_log_printf(ptr noundef @.str.7, i64 noundef %48, i64 noundef %49, ptr noundef %50)
  br label %51

51:                                               ; preds = %47, %46
  %52 = load i64, ptr %7, align 8, !tbaa !9
  %53 = icmp ugt i64 %52, 0
  br i1 %53, label %54, label %61

54:                                               ; preds = %51
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = ptrtoint ptr %55 to i64
  %57 = load i64, ptr %6, align 8, !tbaa !9
  %58 = add i64 %56, %57
  %59 = inttoptr i64 %58 to ptr
  %60 = load i64, ptr %7, align 8, !tbaa !9
  call void @GC_add_to_heap(ptr noundef %59, i64 noundef %60)
  br label %61

61:                                               ; preds = %54, %51
  store i32 0, ptr %8, align 4
  br label %62

62:                                               ; preds = %61, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  %63 = load i32, ptr %8, align 4
  switch i32 %63, label %65 [
    i32 0, label %64
    i32 1, label %64
  ]

64:                                               ; preds = %62, %62
  ret void

65:                                               ; preds = %62
  unreachable
}

; Function Attrs: nounwind uwtable
declare hidden void @GC_add_to_heap(ptr noundef, i64 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(none) }
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 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 #4 = { 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}


; 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_print_stats = external local_unnamed_addr global i32, align 4
@GC_page_size = external local_unnamed_addr global i64, align 8
@.str.7 = external hidden unnamed_addr constant [47 x i8], align 1

declare void @GC_log_printf(ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @GC_scratch_recycle_inner(ptr noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %21, label %4

4:                                                ; preds = %2
  %5 = ptrtoint ptr %0 to i64
  %6 = load i64, ptr @GC_page_size, align 8
  %7 = add i64 %6, -1
  %8 = and i64 %7, %5
  %.not = icmp eq i64 %8, 0
  %9 = sub i64 %6, %8
  %spec.select = select i1 %.not, i64 0, i64 %9
  %10 = icmp ult i64 %spec.select, %1
  %11 = sub i64 %1, %spec.select
  %12 = sub i64 0, %6
  %13 = and i64 %11, %12
  %14 = select i1 %10, i64 %13, i64 0
  %15 = load i32, ptr @GC_print_stats, align 4, !tbaa !5
  %.not18 = icmp eq i32 %15, 0
  br i1 %.not18, label %17, label %16, !prof !9

16:                                               ; preds = %4
  tail call void (ptr, ...) @GC_log_printf(ptr noundef nonnull @.str.7, i64 noundef %14, i64 noundef %1, ptr noundef nonnull %0) #2
  br label %17

17:                                               ; preds = %4, %16
  %.not19 = icmp eq i64 %14, 0
  br i1 %.not19, label %21, label %18

18:                                               ; preds = %17
  %19 = add i64 %spec.select, %5
  %20 = inttoptr i64 %19 to ptr
  tail call void @GC_add_to_heap(ptr noundef %20, i64 noundef %14)
  br label %21

21:                                               ; preds = %17, %18, %2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @GC_add_to_heap(ptr noundef, i64 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"branch_weights", !"expected", i32 2000, i32 1}


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