; LLVM IR from Meow8p/nmf :: src (libnmf)

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

@nmf_malloc_pool = external dso_local global ptr, align 8
@nmf_malloc_pool_position = external dso_local global i32, align 4
@nmf_initialized = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [57 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: nounwind uwtable
declare dso_local i32 @nmf_free_internal(ptr noundef, ptr noundef, i32 noundef) #1

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

8:                                                ; preds = %1
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %38

9:                                                ; preds = %1
  %10 = load i32, ptr @nmf_malloc_pool_position, align 4, !tbaa !5
  %11 = sub i32 %10, 1
  store i32 %11, ptr %4, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %34, %9
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = icmp sge i32 %13, 0
  br i1 %14, label %15, label %37

15:                                               ; preds = %12
  %16 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %16, i64 %18
  %20 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %19, i32 0, i32 0
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = icmp eq i32 %21, %22
  br i1 %23, label %24, label %32

24:                                               ; preds = %15
  %25 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %25, i64 %27
  %29 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = call i32 @nmf_free_internal(ptr noundef %30, ptr noundef @.str, i32 noundef 172)
  br label %33

32:                                               ; preds = %15
  br label %37

33:                                               ; preds = %24
  br label %34

34:                                               ; preds = %33
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = add nsw i32 %35, -1
  store i32 %36, ptr %4, align 4, !tbaa !5
  br label %12, !llvm.loop !14

37:                                               ; preds = %32, %12
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %38

38:                                               ; preds = %37, %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %39 = load i32, ptr %2, align 4
  ret i32 %39
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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 0}
!12 = !{!"dummy_nmf_mem_chunk", !6, i64 0, !10, i64 8}
!13 = !{!12, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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

@nmf_malloc_pool = external dso_local local_unnamed_addr global ptr, align 8
@nmf_malloc_pool_position = external dso_local local_unnamed_addr global i32, align 4
@nmf_initialized = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [57 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @nmf_free_internal(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @nmf_free_allocation_id(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @nmf_initialized, align 4, !tbaa !5
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %.loopexit, label %3

3:                                                ; preds = %1
  %4 = load i32, ptr @nmf_malloc_pool_position, align 4, !tbaa !5
  %.07 = add i32 %4, -1
  %5 = icmp sgt i32 %.07, -1
  br i1 %5, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %3, %11
  %.08 = phi i32 [ %.0, %11 ], [ %.07, %3 ]
  %6 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %7 = zext nneg i32 %.08 to i64
  %8 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %6, i64 %7
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = icmp eq i32 %9, %0
  br i1 %10, label %11, label %.loopexit

11:                                               ; preds = %.lr.ph
  %12 = getelementptr inbounds i8, ptr %8, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = tail call i32 @nmf_free_internal(ptr noundef %13, ptr noundef nonnull @.str, i32 noundef 172)
  %.0 = add nsw i32 %.08, -1
  %15 = icmp sgt i32 %.08, 0
  br i1 %15, label %.lr.ph, label %.loopexit, !llvm.loop !14

.loopexit:                                        ; preds = %.lr.ph, %11, %3, %1
  %.06 = phi i32 [ 0, %1 ], [ 1, %3 ], [ 1, %11 ], [ 1, %.lr.ph ]
  ret i32 %.06
}

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

!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 0}
!12 = !{!"dummy_nmf_mem_chunk", !6, i64 0, !10, i64 8}
!13 = !{!12, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass