; 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: 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 void @Longtail_LockSpinLock(ptr noundef) #1

declare void @Longtail_UnlockSpinLock(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @Longtail_MemTracer_GetAllocationCount(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !9
  %7 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.MemTracer_Context, ptr %7, i32 0, i32 2
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  call void @Longtail_LockSpinLock(ptr noundef %9)
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %39

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 0
  %15 = load i8, ptr %14, align 1, !tbaa !14
  %16 = sext i8 %15 to i32
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %12
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = call i32 @MemTracer_ContextIdHash(ptr noundef %19)
  br label %22

21:                                               ; preds = %12
  br label %22

22:                                               ; preds = %21, %18
  %23 = phi i32 [ %20, %18 ], [ 0, %21 ]
  store i32 %23, ptr %4, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %24 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.MemTracer_Context, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !16
  %27 = load i32, ptr %4, align 4, !tbaa !15
  %28 = zext i32 %27 to i64
  %29 = call ptr @LongtailPrivate_LookupTable_Get(ptr noundef %26, i64 noundef %28)
  store ptr %29, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %30 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.MemTracer_Context, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load i32, ptr %32, align 4, !tbaa !15
  %34 = zext i32 %33 to i64
  %35 = getelementptr inbounds [128 x %struct.MemTracer_ContextStats], ptr %31, i64 0, i64 %34
  store ptr %35, ptr %6, align 8, !tbaa !5
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.MemTracer_ContextStats, ptr %36, i32 0, i32 4
  %38 = load i64, ptr %37, align 8, !tbaa !17
  store i64 %38, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %43

39:                                               ; preds = %1
  %40 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.MemTracer_Context, ptr %40, i32 0, i32 4
  %42 = load i64, ptr %41, align 8, !tbaa !19
  store i64 %42, ptr %3, align 8, !tbaa !9
  br label %43

43:                                               ; preds = %39, %22
  %44 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.MemTracer_Context, ptr %44, i32 0, i32 2
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  call void @Longtail_UnlockSpinLock(ptr noundef %46)
  %47 = load i64, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i64 %47
}

; Function Attrs: nounwind uwtable
declare hidden i32 @MemTracer_ContextIdHash(ptr noundef) #2

declare ptr @LongtailPrivate_LookupTable_Get(ptr noundef, i64 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 }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 9224}
!12 = !{!"MemTracer_Context", !6, i64 0, !7, i64 8, !6, i64 9224, !10, i64 9232, !10, i64 9240, !10, i64 9248, !10, i64 9256, !10, i64 9264, !10, i64 9272, !13, i64 9280}
!13 = !{!"int", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !6, i64 0}
!17 = !{!18, !10, i64 32}
!18 = !{!"MemTracer_ContextStats", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !6, i64 64}
!19 = !{!12, !10, i64 9240}


; 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.MemTracer_ContextStats = type { i64, i64, i64, i64, i64, i64, i64, i64, ptr }

@gMemTracer_Context = external hidden local_unnamed_addr global ptr, align 8

declare void @Longtail_LockSpinLock(ptr noundef) local_unnamed_addr #0

declare void @Longtail_UnlockSpinLock(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @Longtail_MemTracer_GetAllocationCount(ptr noundef %0) local_unnamed_addr #1 {
  %2 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 9224
  %4 = load ptr, ptr %3, align 8, !tbaa !9
  tail call void @Longtail_LockSpinLock(ptr noundef %4) #2
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %20, label %5

5:                                                ; preds = %1
  %6 = load i8, ptr %0, align 1, !tbaa !13
  %.not7 = icmp eq i8 %6, 0
  br i1 %.not7, label %10, label %7

7:                                                ; preds = %5
  %8 = tail call i32 @MemTracer_ContextIdHash(ptr noundef nonnull %0)
  %9 = zext i32 %8 to i64
  br label %10

10:                                               ; preds = %5, %7
  %11 = phi i64 [ %9, %7 ], [ 0, %5 ]
  %12 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %14 = tail call ptr @LongtailPrivate_LookupTable_Get(ptr noundef %13, i64 noundef %11) #2
  %15 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 8
  %17 = load i32, ptr %14, align 4, !tbaa !15
  %18 = zext i32 %17 to i64
  %19 = getelementptr inbounds [128 x %struct.MemTracer_ContextStats], ptr %16, i64 0, i64 %18, i32 4
  br label %23

20:                                               ; preds = %1
  %21 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %21, i64 9240
  br label %23

23:                                               ; preds = %20, %10
  %24 = phi ptr [ %15, %10 ], [ %21, %20 ]
  %.0.in = phi ptr [ %19, %10 ], [ %22, %20 ]
  %.0 = load i64, ptr %.0.in, align 8, !tbaa !16
  %25 = getelementptr inbounds i8, ptr %24, i64 9224
  %26 = load ptr, ptr %25, align 8, !tbaa !9
  tail call void @Longtail_UnlockSpinLock(ptr noundef %26) #2
  ret i64 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i32 @MemTracer_ContextIdHash(ptr noundef) local_unnamed_addr #1

declare ptr @LongtailPrivate_LookupTable_Get(ptr noundef, i64 noundef) local_unnamed_addr #0

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 = { 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 9224}
!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}
!13 = !{!7, !7, i64 0}
!14 = !{!10, !6, i64 0}
!15 = !{!12, !12, i64 0}
!16 = !{!11, !11, i64 0}


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