; 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

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

; Function Attrs: nounwind uwtable
define dso_local void @gc_free(ptr noundef %0) #2 {
  %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
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = icmp ne ptr %7, null
  br i1 %8, label %10, label %9

9:                                                ; preds = %1
  br label %77

10:                                               ; preds = %1
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %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 @search(ptr %12, i32 %13, ptr noundef %11)
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %17, label %16

16:                                               ; preds = %10
  br label %77

17:                                               ; preds = %10
  %18 = load i8, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 5), align 8, !tbaa !9
  %19 = sext i8 %18 to i32
  %20 = icmp eq i32 %19, 1
  br i1 %20, label %21, label %66

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i64 -24
  store ptr %23, ptr %3, align 8, !tbaa !5
  %24 = load ptr, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 3), align 8, !tbaa !14
  %25 = load ptr, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 4), align 8, !tbaa !15
  %26 = icmp eq ptr %24, %25
  br i1 %26, label %27, label %28

27:                                               ; preds = %21
  store ptr null, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 4), align 8, !tbaa !15
  store ptr null, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 3), align 8, !tbaa !14
  br label %28

28:                                               ; preds = %27, %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %29 = load ptr, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 3), align 8, !tbaa !14
  store ptr %29, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store ptr null, ptr %5, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %60, %28
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = icmp ne ptr %31, null
  br i1 %32, label %33, label %65

33:                                               ; preds = %30
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = icmp eq ptr %34, %35
  br i1 %36, label %37, label %60

37:                                               ; preds = %33
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.collectorBlock, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = icmp eq ptr %40, null
  br i1 %41, label %42, label %46

42:                                               ; preds = %37
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.collectorBlock, ptr %43, i32 0, i32 0
  store ptr null, ptr %44, align 8, !tbaa !16
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %45, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 4), align 8, !tbaa !15
  br label %65

46:                                               ; preds = %37
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = icmp eq ptr %47, null
  br i1 %48, label %49, label %53

49:                                               ; preds = %46
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.collectorBlock, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !16
  store ptr %52, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 3), align 8, !tbaa !14
  br label %65

53:                                               ; preds = %46
  br label %54

54:                                               ; preds = %53
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.collectorBlock, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !16
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.collectorBlock, ptr %58, i32 0, i32 0
  store ptr %57, ptr %59, align 8, !tbaa !16
  br label %65

60:                                               ; preds = %33
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %61, ptr %5, align 8, !tbaa !5
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.collectorBlock, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !16
  store ptr %64, ptr %4, align 8, !tbaa !5
  br label %30, !llvm.loop !18

65:                                               ; preds = %54, %49, %42, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  br label %66

66:                                               ; preds = %65, %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %67 = load ptr, ptr %2, align 8, !tbaa !5
  %68 = getelementptr inbounds i8, ptr %67, i64 -24
  %69 = getelementptr inbounds %struct.collectorBlock, ptr %68, i32 0, i32 2
  %70 = load i32, ptr %69, align 8, !tbaa !20
  store i32 %70, ptr %6, align 4, !tbaa !21
  %71 = load ptr, ptr %2, align 8, !tbaa !5
  %72 = getelementptr inbounds i8, ptr %71, i64 -24
  call void @free(ptr noundef %72) #4
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = call i32 @remov(ptr noundef getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 2), ptr noundef %73)
  %75 = load i32, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 1), align 8, !tbaa !22
  %76 = add nsw i32 %75, -1
  store i32 %76, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 1), align 8, !tbaa !22
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %77

77:                                               ; preds = %66, %16, %9
  ret void
}

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

declare i32 @remov(ptr noundef, ptr noundef) #1

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


; 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

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

; Function Attrs: nounwind uwtable
define dso_local void @gc_free(ptr noundef %0) local_unnamed_addr #1 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %30, label %2

2:                                                ; preds = %1
  %3 = load ptr, ptr getelementptr inbounds (i8, ptr @GC, i64 16), align 8
  %4 = load i32, ptr getelementptr inbounds (i8, ptr @GC, i64 24), align 8
  %5 = tail call i32 @search(ptr %3, i32 %4, ptr noundef nonnull %0) #4
  %.not21 = icmp eq i32 %5, 0
  br i1 %.not21, label %30, label %6

6:                                                ; preds = %2
  %7 = load i8, ptr getelementptr inbounds (i8, ptr @GC, i64 48), align 8, !tbaa !5
  %8 = icmp eq i8 %7, 1
  br i1 %8, label %9, label %.loopexit

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %0, i64 -24
  %11 = load ptr, ptr getelementptr inbounds (i8, ptr @GC, i64 32), align 8, !tbaa !13
  %12 = load ptr, ptr getelementptr inbounds (i8, ptr @GC, i64 40), align 8, !tbaa !14
  %13 = icmp eq ptr %11, %12
  br i1 %13, label %14, label %.preheader

14:                                               ; preds = %9
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) getelementptr inbounds (i8, ptr @GC, i64 32), i8 0, i64 16, i1 false)
  br label %.preheader

.preheader:                                       ; preds = %14, %9
  br label %15

15:                                               ; preds = %.preheader, %16
  %.018 = phi ptr [ %.0, %16 ], [ null, %.preheader ]
  %.0.in = phi ptr [ %.0, %16 ], [ getelementptr inbounds (i8, ptr @GC, i64 32), %.preheader ]
  %.0 = load ptr, ptr %.0.in, align 8, !tbaa !15
  %.not22 = icmp eq ptr %.0, null
  br i1 %.not22, label %.loopexit, label %16

16:                                               ; preds = %15
  %17 = icmp eq ptr %.0, %10
  br i1 %17, label %18, label %15, !llvm.loop !16

18:                                               ; preds = %16
  %19 = load ptr, ptr %.0, align 8, !tbaa !18
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %22

21:                                               ; preds = %18
  store ptr null, ptr %.018, align 8, !tbaa !18
  store ptr %.018, ptr getelementptr inbounds (i8, ptr @GC, i64 40), align 8, !tbaa !14
  br label %.loopexit

22:                                               ; preds = %18
  %23 = icmp eq ptr %.018, null
  br i1 %23, label %24, label %25

24:                                               ; preds = %22
  store ptr %19, ptr getelementptr inbounds (i8, ptr @GC, i64 32), align 8, !tbaa !13
  br label %.loopexit

25:                                               ; preds = %22
  store ptr %19, ptr %.018, align 8, !tbaa !18
  br label %.loopexit

.loopexit:                                        ; preds = %15, %21, %24, %25, %6
  %26 = getelementptr inbounds i8, ptr %0, i64 -24
  tail call void @free(ptr noundef nonnull %26) #4
  %27 = tail call i32 @remov(ptr noundef nonnull getelementptr inbounds (i8, ptr @GC, i64 16), ptr noundef nonnull %0) #4
  %28 = load i32, ptr getelementptr inbounds (i8, ptr @GC, i64 8), align 8, !tbaa !20
  %29 = add nsw i32 %28, -1
  store i32 %29, ptr getelementptr inbounds (i8, ptr @GC, i64 8), align 8, !tbaa !20
  br label %30

30:                                               ; preds = %2, %1, %.loopexit
  ret void
}

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

declare i32 @remov(ptr noundef, ptr noundef) local_unnamed_addr #0

; 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 = { "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 = { 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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { 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, !8, i64 48}
!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 = !{!6, !7, i64 32}
!14 = !{!6, !7, i64 40}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!19, !7, i64 0}
!19 = !{!"collectorBlock", !7, i64 0, !7, i64 8, !10, i64 16, !8, i64 20}
!20 = !{!6, !10, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,SROAPass,SimplifyCFGPass,TailCallElimPass