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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN8graphlab20circular_char_buffer18introspective_readERPcl(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef nonnull align 8 dereferenceable(8) %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
  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
  %9 = load ptr, ptr %5, align 8
  %10 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %9, i32 0, i32 4
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %13, label %15

13:                                               ; preds = %3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr null, ptr %14, align 8, !tbaa !5
  store i64 0, ptr %4, align 8
  br label %49

15:                                               ; preds = %3
  %16 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %9, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %9, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !14
  %20 = getelementptr inbounds i8, ptr %17, i64 %19
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %20, ptr %21, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !9
  %22 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %9, i32 0, i32 2
  %23 = load i64, ptr %22, align 8, !tbaa !15
  %24 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %9, i32 0, i32 1
  %25 = load i64, ptr %24, align 8, !tbaa !14
  %26 = icmp sgt i64 %23, %25
  br i1 %26, label %27, label %33

27:                                               ; preds = %15
  %28 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %9, i32 0, i32 2
  %29 = load i64, ptr %28, align 8, !tbaa !15
  %30 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %9, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !14
  %32 = sub nsw i64 %29, %31
  store i64 %32, ptr %8, align 8, !tbaa !9
  br label %39

33:                                               ; preds = %15
  %34 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %9, i32 0, i32 3
  %35 = load i64, ptr %34, align 8, !tbaa !16
  %36 = getelementptr inbounds %"class.graphlab::circular_char_buffer", ptr %9, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !14
  %38 = sub nsw i64 %35, %37
  store i64 %38, ptr %8, align 8, !tbaa !9
  br label %39

39:                                               ; preds = %33, %27
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = load i64, ptr %8, align 8, !tbaa !9
  %42 = icmp slt i64 %40, %41
  br i1 %42, label %43, label %45

43:                                               ; preds = %39
  %44 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %44, ptr %8, align 8, !tbaa !9
  br label %45

45:                                               ; preds = %43, %39
  %46 = load i64, ptr %8, align 8, !tbaa !9
  %47 = call noundef i64 @_ZN8graphlab20circular_char_buffer4skipEl(ptr noundef nonnull align 8 dereferenceable(40) %9, i64 noundef %46)
  %48 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %48, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %49

49:                                               ; preds = %45, %13
  %50 = load i64, ptr %4, align 8
  ret i64 %50
}

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 }

!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, !6, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!12, !10, i64 16}
!16 = !{!12, !10, i64 24}


; 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 uwtable
declare dso_local noundef i64 @_ZN8graphlab20circular_char_buffer4skipEl(ptr noundef nonnull align 8 dereferenceable(40), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN8graphlab20circular_char_buffer18introspective_readERPcl(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr nocapture noundef nonnull writeonly align 8 dereferenceable(8) %1, i64 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 32
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i64 %5, 0
  br i1 %6, label %7, label %8

7:                                                ; preds = %3
  store ptr null, ptr %1, align 8, !tbaa !11
  br label %19

8:                                                ; preds = %3
  %9 = load ptr, ptr %0, align 8, !tbaa !12
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load i64, ptr %10, align 8, !tbaa !13
  %12 = getelementptr inbounds i8, ptr %9, i64 %11
  store ptr %12, ptr %1, align 8, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %0, i64 16
  %14 = load i64, ptr %13, align 8, !tbaa !14
  %15 = icmp sgt i64 %14, %11
  %16 = getelementptr inbounds i8, ptr %0, i64 24
  %17 = load i64, ptr %16, align 8
  %.pn = select i1 %15, i64 %14, i64 %17
  %.0 = sub nsw i64 %.pn, %11
  %.1 = tail call i64 @llvm.smin.i64(i64 %.0, i64 %2)
  %18 = tail call noundef i64 @_ZN8graphlab20circular_char_buffer4skipEl(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %.1)
  br label %19

19:                                               ; preds = %8, %7
  %.08 = phi i64 [ 0, %7 ], [ %.1, %8 ]
  ret i64 %.08
}

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 32}
!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 = !{!7, !7, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!6, !10, i64 16}


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