; 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.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 @relocate() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %7 = load ptr, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 3), align 8, !tbaa !5
  store ptr %7, ptr %1, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !14
  br label %8

8:                                                ; preds = %39, %0
  %9 = load ptr, ptr %1, align 8, !tbaa !13
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %43

11:                                               ; preds = %8
  %12 = load ptr, ptr %1, align 8, !tbaa !13
  %13 = getelementptr inbounds %struct.collectorBlock, ptr %12, i32 0, i32 3
  %14 = load i8, ptr %13, align 4, !tbaa !15
  %15 = sext i8 %14 to i32
  %16 = icmp eq i32 %15, 1
  br i1 %16, label %17, label %34

17:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %18 = load ptr, ptr %1, align 8, !tbaa !13
  %19 = getelementptr inbounds %struct.collectorBlock, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !17
  store ptr %20, ptr %3, align 8, !tbaa !13
  %21 = load ptr, ptr %3, align 8, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %21, i64 24
  %23 = load ptr, ptr %1, align 8, !tbaa !13
  %24 = getelementptr inbounds i8, ptr %23, i64 24
  %25 = load ptr, ptr %1, align 8, !tbaa !13
  %26 = getelementptr inbounds %struct.collectorBlock, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 8, !tbaa !18
  %28 = sext i32 %27 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %22, ptr align 1 %24, i64 %28, i1 false)
  %29 = load ptr, ptr %1, align 8, !tbaa !13
  %30 = getelementptr inbounds %struct.collectorBlock, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 8, !tbaa !18
  %32 = load ptr, ptr %3, align 8, !tbaa !13
  %33 = getelementptr inbounds %struct.collectorBlock, ptr %32, i32 0, i32 2
  store i32 %31, ptr %33, align 8, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %39

34:                                               ; preds = %11
  %35 = load ptr, ptr %1, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct.collectorBlock, ptr %35, i32 0, i32 3
  store i8 1, ptr %36, align 4, !tbaa !15
  %37 = load i32, ptr %2, align 4, !tbaa !14
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %2, align 4, !tbaa !14
  br label %39

39:                                               ; preds = %34, %17
  %40 = load ptr, ptr %1, align 8, !tbaa !13
  %41 = getelementptr inbounds %struct.collectorBlock, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !19
  store ptr %42, ptr %1, align 8, !tbaa !13
  br label %8, !llvm.loop !20

43:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %44 = load ptr, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 3), align 8, !tbaa !5
  store ptr %44, ptr %4, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %45 = load i32, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 1), align 8, !tbaa !22
  %46 = load i32, ptr %2, align 4, !tbaa !14
  %47 = sub nsw i32 %45, %46
  store i32 %47, ptr %5, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !14
  br label %48

48:                                               ; preds = %57, %43
  %49 = load i32, ptr %6, align 4, !tbaa !14
  %50 = load i32, ptr %5, align 4, !tbaa !14
  %51 = icmp slt i32 %49, %50
  br i1 %51, label %53, label %52

52:                                               ; preds = %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %60

53:                                               ; preds = %48
  %54 = load ptr, ptr %4, align 8, !tbaa !13
  %55 = getelementptr inbounds %struct.collectorBlock, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !19
  store ptr %56, ptr %4, align 8, !tbaa !13
  br label %57

57:                                               ; preds = %53
  %58 = load i32, ptr %6, align 4, !tbaa !14
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %6, align 4, !tbaa !14
  br label %48, !llvm.loop !23

60:                                               ; preds = %52
  br label %61

61:                                               ; preds = %64, %60
  %62 = load ptr, ptr %4, align 8, !tbaa !13
  %63 = icmp ne ptr %62, null
  br i1 %63, label %64, label %70

64:                                               ; preds = %61
  %65 = load ptr, ptr %4, align 8, !tbaa !13
  %66 = getelementptr inbounds %struct.collectorBlock, ptr %65, i32 0, i32 3
  store i8 0, ptr %66, align 4, !tbaa !15
  %67 = load ptr, ptr %4, align 8, !tbaa !13
  %68 = getelementptr inbounds %struct.collectorBlock, ptr %67, i32 0, i32 0
  %69 = load ptr, ptr %68, align 8, !tbaa !19
  store ptr %69, ptr %4, align 8, !tbaa !13
  br label %61, !llvm.loop !24

70:                                               ; preds = %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  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

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 = { 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 32}
!6 = !{!"gc", !7, i64 0, !10, i64 8, !11, i64 16, !7, i64 32, !7, i64 40, !8, i64 48}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"", !12, i64 0}
!12 = !{!"hash_map", !7, i64 0, !10, i64 8}
!13 = !{!7, !7, i64 0}
!14 = !{!10, !10, i64 0}
!15 = !{!16, !8, i64 20}
!16 = !{!"collectorBlock", !7, i64 0, !7, i64 8, !10, i64 16, !8, i64 20}
!17 = !{!16, !7, i64 8}
!18 = !{!16, !10, i64 16}
!19 = !{!16, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!6, !10, i64 8}
!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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @relocate() local_unnamed_addr #0 {
  %.01823 = load ptr, ptr getelementptr inbounds (i8, ptr @GC, i64 32), align 8, !tbaa !5
  %.not24 = icmp eq ptr %.01823, null
  br i1 %.not24, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %16
  %.01826 = phi ptr [ %.018, %16 ], [ %.01823, %0 ]
  %.02025 = phi i32 [ %.121, %16 ], [ 0, %0 ]
  %1 = getelementptr inbounds i8, ptr %.01826, i64 20
  %2 = load i8, ptr %1, align 4, !tbaa !9
  %3 = icmp eq i8 %2, 1
  br i1 %3, label %4, label %14

4:                                                ; preds = %.lr.ph
  %5 = getelementptr inbounds i8, ptr %.01826, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !12
  %7 = getelementptr inbounds i8, ptr %6, i64 24
  %8 = getelementptr inbounds i8, ptr %.01826, i64 24
  %9 = getelementptr inbounds i8, ptr %.01826, i64 16
  %10 = load i32, ptr %9, align 8, !tbaa !13
  %11 = sext i32 %10 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %7, ptr nonnull align 1 %8, i64 %11, i1 false)
  %12 = load i32, ptr %9, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %6, i64 16
  store i32 %12, ptr %13, align 8, !tbaa !13
  br label %16

14:                                               ; preds = %.lr.ph
  store i8 1, ptr %1, align 4, !tbaa !9
  %15 = add nsw i32 %.02025, 1
  br label %16

16:                                               ; preds = %14, %4
  %.121 = phi i32 [ %.02025, %4 ], [ %15, %14 ]
  %.018 = load ptr, ptr %.01826, align 8, !tbaa !5
  %.not = icmp eq ptr %.018, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %16, %0
  %.020.lcssa = phi i32 [ 0, %0 ], [ %.121, %16 ]
  %17 = load i32, ptr getelementptr inbounds (i8, ptr @GC, i64 8), align 8, !tbaa !16
  %18 = sub nsw i32 %17, %.020.lcssa
  %smax = tail call i32 @llvm.smax.i32(i32 %18, i32 0)
  %19 = add nuw i32 %smax, 1
  %xtraiter = and i32 %19, 7
  %20 = icmp slt i32 %18, 7
  br i1 %20, label %.preheader.unr-lcssa, label %._crit_edge.new

._crit_edge.new:                                  ; preds = %._crit_edge
  %unroll_iter = and i32 %19, -8
  br label %21

21:                                               ; preds = %21, %._crit_edge.new
  %.019.in = phi ptr [ getelementptr inbounds (i8, ptr @GC, i64 32), %._crit_edge.new ], [ %.019.7, %21 ]
  %niter = phi i32 [ 0, %._crit_edge.new ], [ %niter.next.7, %21 ]
  %.019 = load ptr, ptr %.019.in, align 8, !tbaa !5
  %.019.1 = load ptr, ptr %.019, align 8, !tbaa !5
  %.019.2 = load ptr, ptr %.019.1, align 8, !tbaa !5
  %.019.3 = load ptr, ptr %.019.2, align 8, !tbaa !5
  %.019.4 = load ptr, ptr %.019.3, align 8, !tbaa !5
  %.019.5 = load ptr, ptr %.019.4, align 8, !tbaa !5
  %.019.6 = load ptr, ptr %.019.5, align 8, !tbaa !5
  %.019.7 = load ptr, ptr %.019.6, align 8, !tbaa !5
  %niter.next.7 = add i32 %niter, 8
  %niter.ncmp.7 = icmp eq i32 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %.preheader.unr-lcssa, label %21, !llvm.loop !20

.preheader.unr-lcssa:                             ; preds = %21, %._crit_edge
  %.019.lcssa.ph = phi ptr [ poison, %._crit_edge ], [ %.019.7, %21 ]
  %.019.in.unr = phi ptr [ getelementptr inbounds (i8, ptr @GC, i64 32), %._crit_edge ], [ %.019.7, %21 ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader, label %.epil.preheader

.epil.preheader:                                  ; preds = %.preheader.unr-lcssa, %.epil.preheader
  %.019.in.epil = phi ptr [ %.019.epil, %.epil.preheader ], [ %.019.in.unr, %.preheader.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.epil.preheader ], [ 0, %.preheader.unr-lcssa ]
  %.019.epil = load ptr, ptr %.019.in.epil, align 8, !tbaa !5
  %epil.iter.next = add i32 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i32 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.preheader, label %.epil.preheader, !llvm.loop !21

.preheader:                                       ; preds = %.epil.preheader, %.preheader.unr-lcssa
  %.019.lcssa = phi ptr [ %.019.lcssa.ph, %.preheader.unr-lcssa ], [ %.019.epil, %.epil.preheader ]
  %.not2227 = icmp eq ptr %.019.lcssa, null
  br i1 %.not2227, label %._crit_edge30, label %.lr.ph29

.lr.ph29:                                         ; preds = %.preheader, %.lr.ph29
  %.128 = phi ptr [ %23, %.lr.ph29 ], [ %.019.lcssa, %.preheader ]
  %22 = getelementptr inbounds i8, ptr %.128, i64 20
  store i8 0, ptr %22, align 4, !tbaa !9
  %23 = load ptr, ptr %.128, align 8, !tbaa !23
  %.not22 = icmp eq ptr %23, null
  br i1 %.not22, label %._crit_edge30, label %.lr.ph29, !llvm.loop !24

._crit_edge30:                                    ; preds = %.lr.ph29, %.preheader
  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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !7, i64 20}
!10 = !{!"collectorBlock", !6, i64 0, !6, i64 8, !11, i64 16, !7, i64 20}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !11, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !11, i64 8}
!17 = !{!"gc", !6, i64 0, !11, i64 8, !18, i64 16, !6, i64 32, !6, i64 40, !7, i64 48}
!18 = !{!"", !19, i64 0}
!19 = !{!"hash_map", !6, i64 0, !11, i64 8}
!20 = distinct !{!20, !15}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.unroll.disable"}
!23 = !{!10, !6, i64 0}
!24 = distinct !{!24, !15}


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