; LLVM IR from sameerkavthekar/garbage-collector :: src (gc)

; 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 = type { ptr, i32, %struct.set_t, ptr, ptr, i8 }
%struct.set_t = type { %struct.hash_map }
%struct.hash_map = type { ptr, i32 }
%struct.hash_node = type { ptr, ptr, ptr }
%struct.collectorBlock = type { ptr, ptr, i32, i8 }

@GC = external hidden global %struct.gc, 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 @gc_sweep() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %5 = call ptr @get_set_iter(ptr noundef getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 2))
  store ptr %5, ptr %1, align 8, !tbaa !5
  %6 = load ptr, ptr %1, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %9, label %8

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

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

10:                                               ; preds = %50, %9
  %11 = load i32, ptr %3, align 4, !tbaa !9
  %12 = load ptr, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 2), align 8
  %13 = load i32, ptr getelementptr inbounds ({ ptr, i32 }, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 2), i32 0, i32 1), align 8
  %14 = call i32 @get_set_size(ptr %12, i32 %13)
  %15 = icmp slt i32 %11, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %10
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %53

17:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %18 = load ptr, ptr %1, align 8, !tbaa !5
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds ptr, ptr %18, i64 %20
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  store ptr %22, ptr %4, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %45, %17
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %49

26:                                               ; preds = %23
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.hash_node, ptr %27, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !11
  %30 = getelementptr inbounds i8, ptr %29, i64 -24
  %31 = getelementptr inbounds %struct.collectorBlock, ptr %30, i32 0, i32 3
  %32 = load i8, ptr %31, align 4, !tbaa !13
  %33 = sext i8 %32 to i32
  %34 = icmp eq i32 %33, 1
  br i1 %34, label %39, label %35

35:                                               ; preds = %26
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.hash_node, ptr %36, i32 0, i32 1
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  call void @gc_free(ptr noundef %38)
  br label %45

39:                                               ; preds = %26
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.hash_node, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  %43 = getelementptr inbounds i8, ptr %42, i64 -24
  %44 = getelementptr inbounds %struct.collectorBlock, ptr %43, i32 0, i32 3
  store i8 0, ptr %44, align 4, !tbaa !13
  br label %45

45:                                               ; preds = %39, %35
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.hash_node, ptr %46, i32 0, i32 2
  %48 = load ptr, ptr %47, align 8, !tbaa !15
  store ptr %48, ptr %4, align 8, !tbaa !5
  br label %23, !llvm.loop !16

49:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %50

50:                                               ; preds = %49
  %51 = load i32, ptr %3, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %3, align 4, !tbaa !9
  br label %10, !llvm.loop !18

53:                                               ; preds = %16
  store i32 0, ptr %2, align 4
  br label %54

54:                                               ; preds = %53, %8
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  %55 = load i32, ptr %2, align 4
  switch i32 %55, label %57 [
    i32 0, label %56
    i32 1, label %56
  ]

56:                                               ; preds = %54, %54
  ret void

57:                                               ; preds = %54
  unreachable
}

declare ptr @get_set_iter(ptr noundef) #2

declare i32 @get_set_size(ptr, i32) #2

; Function Attrs: nounwind uwtable
declare dso_local void @gc_free(ptr 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 = { "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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"hash_node", !6, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!14, !7, i64 20}
!14 = !{!"collectorBlock", !6, i64 0, !6, i64 8, !10, i64 16, !7, i64 20}
!15 = !{!12, !6, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


; 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 = type { ptr, i32, %struct.set_t, ptr, ptr, i8 }
%struct.set_t = type { %struct.hash_map }
%struct.hash_map = type { ptr, i32 }

@GC = external hidden global %struct.gc, align 8

; Function Attrs: nounwind uwtable
define dso_local void @gc_sweep() local_unnamed_addr #0 {
  %1 = tail call ptr @get_set_iter(ptr noundef nonnull getelementptr inbounds (i8, ptr @GC, i64 16)) #2
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %0
  %2 = load ptr, ptr getelementptr inbounds (i8, ptr @GC, i64 16), align 8
  %3 = load i32, ptr getelementptr inbounds (i8, ptr @GC, i64 24), align 8
  %4 = tail call i32 @get_set_size(ptr %2, i32 %3) #2
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph16, label %.loopexit

.lr.ph16:                                         ; preds = %.preheader, %._crit_edge
  %indvars.iv = phi i64 [ %indvars.iv.next, %._crit_edge ], [ 0, %.preheader ]
  %6 = getelementptr inbounds ptr, ptr %1, i64 %indvars.iv
  %.012 = load ptr, ptr %6, align 8, !tbaa !5
  %.not1113 = icmp eq ptr %.012, null
  br i1 %.not1113, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph16, %14
  %.014 = phi ptr [ %.0, %14 ], [ %.012, %.lr.ph16 ]
  %7 = getelementptr inbounds i8, ptr %.014, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %8, i64 -4
  %10 = load i8, ptr %9, align 4, !tbaa !11
  %11 = icmp eq i8 %10, 1
  br i1 %11, label %13, label %12

12:                                               ; preds = %.lr.ph
  tail call void @gc_free(ptr noundef nonnull %8)
  br label %14

13:                                               ; preds = %.lr.ph
  store i8 0, ptr %9, align 4, !tbaa !11
  br label %14

14:                                               ; preds = %13, %12
  %15 = getelementptr inbounds i8, ptr %.014, i64 16
  %.0 = load ptr, ptr %15, align 8, !tbaa !5
  %.not11 = icmp eq ptr %.0, null
  br i1 %.not11, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %14, %.lr.ph16
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %16 = load ptr, ptr getelementptr inbounds (i8, ptr @GC, i64 16), align 8
  %17 = load i32, ptr getelementptr inbounds (i8, ptr @GC, i64 24), align 8
  %18 = tail call i32 @get_set_size(ptr %16, i32 %17) #2
  %19 = sext i32 %18 to i64
  %20 = icmp slt i64 %indvars.iv.next, %19
  br i1 %20, label %.lr.ph16, label %.loopexit, !llvm.loop !16

.loopexit:                                        ; preds = %._crit_edge, %.preheader, %0
  ret void
}

declare ptr @get_set_iter(ptr noundef) local_unnamed_addr #1

declare i32 @get_set_size(ptr, i32) local_unnamed_addr #1

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

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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"hash_node", !6, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!12, !7, i64 20}
!12 = !{!"collectorBlock", !6, i64 0, !6, i64 8, !13, i64 16, !7, i64 20}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass