; LLVM IR from brewlin/garbage-collect :: compact-two-finger (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.gc_heap = type { ptr, i64 }
%struct.header = type { i64, i64, i64, i64, ptr, ptr }

@gc_heaps_used = external global i64, align 8
@gc_heaps = external global [100000 x %struct.gc_heap], align 16
@free_list = external global ptr, align 8

; 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 void @move_obj() #1 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  store i64 0, ptr %1, align 8, !tbaa !5
  br label %4

4:                                                ; preds = %110, %0
  %5 = load i64, ptr %1, align 8, !tbaa !5
  %6 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %7 = icmp ult i64 %5, %6
  br i1 %7, label %8, label %113

8:                                                ; preds = %4
  %9 = load i64, ptr %1, align 8, !tbaa !5
  %10 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %9
  %11 = getelementptr inbounds %struct.gc_heap, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 16, !tbaa !9
  %13 = getelementptr inbounds %struct.header, ptr %12, i64 1
  %14 = load i64, ptr %1, align 8, !tbaa !5
  %15 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %14
  %16 = getelementptr inbounds %struct.gc_heap, ptr %15, i32 0, i32 1
  %17 = load i64, ptr %16, align 8, !tbaa !12
  %18 = getelementptr inbounds i8, ptr %13, i64 %17
  store ptr %18, ptr %3, align 8, !tbaa !13
  %19 = load i64, ptr %1, align 8, !tbaa !5
  %20 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.gc_heap, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 16, !tbaa !9
  store ptr %22, ptr @free_list, align 8, !tbaa !13
  %23 = load i64, ptr %1, align 8, !tbaa !5
  %24 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.gc_heap, ptr %24, i32 0, i32 1
  %26 = load i64, ptr %25, align 8, !tbaa !12
  store i64 %26, ptr %2, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %108, %8
  br label %28

28:                                               ; preds = %27
  br label %29

29:                                               ; preds = %47, %28
  %30 = load ptr, ptr @free_list, align 8, !tbaa !13
  %31 = getelementptr inbounds %struct.header, ptr %30, i32 0, i32 1
  %32 = load i64, ptr %31, align 8, !tbaa !14
  %33 = and i64 %32, 1
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %45

35:                                               ; preds = %29
  %36 = load ptr, ptr @free_list, align 8, !tbaa !13
  %37 = getelementptr inbounds %struct.header, ptr %36, i32 0, i32 1
  %38 = load i64, ptr %37, align 8, !tbaa !14
  %39 = and i64 %38, 2
  %40 = icmp ne i64 %39, 0
  br i1 %40, label %41, label %45

41:                                               ; preds = %35
  %42 = load ptr, ptr @free_list, align 8, !tbaa !13
  %43 = load ptr, ptr %3, align 8, !tbaa !13
  %44 = icmp ult ptr %42, %43
  br label %45

45:                                               ; preds = %41, %35, %29
  %46 = phi i1 [ false, %35 ], [ false, %29 ], [ %44, %41 ]
  br i1 %46, label %47, label %66

47:                                               ; preds = %45
  %48 = load ptr, ptr @free_list, align 8, !tbaa !13
  %49 = getelementptr inbounds %struct.header, ptr %48, i32 0, i32 1
  %50 = load i64, ptr %49, align 8, !tbaa !14
  %51 = and i64 %50, -3
  store i64 %51, ptr %49, align 8, !tbaa !14
  %52 = load ptr, ptr @free_list, align 8, !tbaa !13
  %53 = getelementptr inbounds %struct.header, ptr %52, i32 0, i32 2
  %54 = load i64, ptr %53, align 8, !tbaa !16
  %55 = load i64, ptr %2, align 8, !tbaa !5
  %56 = sub i64 %55, %54
  store i64 %56, ptr %2, align 8, !tbaa !5
  %57 = load ptr, ptr @free_list, align 8, !tbaa !13
  %58 = getelementptr inbounds %struct.header, ptr %57, i64 1
  %59 = ptrtoint ptr %58 to i64
  %60 = load ptr, ptr @free_list, align 8, !tbaa !13
  %61 = getelementptr inbounds %struct.header, ptr %60, i32 0, i32 2
  %62 = load i64, ptr %61, align 8, !tbaa !16
  %63 = sub i64 %62, 48
  %64 = add i64 %59, %63
  %65 = inttoptr i64 %64 to ptr
  store ptr %65, ptr @free_list, align 8, !tbaa !13
  br label %29, !llvm.loop !17

66:                                               ; preds = %45
  br label %67

67:                                               ; preds = %82, %66
  %68 = load ptr, ptr %3, align 8, !tbaa !13
  %69 = getelementptr inbounds %struct.header, ptr %68, i32 0, i32 1
  %70 = load i64, ptr %69, align 8, !tbaa !14
  %71 = and i64 %70, 2
  %72 = icmp ne i64 %71, 0
  br i1 %72, label %80, label %73

73:                                               ; preds = %67
  %74 = load ptr, ptr %3, align 8, !tbaa !13
  %75 = load i64, ptr %1, align 8, !tbaa !5
  %76 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %75
  %77 = getelementptr inbounds %struct.gc_heap, ptr %76, i32 0, i32 0
  %78 = load ptr, ptr %77, align 16, !tbaa !9
  %79 = icmp ugt ptr %74, %78
  br label %80

80:                                               ; preds = %73, %67
  %81 = phi i1 [ false, %67 ], [ %79, %73 ]
  br i1 %81, label %82, label %85

82:                                               ; preds = %80
  %83 = load ptr, ptr %3, align 8, !tbaa !13
  %84 = getelementptr inbounds i8, ptr %83, i64 -1
  store ptr %84, ptr %3, align 8, !tbaa !13
  br label %67, !llvm.loop !19

85:                                               ; preds = %80
  %86 = load ptr, ptr @free_list, align 8, !tbaa !13
  %87 = load ptr, ptr %3, align 8, !tbaa !13
  %88 = icmp ult ptr %86, %87
  br i1 %88, label %89, label %107

89:                                               ; preds = %85
  %90 = load ptr, ptr %3, align 8, !tbaa !13
  %91 = getelementptr inbounds %struct.header, ptr %90, i32 0, i32 1
  %92 = load i64, ptr %91, align 8, !tbaa !14
  %93 = and i64 %92, -3
  store i64 %93, ptr %91, align 8, !tbaa !14
  %94 = load ptr, ptr @free_list, align 8, !tbaa !13
  %95 = load ptr, ptr %3, align 8, !tbaa !13
  %96 = load ptr, ptr %3, align 8, !tbaa !13
  %97 = getelementptr inbounds %struct.header, ptr %96, i32 0, i32 2
  %98 = load i64, ptr %97, align 8, !tbaa !16
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %94, ptr align 8 %95, i64 %98, i1 false)
  %99 = load ptr, ptr @free_list, align 8, !tbaa !13
  %100 = load ptr, ptr %3, align 8, !tbaa !13
  %101 = getelementptr inbounds %struct.header, ptr %100, i32 0, i32 5
  store ptr %99, ptr %101, align 8, !tbaa !20
  %102 = load ptr, ptr %3, align 8, !tbaa !13
  %103 = getelementptr inbounds %struct.header, ptr %102, i32 0, i32 2
  %104 = load i64, ptr %103, align 8, !tbaa !16
  %105 = load i64, ptr %2, align 8, !tbaa !5
  %106 = sub i64 %105, %104
  store i64 %106, ptr %2, align 8, !tbaa !5
  br label %108

107:                                              ; preds = %85
  br label %109

108:                                              ; preds = %89
  br label %27

109:                                              ; preds = %107
  br label %110

110:                                              ; preds = %109
  %111 = load i64, ptr %1, align 8, !tbaa !5
  %112 = add i64 %111, 1
  store i64 %112, ptr %1, align 8, !tbaa !5
  br label %4, !llvm.loop !21

113:                                              ; preds = %4
  %114 = load i64, ptr %2, align 8, !tbaa !5
  %115 = load ptr, ptr @free_list, align 8, !tbaa !13
  %116 = getelementptr inbounds %struct.header, ptr %115, i32 0, i32 2
  store i64 %114, ptr %116, align 8, !tbaa !16
  %117 = load ptr, ptr @free_list, align 8, !tbaa !13
  %118 = getelementptr inbounds %struct.header, ptr %117, i32 0, i32 4
  store ptr null, ptr %118, align 8, !tbaa !22
  %119 = load ptr, ptr @free_list, align 8, !tbaa !13
  %120 = getelementptr inbounds %struct.header, ptr %119, i64 1
  %121 = load i64, ptr %2, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %120, i8 0, i64 %121, i1 false)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"gc_heap", !11, i64 0, !6, i64 8}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !6, i64 8}
!15 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !11, i64 40}
!16 = !{!15, !6, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}
!20 = !{!15, !11, i64 40}
!21 = distinct !{!21, !18}
!22 = !{!15, !11, i64 32}


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

@gc_heaps_used = external local_unnamed_addr global i64, align 8
@gc_heaps = external local_unnamed_addr global [100000 x %struct.gc_heap], align 16
@free_list = external local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @move_obj() local_unnamed_addr #0 {
  %1 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %.._crit_edge_crit_edge, label %.lr.ph47

.._crit_edge_crit_edge:                           ; preds = %0
  %.pre = load ptr, ptr @free_list, align 8, !tbaa !9
  br label %._crit_edge

.lr.ph47:                                         ; preds = %0, %49
  %.02446 = phi i64 [ %50, %49 ], [ 0, %0 ]
  %2 = getelementptr inbounds [100000 x %struct.gc_heap], ptr @gc_heaps, i64 0, i64 %.02446
  %3 = load ptr, ptr %2, align 16, !tbaa !11
  %4 = getelementptr inbounds i8, ptr %3, i64 48
  %5 = getelementptr inbounds i8, ptr %2, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !13
  %7 = getelementptr inbounds i8, ptr %4, i64 %6
  store ptr %3, ptr @free_list, align 8, !tbaa !9
  br label %8

8:                                                ; preds = %40, %.lr.ph47
  %free_list.promoted = phi ptr [ %3, %.lr.ph47 ], [ %45, %40 ]
  %.123 = phi i64 [ %6, %.lr.ph47 ], [ %48, %40 ]
  %.0 = phi ptr [ %7, %.lr.ph47 ], [ %.1.lcssa36, %40 ]
  %9 = getelementptr inbounds i8, ptr %free_list.promoted, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !14
  %11 = and i64 %10, 3
  %or.cond.not2831 = icmp eq i64 %11, 3
  %12 = icmp ult ptr %free_list.promoted, %.0
  %or.cond2732 = select i1 %or.cond.not2831, i1 %12, i1 false
  br i1 %or.cond2732, label %.lr.ph, label %.critedge.preheader

..critedge.preheader_crit_edge:                   ; preds = %.lr.ph
  store ptr %29, ptr @free_list, align 8, !tbaa !9
  br label %.critedge.preheader

.critedge.preheader:                              ; preds = %..critedge.preheader_crit_edge, %8
  %.2.lcssa = phi i64 [ %24, %..critedge.preheader_crit_edge ], [ %.123, %8 ]
  %.lcssa = phi ptr [ %29, %..critedge.preheader_crit_edge ], [ %free_list.promoted, %8 ]
  %13 = getelementptr inbounds i8, ptr %.0, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !14
  %15 = and i64 %14, 2
  %.not2637 = icmp eq i64 %15, 0
  br i1 %.not2637, label %.lr.ph39, label %.critedge2

.lr.ph39:                                         ; preds = %.critedge.preheader
  %16 = load ptr, ptr %2, align 16, !tbaa !11
  %17 = icmp ugt ptr %.0, %16
  br i1 %17, label %.critedge, label %.critedge2

.lr.ph:                                           ; preds = %8, %.lr.ph
  %18 = phi i64 [ %31, %.lr.ph ], [ %10, %8 ]
  %19 = phi ptr [ %30, %.lr.ph ], [ %9, %8 ]
  %.233 = phi i64 [ %24, %.lr.ph ], [ %.123, %8 ]
  %20 = phi ptr [ %29, %.lr.ph ], [ %free_list.promoted, %8 ]
  %21 = and i64 %18, -3
  store i64 %21, ptr %19, align 8, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %20, i64 16
  %23 = load i64, ptr %22, align 8, !tbaa !16
  %24 = sub i64 %.233, %23
  %25 = getelementptr inbounds i8, ptr %20, i64 48
  %26 = ptrtoint ptr %25 to i64
  %27 = add i64 %26, -48
  %28 = add i64 %27, %23
  %29 = inttoptr i64 %28 to ptr
  %30 = getelementptr inbounds i8, ptr %29, i64 8
  %31 = load i64, ptr %30, align 8, !tbaa !14
  %32 = and i64 %31, 3
  %or.cond.not28 = icmp eq i64 %32, 3
  %33 = icmp ugt ptr %.0, %29
  %or.cond27 = select i1 %or.cond.not28, i1 %33, i1 false
  br i1 %or.cond27, label %.lr.ph, label %..critedge.preheader_crit_edge, !llvm.loop !17

.critedge:                                        ; preds = %.lr.ph39, %.critedge
  %.13856 = phi ptr [ %34, %.critedge ], [ %.0, %.lr.ph39 ]
  %34 = getelementptr inbounds i8, ptr %.13856, i64 -1
  %35 = getelementptr inbounds i8, ptr %.13856, i64 7
  %36 = load i64, ptr %35, align 8, !tbaa !14
  %37 = and i64 %36, 2
  %.not26 = icmp eq i64 %37, 0
  %38 = icmp ugt ptr %34, %16
  %or.cond = select i1 %.not26, i1 %38, i1 false
  br i1 %or.cond, label %.critedge, label %.critedge2, !llvm.loop !19

.critedge2:                                       ; preds = %.critedge, %.lr.ph39, %.critedge.preheader
  %.1.lcssa36 = phi ptr [ %.0, %.critedge.preheader ], [ %.0, %.lr.ph39 ], [ %34, %.critedge ]
  %.lcssa29 = phi i64 [ %14, %.critedge.preheader ], [ %14, %.lr.ph39 ], [ %36, %.critedge ]
  %39 = icmp ult ptr %.lcssa, %.1.lcssa36
  br i1 %39, label %40, label %49

40:                                               ; preds = %.critedge2
  %41 = getelementptr inbounds i8, ptr %.1.lcssa36, i64 8
  %42 = and i64 %.lcssa29, -3
  store i64 %42, ptr %41, align 8, !tbaa !14
  %43 = getelementptr inbounds i8, ptr %.1.lcssa36, i64 16
  %44 = load i64, ptr %43, align 8, !tbaa !16
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 8 %.lcssa, ptr nonnull align 8 %.1.lcssa36, i64 %44, i1 false)
  %45 = load ptr, ptr @free_list, align 8, !tbaa !9
  %46 = getelementptr inbounds i8, ptr %.1.lcssa36, i64 40
  store ptr %45, ptr %46, align 8, !tbaa !20
  %47 = load i64, ptr %43, align 8, !tbaa !16
  %48 = sub i64 %.2.lcssa, %47
  br label %8

49:                                               ; preds = %.critedge2
  %50 = add nuw i64 %.02446, 1
  %51 = load i64, ptr @gc_heaps_used, align 8, !tbaa !5
  %52 = icmp ult i64 %50, %51
  br i1 %52, label %.lr.ph47, label %._crit_edge, !llvm.loop !21

._crit_edge:                                      ; preds = %49, %.._crit_edge_crit_edge
  %53 = phi ptr [ %.pre, %.._crit_edge_crit_edge ], [ %.lcssa, %49 ]
  %.022.lcssa = phi i64 [ undef, %.._crit_edge_crit_edge ], [ %.2.lcssa, %49 ]
  %54 = getelementptr inbounds i8, ptr %53, i64 16
  store i64 %.022.lcssa, ptr %54, align 8, !tbaa !16
  %55 = getelementptr inbounds i8, ptr %53, i64 32
  store ptr null, ptr %55, align 8, !tbaa !22
  %56 = getelementptr inbounds i8, ptr %53, i64 48
  tail call void @llvm.memset.p0.i64(ptr nonnull align 8 %56, i8 0, i64 %.022.lcssa, i1 false)
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"gc_heap", !10, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = !{!15, !6, i64 8}
!15 = !{!"header", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 40}
!16 = !{!15, !6, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}
!20 = !{!15, !10, i64 40}
!21 = distinct !{!21, !18}
!22 = !{!15, !10, i64 32}


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