; 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
declare dso_local void @mark_helper(ptr noundef) #1

declare i32 @search(ptr, i32, ptr noundef) #2

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

10:                                               ; preds = %70, %1
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.hash_map, ptr %12, i32 0, i32 1
  %14 = load i32, ptr %13, align 8, !tbaa !11
  %15 = icmp slt i32 %11, %14
  br i1 %15, label %17, label %16

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

17:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load i32, ptr %4, 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 %5, align 8, !tbaa !5
  br label %23

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

26:                                               ; preds = %23
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.hash_node, ptr %27, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !13
  %30 = getelementptr inbounds i8, ptr %29, i64 -24
  %31 = getelementptr inbounds %struct.collectorBlock, ptr %30, i32 0, i32 3
  store i8 1, ptr %31, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.hash_node, ptr %32, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !13
  store ptr %34, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.hash_node, ptr %35, i32 0, i32 1
  %37 = load ptr, ptr %36, align 8, !tbaa !13
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.hash_node, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !13
  %41 = getelementptr inbounds i8, ptr %40, i64 -24
  %42 = getelementptr inbounds %struct.collectorBlock, ptr %41, i32 0, i32 2
  %43 = load i32, ptr %42, align 8, !tbaa !17
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %37, i64 %44
  store ptr %45, ptr %7, align 8, !tbaa !5
  br label %46

46:                                               ; preds = %62, %26
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = icmp ult ptr %47, %48
  br i1 %49, label %50, label %65

50:                                               ; preds = %46
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = load i64, ptr %51, align 8, !tbaa !18
  %53 = inttoptr i64 %52 to ptr
  %54 = load ptr, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 2), align 8
  %55 = load i32, ptr getelementptr inbounds ({ ptr, i32 }, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 2), i32 0, i32 1), align 8
  %56 = call i32 @search(ptr %54, i32 %55, ptr noundef %53)
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %62

58:                                               ; preds = %50
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = load i64, ptr %59, align 8, !tbaa !18
  %61 = inttoptr i64 %60 to ptr
  call void @mark_helper(ptr noundef %61)
  br label %62

62:                                               ; preds = %58, %50
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds i8, ptr %63, i32 1
  store ptr %64, ptr %6, align 8, !tbaa !5
  br label %46, !llvm.loop !20

65:                                               ; preds = %46
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.hash_node, ptr %66, i32 0, i32 2
  %68 = load ptr, ptr %67, align 8, !tbaa !22
  store ptr %68, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %23, !llvm.loop !23

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

70:                                               ; preds = %69
  %71 = load i32, ptr %4, align 4, !tbaa !9
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %4, align 4, !tbaa !9
  br label %10, !llvm.loop !24

73:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

declare ptr @get_map_iter(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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"hash_map", !6, i64 0, !10, i64 8}
!13 = !{!14, !6, i64 8}
!14 = !{!"hash_node", !6, i64 0, !6, i64 8, !6, i64 16}
!15 = !{!16, !7, i64 20}
!16 = !{!"collectorBlock", !6, i64 0, !6, i64 8, !10, i64 16, !7, i64 20}
!17 = !{!16, !10, i64 16}
!18 = !{!19, !19, i64 0}
!19 = !{!"long", !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!14, !6, i64 16}
!23 = distinct !{!23, !21}
!24 = distinct !{!24, !21}


; 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 local_unnamed_addr global %struct.gc, align 8

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

declare i32 @search(ptr, i32, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @gc_mark(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call ptr @get_map_iter(ptr noundef %0) #2
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph28, label %._crit_edge29

._crit_edge29:                                    ; preds = %._crit_edge25, %1
  ret void

.lr.ph28:                                         ; preds = %1, %._crit_edge25
  %6 = phi i32 [ %28, %._crit_edge25 ], [ %4, %1 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %._crit_edge25 ], [ 0, %1 ]
  %7 = getelementptr inbounds ptr, ptr %2, i64 %indvars.iv
  %.01720 = load ptr, ptr %7, align 8, !tbaa !11
  %.not21 = icmp eq ptr %.01720, null
  br i1 %.not21, label %._crit_edge25, label %.lr.ph24

.lr.ph24:                                         ; preds = %.lr.ph28, %._crit_edge
  %.01722 = phi ptr [ %.017, %._crit_edge ], [ %.01720, %.lr.ph28 ]
  %8 = getelementptr inbounds i8, ptr %.01722, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !12
  %10 = getelementptr inbounds i8, ptr %9, i64 -4
  store i8 1, ptr %10, align 4, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %9, i64 -8
  %12 = load i32, ptr %11, align 8, !tbaa !16
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i8, ptr %9, i64 %13
  %15 = icmp sgt i32 %12, 0
  br i1 %15, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph24, %24
  %.01619 = phi ptr [ %25, %24 ], [ %9, %.lr.ph24 ]
  %16 = load i64, ptr %.01619, align 8, !tbaa !17
  %17 = inttoptr i64 %16 to ptr
  %18 = load ptr, ptr getelementptr inbounds (i8, ptr @GC, i64 16), align 8
  %19 = load i32, ptr getelementptr inbounds (i8, ptr @GC, i64 24), align 8
  %20 = tail call i32 @search(ptr %18, i32 %19, ptr noundef %17) #2
  %.not18 = icmp eq i32 %20, 0
  br i1 %.not18, label %24, label %21

21:                                               ; preds = %.lr.ph
  %22 = load i64, ptr %.01619, align 8, !tbaa !17
  %23 = inttoptr i64 %22 to ptr
  tail call void @mark_helper(ptr noundef %23)
  br label %24

24:                                               ; preds = %21, %.lr.ph
  %25 = getelementptr inbounds i8, ptr %.01619, i64 1
  %26 = icmp ult ptr %25, %14
  br i1 %26, label %.lr.ph, label %._crit_edge, !llvm.loop !19

._crit_edge:                                      ; preds = %24, %.lr.ph24
  %27 = getelementptr inbounds i8, ptr %.01722, i64 16
  %.017 = load ptr, ptr %27, align 8, !tbaa !11
  %.not = icmp eq ptr %.017, null
  br i1 %.not, label %._crit_edge25.loopexit, label %.lr.ph24, !llvm.loop !21

._crit_edge25.loopexit:                           ; preds = %._crit_edge
  %.pre = load i32, ptr %3, align 8, !tbaa !5
  br label %._crit_edge25

._crit_edge25:                                    ; preds = %._crit_edge25.loopexit, %.lr.ph28
  %28 = phi i32 [ %.pre, %._crit_edge25.loopexit ], [ %6, %.lr.ph28 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %29 = sext i32 %28 to i64
  %30 = icmp slt i64 %indvars.iv.next, %29
  br i1 %30, label %.lr.ph28, label %._crit_edge29, !llvm.loop !22
}

declare ptr @get_map_iter(ptr noundef) local_unnamed_addr #1

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


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