; LLVM IR from turi-code/SFrame :: oss_src/graphlab/util (safe_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.anon.5 = type { ptr }
%class.anon.6 = type { ptr, ptr }
%"class.graphlab::safe_circular_char_buffer" = type <{ ptr, i64, i64, i64, %"class.graphlab::mutex", %"class.graphlab::conditional", i8, i8, [6 x i8] }>
%"class.graphlab::mutex" = type { %union.pthread_mutex_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%"class.graphlab::conditional" = type { %union.pthread_cond_t }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { 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(none)
declare i64 @llvm.expect.i64(i64, i64) #1

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK8graphlab25safe_circular_char_buffer4sizeEv(ptr noundef nonnull align 8 dereferenceable(122)) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN8graphlab25safe_circular_char_buffer12advance_headEl(ptr noundef nonnull align 8 dereferenceable(122) %0, i64 noundef %1) #3 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca %class.anon.5, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca %class.anon.6, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  %11 = load ptr, ptr %3, align 8
  br label %12

12:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %13 = load i64, ptr %4, align 8, !tbaa !9
  store i64 %13, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  store i32 0, ptr %6, align 4, !tbaa !11
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = icmp sge i64 %14, 0
  %16 = xor i1 %15, true
  %17 = zext i1 %16 to i64
  %18 = call i64 @llvm.expect.i64(i64 %17, i64 0)
  %19 = icmp ne i64 %18, 0
  br i1 %19, label %20, label %22

20:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %21 = getelementptr inbounds %class.anon.5, ptr %7, i32 0, i32 0
  store ptr %5, ptr %21, align 8, !tbaa !5
  call void @"_ZZN8graphlab25safe_circular_char_buffer12advance_headElENK3$_0clEv"(ptr noundef nonnull align 8 dereferenceable(8) %7)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %22

22:                                               ; preds = %20, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  br label %23

23:                                               ; preds = %22
  br label %24

24:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %25 = load i64, ptr %4, align 8, !tbaa !9
  store i64 %25, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %26 = call noundef i64 @_ZNK8graphlab25safe_circular_char_buffer4sizeEv(ptr noundef nonnull align 8 dereferenceable(122) %11)
  store i64 %26, ptr %9, align 8, !tbaa !9
  %27 = load i64, ptr %8, align 8, !tbaa !9
  %28 = load i64, ptr %9, align 8, !tbaa !9
  %29 = icmp sle i64 %27, %28
  %30 = xor i1 %29, true
  %31 = zext i1 %30 to i64
  %32 = call i64 @llvm.expect.i64(i64 %31, i64 0)
  %33 = icmp ne i64 %32, 0
  br i1 %33, label %34, label %37

34:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 16, ptr %10) #5
  %35 = getelementptr inbounds %class.anon.6, ptr %10, i32 0, i32 0
  store ptr %8, ptr %35, align 8, !tbaa !5
  %36 = getelementptr inbounds %class.anon.6, ptr %10, i32 0, i32 1
  store ptr %9, ptr %36, align 8, !tbaa !5
  call void @"_ZZN8graphlab25safe_circular_char_buffer12advance_headElENK3$_1clEv"(ptr noundef nonnull align 8 dereferenceable(16) %10)
  call void @llvm.lifetime.end.p0(i64 16, ptr %10) #5
  br label %37

37:                                               ; preds = %34, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %38

38:                                               ; preds = %37
  %39 = load i64, ptr %4, align 8, !tbaa !9
  %40 = getelementptr inbounds %"class.graphlab::safe_circular_char_buffer", ptr %11, i32 0, i32 2
  %41 = load i64, ptr %40, align 8, !tbaa !13
  %42 = add nsw i64 %41, %39
  store i64 %42, ptr %40, align 8, !tbaa !13
  %43 = getelementptr inbounds %"class.graphlab::safe_circular_char_buffer", ptr %11, i32 0, i32 2
  %44 = load i64, ptr %43, align 8, !tbaa !13
  %45 = getelementptr inbounds %"class.graphlab::safe_circular_char_buffer", ptr %11, i32 0, i32 1
  %46 = load i64, ptr %45, align 8, !tbaa !18
  %47 = icmp sge i64 %44, %46
  br i1 %47, label %48, label %54

48:                                               ; preds = %38
  %49 = getelementptr inbounds %"class.graphlab::safe_circular_char_buffer", ptr %11, i32 0, i32 1
  %50 = load i64, ptr %49, align 8, !tbaa !18
  %51 = getelementptr inbounds %"class.graphlab::safe_circular_char_buffer", ptr %11, i32 0, i32 2
  %52 = load i64, ptr %51, align 8, !tbaa !13
  %53 = sub nsw i64 %52, %50
  store i64 %53, ptr %51, align 8, !tbaa !13
  br label %54

54:                                               ; preds = %48, %38
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare hidden void @"_ZZN8graphlab25safe_circular_char_buffer12advance_headElENK3$_0clEv"(ptr noundef nonnull align 8 dereferenceable(8)) #4 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare hidden void @"_ZZN8graphlab25safe_circular_char_buffer12advance_headElENK3$_1clEv"(ptr noundef nonnull align 8 dereferenceable(16)) #4 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #2 = { 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 #3 = { 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 #4 = { inlinehint 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 #5 = { 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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !10, i64 16}
!14 = !{!"_ZTSN8graphlab25safe_circular_char_bufferE", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !15, i64 32, !16, i64 72, !17, i64 120, !17, i64 121}
!15 = !{!"_ZTSN8graphlab5mutexE", !7, i64 0}
!16 = !{!"_ZTSN8graphlab11conditionalE", !7, i64 0}
!17 = !{!"bool", !7, i64 0}
!18 = !{!14, !10, i64 8}


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

%class.anon.5 = type { ptr }
%class.anon.6 = type { ptr, ptr }

; 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
declare dso_local noundef i64 @_ZNK8graphlab25safe_circular_char_buffer4sizeEv(ptr noundef nonnull align 8 dereferenceable(122)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN8graphlab25safe_circular_char_buffer12advance_headEl(ptr noundef nonnull align 8 dereferenceable(122) %0, i64 noundef %1) local_unnamed_addr #2 align 2 {
  %3 = alloca i64, align 8
  %4 = alloca %class.anon.5, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca %class.anon.6, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  store i64 %1, ptr %3, align 8, !tbaa !5
  %8 = icmp slt i64 %1, 0
  br i1 %8, label %9, label %10, !prof !9

9:                                                ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #4
  store ptr %3, ptr %4, align 8, !tbaa !10
  call void @"_ZZN8graphlab25safe_circular_char_buffer12advance_headElENK3$_0clEv"(ptr noundef nonnull align 8 dereferenceable(8) %4)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #4
  br label %10

10:                                               ; preds = %9, %2
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #4
  store i64 %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #4
  %11 = call noundef i64 @_ZNK8graphlab25safe_circular_char_buffer4sizeEv(ptr noundef nonnull align 8 dereferenceable(122) %0)
  store i64 %11, ptr %6, align 8, !tbaa !5
  %.not = icmp slt i64 %11, %1
  br i1 %.not, label %12, label %14, !prof !9

12:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %7) #4
  store ptr %5, ptr %7, align 8, !tbaa !10
  %13 = getelementptr inbounds i8, ptr %7, i64 8
  store ptr %6, ptr %13, align 8, !tbaa !10
  call void @"_ZZN8graphlab25safe_circular_char_buffer12advance_headElENK3$_1clEv"(ptr noundef nonnull align 8 dereferenceable(16) %7)
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #4
  br label %14

14:                                               ; preds = %12, %10
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #4
  %15 = getelementptr inbounds i8, ptr %0, i64 16
  %16 = load i64, ptr %15, align 8, !tbaa !12
  %17 = add nsw i64 %16, %1
  store i64 %17, ptr %15, align 8, !tbaa !12
  %18 = getelementptr inbounds i8, ptr %0, i64 8
  %19 = load i64, ptr %18, align 8, !tbaa !17
  %.not4 = icmp slt i64 %17, %19
  br i1 %.not4, label %22, label %20

20:                                               ; preds = %14
  %21 = sub nsw i64 %17, %19
  store i64 %21, ptr %15, align 8, !tbaa !12
  br label %22

22:                                               ; preds = %20, %14
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare hidden void @"_ZZN8graphlab25safe_circular_char_buffer12advance_headElENK3$_0clEv"(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #3 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare hidden void @"_ZZN8graphlab25safe_circular_char_buffer12advance_headElENK3$_1clEv"(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #3 align 2

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 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 #3 = { inlinehint 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 #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 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!13, !6, i64 16}
!13 = !{!"_ZTSN8graphlab25safe_circular_char_bufferE", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !14, i64 32, !15, i64 72, !16, i64 120, !16, i64 121}
!14 = !{!"_ZTSN8graphlab5mutexE", !7, i64 0}
!15 = !{!"_ZTSN8graphlab11conditionalE", !7, i64 0}
!16 = !{!"bool", !7, i64 0}
!17 = !{!13, !6, i64 8}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LowerExpectIntrinsicPass,SROAPass,SimplifyCFGPass