; 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
@.str.1 = external hidden unnamed_addr constant [15 x i8], align 1

; 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: 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: nounwind uwtable
declare dso_local void @gc_run() #2

; Function Attrs: nounwind uwtable
define dso_local ptr @gc_malloc(i32 noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.collectorBlock, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = add i64 24, %10
  %12 = call noalias ptr @malloc(i64 noundef %11) #7
  store ptr %12, ptr %4, align 8, !tbaa !9
  %13 = load ptr, ptr %4, align 8, !tbaa !9
  %14 = icmp ne ptr %13, null
  br i1 %14, label %25, label %15

15:                                               ; preds = %1
  call void @gc_run()
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = add i64 24, %17
  %19 = call noalias ptr @malloc(i64 noundef %18) #7
  store ptr %19, ptr %5, align 8, !tbaa !9
  %20 = load ptr, ptr %5, align 8, !tbaa !9
  %21 = icmp ne ptr %20, null
  br i1 %21, label %24, label %22

22:                                               ; preds = %15
  %23 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  call void @exit(i32 noundef 0) #8
  unreachable

24:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  br label %25

25:                                               ; preds = %24, %1
  call void @llvm.lifetime.start.p0(i64 24, ptr %6) #6
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = getelementptr inbounds %struct.collectorBlock, ptr %6, i32 0, i32 2
  store i32 %26, ptr %27, align 8, !tbaa !11
  %28 = getelementptr inbounds %struct.collectorBlock, ptr %6, i32 0, i32 3
  store i8 0, ptr %28, align 4, !tbaa !13
  %29 = getelementptr inbounds %struct.collectorBlock, ptr %6, i32 0, i32 0
  store ptr null, ptr %29, align 8, !tbaa !14
  %30 = getelementptr inbounds %struct.collectorBlock, ptr %6, i32 0, i32 1
  store ptr null, ptr %30, align 8, !tbaa !15
  %31 = load ptr, ptr %4, align 8, !tbaa !9
  %32 = icmp ne ptr %31, null
  br i1 %32, label %34, label %33

33:                                               ; preds = %25
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %59

34:                                               ; preds = %25
  %35 = load ptr, ptr %4, align 8, !tbaa !9
  %36 = getelementptr inbounds i8, ptr %35, i64 24
  %37 = call i32 @insert(ptr noundef getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 2), ptr noundef %36)
  %38 = load i32, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 1), align 8, !tbaa !16
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 1), align 8, !tbaa !16
  %40 = load ptr, ptr %4, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %40, ptr align 8 %6, i64 24, i1 false)
  %41 = load i8, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 5), align 8, !tbaa !20
  %42 = sext i8 %41 to i32
  %43 = icmp eq i32 %42, 1
  br i1 %43, label %44, label %56

44:                                               ; preds = %34
  %45 = load ptr, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 3), align 8, !tbaa !21
  %46 = icmp eq ptr %45, null
  br i1 %46, label %47, label %49

47:                                               ; preds = %44
  %48 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %48, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 4), align 8, !tbaa !22
  store ptr %48, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 3), align 8, !tbaa !21
  br label %55

49:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  %50 = load ptr, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 4), align 8, !tbaa !22
  store ptr %50, ptr %8, align 8, !tbaa !9
  %51 = load ptr, ptr %4, align 8, !tbaa !9
  %52 = load ptr, ptr %8, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.collectorBlock, ptr %52, i32 0, i32 0
  store ptr %51, ptr %53, align 8, !tbaa !14
  %54 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %54, ptr getelementptr inbounds (%struct.gc, ptr @GC, i32 0, i32 4), align 8, !tbaa !22
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  br label %55

55:                                               ; preds = %49, %47
  br label %56

56:                                               ; preds = %55, %34
  %57 = load ptr, ptr %4, align 8, !tbaa !9
  %58 = getelementptr inbounds i8, ptr %57, i64 24
  store ptr %58, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %59

59:                                               ; preds = %56, %33
  call void @llvm.lifetime.end.p0(i64 24, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  %60 = load ptr, ptr %2, align 8
  ret ptr %60
}

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #3

declare i32 @printf(ptr noundef, ...) #4

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #5

declare i32 @insert(ptr noundef, ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 allocsize(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 #4 = { "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 #5 = { noreturn 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }
attributes #8 = { noreturn 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 = !{!"int", !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, !6, i64 16}
!12 = !{!"collectorBlock", !10, i64 0, !10, i64 8, !6, i64 16, !7, i64 20}
!13 = !{!12, !7, i64 20}
!14 = !{!12, !10, i64 0}
!15 = !{!12, !10, i64 8}
!16 = !{!17, !6, i64 8}
!17 = !{!"gc", !10, i64 0, !6, i64 8, !18, i64 16, !10, i64 32, !10, i64 40, !7, i64 48}
!18 = !{!"", !19, i64 0}
!19 = !{!"hash_map", !10, i64 0, !6, i64 8}
!20 = !{!17, !7, i64 48}
!21 = !{!17, !10, i64 32}
!22 = !{!17, !10, i64 40}


; 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
declare dso_local void @gc_run() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @gc_malloc(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = add nsw i64 %2, 24
  %4 = tail call noalias ptr @malloc(i64 noundef %3) #4
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %5, label %.critedge

5:                                                ; preds = %1
  tail call void @gc_run()
  br label %18

.critedge:                                        ; preds = %1
  %6 = getelementptr inbounds i8, ptr %4, i64 24
  %7 = tail call i32 @insert(ptr noundef nonnull getelementptr inbounds (i8, ptr @GC, i64 16), ptr noundef nonnull %6) #5
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @GC, i64 8), align 8, !tbaa !5
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr getelementptr inbounds (i8, ptr @GC, i64 8), align 8, !tbaa !5
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %4, i64 16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  store i32 %0, ptr %.sroa.5.0..sroa_idx, align 1
  %.sroa.6.0..sroa_idx = getelementptr inbounds i8, ptr %4, i64 20
  store i8 0, ptr %.sroa.6.0..sroa_idx, align 1
  %10 = load i8, ptr getelementptr inbounds (i8, ptr @GC, i64 48), align 8, !tbaa !13
  %11 = icmp eq i8 %10, 1
  br i1 %11, label %12, label %18

12:                                               ; preds = %.critedge
  %13 = load ptr, ptr getelementptr inbounds (i8, ptr @GC, i64 32), align 8, !tbaa !14
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %16

15:                                               ; preds = %12
  store ptr %4, ptr getelementptr inbounds (i8, ptr @GC, i64 40), align 8, !tbaa !15
  store ptr %4, ptr getelementptr inbounds (i8, ptr @GC, i64 32), align 8, !tbaa !14
  br label %18

16:                                               ; preds = %12
  %17 = load ptr, ptr getelementptr inbounds (i8, ptr @GC, i64 40), align 8, !tbaa !15
  store ptr %4, ptr %17, align 8, !tbaa !16
  store ptr %4, ptr getelementptr inbounds (i8, ptr @GC, i64 40), align 8, !tbaa !15
  br label %18

18:                                               ; preds = %5, %.critedge, %16, %15
  %.0 = phi ptr [ null, %5 ], [ %6, %15 ], [ %6, %16 ], [ %6, %.critedge ]
  ret ptr %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

declare i32 @insert(ptr noundef, ptr noundef) local_unnamed_addr #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 = { 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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nounwind allocsize(0) }
attributes #5 = { 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 = !{!"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, !8, i64 48}
!14 = !{!6, !7, i64 32}
!15 = !{!6, !7, i64 40}
!16 = !{!17, !7, i64 0}
!17 = !{!"collectorBlock", !7, i64 0, !7, i64 8, !10, i64 16, !8, i64 20}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass