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

; 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 }
%struct.AllocGen = type { [10 x ptr], ptr }

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @allocator_gen_trim(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.Allocator, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = icmp eq ptr %10, @alloc_gen_alloc
  br i1 %11, label %12, label %17

12:                                               ; preds = %1
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Allocator, ptr %13, i32 0, i32 3
  %15 = load ptr, ptr %14, align 8, !tbaa !12
  %16 = icmp ne ptr %15, null
  br i1 %16, label %18, label %17

17:                                               ; preds = %12, %1
  br label %71

18:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.Allocator, ptr %19, i32 0, i32 3
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  store ptr %21, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.AllocGen, ptr %22, i32 0, i32 1
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  store ptr %24, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load ptr, ptr %25, align 8, !tbaa !5
  store ptr %26, ptr %5, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %66, %18
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br i1 %29, label %31, label %30

30:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %70

31:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds i32, ptr %32, i64 4
  store ptr %33, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds i32, ptr %35, i64 2
  %37 = load i32, ptr %36, align 4, !tbaa !15
  %38 = zext i32 %37 to i64
  %39 = udiv i64 %38, 4
  %40 = getelementptr inbounds i32, ptr %34, i64 %39
  store ptr %40, ptr %7, align 8, !tbaa !5
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds i32, ptr %41, i64 -1
  %43 = load i32, ptr %42, align 4, !tbaa !15
  %44 = and i32 %43, 536870912
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %65

46:                                               ; preds = %31
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds i32, ptr %48, i64 -1
  %50 = load i32, ptr %49, align 4, !tbaa !15
  %51 = and i32 %50, 268435452
  %52 = zext i32 %51 to i64
  %53 = udiv i64 %52, 4
  %54 = getelementptr inbounds i32, ptr %47, i64 %53
  %55 = getelementptr inbounds i32, ptr %54, i64 1
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = icmp eq ptr %55, %56
  br i1 %57, label %58, label %65

58:                                               ; preds = %46
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  call void @chunk_free_remove(ptr noundef %59)
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %61, ptr %62, align 8, !tbaa !5
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  call void @alloc_gen_seg_free(ptr noundef %63)
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %64, ptr %5, align 8, !tbaa !5
  br label %65

65:                                               ; preds = %58, %46, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %66

66:                                               ; preds = %65
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %67, ptr %4, align 8, !tbaa !5
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = load ptr, ptr %68, align 8, !tbaa !5
  store ptr %69, ptr %5, align 8, !tbaa !5
  br label %27, !llvm.loop !16

70:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %71

71:                                               ; preds = %70, %17
  ret void
}

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

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 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"Allocator", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 24}
!13 = !{!14, !6, i64 80}
!14 = !{!"AllocGen", !7, i64 0, !6, i64 80}
!15 = !{!11, !11, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

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

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

; Function Attrs: nounwind uwtable
define dso_local void @allocator_gen_trim(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr %0, align 8, !tbaa !5
  %3 = icmp eq ptr %2, @alloc_gen_alloc
  br i1 %3, label %4, label %.loopexit

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %.loopexit, label %7

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %6, i64 80
  %9 = load ptr, ptr %8, align 8, !tbaa !12
  %.02024 = load ptr, ptr %9, align 8, !tbaa !14
  %.not2225 = icmp eq ptr %.02024, null
  br i1 %.not2225, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %7, %28
  %.02027 = phi ptr [ %.020, %28 ], [ %.02024, %7 ]
  %.026 = phi ptr [ %.1, %28 ], [ %9, %7 ]
  %10 = getelementptr inbounds i8, ptr %.02027, i64 16
  %11 = getelementptr inbounds i8, ptr %.02027, i64 12
  %12 = load i32, ptr %11, align 4, !tbaa !15
  %13 = and i32 %12, 536870912
  %.not23 = icmp eq i32 %13, 0
  br i1 %.not23, label %28, label %14

14:                                               ; preds = %.lr.ph
  %15 = getelementptr inbounds i8, ptr %.02027, i64 8
  %16 = load i32, ptr %15, align 4, !tbaa !15
  %17 = lshr i32 %16, 2
  %18 = zext nneg i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr %.02027, i64 %18
  %20 = lshr i32 %12, 2
  %21 = and i32 %20, 67108863
  %22 = zext nneg i32 %21 to i64
  %23 = getelementptr inbounds i32, ptr %10, i64 %22
  %24 = getelementptr inbounds i8, ptr %23, i64 4
  %25 = icmp eq ptr %24, %19
  br i1 %25, label %26, label %28

26:                                               ; preds = %14
  tail call void @chunk_free_remove(ptr noundef nonnull %10)
  %27 = load ptr, ptr %.02027, align 8, !tbaa !14
  store ptr %27, ptr %.026, align 8, !tbaa !14
  tail call void @alloc_gen_seg_free(ptr noundef nonnull %.02027)
  br label %28

28:                                               ; preds = %26, %14, %.lr.ph
  %.1 = phi ptr [ %.026, %26 ], [ %.02027, %14 ], [ %.02027, %.lr.ph ]
  %.020 = load ptr, ptr %.1, align 8, !tbaa !14
  %.not22 = icmp eq ptr %.020, null
  br i1 %.not22, label %.loopexit, label %.lr.ph, !llvm.loop !16

.loopexit:                                        ; preds = %28, %7, %1, %4
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @chunk_free_remove(ptr 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 = { 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" }

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


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