; LLVM IR from ImChong/Practice_LeetCode_C :: F01_Common_Functions/src (common_ring_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"

%struct.RingBuffer = type { ptr, i16, i16, i16 }

; Function Attrs: nounwind uwtable
define dso_local signext i16 @RingBuffer_AddByte(ptr noundef %0, i8 noundef zeroext %1) #0 {
  %3 = alloca i16, align 2
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i16, align 2
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  br label %7

7:                                                ; preds = %2
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %11

10:                                               ; preds = %7
  store i16 -1, ptr %3, align 2
  br label %62

11:                                               ; preds = %7
  br label %12

12:                                               ; preds = %11
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.RingBuffer, ptr %13, i32 0, i32 2
  %15 = load i16, ptr %14, align 2, !tbaa !10
  %16 = sext i16 %15 to i32
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.RingBuffer, ptr %17, i32 0, i32 1
  %19 = load i16, ptr %18, align 8, !tbaa !13
  %20 = sext i16 %19 to i32
  %21 = icmp eq i32 %16, %20
  br i1 %21, label %22, label %23

22:                                               ; preds = %12
  store i16 -1, ptr %3, align 2
  br label %62

23:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #2
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.RingBuffer, ptr %24, i32 0, i32 3
  %26 = load i16, ptr %25, align 4, !tbaa !14
  %27 = sext i16 %26 to i32
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.RingBuffer, ptr %28, i32 0, i32 2
  %30 = load i16, ptr %29, align 2, !tbaa !10
  %31 = sext i16 %30 to i32
  %32 = add nsw i32 %27, %31
  %33 = trunc i32 %32 to i16
  store i16 %33, ptr %6, align 2, !tbaa !15
  %34 = load i16, ptr %6, align 2, !tbaa !15
  %35 = zext i16 %34 to i32
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.RingBuffer, ptr %36, i32 0, i32 1
  %38 = load i16, ptr %37, align 8, !tbaa !13
  %39 = sext i16 %38 to i32
  %40 = icmp sge i32 %35, %39
  br i1 %40, label %41, label %50

41:                                               ; preds = %23
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.RingBuffer, ptr %42, i32 0, i32 1
  %44 = load i16, ptr %43, align 8, !tbaa !13
  %45 = sext i16 %44 to i32
  %46 = load i16, ptr %6, align 2, !tbaa !15
  %47 = zext i16 %46 to i32
  %48 = sub nsw i32 %47, %45
  %49 = trunc i32 %48 to i16
  store i16 %49, ptr %6, align 2, !tbaa !15
  br label %50

50:                                               ; preds = %41, %23
  %51 = load i8, ptr %5, align 1, !tbaa !9
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.RingBuffer, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !16
  %55 = load i16, ptr %6, align 2, !tbaa !15
  %56 = zext i16 %55 to i64
  %57 = getelementptr inbounds i8, ptr %54, i64 %56
  store i8 %51, ptr %57, align 1, !tbaa !9
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.RingBuffer, ptr %58, i32 0, i32 2
  %60 = load i16, ptr %59, align 2, !tbaa !10
  %61 = add i16 %60, 1
  store i16 %61, ptr %59, align 2, !tbaa !10
  store i16 0, ptr %3, align 2
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #2
  br label %62

62:                                               ; preds = %50, %22, %10
  %63 = load i16, ptr %3, align 2
  ret i16 %63
}

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

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

attributes #0 = { 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 nosync nounwind willreturn memory(argmem: readwrite) }
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/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !12, i64 10}
!11 = !{!"", !6, i64 0, !12, i64 8, !12, i64 10, !12, i64 12}
!12 = !{!"short", !7, i64 0}
!13 = !{!11, !12, i64 8}
!14 = !{!11, !12, i64 12}
!15 = !{!12, !12, i64 0}
!16 = !{!11, !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 nofree norecurse nosync nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local signext range(i16 -1, 1) i16 @RingBuffer_AddByte(ptr noundef %0, i8 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %22, label %4

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 10
  %6 = load i16, ptr %5, align 2, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load i16, ptr %7, align 8, !tbaa !11
  %9 = icmp eq i16 %6, %8
  br i1 %9, label %22, label %10

10:                                               ; preds = %4
  %11 = sext i16 %8 to i32
  %12 = getelementptr inbounds i8, ptr %0, i64 12
  %13 = load i16, ptr %12, align 4, !tbaa !12
  %14 = add i16 %13, %6
  %15 = zext i16 %14 to i32
  %.not = icmp slt i32 %15, %11
  %16 = select i1 %.not, i16 0, i16 %8
  %spec.select = sub i16 %14, %16
  %17 = load ptr, ptr %0, align 8, !tbaa !13
  %18 = zext i16 %spec.select to i64
  %19 = getelementptr inbounds i8, ptr %17, i64 %18
  store i8 %1, ptr %19, align 1, !tbaa !14
  %20 = load i16, ptr %5, align 2, !tbaa !5
  %21 = add i16 %20, 1
  store i16 %21, ptr %5, align 2, !tbaa !5
  br label %22

22:                                               ; preds = %4, %2, %10
  %.013 = phi i16 [ 0, %10 ], [ -1, %2 ], [ -1, %4 ]
  ret i16 %.013
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(write, argmem: readwrite, inaccessiblemem: none) 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" }

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


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