; LLVM IR from avaxar/ccgc :: ccgc (ccgc_collect)

; 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.Registers = type { i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64 }
%struct.Block = type { i64, i8, i8, [6 x i8], [0 x i8] }

@stack_base = external hidden global ptr, align 8
@page = external hidden global [65536 x i8], 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

; Function Attrs: nounwind uwtable
declare dso_local void @ccgc_free(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @ccgc_collect() #1 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca %struct.Registers, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = load i64, ptr @page, align 16, !tbaa !5
  %8 = icmp eq i64 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %0
  store i64 0, ptr %1, align 8
  br label %65

10:                                               ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  store ptr @page, ptr %2, align 8, !tbaa !11
  br label %11

11:                                               ; preds = %18, %10
  %12 = load ptr, ptr %2, align 8, !tbaa !11
  %13 = icmp ult ptr %12, getelementptr inbounds (i8, ptr @page, i64 65536)
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  br label %26

15:                                               ; preds = %11
  %16 = load ptr, ptr %2, align 8, !tbaa !11
  %17 = getelementptr inbounds %struct.Block, ptr %16, i32 0, i32 2
  store i8 0, ptr %17, align 1, !tbaa !13
  br label %18

18:                                               ; preds = %15
  %19 = load ptr, ptr %2, align 8, !tbaa !11
  %20 = getelementptr inbounds %struct.Block, ptr %19, i32 0, i32 4
  %21 = getelementptr inbounds [0 x i8], ptr %20, i64 0, i64 0
  %22 = load ptr, ptr %2, align 8, !tbaa !11
  %23 = getelementptr inbounds %struct.Block, ptr %22, i32 0, i32 0
  %24 = load i64, ptr %23, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %21, i64 %24
  store ptr %25, ptr %2, align 8, !tbaa !11
  br label %11, !llvm.loop !14

26:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 128, ptr %3) #3
  call void @getRegisters(ptr dead_on_unwind writable sret(%struct.Registers) align 8 %3)
  call void @markPointers(ptr noundef %3, i64 noundef 128)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %27 = load ptr, ptr @stack_base, align 8, !tbaa !11
  %28 = ptrtoint ptr %27 to i64
  %29 = ptrtoint ptr %4 to i64
  %30 = sub i64 %28, %29
  call void @markPointers(ptr noundef %4, i64 noundef %30)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 0, ptr %5, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store ptr @page, ptr %6, align 8, !tbaa !11
  br label %31

31:                                               ; preds = %55, %26
  %32 = load ptr, ptr %6, align 8, !tbaa !11
  %33 = icmp ult ptr %32, getelementptr inbounds (i8, ptr @page, i64 65536)
  br i1 %33, label %35, label %34

34:                                               ; preds = %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %63

35:                                               ; preds = %31
  %36 = load ptr, ptr %6, align 8, !tbaa !11
  %37 = getelementptr inbounds %struct.Block, ptr %36, i32 0, i32 1
  %38 = load i8, ptr %37, align 8, !tbaa !17, !range !18, !noundef !19
  %39 = trunc i8 %38 to i1
  br i1 %39, label %40, label %54

40:                                               ; preds = %35
  %41 = load ptr, ptr %6, align 8, !tbaa !11
  %42 = getelementptr inbounds %struct.Block, ptr %41, i32 0, i32 2
  %43 = load i8, ptr %42, align 1, !tbaa !13, !range !18, !noundef !19
  %44 = trunc i8 %43 to i1
  br i1 %44, label %54, label %45

45:                                               ; preds = %40
  %46 = load ptr, ptr %6, align 8, !tbaa !11
  %47 = getelementptr inbounds %struct.Block, ptr %46, i32 0, i32 0
  %48 = load i64, ptr %47, align 8, !tbaa !5
  %49 = load i64, ptr %5, align 8, !tbaa !16
  %50 = add i64 %49, %48
  store i64 %50, ptr %5, align 8, !tbaa !16
  %51 = load ptr, ptr %6, align 8, !tbaa !11
  %52 = getelementptr inbounds %struct.Block, ptr %51, i32 0, i32 4
  %53 = getelementptr inbounds [0 x i8], ptr %52, i64 0, i64 0
  call void @ccgc_free(ptr noundef %53)
  br label %54

54:                                               ; preds = %45, %40, %35
  br label %55

55:                                               ; preds = %54
  %56 = load ptr, ptr %6, align 8, !tbaa !11
  %57 = getelementptr inbounds %struct.Block, ptr %56, i32 0, i32 4
  %58 = getelementptr inbounds [0 x i8], ptr %57, i64 0, i64 0
  %59 = load ptr, ptr %6, align 8, !tbaa !11
  %60 = getelementptr inbounds %struct.Block, ptr %59, i32 0, i32 0
  %61 = load i64, ptr %60, align 8, !tbaa !5
  %62 = getelementptr inbounds i8, ptr %58, i64 %61
  store ptr %62, ptr %6, align 8, !tbaa !11
  br label %31, !llvm.loop !20

63:                                               ; preds = %34
  %64 = load i64, ptr %5, align 8, !tbaa !16
  store i64 %64, ptr %1, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr %3) #3
  br label %65

65:                                               ; preds = %63, %9
  %66 = load i64, ptr %1, align 8
  ret i64 %66
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @getRegisters(ptr dead_on_unwind noalias writable sret(%struct.Registers) align 8) #2

; Function Attrs: nounwind uwtable
declare hidden void @markPointers(ptr 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 = { inlinehint 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 9, !8, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"_Bool", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!6, !10, i64 9}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!7, !7, i64 0}
!17 = !{!6, !10, i64 8}
!18 = !{i8 0, i8 2}
!19 = !{}
!20 = distinct !{!20, !15}


; 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.Registers = type { i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64 }

@stack_base = external hidden local_unnamed_addr global ptr, align 8
@page = external hidden global [65536 x i8], align 16

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare dso_local void @ccgc_free(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i64 @ccgc_collect() local_unnamed_addr #1 {
  %1 = alloca %struct.Registers, align 8
  %2 = alloca i64, align 8
  %3 = load i64, ptr @page, align 16, !tbaa !5
  %4 = icmp eq i64 %3, 0
  br i1 %4, label %29, label %.preheader

5:                                                ; preds = %.preheader
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %1) #3
  call void @getRegisters(ptr dead_on_unwind nonnull writable sret(%struct.Registers) align 8 %1)
  call void @markPointers(ptr noundef nonnull %1, i64 noundef 128)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %6 = load ptr, ptr @stack_base, align 8, !tbaa !11
  %7 = ptrtoint ptr %6 to i64
  %8 = ptrtoint ptr %2 to i64
  %9 = sub i64 %7, %8
  call void @markPointers(ptr noundef nonnull %2, i64 noundef %9)
  br label %14

.preheader:                                       ; preds = %0, %.preheader
  %.014.idx20 = phi i64 [ %.add, %.preheader ], [ 0, %0 ]
  %.014.ptr = getelementptr inbounds i8, ptr @page, i64 %.014.idx20
  %10 = getelementptr inbounds i8, ptr %.014.ptr, i64 9
  store i8 0, ptr %10, align 1, !tbaa !13
  %.014.add = add nsw i64 %.014.idx20, 16
  %11 = load i64, ptr %.014.ptr, align 8, !tbaa !5
  %.add = add nsw i64 %.014.add, %11
  %12 = icmp slt i64 %.add, 65536
  br i1 %12, label %.preheader, label %5, !llvm.loop !14

13:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %1) #3
  br label %29

14:                                               ; preds = %5, %26
  %.0.ptr23 = phi ptr [ @page, %5 ], [ %.0.ptr, %26 ]
  %.0.idx22 = phi i64 [ 0, %5 ], [ %.add17, %26 ]
  %.01321 = phi i64 [ 0, %5 ], [ %.1, %26 ]
  %15 = getelementptr inbounds i8, ptr %.0.ptr23, i64 8
  %16 = load i8, ptr %15, align 8, !tbaa !16, !range !17, !noundef !18
  %17 = trunc nuw i8 %16 to i1
  br i1 %17, label %18, label %26

18:                                               ; preds = %14
  %19 = getelementptr inbounds i8, ptr %.0.ptr23, i64 9
  %20 = load i8, ptr %19, align 1, !tbaa !13, !range !17, !noundef !18
  %21 = trunc nuw i8 %20 to i1
  br i1 %21, label %26, label %22

22:                                               ; preds = %18
  %23 = load i64, ptr %.0.ptr23, align 8, !tbaa !5
  %24 = add i64 %23, %.01321
  %25 = getelementptr inbounds i8, ptr %.0.ptr23, i64 16
  call void @ccgc_free(ptr noundef nonnull %25)
  br label %26

26:                                               ; preds = %14, %18, %22
  %.1 = phi i64 [ %.01321, %18 ], [ %24, %22 ], [ %.01321, %14 ]
  %.0.add = add nsw i64 %.0.idx22, 16
  %27 = load i64, ptr %.0.ptr23, align 8, !tbaa !5
  %.add17 = add nsw i64 %.0.add, %27
  %.0.ptr = getelementptr inbounds i8, ptr @page, i64 %.add17
  %28 = icmp slt i64 %.add17, 65536
  br i1 %28, label %14, label %13, !llvm.loop !19

29:                                               ; preds = %0, %13
  %.015 = phi i64 [ %.1, %13 ], [ 0, %0 ]
  ret i64 %.015
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @getRegisters(ptr dead_on_unwind noalias writable sret(%struct.Registers) align 8) local_unnamed_addr #2

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

attributes #0 = { mustprogress 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 = { inlinehint 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 9, !8, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"_Bool", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!6, !10, i64 9}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !10, i64 8}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = distinct !{!19, !15}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass