; LLVM IR from brewlin/garbage-collect :: compact-lisp2 (compact)

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

@.str = external hidden unnamed_addr constant [20 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: nounwind uwtable
define dso_local ptr @gc_mark(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = call ptr @is_pointer_to_heap(ptr noundef %8)
  store ptr %9, ptr %4, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %13, label %11

11:                                               ; preds = %1
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %12, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %65

13:                                               ; preds = %1
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = call ptr @get_header(ptr noundef %14, ptr noundef %15)
  store ptr %16, ptr %5, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %20, label %18

18:                                               ; preds = %13
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %19, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %65

20:                                               ; preds = %13
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.header, ptr %21, i32 0, i32 1
  %23 = load i64, ptr %22, align 8, !tbaa !9
  %24 = and i64 %23, 1
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %29, label %26

26:                                               ; preds = %20
  %27 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %28, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %65

29:                                               ; preds = %20
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.header, ptr %30, i32 0, i32 1
  %32 = load i64, ptr %31, align 8, !tbaa !9
  %33 = and i64 %32, 2
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %37

35:                                               ; preds = %29
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %36, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %65

37:                                               ; preds = %29
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.header, ptr %38, i32 0, i32 1
  %40 = load i64, ptr %39, align 8, !tbaa !9
  %41 = or i64 %40, 2
  store i64 %41, ptr %39, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %42, ptr %7, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %60, %37
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.header, ptr %45, i64 1
  %47 = ptrtoint ptr %46 to i64
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.header, ptr %48, i32 0, i32 2
  %50 = load i64, ptr %49, align 8, !tbaa !12
  %51 = sub i64 %50, 48
  %52 = add i64 %47, %51
  %53 = inttoptr i64 %52 to ptr
  %54 = icmp ult ptr %44, %53
  br i1 %54, label %56, label %55

55:                                               ; preds = %43
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %63

56:                                               ; preds = %43
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = load ptr, ptr %57, align 8, !tbaa !5
  %59 = call ptr @gc_mark(ptr noundef %58)
  br label %60

60:                                               ; preds = %56
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = getelementptr inbounds i8, ptr %61, i32 1
  store ptr %62, ptr %7, align 8, !tbaa !5
  br label %43, !llvm.loop !13

63:                                               ; preds = %55
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %64, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %65

65:                                               ; preds = %63, %35, %26, %18, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %66 = load ptr, ptr %2, align 8
  ret ptr %66
}

declare ptr @is_pointer_to_heap(ptr noundef) #2

declare ptr @get_header(ptr noundef, ptr noundef) #2

declare i32 @printf(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, !11, i64 8}
!10 = !{!"header", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !6, i64 32, !6, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 16}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@.str = external hidden unnamed_addr constant [20 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @gc_mark(ptr noundef returned %0) local_unnamed_addr #0 {
  %2 = tail call ptr @is_pointer_to_heap(ptr noundef %0) #3
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %.loopexit, label %3

3:                                                ; preds = %1
  %4 = tail call ptr @get_header(ptr noundef nonnull %2, ptr noundef %0) #3
  %.not21 = icmp eq ptr %4, null
  br i1 %.not21, label %.loopexit, label %5

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %4, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = and i64 %7, 1
  %.not22 = icmp eq i64 %8, 0
  br i1 %.not22, label %9, label %11

9:                                                ; preds = %5
  %10 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  br label %.loopexit

11:                                               ; preds = %5
  %12 = and i64 %7, 2
  %.not23 = icmp eq i64 %12, 0
  br i1 %.not23, label %13, label %.loopexit

13:                                               ; preds = %11
  %14 = or disjoint i64 %7, 2
  store i64 %14, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %4, i64 48
  %16 = ptrtoint ptr %15 to i64
  %17 = getelementptr inbounds i8, ptr %4, i64 16
  %18 = add i64 %16, -48
  %19 = load i64, ptr %17, align 8, !tbaa !11
  %20 = add i64 %18, %19
  %21 = inttoptr i64 %20 to ptr
  %22 = icmp ugt ptr %21, %0
  br i1 %22, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %13, %.lr.ph
  %.024 = phi ptr [ %25, %.lr.ph ], [ %0, %13 ]
  %23 = load ptr, ptr %.024, align 8, !tbaa !12
  %24 = tail call ptr @gc_mark(ptr noundef %23)
  %25 = getelementptr inbounds i8, ptr %.024, i64 1
  %26 = load i64, ptr %17, align 8, !tbaa !11
  %27 = add i64 %18, %26
  %28 = inttoptr i64 %27 to ptr
  %29 = icmp ult ptr %25, %28
  br i1 %29, label %.lr.ph, label %.loopexit, !llvm.loop !13

.loopexit:                                        ; preds = %.lr.ph, %13, %11, %3, %1, %9
  ret ptr %0
}

declare ptr @is_pointer_to_heap(ptr noundef) local_unnamed_addr #1

declare ptr @get_header(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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 = { nofree nounwind "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 8}
!6 = !{!"header", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !10, i64 40}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 16}
!12 = !{!10, !10, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass