; LLVM IR from DanEngelbrecht/longtail :: lib/memtracer (longtail_memtracer)

; 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.MemTracer_Context = type { ptr, [128 x %struct.MemTracer_ContextStats], ptr, i64, i64, i64, i64, i64, i64, i32 }
%struct.MemTracer_ContextStats = type { i64, i64, i64, i64, i64, i64, i64, i64, ptr }

@gMemTracer_Context = external hidden global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @Longtail_MemTracer_Init() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #5
  %5 = call i64 @LongtailPrivate_LookupTable_GetSize(i32 noundef 128)
  store i64 %5, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  %6 = load i64, ptr %1, align 8, !tbaa !5
  %7 = add i64 9288, %6
  %8 = call i64 (...) @Longtail_GetSpinLockSize()
  %9 = add i64 %7, %8
  store i64 %9, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %10 = load i64, ptr %2, align 8, !tbaa !5
  %11 = call noalias ptr @malloc(i64 noundef %10) #6
  store ptr %11, ptr %3, align 8, !tbaa !9
  %12 = load ptr, ptr %3, align 8, !tbaa !9
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %0
  store i32 1, ptr %4, align 4
  br label %32

15:                                               ; preds = %0
  %16 = load ptr, ptr %3, align 8, !tbaa !9
  %17 = load i64, ptr %2, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 1 %16, i8 0, i64 %17, i1 false)
  %18 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %18, ptr @gMemTracer_Context, align 8, !tbaa !9
  %19 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.MemTracer_Context, ptr %19, i64 1
  %21 = call ptr @LongtailPrivate_LookupTable_Create(ptr noundef %20, i32 noundef 128, ptr noundef null)
  %22 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.MemTracer_Context, ptr %22, i32 0, i32 0
  store ptr %21, ptr %23, align 8, !tbaa !11
  %24 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.MemTracer_Context, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  %27 = load i64, ptr %1, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %26, i64 %27
  %29 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.MemTracer_Context, ptr %29, i32 0, i32 2
  %31 = call i32 @Longtail_CreateSpinLock(ptr noundef %28, ptr noundef %30)
  store i32 0, ptr %4, align 4
  br label %32

32:                                               ; preds = %15, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #5
  %33 = load i32, ptr %4, align 4
  switch i32 %33, label %35 [
    i32 0, label %34
    i32 1, label %34
  ]

34:                                               ; preds = %32, %32
  ret void

35:                                               ; preds = %32
  unreachable
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare i64 @LongtailPrivate_LookupTable_GetSize(i32 noundef) #2

declare i64 @Longtail_GetSpinLockSize(...) #2

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }

!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 = !{!"long", !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, !10, i64 0}
!12 = !{!"MemTracer_Context", !10, i64 0, !7, i64 8, !10, i64 9224, !6, i64 9232, !6, i64 9240, !6, i64 9248, !6, i64 9256, !6, i64 9264, !6, i64 9272, !13, i64 9280}
!13 = !{!"int", !7, i64 0}


; 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"

@gMemTracer_Context = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @Longtail_MemTracer_Init() local_unnamed_addr #0 {
  %1 = tail call i64 @LongtailPrivate_LookupTable_GetSize(i32 noundef 128) #3
  %2 = add i64 %1, 9288
  %3 = tail call i64 (...) @Longtail_GetSpinLockSize() #3
  %4 = add i64 %2, %3
  %calloc = tail call ptr @calloc(i64 1, i64 %4)
  %5 = icmp eq ptr %calloc, null
  br i1 %5, label %15, label %6

6:                                                ; preds = %0
  store ptr %calloc, ptr @gMemTracer_Context, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %calloc, i64 9288
  %8 = tail call ptr @LongtailPrivate_LookupTable_Create(ptr noundef nonnull %7, i32 noundef 128, ptr noundef null) #3
  %9 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  store ptr %8, ptr %9, align 8, !tbaa !9
  %10 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = getelementptr inbounds i8, ptr %11, i64 %1
  %13 = getelementptr inbounds i8, ptr %10, i64 9224
  %14 = tail call i32 @Longtail_CreateSpinLock(ptr noundef %12, ptr noundef nonnull %13) #3
  br label %15

15:                                               ; preds = %0, %6
  ret void
}

declare i64 @LongtailPrivate_LookupTable_GetSize(i32 noundef) local_unnamed_addr #1

declare i64 @Longtail_GetSpinLockSize(...) local_unnamed_addr #1

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

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

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

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 = { nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
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, !6, i64 0}
!10 = !{!"MemTracer_Context", !6, i64 0, !7, i64 8, !6, i64 9224, !11, i64 9232, !11, i64 9240, !11, i64 9248, !11, i64 9256, !11, i64 9264, !11, i64 9272, !12, i64 9280}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass