; 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_Header = type { i64, i32 }
%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
@.str.24 = external hidden unnamed_addr constant [92 x i8], align 1
@__func__.Longtail_MemTracer_Free = external hidden unnamed_addr constant [24 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.26 = external hidden unnamed_addr constant [18 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; 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) #2

declare void @Longtail_UnlockSpinLock(ptr noundef) #2

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

declare ptr @LongtailPrivate_LookupTable_Get(ptr noundef, i64 noundef) #2

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

12:                                               ; preds = %1
  store i32 1, ptr %4, align 4
  br label %67

13:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %14, ptr %5, align 8, !tbaa !5
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.MemTracer_Header, ptr %15, i32 -1
  store ptr %16, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.MemTracer_Header, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 8, !tbaa !9
  store i32 %19, ptr %6, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.MemTracer_Header, ptr %20, i32 0, i32 0
  %22 = load i64, ptr %21, align 8, !tbaa !14
  store i64 %22, ptr %7, align 8, !tbaa !15
  %23 = load i64, ptr %7, align 8, !tbaa !15
  %24 = icmp ne i64 %23, 4294967295
  br i1 %24, label %27, label %25

25:                                               ; preds = %13
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  call void (ptr, ptr, i32, ptr, i32, ptr, ...) @Longtail_CallLogger(ptr noundef @.str.24, ptr noundef @__func__.Longtail_MemTracer_Free, i32 noundef 394, ptr noundef %26, i32 noundef 3, ptr noundef @.str.25, ptr noundef @.str.26)
  store i32 1, ptr %4, align 4
  br label %66

27:                                               ; preds = %13
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %28, i8 -1, i64 16, i1 false)
  %29 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.MemTracer_Context, ptr %29, i32 0, i32 2
  %31 = load ptr, ptr %30, align 8, !tbaa !16
  call void @Longtail_LockSpinLock(ptr noundef %31)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %32 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.MemTracer_Context, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !18
  %35 = load i32, ptr %6, align 4, !tbaa !13
  %36 = zext i32 %35 to i64
  %37 = call ptr @LongtailPrivate_LookupTable_Get(ptr noundef %34, i64 noundef %36)
  store ptr %37, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %38 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.MemTracer_Context, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = load i32, ptr %40, align 4, !tbaa !13
  %42 = zext i32 %41 to i64
  %43 = getelementptr inbounds [128 x %struct.MemTracer_ContextStats], ptr %39, i64 0, i64 %42
  store ptr %43, ptr %9, align 8, !tbaa !5
  %44 = load i64, ptr %7, align 8, !tbaa !15
  %45 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.MemTracer_Context, ptr %45, i32 0, i32 7
  %47 = load i64, ptr %46, align 8, !tbaa !19
  %48 = sub i64 %47, %44
  store i64 %48, ptr %46, align 8, !tbaa !19
  %49 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.MemTracer_Context, ptr %49, i32 0, i32 4
  %51 = load i64, ptr %50, align 8, !tbaa !20
  %52 = add i64 %51, -1
  store i64 %52, ptr %50, align 8, !tbaa !20
  %53 = load i64, ptr %7, align 8, !tbaa !15
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.MemTracer_ContextStats, ptr %54, i32 0, i32 1
  %56 = load i64, ptr %55, align 8, !tbaa !21
  %57 = sub i64 %56, %53
  store i64 %57, ptr %55, align 8, !tbaa !21
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.MemTracer_ContextStats, ptr %58, i32 0, i32 4
  %60 = load i64, ptr %59, align 8, !tbaa !23
  %61 = add i64 %60, -1
  store i64 %61, ptr %59, align 8, !tbaa !23
  %62 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.MemTracer_Context, ptr %62, i32 0, i32 2
  %64 = load ptr, ptr %63, align 8, !tbaa !16
  call void @Longtail_UnlockSpinLock(ptr noundef %64)
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %65) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  store i32 0, ptr %4, align 4
  br label %66

66:                                               ; preds = %27, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  br label %67

67:                                               ; preds = %66, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  %68 = load i32, ptr %4, align 4
  switch i32 %68, label %70 [
    i32 0, label %69
    i32 1, label %69
  ]

69:                                               ; preds = %67, %67
  ret void

70:                                               ; preds = %67
  unreachable
}

declare void @Longtail_CallLogger(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ...) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 "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 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 #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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 8}
!10 = !{!"MemTracer_Header", !11, i64 0, !12, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!10, !11, i64 0}
!15 = !{!11, !11, i64 0}
!16 = !{!17, !6, i64 9224}
!17 = !{!"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}
!18 = !{!17, !6, i64 0}
!19 = !{!17, !11, i64 9264}
!20 = !{!17, !11, i64 9240}
!21 = !{!22, !11, i64 8}
!22 = !{!"MemTracer_ContextStats", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !11, i64 56, !6, i64 64}
!23 = !{!22, !11, i64 32}


; 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
@.str.24 = external hidden unnamed_addr constant [92 x i8], align 1
@__func__.Longtail_MemTracer_Free = external hidden unnamed_addr constant [24 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [43 x i8], align 1
@.str.26 = external hidden unnamed_addr constant [18 x i8], align 1

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

declare void @Longtail_LockSpinLock(ptr noundef) local_unnamed_addr #1

declare void @Longtail_UnlockSpinLock(ptr noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @Longtail_MemTracer_Free(ptr noundef %0) local_unnamed_addr #3 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %36, label %3

3:                                                ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 -16
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %.not = icmp eq i64 %5, 4294967295
  br i1 %.not, label %6, label %7

6:                                                ; preds = %3
  tail call void (ptr, ptr, i32, ptr, i32, ptr, ...) @Longtail_CallLogger(ptr noundef nonnull @.str.24, ptr noundef nonnull @__func__.Longtail_MemTracer_Free, i32 noundef 394, ptr noundef null, i32 noundef 3, ptr noundef nonnull @.str.25, ptr noundef nonnull @.str.26) #4
  br label %36

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 -8
  %9 = load i32, ptr %8, align 8, !tbaa !11
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %4, i8 -1, i64 16, i1 false)
  %10 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %10, i64 9224
  %12 = load ptr, ptr %11, align 8, !tbaa !14
  tail call void @Longtail_LockSpinLock(ptr noundef %12) #4
  %13 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !12
  %14 = load ptr, ptr %13, align 8, !tbaa !16
  %15 = zext i32 %9 to i64
  %16 = tail call ptr @LongtailPrivate_LookupTable_Get(ptr noundef %14, i64 noundef %15) #4
  %17 = load ptr, ptr @gMemTracer_Context, align 8, !tbaa !12
  %18 = getelementptr inbounds i8, ptr %17, i64 8
  %19 = load i32, ptr %16, align 4, !tbaa !17
  %20 = zext i32 %19 to i64
  %21 = getelementptr inbounds [128 x %struct.MemTracer_ContextStats], ptr %18, i64 0, i64 %20
  %22 = getelementptr inbounds i8, ptr %17, i64 9264
  %23 = load i64, ptr %22, align 8, !tbaa !18
  %24 = sub i64 %23, %5
  store i64 %24, ptr %22, align 8, !tbaa !18
  %25 = getelementptr inbounds i8, ptr %17, i64 9240
  %26 = load i64, ptr %25, align 8, !tbaa !19
  %27 = add i64 %26, -1
  store i64 %27, ptr %25, align 8, !tbaa !19
  %28 = getelementptr inbounds i8, ptr %21, i64 8
  %29 = load i64, ptr %28, align 8, !tbaa !20
  %30 = sub i64 %29, %5
  store i64 %30, ptr %28, align 8, !tbaa !20
  %31 = getelementptr inbounds i8, ptr %21, i64 32
  %32 = load i64, ptr %31, align 8, !tbaa !22
  %33 = add i64 %32, -1
  store i64 %33, ptr %31, align 8, !tbaa !22
  %34 = getelementptr inbounds i8, ptr %17, i64 9224
  %35 = load ptr, ptr %34, align 8, !tbaa !14
  tail call void @Longtail_UnlockSpinLock(ptr noundef %35) #4
  tail call void @free(ptr noundef nonnull %4) #4
  br label %36

36:                                               ; preds = %6, %7, %1
  ret void
}

declare void @Longtail_CallLogger(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ...) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { 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 = { 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 #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, !7, i64 0}
!6 = !{!"MemTracer_Header", !7, i64 0, !10, i64 8}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!15, !13, i64 9224}
!15 = !{!"MemTracer_Context", !13, i64 0, !8, i64 8, !13, i64 9224, !7, i64 9232, !7, i64 9240, !7, i64 9248, !7, i64 9256, !7, i64 9264, !7, i64 9272, !10, i64 9280}
!16 = !{!15, !13, i64 0}
!17 = !{!10, !10, i64 0}
!18 = !{!15, !7, i64 9264}
!19 = !{!15, !7, i64 9240}
!20 = !{!21, !7, i64 8}
!21 = !{!"MemTracer_ContextStats", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !13, i64 64}
!22 = !{!21, !7, i64 32}


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