; LLVM IR from ahampriyanshu/algo-ds-101 :: Data-Structures/Queue/Circular-Queue (circular_queue)

; 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.queue = type { i32, ptr }

@size = external dso_local global i32, align 4
@temp = external dso_local global ptr, align 8
@front = external dso_local global ptr, align 8
@rear = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @enqueue(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %3 = call noalias ptr @malloc(i64 noundef 16) #2
  store ptr %3, ptr @temp, align 8, !tbaa !9
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = load ptr, ptr @temp, align 8, !tbaa !9
  %6 = getelementptr inbounds %struct.queue, ptr %5, i32 0, i32 0
  store i32 %4, ptr %6, align 8, !tbaa !11
  %7 = load ptr, ptr @front, align 8, !tbaa !9
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %11

9:                                                ; preds = %1
  %10 = load ptr, ptr @temp, align 8, !tbaa !9
  store ptr %10, ptr @front, align 8, !tbaa !9
  br label %16

11:                                               ; preds = %1
  %12 = load ptr, ptr @temp, align 8, !tbaa !9
  %13 = load ptr, ptr @rear, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.queue, ptr %13, i32 0, i32 1
  store ptr %12, ptr %14, align 8, !tbaa !13
  %15 = load ptr, ptr @temp, align 8, !tbaa !9
  store ptr %15, ptr @rear, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %11, %9
  %17 = load ptr, ptr @front, align 8, !tbaa !9
  %18 = load ptr, ptr @rear, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.queue, ptr %18, i32 0, i32 1
  store ptr %17, ptr %19, align 8, !tbaa !13
  %20 = load i32, ptr @size, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr @size, align 4, !tbaa !5
  ret void
}

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #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 = { nounwind allocsize(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 allocsize(0) }

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

@size = external dso_local local_unnamed_addr global i32, align 4
@temp = external dso_local local_unnamed_addr global ptr, align 8
@front = external dso_local local_unnamed_addr global ptr, align 8
@rear = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree nounwind willreturn memory(readwrite, argmem: write) uwtable
define dso_local void @enqueue(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #2
  store ptr %2, ptr @temp, align 8, !tbaa !5
  store i32 %0, ptr %2, align 8, !tbaa !9
  %3 = load ptr, ptr @front, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %6

5:                                                ; preds = %1
  store ptr %2, ptr @front, align 8, !tbaa !5
  %.pre = load ptr, ptr @rear, align 8, !tbaa !5
  br label %9

6:                                                ; preds = %1
  %7 = load ptr, ptr @rear, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i64 8
  store ptr %2, ptr %8, align 8, !tbaa !12
  store ptr %2, ptr @rear, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %6, %5
  %10 = phi ptr [ %2, %6 ], [ %.pre, %5 ]
  %11 = phi ptr [ %3, %6 ], [ %2, %5 ]
  %12 = getelementptr inbounds i8, ptr %10, i64 8
  store ptr %11, ptr %12, align 8, !tbaa !12
  %13 = load i32, ptr @size, align 4, !tbaa !13
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr @size, align 4, !tbaa !13
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nofree nounwind willreturn memory(readwrite, argmem: write) 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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }

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


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