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

; 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
declare dso_local void @_ZN8graphlab20circular_char_buffer7reserveEl(ptr noundef nonnull align 8 dereferenceable(40), i64 noundef) #1 align 2

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN8graphlab20circular_char_buffer5writeEPKcl(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, i64 noundef %2) #1 align 2 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = 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
  %12 = load ptr, ptr %5, align 8
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 4
  %15 = load i64, ptr %14, align 8, !tbaa !11
  %16 = add nsw i64 %13, %15
  %17 = add nsw i64 %16, 1
  call void @_ZN8graphlab20circular_char_buffer7reserveEl(ptr noundef nonnull align 8 dereferenceable(40) %12, i64 noundef %17)
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 4
  %20 = load i64, ptr %19, align 8, !tbaa !11
  %21 = add nsw i64 %20, %18
  store i64 %21, ptr %19, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %22 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 3
  %23 = load i64, ptr %22, align 8, !tbaa !13
  %24 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 2
  %25 = load i64, ptr %24, align 8, !tbaa !14
  %26 = sub nsw i64 %23, %25
  store i64 %26, ptr %9, align 8, !tbaa !9
  %27 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIlERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull align 8 dereferenceable(8) %9)
  %28 = load i64, ptr %27, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  store i64 %28, ptr %8, align 8, !tbaa !9
  %29 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 2
  %32 = load i64, ptr %31, align 8, !tbaa !14
  %33 = getelementptr inbounds i8, ptr %30, i64 %32
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %33, ptr align 1 %34, i64 %35, i1 false)
  %36 = load i64, ptr %8, align 8, !tbaa !9
  %37 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 2
  %38 = load i64, ptr %37, align 8, !tbaa !14
  %39 = add nsw i64 %38, %36
  store i64 %39, ptr %37, align 8, !tbaa !14
  %40 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 2
  %41 = load i64, ptr %40, align 8, !tbaa !14
  %42 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 3
  %43 = load i64, ptr %42, align 8, !tbaa !13
  %44 = icmp eq i64 %41, %43
  br i1 %44, label %45, label %47

45:                                               ; preds = %3
  %46 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 2
  store i64 0, ptr %46, align 8, !tbaa !14
  br label %47

47:                                               ; preds = %45, %3
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = load i64, ptr %7, align 8, !tbaa !9
  %50 = icmp eq i64 %48, %49
  br i1 %50, label %51, label %53

51:                                               ; preds = %47
  call void @_ZNK8graphlab20circular_char_buffer17consistency_checkEv(ptr noundef nonnull align 8 dereferenceable(40) %12)
  %52 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %52, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %68

53:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %54 = load i64, ptr %7, align 8, !tbaa !9
  %55 = load i64, ptr %8, align 8, !tbaa !9
  %56 = sub nsw i64 %54, %55
  store i64 %56, ptr %11, align 8, !tbaa !9
  %57 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !15
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = load i64, ptr %8, align 8, !tbaa !9
  %61 = getelementptr inbounds i8, ptr %59, i64 %60
  %62 = load i64, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %58, ptr align 1 %61, i64 %62, i1 false)
  %63 = load i64, ptr %11, align 8, !tbaa !9
  %64 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %12, i32 0, i32 2
  %65 = load i64, ptr %64, align 8, !tbaa !14
  %66 = add nsw i64 %65, %63
  store i64 %66, ptr %64, align 8, !tbaa !14
  call void @_ZNK8graphlab20circular_char_buffer17consistency_checkEv(ptr noundef nonnull align 8 dereferenceable(40) %12)
  %67 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %67, ptr %4, align 8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %68

68:                                               ; preds = %53, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %69 = load i64, ptr %4, align 8
  ret i64 %69
}

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 32}
!12 = !{!"_ZTSN8graphlab20circular_char_bufferE", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!13 = !{!12, !10, i64 24}
!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 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 uwtable
declare dso_local void @_ZN8graphlab20circular_char_buffer7reserveEl(ptr noundef nonnull align 8 dereferenceable(40), i64 noundef) local_unnamed_addr #1 align 2

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN8graphlab20circular_char_buffer5writeEPKcl(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #1 align 2 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store i64 %2, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 32
  %7 = load i64, ptr %6, align 8, !tbaa !9
  %8 = add i64 %2, 1
  %9 = add i64 %8, %7
  tail call void @_ZN8graphlab20circular_char_buffer7reserveEl(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %9)
  %10 = load i64, ptr %6, align 8, !tbaa !9
  %11 = add nsw i64 %10, %2
  store i64 %11, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #4
  %12 = getelementptr inbounds i8, ptr %0, i64 24
  %13 = load i64, ptr %12, align 8, !tbaa !12
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load i64, ptr %14, align 8, !tbaa !13
  %16 = sub nsw i64 %13, %15
  store i64 %16, ptr %5, align 8, !tbaa !5
  %17 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIlERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %4, ptr noundef nonnull align 8 dereferenceable(8) %5)
  %18 = load i64, ptr %17, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #4
  %19 = load ptr, ptr %0, align 8, !tbaa !14
  %20 = load i64, ptr %14, align 8, !tbaa !13
  %21 = getelementptr inbounds i8, ptr %19, i64 %20
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %21, ptr align 1 %1, i64 %18, i1 false)
  %22 = load i64, ptr %14, align 8, !tbaa !13
  %23 = add nsw i64 %22, %18
  %24 = load i64, ptr %12, align 8, !tbaa !12
  %25 = icmp eq i64 %23, %24
  %spec.store.select = select i1 %25, i64 0, i64 %23
  store i64 %spec.store.select, ptr %14, align 8
  %26 = load i64, ptr %4, align 8, !tbaa !5
  %27 = icmp eq i64 %18, %26
  br i1 %27, label %34, label %28

28:                                               ; preds = %3
  %29 = sub nsw i64 %26, %18
  %30 = load ptr, ptr %0, align 8, !tbaa !14
  %31 = getelementptr inbounds i8, ptr %1, i64 %18
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %30, ptr align 1 %31, i64 %29, i1 false)
  %32 = load i64, ptr %14, align 8, !tbaa !13
  %33 = add nsw i64 %32, %29
  store i64 %33, ptr %14, align 8, !tbaa !13
  br label %34

34:                                               ; preds = %3, %28
  call void @_ZNK8graphlab20circular_char_buffer17consistency_checkEv(ptr noundef nonnull align 8 dereferenceable(40) %0)
  %.0 = load i64, ptr %4, align 8, !tbaa !5
  ret i64 %.0
}

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


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