; LLVM IR from tyoma/agge :: src/agge.text/layout_builder.ll (ZN4agge14layout_builder17trim_current_lineERKNS0_5stateE)

; 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.agge::layout_builder::state" = type { float, i32 }
%"class.agge::layout_builder" = type { %"struct.agge::layout_builder::state", ptr, ptr, float, ptr, ptr, ptr }
%"class.agge::range" = type { i64, i64, ptr }

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN4agge14layout_builder17trim_current_lineERKNS0_5stateE(ptr noundef nonnull align 8 dereferenceable(56) %0, ptr noundef nonnull align 4 dereferenceable(8) %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %5 = load ptr, ptr %3, align 8
  br label %6

6:                                                ; preds = %16, %2
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = getelementptr inbounds %"struct.agge::layout_builder::state", ptr %7, i32 0, i32 1
  %9 = load i32, ptr %8, align 4, !tbaa !9
  %10 = zext i32 %9 to i64
  %11 = getelementptr inbounds %"class.agge::layout_builder", ptr %5, i32 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  %13 = getelementptr inbounds %"class.agge::range", ptr %12, i32 0, i32 0
  %14 = load i64, ptr %13, align 8, !tbaa !15
  %15 = icmp ult i64 %10, %14
  br i1 %15, label %16, label %23

16:                                               ; preds = %6
  %17 = getelementptr inbounds %"class.agge::layout_builder", ptr %5, i32 0, i32 5
  %18 = load ptr, ptr %17, align 8, !tbaa !18
  call void @_ZNSt6vectorIN4agge9glyph_runESaIS1_EE8pop_backEv(ptr noundef nonnull align 8 dereferenceable(24) %18) #2
  %19 = getelementptr inbounds %"class.agge::layout_builder", ptr %5, i32 0, i32 5
  %20 = load ptr, ptr %19, align 8, !tbaa !18
  %21 = call noundef nonnull align 8 dereferenceable(48) ptr @_ZNSt6vectorIN4agge9glyph_runESaIS1_EE4backEv(ptr noundef nonnull align 8 dereferenceable(24) %20) #2
  %22 = getelementptr inbounds %"class.agge::layout_builder", ptr %5, i32 0, i32 1
  store ptr %21, ptr %22, align 8, !tbaa !13
  br label %6, !llvm.loop !19

23:                                               ; preds = %6
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %"struct.agge::layout_builder::state", ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = zext i32 %26 to i64
  %28 = getelementptr inbounds %"class.agge::layout_builder", ptr %5, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !13
  %30 = getelementptr inbounds %"class.agge::range", ptr %29, i32 0, i32 1
  store i64 %27, ptr %30, align 8, !tbaa !21
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %"class.agge::layout_builder", ptr %5, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %32, ptr align 4 %31, i64 8, i1 false), !tbaa.struct !22
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt6vectorIN4agge9glyph_runESaIS1_EE8pop_backEv(ptr noundef nonnull align 8 dereferenceable(24)) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(48) ptr @_ZNSt6vectorIN4agge9glyph_runESaIS1_EE4backEv(ptr noundef nonnull align 8 dereferenceable(24)) #1 align 2

attributes #0 = { nocallback nofree 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 }

!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++ TBAA"}
!9 = !{!10, !12, i64 4}
!10 = !{!"_ZTSN4agge14layout_builder5stateE", !11, i64 0, !12, i64 4}
!11 = !{!"float", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"_ZTSN4agge14layout_builderE", !10, i64 0, !6, i64 8, !6, i64 16, !11, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!15 = !{!16, !17, i64 0}
!16 = !{!"_ZTSN4agge5rangeIKNS_10pod_vectorINS_16positioned_glyphEEEEE", !17, i64 0, !17, i64 8, !6, i64 16}
!17 = !{!"long", !7, i64 0}
!18 = !{!14, !6, i64 40}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!16, !17, i64 8}
!22 = !{i64 0, i64 4, !23, i64 4, i64 4, !24}
!23 = !{!11, !11, i64 0}
!24 = !{!12, !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"

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN4agge14layout_builder17trim_current_lineERKNS0_5stateE(ptr nocapture noundef nonnull align 8 dereferenceable(56) %0, ptr nocapture noundef nonnull readonly align 4 dereferenceable(8) %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %1, i64 4
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i32, ptr %3, align 4, !tbaa !5
  %6 = zext i32 %5 to i64
  %7 = load ptr, ptr %4, align 8, !tbaa !11
  %8 = load i64, ptr %7, align 8, !tbaa !14
  %9 = icmp ugt i64 %8, %6
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2
  %10 = getelementptr inbounds i8, ptr %0, i64 40
  br label %11

11:                                               ; preds = %.lr.ph, %11
  %12 = load ptr, ptr %10, align 8, !tbaa !17
  tail call void @_ZNSt6vectorIN4agge9glyph_runESaIS1_EE8pop_backEv(ptr noundef nonnull align 8 dereferenceable(24) %12) #1
  %13 = load ptr, ptr %10, align 8, !tbaa !17
  %14 = tail call noundef nonnull align 8 dereferenceable(48) ptr @_ZNSt6vectorIN4agge9glyph_runESaIS1_EE4backEv(ptr noundef nonnull align 8 dereferenceable(24) %13) #1
  store ptr %14, ptr %4, align 8, !tbaa !11
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = zext i32 %15 to i64
  %17 = load i64, ptr %14, align 8, !tbaa !14
  %18 = icmp ugt i64 %17, %16
  br i1 %18, label %11, label %._crit_edge, !llvm.loop !18

._crit_edge:                                      ; preds = %11, %2
  %.lcssa4 = phi i64 [ %6, %2 ], [ %16, %11 ]
  %.lcssa = phi ptr [ %7, %2 ], [ %14, %11 ]
  %19 = getelementptr inbounds i8, ptr %.lcssa, i64 8
  store i64 %.lcssa4, ptr %19, align 8, !tbaa !20
  %20 = load i64, ptr %1, align 4
  store i64 %20, ptr %0, align 8
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt6vectorIN4agge9glyph_runESaIS1_EE8pop_backEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(48) ptr @_ZNSt6vectorIN4agge9glyph_runESaIS1_EE4backEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #0 align 2

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 = { 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, !10, i64 4}
!6 = !{!"_ZTSN4agge14layout_builder5stateE", !7, i64 0, !10, i64 4}
!7 = !{!"float", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !13, i64 8}
!12 = !{!"_ZTSN4agge14layout_builderE", !6, i64 0, !13, i64 8, !13, i64 16, !7, i64 24, !13, i64 32, !13, i64 40, !13, i64 48}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!15, !16, i64 0}
!15 = !{!"_ZTSN4agge5rangeIKNS_10pod_vectorINS_16positioned_glyphEEEEE", !16, i64 0, !16, i64 8, !13, i64 16}
!16 = !{!"long", !8, i64 0}
!17 = !{!12, !13, i64 40}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!15, !16, i64 8}


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