; 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_GetByte(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i16, align 2
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  br label %6

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

9:                                                ; preds = %6
  store i16 -1, ptr %3, align 2
  br label %56

10:                                               ; preds = %6
  br label %11

11:                                               ; preds = %10
  br label %12

12:                                               ; preds = %11
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %16

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

16:                                               ; preds = %12
  br label %17

17:                                               ; preds = %16
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.RingBuffer, ptr %18, i32 0, i32 2
  %20 = load i16, ptr %19, align 2, !tbaa !9
  %21 = sext i16 %20 to i32
  %22 = icmp sle i32 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %17
  store i16 -1, ptr %3, align 2
  br label %56

24:                                               ; preds = %17
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.RingBuffer, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !12
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.RingBuffer, ptr %28, i32 0, i32 3
  %30 = load i16, ptr %29, align 4, !tbaa !13
  %31 = sext i16 %30 to i64
  %32 = getelementptr inbounds i8, ptr %27, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !14
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  store i8 %33, ptr %34, align 1, !tbaa !14
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.RingBuffer, ptr %35, i32 0, i32 3
  %37 = load i16, ptr %36, align 4, !tbaa !13
  %38 = add i16 %37, 1
  store i16 %38, ptr %36, align 4, !tbaa !13
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.RingBuffer, ptr %39, i32 0, i32 3
  %41 = load i16, ptr %40, align 4, !tbaa !13
  %42 = sext i16 %41 to i32
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.RingBuffer, ptr %43, i32 0, i32 1
  %45 = load i16, ptr %44, align 8, !tbaa !15
  %46 = sext i16 %45 to i32
  %47 = icmp eq i32 %42, %46
  br i1 %47, label %48, label %51

48:                                               ; preds = %24
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.RingBuffer, ptr %49, i32 0, i32 3
  store i16 0, ptr %50, align 4, !tbaa !13
  br label %51

51:                                               ; preds = %48, %24
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.RingBuffer, ptr %52, i32 0, i32 2
  %54 = load i16, ptr %53, align 2, !tbaa !9
  %55 = add i16 %54, -1
  store i16 %55, ptr %53, align 2, !tbaa !9
  store i16 0, ptr %3, align 2
  br label %56

56:                                               ; preds = %51, %23, %15, %9
  %57 = load i16, ptr %3, align 2
  ret i16 %57
}

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

!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 = !{!10, !11, i64 10}
!10 = !{!"", !6, i64 0, !11, i64 8, !11, i64 10, !11, i64 12}
!11 = !{!"short", !7, i64 0}
!12 = !{!10, !6, i64 0}
!13 = !{!10, !11, i64 12}
!14 = !{!7, !7, i64 0}
!15 = !{!10, !11, 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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local signext range(i16 -1, 1) i16 @RingBuffer_GetByte(ptr noundef %0, ptr noundef writeonly %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp eq ptr %1, null
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %23, label %5

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

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

23:                                               ; preds = %5, %2, %9
  %.0 = phi i16 [ 0, %9 ], [ -1, %2 ], [ -1, %5 ]
  ret i16 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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, !7, i64 0}
!12 = !{!6, !10, i64 12}
!13 = !{!8, !8, i64 0}
!14 = !{!6, !10, i64 8}


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