; LLVM IR from Dimandragon/non-parametric-dsp :: alglib-cpp/src/ap.ll (ZN11alglib_impl14aligned_mallocEmm)

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

@_ZN11alglib_impl14_alloc_counterE = external dso_local global i64, align 8
@_ZN11alglib_impl20_alloc_counter_totalE = external dso_local global i64, align 8
@_ZN11alglib_impl18_use_alloc_counterE = external dso_local global i8, align 1
@_ZN11alglib_impl16_dbg_alloc_totalE = external dso_local global i64, align 8
@_ZN11alglib_impl17_use_dbg_countersE = external dso_local global i8, align 1
@_ZN11alglib_impl21_force_malloc_failureE = external dso_local global i8, align 1
@_ZN11alglib_impl21_malloc_failure_afterE = external dso_local global i64, 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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN11alglib_impl8ae_alignEPvm(ptr noundef, i64 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN11alglib_impl24ae_optional_atomic_add_iEPll(ptr noundef, i64 noundef) #1

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

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

13:                                               ; preds = %2
  %14 = load i8, ptr @_ZN11alglib_impl21_force_malloc_failureE, align 1, !tbaa !11, !range !13, !noundef !14
  %15 = trunc i8 %14 to i1
  br i1 %15, label %16, label %17

16:                                               ; preds = %13
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %64

17:                                               ; preds = %13
  %18 = load i64, ptr @_ZN11alglib_impl21_malloc_failure_afterE, align 8, !tbaa !5
  %19 = icmp sgt i64 %18, 0
  br i1 %19, label %20, label %25

20:                                               ; preds = %17
  %21 = load i64, ptr @_ZN11alglib_impl20_alloc_counter_totalE, align 8, !tbaa !5
  %22 = load i64, ptr @_ZN11alglib_impl21_malloc_failure_afterE, align 8, !tbaa !5
  %23 = icmp sge i64 %21, %22
  br i1 %23, label %24, label %25

24:                                               ; preds = %20
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %64

25:                                               ; preds = %20, %17
  %26 = load i64, ptr %5, align 8, !tbaa !5
  %27 = icmp eq i64 %26, 0
  br i1 %27, label %28, label %29

28:                                               ; preds = %25
  store i64 64, ptr %5, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %28, %25
  %30 = load i64, ptr %4, align 8, !tbaa !5
  %31 = add i64 16, %30
  store i64 %31, ptr %8, align 8, !tbaa !5
  %32 = load i64, ptr %5, align 8, !tbaa !5
  %33 = icmp ugt i64 %32, 1
  br i1 %33, label %34, label %39

34:                                               ; preds = %29
  %35 = load i64, ptr %5, align 8, !tbaa !5
  %36 = sub i64 %35, 1
  %37 = load i64, ptr %8, align 8, !tbaa !5
  %38 = add i64 %37, %36
  store i64 %38, ptr %8, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %34, %29
  %40 = load i64, ptr %8, align 8, !tbaa !5
  %41 = call noalias ptr @malloc(i64 noundef %40) #4
  store ptr %41, ptr %7, align 8, !tbaa !9
  %42 = load ptr, ptr %7, align 8, !tbaa !9
  %43 = icmp eq ptr %42, null
  br i1 %43, label %44, label %45

44:                                               ; preds = %39
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %64

45:                                               ; preds = %39
  %46 = load ptr, ptr %7, align 8, !tbaa !9
  %47 = getelementptr inbounds i8, ptr %46, i64 16
  store ptr %47, ptr %6, align 8, !tbaa !9
  %48 = load ptr, ptr %6, align 8, !tbaa !9
  %49 = load i64, ptr %5, align 8, !tbaa !5
  %50 = call noundef ptr @_ZN11alglib_impl8ae_alignEPvm(ptr noundef %48, i64 noundef %49)
  store ptr %50, ptr %6, align 8, !tbaa !9
  %51 = load ptr, ptr %7, align 8, !tbaa !9
  %52 = load ptr, ptr %6, align 8, !tbaa !9
  %53 = getelementptr inbounds i8, ptr %52, i64 -8
  store ptr %51, ptr %53, align 8, !tbaa !9
  %54 = load i8, ptr @_ZN11alglib_impl18_use_alloc_counterE, align 1, !tbaa !11, !range !13, !noundef !14
  %55 = trunc i8 %54 to i1
  br i1 %55, label %56, label %57

56:                                               ; preds = %45
  call void @_ZN11alglib_impl24ae_optional_atomic_add_iEPll(ptr noundef @_ZN11alglib_impl14_alloc_counterE, i64 noundef 1)
  call void @_ZN11alglib_impl24ae_optional_atomic_add_iEPll(ptr noundef @_ZN11alglib_impl20_alloc_counter_totalE, i64 noundef 1)
  br label %57

57:                                               ; preds = %56, %45
  %58 = load i8, ptr @_ZN11alglib_impl17_use_dbg_countersE, align 1, !tbaa !11, !range !13, !noundef !14
  %59 = trunc i8 %58 to i1
  br i1 %59, label %60, label %62

60:                                               ; preds = %57
  %61 = load i64, ptr %4, align 8, !tbaa !5
  call void @_ZN11alglib_impl24ae_optional_atomic_add_iEPll(ptr noundef @_ZN11alglib_impl16_dbg_alloc_totalE, i64 noundef %61)
  br label %62

62:                                               ; preds = %60, %57
  %63 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %63, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %64

64:                                               ; preds = %62, %44, %24, %16, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %65 = load ptr, ptr %3, align 8
  ret ptr %65
}

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}


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

@_ZN11alglib_impl14_alloc_counterE = external dso_local global i64, align 8
@_ZN11alglib_impl20_alloc_counter_totalE = external dso_local global i64, align 8
@_ZN11alglib_impl18_use_alloc_counterE = external dso_local local_unnamed_addr global i8, align 1
@_ZN11alglib_impl16_dbg_alloc_totalE = external dso_local global i64, align 8
@_ZN11alglib_impl17_use_dbg_countersE = external dso_local local_unnamed_addr global i8, align 1
@_ZN11alglib_impl21_force_malloc_failureE = external dso_local local_unnamed_addr global i8, align 1
@_ZN11alglib_impl21_malloc_failure_afterE = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN11alglib_impl8ae_alignEPvm(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN11alglib_impl24ae_optional_atomic_add_iEPll(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN11alglib_impl14aligned_mallocEmm(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i64 %0, 0
  br i1 %3, label %27, label %4

4:                                                ; preds = %2
  %5 = load i8, ptr @_ZN11alglib_impl21_force_malloc_failureE, align 1, !tbaa !5, !range !9, !noundef !10
  %6 = trunc nuw i8 %5 to i1
  br i1 %6, label %27, label %7

7:                                                ; preds = %4
  %8 = load i64, ptr @_ZN11alglib_impl21_malloc_failure_afterE, align 8, !tbaa !11
  %9 = icmp slt i64 %8, 1
  %10 = load i64, ptr @_ZN11alglib_impl20_alloc_counter_totalE, align 8
  %.not = icmp slt i64 %10, %8
  %or.cond = select i1 %9, i1 true, i1 %.not
  br i1 %or.cond, label %11, label %27

11:                                               ; preds = %7
  %12 = icmp eq i64 %1, 0
  %spec.store.select = select i1 %12, i64 64, i64 %1
  %13 = add i64 %spec.store.select, %0
  %.0 = add i64 %13, 15
  %14 = tail call noalias ptr @malloc(i64 noundef %.0) #2
  %15 = icmp eq ptr %14, null
  br i1 %15, label %27, label %16

16:                                               ; preds = %11
  %17 = getelementptr inbounds i8, ptr %14, i64 16
  %18 = tail call noundef ptr @_ZN11alglib_impl8ae_alignEPvm(ptr noundef nonnull %17, i64 noundef %spec.store.select)
  %19 = getelementptr inbounds i8, ptr %18, i64 -8
  store ptr %14, ptr %19, align 8, !tbaa !13
  %20 = load i8, ptr @_ZN11alglib_impl18_use_alloc_counterE, align 1, !tbaa !5, !range !9, !noundef !10
  %21 = trunc nuw i8 %20 to i1
  br i1 %21, label %22, label %23

22:                                               ; preds = %16
  tail call void @_ZN11alglib_impl24ae_optional_atomic_add_iEPll(ptr noundef nonnull @_ZN11alglib_impl14_alloc_counterE, i64 noundef 1)
  tail call void @_ZN11alglib_impl24ae_optional_atomic_add_iEPll(ptr noundef nonnull @_ZN11alglib_impl20_alloc_counter_totalE, i64 noundef 1)
  br label %23

23:                                               ; preds = %22, %16
  %24 = load i8, ptr @_ZN11alglib_impl17_use_dbg_countersE, align 1, !tbaa !5, !range !9, !noundef !10
  %25 = trunc nuw i8 %24 to i1
  br i1 %25, label %26, label %27

26:                                               ; preds = %23
  tail call void @_ZN11alglib_impl24ae_optional_atomic_add_iEPll(ptr noundef nonnull @_ZN11alglib_impl16_dbg_alloc_totalE, i64 noundef %0)
  br label %27

27:                                               ; preds = %23, %26, %11, %7, %4, %2
  %.015 = phi ptr [ null, %2 ], [ null, %4 ], [ null, %7 ], [ null, %11 ], [ %18, %26 ], [ %18, %23 ]
  ret ptr %.015
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 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 #2 = { nounwind allocsize(0) }

!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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !7, i64 0}


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