; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 12. Queue (priorityQueue_array)

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

@front = external dso_local global i32, align 4
@rear = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [33 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@pq = external dso_local global [5 x %struct.PQueue], align 16
@.str.3 = external hidden unnamed_addr constant [22 x i8], align 1
@temp = external dso_local global %struct.PQueue, align 4

; Function Attrs: nounwind uwtable
define dso_local void @enqueue() #0 {
  %1 = alloca i32, align 4
  %2 = load i32, ptr @rear, align 4, !tbaa !5
  %3 = icmp sge i32 %2, 4
  br i1 %3, label %4, label %6

4:                                                ; preds = %0
  %5 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  br label %64

6:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  %7 = load i32, ptr @front, align 4, !tbaa !5
  %8 = icmp eq i32 %7, -1
  br i1 %8, label %9, label %10

9:                                                ; preds = %6
  store i32 0, ptr @rear, align 4, !tbaa !5
  store i32 0, ptr @front, align 4, !tbaa !5
  br label %13

10:                                               ; preds = %6
  %11 = load i32, ptr @rear, align 4, !tbaa !5
  %12 = add nsw i32 %11, 1
  store i32 %12, ptr @rear, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %10, %9
  %14 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  %15 = load i32, ptr @rear, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %16
  %18 = getelementptr inbounds %struct.PQueue, ptr %17, i32 0, i32 0
  %19 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %18)
  %20 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  %21 = load i32, ptr @rear, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.PQueue, ptr %23, i32 0, i32 1
  %25 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %24)
  %26 = load i32, ptr @rear, align 4, !tbaa !5
  %27 = icmp sgt i32 %26, 0
  br i1 %27, label %28, label %63

28:                                               ; preds = %13
  %29 = load i32, ptr @rear, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %30
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 @temp, ptr align 8 %31, i64 8, i1 false), !tbaa.struct !9
  %32 = load i32, ptr @rear, align 4, !tbaa !5
  %33 = sub nsw i32 %32, 1
  store i32 %33, ptr %1, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %55, %28
  %35 = load i32, ptr %1, align 4, !tbaa !5
  %36 = icmp sge i32 %35, 0
  br i1 %36, label %37, label %58

37:                                               ; preds = %34
  %38 = load i32, ptr getelementptr inbounds (%struct.PQueue, ptr @temp, i32 0, i32 1), align 4, !tbaa !10
  %39 = load i32, ptr %1, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %40
  %42 = getelementptr inbounds %struct.PQueue, ptr %41, i32 0, i32 1
  %43 = load i32, ptr %42, align 4, !tbaa !10
  %44 = icmp sgt i32 %38, %43
  br i1 %44, label %45, label %53

45:                                               ; preds = %37
  %46 = load i32, ptr %1, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %48
  %50 = load i32, ptr %1, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %51
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %49, ptr align 8 %52, i64 8, i1 false), !tbaa.struct !9
  br label %54

53:                                               ; preds = %37
  br label %58

54:                                               ; preds = %45
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %1, align 4, !tbaa !5
  %57 = add nsw i32 %56, -1
  store i32 %57, ptr %1, align 4, !tbaa !5
  br label %34, !llvm.loop !12

58:                                               ; preds = %53, %34
  %59 = load i32, ptr %1, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %61
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %62, ptr align 4 @temp, i64 8, i1 false), !tbaa.struct !9
  br label %63

63:                                               ; preds = %58, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  br label %64

64:                                               ; preds = %63, %4
  ret void
}

declare i32 @printf(ptr noundef, ...) #1

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

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 = { "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i64 0, i64 4, !5, i64 4, i64 4, !5}
!10 = !{!11, !6, i64 4}
!11 = !{!"PQueue", !6, i64 0, !6, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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

%struct.PQueue = type { i32, i32 }

@front = external dso_local local_unnamed_addr global i32, align 4
@rear = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [33 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@pq = external dso_local global [5 x %struct.PQueue], align 16
@.str.3 = external hidden unnamed_addr constant [22 x i8], align 1
@temp = external dso_local local_unnamed_addr global %struct.PQueue, align 4

; Function Attrs: nofree nounwind uwtable
define dso_local void @enqueue() local_unnamed_addr #0 {
  %1 = load i32, ptr @rear, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 3
  br i1 %2, label %3, label %5

3:                                                ; preds = %0
  %4 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  br label %42

5:                                                ; preds = %0
  %6 = load i32, ptr @front, align 4, !tbaa !5
  %7 = icmp eq i32 %6, -1
  br i1 %7, label %8, label %9

8:                                                ; preds = %5
  store i32 0, ptr @front, align 4, !tbaa !5
  br label %11

9:                                                ; preds = %5
  %10 = add nsw i32 %1, 1
  br label %11

11:                                               ; preds = %9, %8
  %.sink = phi i32 [ 0, %8 ], [ %10, %9 ]
  store i32 %.sink, ptr @rear, align 4
  %12 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  %13 = load i32, ptr @rear, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %14
  %16 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %15)
  %17 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %18 = load i32, ptr @rear, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %19, i32 1
  %21 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %20)
  %22 = load i32, ptr @rear, align 4, !tbaa !5
  %23 = icmp sgt i32 %22, 0
  br i1 %23, label %24, label %42

24:                                               ; preds = %11
  %25 = zext nneg i32 %22 to i64
  %26 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %25
  %27 = load i64, ptr %26, align 8
  store i64 %27, ptr @temp, align 4
  %28 = lshr i64 %27, 32
  %29 = trunc nuw i64 %28 to i32
  br label %30

30:                                               ; preds = %24, %35
  %indvars.iv = phi i64 [ %25, %24 ], [ %indvars.iv.next, %35 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %31 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %indvars.iv.next
  %32 = getelementptr inbounds i8, ptr %31, i64 4
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = icmp slt i32 %33, %29
  br i1 %34, label %35, label %39

35:                                               ; preds = %30
  %36 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %indvars.iv
  %37 = load i64, ptr %31, align 8
  store i64 %37, ptr %36, align 8
  %38 = icmp ugt i64 %indvars.iv, 1
  br i1 %38, label %30, label %39, !llvm.loop !11

39:                                               ; preds = %30, %35
  %.0.in.lcssa = phi i64 [ %indvars.iv, %30 ], [ 0, %35 ]
  %sext = shl i64 %.0.in.lcssa, 32
  %40 = ashr exact i64 %sext, 32
  %41 = getelementptr inbounds [5 x %struct.PQueue], ptr @pq, i64 0, i64 %40
  store i64 %27, ptr %41, align 8
  br label %42

42:                                               ; preds = %11, %39, %3
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { nofree 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 = { nofree nounwind "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 4}
!10 = !{!"PQueue", !6, i64 0, !6, i64 4}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LICMPass,LoopRotatePass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass