; 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_malloc_max = external dso_local global i32, align 4
@nmf_initialized = external dso_local global i32, align 4

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

6:                                                ; preds = %0
  store i32 0, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %33

7:                                                ; preds = %0
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %28, %7
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = load i32, ptr @nmf_malloc_max, align 4, !tbaa !5
  %11 = icmp ult i32 %9, %10
  br i1 %11, label %12, label %31

12:                                               ; preds = %8
  %13 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = zext i32 %14 to i64
  %16 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %13, i64 %15
  %17 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %16, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %27

20:                                               ; preds = %12
  %21 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = zext i32 %22 to i64
  %24 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %21, i64 %23
  %25 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  call void @free(ptr noundef %26) #3
  br label %27

27:                                               ; preds = %20, %12
  br label %28

28:                                               ; preds = %27
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = add i32 %29, 1
  store i32 %30, ptr %2, align 4, !tbaa !5
  br label %8, !llvm.loop !13

31:                                               ; preds = %8
  %32 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  call void @free(ptr noundef %32) #3
  store i32 0, ptr @nmf_initialized, align 4, !tbaa !5
  store i32 0, ptr @nmf_malloc_pool_position, align 4, !tbaa !5
  store i32 1, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %33

33:                                               ; preds = %31, %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  %34 = load i32, ptr %1, align 4
  ret i32 %34
}

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

; Function Attrs: nounwind
declare void @free(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 = { 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 #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 = !{!"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, !10, i64 8}
!12 = !{!"dummy_nmf_mem_chunk", !6, i64 0, !10, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"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_malloc_max = external dso_local local_unnamed_addr global i32, align 4
@nmf_initialized = external dso_local local_unnamed_addr global i32, align 4

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

.preheader:                                       ; preds = %0
  %2 = load i32, ptr @nmf_malloc_max, align 4, !tbaa !5
  %.not9 = icmp eq i32 %2, 0
  %.pre13 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  br i1 %.not9, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %8
  %3 = phi i32 [ %9, %8 ], [ %2, %.preheader ]
  %4 = phi ptr [ %10, %8 ], [ %.pre13, %.preheader ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %8 ], [ 0, %.preheader ]
  %5 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %4, i64 %indvars.iv, i32 1
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %.not7 = icmp eq ptr %6, null
  br i1 %.not7, label %8, label %7

7:                                                ; preds = %.lr.ph
  tail call void @free(ptr noundef nonnull %6) #2
  %.pre = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %.pre12 = load i32, ptr @nmf_malloc_max, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %.lr.ph, %7
  %9 = phi i32 [ %3, %.lr.ph ], [ %.pre12, %7 ]
  %10 = phi ptr [ %4, %.lr.ph ], [ %.pre, %7 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %11 = zext i32 %9 to i64
  %12 = icmp ult i64 %indvars.iv.next, %11
  br i1 %12, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %8, %.preheader
  %13 = phi ptr [ %.pre13, %.preheader ], [ %10, %8 ]
  tail call void @free(ptr noundef %13) #2
  store i32 0, ptr @nmf_initialized, align 4, !tbaa !5
  store i32 0, ptr @nmf_malloc_pool_position, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %0, %._crit_edge
  %.05 = phi i32 [ 1, %._crit_edge ], [ 0, %0 ]
  ret i32 %.05
}

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #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 = { 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 #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, !10, i64 8}
!12 = !{!"dummy_nmf_mem_chunk", !6, i64 0, !10, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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