; 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_actual_id = 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 allocsize(0)
declare noalias ptr @malloc(i64 noundef) #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #1

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

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

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

12:                                               ; preds = %3
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %49

13:                                               ; preds = %3
  %14 = load i64, ptr %5, align 8, !tbaa !5
  %15 = call noalias ptr @malloc(i64 noundef %14) #6
  store ptr %15, ptr %8, align 8, !tbaa !9
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %13
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %49

18:                                               ; preds = %13
  %19 = load i32, ptr @nmf_malloc_pool_position, align 4, !tbaa !11
  %20 = load i32, ptr @nmf_malloc_max, align 4, !tbaa !11
  %21 = icmp uge i32 %19, %20
  br i1 %21, label %22, label %33

22:                                               ; preds = %18
  %23 = load i32, ptr @nmf_malloc_max, align 4, !tbaa !11
  %24 = add i32 %23, 2
  store i32 %24, ptr @nmf_malloc_max, align 4, !tbaa !11
  %25 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %26 = load i32, ptr @nmf_malloc_max, align 4, !tbaa !11
  %27 = zext i32 %26 to i64
  %28 = mul i64 %27, 16
  %29 = call ptr @realloc(ptr noundef %25, i64 noundef %28) #7
  store ptr %29, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %32

31:                                               ; preds = %22
  call void @exit(i32 noundef -1) #8
  unreachable

32:                                               ; preds = %22
  call void @nmf_print_status()
  br label %33

33:                                               ; preds = %32, %18
  %34 = load ptr, ptr %8, align 8, !tbaa !9
  %35 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %36 = load i32, ptr @nmf_malloc_pool_position, align 4, !tbaa !11
  %37 = zext i32 %36 to i64
  %38 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %35, i64 %37
  %39 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %38, i32 0, i32 1
  store ptr %34, ptr %39, align 8, !tbaa !13
  %40 = load i32, ptr @nmf_malloc_actual_id, align 4, !tbaa !11
  %41 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %42 = load i32, ptr @nmf_malloc_pool_position, align 4, !tbaa !11
  %43 = zext i32 %42 to i64
  %44 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %41, i64 %43
  %45 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %44, i32 0, i32 0
  store i32 %40, ptr %45, align 8, !tbaa !15
  %46 = load i32, ptr @nmf_malloc_pool_position, align 4, !tbaa !11
  %47 = add i32 %46, 1
  store i32 %47, ptr @nmf_malloc_pool_position, align 4, !tbaa !11
  %48 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %48, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %49

49:                                               ; preds = %33, %17, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %50 = load ptr, ptr %4, align 8
  ret ptr %50
}

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #4

; Function Attrs: nounwind uwtable
declare dso_local void @nmf_print_status() #3

attributes #0 = { 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 #1 = { noreturn 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 allocsize(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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }
attributes #7 = { nounwind allocsize(1) }
attributes #8 = { noreturn 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 = !{!"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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !10, i64 8}
!14 = !{!"dummy_nmf_mem_chunk", !12, i64 0, !10, i64 8}
!15 = !{!14, !12, 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"

%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_actual_id = 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: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @nmf_malloc_internal(i64 noundef %0, ptr nocapture noundef readnone %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = load i32, ptr @nmf_initialized, align 4, !tbaa !5
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %30, label %5

5:                                                ; preds = %3
  %6 = tail call noalias ptr @malloc(i64 noundef %0) #4
  %7 = icmp eq ptr %6, null
  br i1 %7, label %30, label %8

8:                                                ; preds = %5
  %9 = load i32, ptr @nmf_malloc_pool_position, align 4, !tbaa !5
  %10 = load i32, ptr @nmf_malloc_max, align 4, !tbaa !5
  %.not5 = icmp ult i32 %9, %10
  br i1 %.not5, label %20, label %11

11:                                               ; preds = %8
  %12 = add i32 %10, 2
  store i32 %12, ptr @nmf_malloc_max, align 4, !tbaa !5
  %13 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %14 = zext i32 %12 to i64
  %15 = shl nuw nsw i64 %14, 4
  %16 = tail call ptr @realloc(ptr noundef %13, i64 noundef %15) #5
  store ptr %16, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %19

18:                                               ; preds = %11
  tail call void @exit(i32 noundef -1) #6
  unreachable

19:                                               ; preds = %11
  tail call void @nmf_print_status()
  %.pre = load i32, ptr @nmf_malloc_pool_position, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %19, %8
  %21 = phi i32 [ %.pre, %19 ], [ %9, %8 ]
  %22 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %23 = zext i32 %21 to i64
  %24 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %22, i64 %23, i32 1
  store ptr %6, ptr %24, align 8, !tbaa !11
  %25 = load i32, ptr @nmf_malloc_actual_id, align 4, !tbaa !5
  %26 = load ptr, ptr @nmf_malloc_pool, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.dummy_nmf_mem_chunk, ptr %26, i64 %23
  store i32 %25, ptr %27, align 8, !tbaa !13
  %28 = load i32, ptr @nmf_malloc_pool_position, align 4, !tbaa !5
  %29 = add i32 %28, 1
  store i32 %29, ptr @nmf_malloc_pool_position, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %5, %3, %20
  %.0 = phi ptr [ %6, %20 ], [ null, %3 ], [ null, %5 ]
  ret ptr %.0
}

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare dso_local void @nmf_print_status() local_unnamed_addr #2

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #1 = { noreturn 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 #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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) 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 #4 = { nounwind allocsize(0) }
attributes #5 = { nounwind allocsize(1) }
attributes #6 = { noreturn 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 = !{!12, !6, i64 0}


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