; LLVM IR from JessonYue/LeetCodeLearning :: Array/yangxin/第五周练习6_28-7_4/FIFO缓存算法 (FIFO)

; 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.FIFO = type { [5 x i32], i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @FIFOSize(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.FIFO, ptr %4, i32 0, i32 1
  %6 = load i32, ptr %5, align 4, !tbaa !9
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.FIFO, ptr %7, i32 0, i32 2
  %9 = load i32, ptr %8, align 4, !tbaa !12
  %10 = icmp eq i32 %6, %9
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store i32 0, ptr %2, align 4
  br label %37

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.FIFO, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 4, !tbaa !9
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.FIFO, ptr %16, i32 0, i32 2
  %18 = load i32, ptr %17, align 4, !tbaa !12
  %19 = icmp sgt i32 %15, %18
  br i1 %19, label %20, label %29

20:                                               ; preds = %12
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.FIFO, ptr %21, i32 0, i32 2
  %23 = load i32, ptr %22, align 4, !tbaa !12
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.FIFO, ptr %24, i32 0, i32 1
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = sub nsw i32 %23, %26
  %28 = add nsw i32 %27, 5
  store i32 %28, ptr %2, align 4
  br label %37

29:                                               ; preds = %12
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.FIFO, ptr %30, i32 0, i32 2
  %32 = load i32, ptr %31, align 4, !tbaa !12
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.FIFO, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = sub nsw i32 %32, %35
  store i32 %36, ptr %2, align 4
  br label %37

37:                                               ; preds = %29, %20, %11
  %38 = load i32, ptr %2, align 4
  ret i32 %38
}

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 20}
!10 = !{!"FIFO", !7, i64 0, !11, i64 20, !11, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, 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 nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local i32 @FIFOSize(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 20
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i32, ptr %4, align 4, !tbaa !10
  %6 = icmp eq i32 %3, %5
  %7 = icmp sgt i32 %3, %5
  %reass.sub = sub i32 %5, %3
  %8 = add i32 %reass.sub, 5
  %spec.select = select i1 %7, i32 %8, i32 %reass.sub
  %.0 = select i1 %6, i32 0, i32 %spec.select
  ret i32 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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, !9, i64 20}
!6 = !{!"FIFO", !7, i64 0, !9, i64 20, !9, i64 24}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!6, !9, i64 24}


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