; LLVM IR from turi-code/SFrame :: oss_src/rpc/circular_char_buffer.ll (ZN8graphlab20circular_char_buffer7reserveEl)

; 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.graphlab::circular_char_buffer" = type { ptr, i64, i64, i64, i64 }

; 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 uwtable
define dso_local void @_ZN8graphlab20circular_char_buffer7reserveEl(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  %8 = load ptr, ptr %3, align 8
  %9 = load i64, ptr %4, align 8, !tbaa !9
  %10 = icmp sle i64 %9, 4
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store i64 4, ptr %4, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %11, %2
  %13 = load i64, ptr %4, align 8, !tbaa !9
  %14 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 3
  %15 = load i64, ptr %14, align 8, !tbaa !11
  %16 = icmp sle i64 %13, %15
  br i1 %16, label %17, label %18

17:                                               ; preds = %12
  br label %71

18:                                               ; preds = %12
  %19 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = load i64, ptr %4, align 8, !tbaa !9
  %22 = call ptr @realloc(ptr noundef %20, i64 noundef %21) #5
  %23 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 0
  store ptr %22, ptr %23, align 8, !tbaa !13
  %24 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 2
  %25 = load i64, ptr %24, align 8, !tbaa !14
  %26 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 1
  %27 = load i64, ptr %26, align 8, !tbaa !15
  %28 = icmp sge i64 %25, %27
  br i1 %28, label %29, label %32

29:                                               ; preds = %18
  %30 = load i64, ptr %4, align 8, !tbaa !9
  %31 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 3
  store i64 %30, ptr %31, align 8, !tbaa !11
  br label %70

32:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %33 = load i64, ptr %4, align 8, !tbaa !9
  %34 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 3
  %35 = load i64, ptr %34, align 8, !tbaa !11
  %36 = sub i64 %33, %35
  store i64 %36, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  %37 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 2
  %38 = load i64, ptr %37, align 8, !tbaa !14
  store i64 %38, ptr %7, align 8, !tbaa !9
  %39 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minImERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull align 8 dereferenceable(8) %5)
  %40 = load i64, ptr %39, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  store i64 %40, ptr %6, align 8, !tbaa !9
  %41 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !13
  %43 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 3
  %44 = load i64, ptr %43, align 8, !tbaa !11
  %45 = getelementptr inbounds i8, ptr %42, i64 %44
  %46 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %45, ptr align 1 %47, i64 %48, i1 false)
  %49 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 0
  %50 = load ptr, ptr %49, align 8, !tbaa !13
  %51 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !13
  %53 = load i64, ptr %6, align 8, !tbaa !9
  %54 = getelementptr inbounds i8, ptr %52, i64 %53
  %55 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 2
  %56 = load i64, ptr %55, align 8, !tbaa !14
  %57 = load i64, ptr %6, align 8, !tbaa !9
  %58 = sub i64 %56, %57
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %50, ptr align 1 %54, i64 %58, i1 false)
  %59 = load i64, ptr %4, align 8, !tbaa !9
  %60 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 3
  store i64 %59, ptr %60, align 8, !tbaa !11
  %61 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 1
  %62 = load i64, ptr %61, align 8, !tbaa !15
  %63 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 4
  %64 = load i64, ptr %63, align 8, !tbaa !16
  %65 = add nsw i64 %62, %64
  %66 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 3
  %67 = load i64, ptr %66, align 8, !tbaa !11
  %68 = srem i64 %65, %67
  %69 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %8, i32 0, i32 2
  store i64 %68, ptr %69, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  br label %70

70:                                               ; preds = %32, %29
  call void @_ZNK8graphlab20circular_char_buffer17consistency_checkEv(ptr noundef nonnull align 8 dereferenceable(40) %8)
  br label %71

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minImERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #3

; 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) #4

; 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) #4

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZNK8graphlab20circular_char_buffer17consistency_checkEv(ptr noundef nonnull align 8 dereferenceable(40)) #3 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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(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 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nounwind allocsize(1) }
attributes #6 = { 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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"_ZTSN8graphlab20circular_char_bufferE", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!13 = !{!12, !6, i64 0}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !10, i64 8}
!16 = !{!12, !10, 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN8graphlab20circular_char_buffer7reserveEl(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1) local_unnamed_addr #1 align 2 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %spec.store.select = tail call i64 @llvm.smax.i64(i64 %1, i64 4)
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %.not = icmp sgt i64 %spec.store.select, %6
  br i1 %.not, label %7, label %32

7:                                                ; preds = %2
  %8 = load ptr, ptr %0, align 8, !tbaa !11
  %9 = tail call ptr @realloc(ptr noundef %8, i64 noundef %spec.store.select) #6
  store ptr %9, ptr %0, align 8, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %0, i64 16
  %11 = load i64, ptr %10, align 8, !tbaa !12
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load i64, ptr %12, align 8, !tbaa !13
  %.not9 = icmp slt i64 %11, %13
  br i1 %.not9, label %14, label %31

14:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #7
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = sub i64 %spec.store.select, %15
  store i64 %16, ptr %3, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #7
  store i64 %11, ptr %4, align 8, !tbaa !14
  %17 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minImERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %4, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %18 = load i64, ptr %17, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #7
  %19 = load ptr, ptr %0, align 8, !tbaa !11
  %20 = load i64, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %19, i64 %20
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %21, ptr align 1 %19, i64 %18, i1 false)
  %22 = load ptr, ptr %0, align 8, !tbaa !11
  %23 = getelementptr inbounds i8, ptr %22, i64 %18
  %24 = load i64, ptr %10, align 8, !tbaa !12
  %25 = sub i64 %24, %18
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %22, ptr align 1 %23, i64 %25, i1 false)
  %26 = load i64, ptr %12, align 8, !tbaa !13
  %27 = getelementptr inbounds i8, ptr %0, i64 32
  %28 = load i64, ptr %27, align 8, !tbaa !15
  %29 = add nsw i64 %28, %26
  %30 = srem i64 %29, %spec.store.select
  store i64 %30, ptr %10, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #7
  br label %31

31:                                               ; preds = %7, %14
  store i64 %spec.store.select, ptr %5, align 8
  call void @_ZNK8graphlab20circular_char_buffer17consistency_checkEv(ptr noundef nonnull align 8 dereferenceable(40) %0)
  br label %32

32:                                               ; preds = %2, %31
  ret void
}

; 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 mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minImERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #3

; Function Attrs: mustprogress 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) #4

; 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) #4

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZNK8graphlab20circular_char_buffer17consistency_checkEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #3 align 2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #5

attributes #0 = { mustprogress 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 = { 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 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { nounwind allocsize(1) }
attributes #7 = { 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 24}
!6 = !{!"_ZTSN8graphlab20circular_char_bufferE", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!6, !10, i64 16}
!13 = !{!6, !10, i64 8}
!14 = !{!10, !10, i64 0}
!15 = !{!6, !10, i64 32}


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