; LLVM IR from shadow-paw/lightcone :: lightcone/buffer.ll (ZN9lightcone6Buffer13reserve_beginEPPhm)

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

%"class.lightcone::Buffer" = type { ptr, i64, i64, i64 }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN9lightcone6Buffer7reallocEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZN9lightcone6Buffer13reserve_beginEPPhm(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef %1, i64 noundef %2) #0 align 2 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  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
  %8 = load ptr, ptr %5, align 8
  %9 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 3
  %10 = load i64, ptr %9, align 8, !tbaa !11
  %11 = load i64, ptr %7, align 8, !tbaa !9
  %12 = add i64 %10, %11
  %13 = call noundef zeroext i1 @_ZN9lightcone6Buffer7reallocEm(ptr noundef nonnull align 8 dereferenceable(32) %8, i64 noundef %12)
  br i1 %13, label %15, label %14

14:                                               ; preds = %3
  store i1 false, ptr %4, align 1
  br label %51

15:                                               ; preds = %3
  %16 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 1
  %17 = load i64, ptr %16, align 8, !tbaa !13
  %18 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 2
  %19 = load i64, ptr %18, align 8, !tbaa !14
  %20 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 3
  %21 = load i64, ptr %20, align 8, !tbaa !11
  %22 = add i64 %19, %21
  %23 = load i64, ptr %7, align 8, !tbaa !9
  %24 = add i64 %22, %23
  %25 = icmp ule i64 %17, %24
  br i1 %25, label %26, label %37

26:                                               ; preds = %15
  %27 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !15
  %29 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 2
  %32 = load i64, ptr %31, align 8, !tbaa !14
  %33 = getelementptr inbounds i8, ptr %30, i64 %32
  %34 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 3
  %35 = load i64, ptr %34, align 8, !tbaa !11
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %28, ptr align 1 %33, i64 %35, i1 false)
  %36 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 2
  store i64 0, ptr %36, align 8, !tbaa !14
  br label %37

37:                                               ; preds = %26, %15
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = icmp ne ptr %38, null
  br i1 %39, label %40, label %50

40:                                               ; preds = %37
  %41 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !15
  %43 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 2
  %44 = load i64, ptr %43, align 8, !tbaa !14
  %45 = getelementptr inbounds i8, ptr %42, i64 %44
  %46 = getelementptr inbounds %"class.lightcone::Buffer", ptr %8, i32 0, i32 3
  %47 = load i64, ptr %46, align 8, !tbaa !11
  %48 = getelementptr inbounds i8, ptr %45, i64 %47
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %48, ptr %49, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %40, %37
  store i1 true, ptr %4, align 1
  br label %51

51:                                               ; preds = %50, %14
  %52 = load i1, ptr %4, align 1
  ret i1 %52
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"_ZTSN9lightcone6BufferE", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !6, 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
declare dso_local noundef zeroext i1 @_ZN9lightcone6Buffer7reallocEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZN9lightcone6Buffer13reserve_beginEPPhm(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef writeonly %1, i64 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = add i64 %5, %2
  %7 = tail call noundef zeroext i1 @_ZN9lightcone6Buffer7reallocEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %6)
  br i1 %7, label %8, label %26

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !11
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  %12 = load i64, ptr %11, align 8, !tbaa !12
  %13 = load i64, ptr %4, align 8, !tbaa !5
  %14 = add i64 %12, %2
  %15 = add i64 %14, %13
  %.not = icmp ugt i64 %10, %15
  br i1 %.not, label %19, label %16

16:                                               ; preds = %8
  %17 = load ptr, ptr %0, align 8, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %17, i64 %12
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %17, ptr align 1 %18, i64 %13, i1 false)
  store i64 0, ptr %11, align 8, !tbaa !12
  br label %19

19:                                               ; preds = %16, %8
  %20 = phi i64 [ 0, %16 ], [ %12, %8 ]
  %.not6 = icmp eq ptr %1, null
  br i1 %.not6, label %26, label %21

21:                                               ; preds = %19
  %22 = load ptr, ptr %0, align 8, !tbaa !13
  %23 = getelementptr inbounds i8, ptr %22, i64 %20
  %24 = load i64, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %23, i64 %24
  store ptr %25, ptr %1, align 8, !tbaa !14
  br label %26

26:                                               ; preds = %19, %21, %3
  ret i1 %7
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #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 nocallback nofree nounwind willreturn memory(argmem: readwrite) }

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


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