; LLVM IR from aagdev/mlimgsynth :: src/ccommon (alloc)

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

; Function Attrs: nounwind uwtable
define dso_local ptr @alloc_stdlib_alloc(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !11
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.Allocator, ptr %9, i32 0, i32 4
  %11 = load i32, ptr %10, align 8, !tbaa !13
  %12 = and i32 %11, 1
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %14, label %29

14:                                               ; preds = %4
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %20

17:                                               ; preds = %14
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i64 -16
  store ptr %19, ptr %6, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %17, %14
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = icmp ne i64 %21, 0
  br i1 %22, label %23, label %28

23:                                               ; preds = %20
  %24 = load i64, ptr %7, align 8, !tbaa !9
  %25 = add i64 %24, 16
  %26 = add i64 %25, 15
  %27 = and i64 %26, -16
  store i64 %27, ptr %7, align 8, !tbaa !9
  br label %28

28:                                               ; preds = %23, %20
  br label %29

29:                                               ; preds = %28, %4
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %51

32:                                               ; preds = %29
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = icmp eq i64 %33, 0
  br i1 %34, label %35, label %37

35:                                               ; preds = %32
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free(ptr noundef %36) #6
  store ptr null, ptr %6, align 8, !tbaa !5
  br label %50

37:                                               ; preds = %32
  %38 = load i64, ptr %7, align 8, !tbaa !9
  %39 = lshr i64 %38, 4
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = add i64 %40, %39
  store i64 %41, ptr %7, align 8, !tbaa !9
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = call ptr @realloc(ptr noundef %42, i64 noundef %43) #7
  store ptr %44, ptr %6, align 8, !tbaa !5
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = icmp ne ptr %45, null
  br i1 %46, label %49, label %47

47:                                               ; preds = %37
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  call void @alloc_fatal(ptr noundef %48)
  br label %49

49:                                               ; preds = %47, %37
  br label %50

50:                                               ; preds = %49, %35
  br label %67

51:                                               ; preds = %29
  %52 = load i32, ptr %8, align 4, !tbaa !11
  %53 = and i32 %52, 1
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %58

55:                                               ; preds = %51
  %56 = load i64, ptr %7, align 8, !tbaa !9
  %57 = call noalias ptr @calloc(i64 noundef 1, i64 noundef %56) #8
  store ptr %57, ptr %6, align 8, !tbaa !5
  br label %61

58:                                               ; preds = %51
  %59 = load i64, ptr %7, align 8, !tbaa !9
  %60 = call noalias ptr @malloc(i64 noundef %59) #9
  store ptr %60, ptr %6, align 8, !tbaa !5
  br label %61

61:                                               ; preds = %58, %55
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = icmp ne ptr %62, null
  br i1 %63, label %66, label %64

64:                                               ; preds = %61
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  call void @alloc_fatal(ptr noundef %65)
  br label %66

66:                                               ; preds = %64, %61
  br label %67

67:                                               ; preds = %66, %50
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.Allocator, ptr %68, i32 0, i32 4
  %70 = load i32, ptr %69, align 8, !tbaa !13
  %71 = and i32 %70, 1
  %72 = icmp ne i32 %71, 0
  br i1 %72, label %73, label %84

73:                                               ; preds = %67
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = icmp ne ptr %74, null
  br i1 %75, label %76, label %84

76:                                               ; preds = %73
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = getelementptr inbounds i8, ptr %77, i64 16
  store ptr %78, ptr %6, align 8, !tbaa !5
  %79 = load i64, ptr %7, align 8, !tbaa !9
  %80 = sub i64 %79, 16
  %81 = trunc i64 %80 to i32
  %82 = load ptr, ptr %6, align 8, !tbaa !5
  %83 = getelementptr inbounds i32, ptr %82, i64 -1
  store i32 %81, ptr %83, align 4, !tbaa !11
  br label %84

84:                                               ; preds = %76, %73, %67
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  ret ptr %85
}

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @alloc_fatal(ptr noundef) #3

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

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

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 = { 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 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 #3 = { inlinehint 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(0,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 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(1) }
attributes #8 = { nounwind allocsize(0,1) }
attributes #9 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 32}
!14 = !{!"Allocator", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !12, 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"

; Function Attrs: nounwind uwtable
define dso_local ptr @alloc_stdlib_alloc(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 32
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = and i32 %6, 1
  %.not = icmp eq i32 %7, 0
  %.not35 = icmp eq ptr %1, null
  br i1 %.not, label %13, label %8

8:                                                ; preds = %4
  %9 = getelementptr inbounds i8, ptr %1, i64 -16
  %.not34 = icmp eq i64 %2, 0
  br i1 %.not34, label %.thread, label %10

10:                                               ; preds = %8
  %11 = add i64 %2, 31
  %12 = and i64 %11, -16
  br i1 %.not35, label %.thread55, label %.thread59

13:                                               ; preds = %4
  br i1 %.not35, label %.thread55, label %.thread59

.thread:                                          ; preds = %8
  br i1 %.not35, label %.thread55, label %.thread45

.thread59:                                        ; preds = %10, %13
  %.164 = phi ptr [ %1, %13 ], [ %9, %10 ]
  %.02563 = phi i64 [ %2, %13 ], [ %12, %10 ]
  %14 = icmp eq i64 %.02563, 0
  br i1 %14, label %.thread45, label %15

.thread45:                                        ; preds = %.thread, %.thread59
  %.14448 = phi ptr [ %.164, %.thread59 ], [ %9, %.thread ]
  tail call void @free(ptr noundef nonnull %.14448) #6
  br label %.thread49

15:                                               ; preds = %.thread59
  %16 = lshr i64 %.02563, 4
  %17 = add i64 %16, %.02563
  %18 = tail call ptr @realloc(ptr noundef nonnull %.164, i64 noundef %17) #7
  %.not38 = icmp eq ptr %18, null
  br i1 %.not38, label %19, label %._crit_edge

._crit_edge:                                      ; preds = %15
  %.pre = load i32, ptr %5, align 8, !tbaa !5
  %.pre54 = and i32 %.pre, 1
  br label %27

19:                                               ; preds = %15
  tail call void @alloc_fatal(ptr noundef nonnull %0)
  br label %.thread49

.thread55:                                        ; preds = %10, %.thread, %13
  %.02543 = phi i64 [ 0, %.thread ], [ %2, %13 ], [ %12, %10 ]
  %20 = and i32 %3, 1
  %.not36 = icmp eq i32 %20, 0
  br i1 %.not36, label %23, label %21

21:                                               ; preds = %.thread55
  %22 = tail call noalias ptr @calloc(i64 noundef 1, i64 noundef %.02543) #8
  br label %25

23:                                               ; preds = %.thread55
  %24 = tail call noalias ptr @malloc(i64 noundef %.02543) #9
  br label %25

25:                                               ; preds = %23, %21
  %.2 = phi ptr [ %22, %21 ], [ %24, %23 ]
  %.not37 = icmp eq ptr %.2, null
  br i1 %.not37, label %26, label %27

26:                                               ; preds = %25
  tail call void @alloc_fatal(ptr noundef nonnull %0)
  br label %.thread49

27:                                               ; preds = %._crit_edge, %25
  %.pre-phi = phi i32 [ %.pre54, %._crit_edge ], [ %7, %25 ]
  %.126 = phi i64 [ %17, %._crit_edge ], [ %.02543, %25 ]
  %.3 = phi ptr [ %18, %._crit_edge ], [ %.2, %25 ]
  %.not53 = icmp eq i32 %.pre-phi, 0
  br i1 %.not53, label %.thread49, label %28

28:                                               ; preds = %27
  %29 = getelementptr inbounds i8, ptr %.3, i64 16
  %30 = trunc i64 %.126 to i32
  %31 = add i32 %30, -16
  %32 = getelementptr inbounds i8, ptr %.3, i64 12
  store i32 %31, ptr %32, align 4, !tbaa !11
  br label %.thread49

.thread49:                                        ; preds = %26, %19, %.thread45, %28, %27
  %.4 = phi ptr [ %29, %28 ], [ %.3, %27 ], [ null, %.thread45 ], [ null, %19 ], [ null, %26 ]
  ret ptr %.4
}

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @alloc_fatal(ptr noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #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 #5

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 = { 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 #3 = { inlinehint 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 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) 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 #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(1) }
attributes #8 = { nounwind allocsize(0,1) }
attributes #9 = { 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, !10, i64 32}
!6 = !{!"Allocator", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!10, !10, i64 0}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass