; LLVM IR from priyankchheda/algorithms :: _other_languages/cpp/Queue/ReverseQueue.ll (Z12ReverseQueueRN5Queue5QueueIiEE)

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

%"class.Stack::Stack" = type { ptr, i32, i32 }

; Function Attrs: mustprogress uwtable
define dso_local void @_Z12ReverseQueueRN5Queue5QueueIiEE(ptr noundef nonnull align 8 dereferenceable(24) %0) #0 personality ptr @__gxx_personality_v0 {
  %2 = alloca ptr, align 8
  %3 = alloca %"class.Stack::Stack", align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %3) #3
  call void @_ZN5Stack5StackIiEC2Ei(ptr noundef nonnull align 8 dereferenceable(16) %3, i32 noundef 10)
  br label %6

6:                                                ; preds = %16, %1
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = invoke noundef zeroext i1 @_ZN5Queue5QueueIiE7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(24) %7)
          to label %9 unwind label %17

9:                                                ; preds = %6
  %10 = xor i1 %8, true
  br i1 %10, label %11, label %21

11:                                               ; preds = %9
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = invoke noundef i32 @_ZN5Queue5QueueIiE7dequeueEv(ptr noundef nonnull align 8 dereferenceable(24) %12)
          to label %14 unwind label %17

14:                                               ; preds = %11
  %15 = invoke noundef i32 @_ZN5Stack5StackIiE4pushEi(ptr noundef nonnull align 8 dereferenceable(16) %3, i32 noundef %13)
          to label %16 unwind label %17

16:                                               ; preds = %14
  br label %6, !llvm.loop !9

17:                                               ; preds = %29, %26, %22, %14, %11, %6
  %18 = landingpad { ptr, i32 }
          cleanup
  %19 = extractvalue { ptr, i32 } %18, 0
  store ptr %19, ptr %4, align 8
  %20 = extractvalue { ptr, i32 } %18, 1
  store i32 %20, ptr %5, align 4
  call void @_ZN5Stack5StackIiED2Ev(ptr noundef nonnull align 8 dereferenceable(16) %3) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %3) #3
  br label %33

21:                                               ; preds = %9
  br label %22

22:                                               ; preds = %31, %21
  %23 = invoke noundef zeroext i1 @_ZN5Stack5StackIiE7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(16) %3)
          to label %24 unwind label %17

24:                                               ; preds = %22
  %25 = xor i1 %23, true
  br i1 %25, label %26, label %32

26:                                               ; preds = %24
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = invoke noundef i32 @_ZN5Stack5StackIiE3popEv(ptr noundef nonnull align 8 dereferenceable(16) %3)
          to label %29 unwind label %17

29:                                               ; preds = %26
  %30 = invoke noundef i32 @_ZN5Queue5QueueIiE7enqueueEi(ptr noundef nonnull align 8 dereferenceable(24) %27, i32 noundef %28)
          to label %31 unwind label %17

31:                                               ; preds = %29
  br label %22, !llvm.loop !11

32:                                               ; preds = %24
  call void @_ZN5Stack5StackIiED2Ev(ptr noundef nonnull align 8 dereferenceable(16) %3) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %3) #3
  ret void

33:                                               ; preds = %17
  %34 = load ptr, ptr %4, align 8
  %35 = load i32, ptr %5, align 4
  %36 = insertvalue { ptr, i32 } poison, ptr %34, 0
  %37 = insertvalue { ptr, i32 } %36, i32 %35, 1
  resume { ptr, i32 } %37
}

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5Stack5StackIiEC2Ei(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN5Queue5QueueIiE7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(24)) #0 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN5Stack5StackIiE4pushEi(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN5Queue5QueueIiE7dequeueEv(ptr noundef nonnull align 8 dereferenceable(24)) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN5Stack5StackIiE7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(16)) #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN5Queue5QueueIiE7enqueueEi(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN5Stack5StackIiE3popEv(ptr noundef nonnull align 8 dereferenceable(16)) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN5Stack5StackIiED2Ev(ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #2 align 2

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

attributes #0 = { mustprogress 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 = { mustprogress 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 #3 = { 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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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

%"class.Stack::Stack" = type { ptr, i32, i32 }

; Function Attrs: mustprogress uwtable
define dso_local void @_Z12ReverseQueueRN5Queue5QueueIiEE(ptr noundef nonnull align 8 dereferenceable(24) %0) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %2 = alloca %"class.Stack::Stack", align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #3
  call void @_ZN5Stack5StackIiEC2Ei(ptr noundef nonnull align 8 dereferenceable(16) %2, i32 noundef 10)
  br label %3

3:                                                ; preds = %8, %1
  %4 = invoke noundef zeroext i1 @_ZN5Queue5QueueIiE7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(24) %0)
          to label %5 unwind label %.loopexit.split-lp

5:                                                ; preds = %3
  br i1 %4, label %.preheader, label %6

6:                                                ; preds = %5
  %7 = invoke noundef i32 @_ZN5Queue5QueueIiE7dequeueEv(ptr noundef nonnull align 8 dereferenceable(24) %0)
          to label %8 unwind label %.loopexit.split-lp

8:                                                ; preds = %6
  %9 = invoke noundef i32 @_ZN5Stack5StackIiE4pushEi(ptr noundef nonnull align 8 dereferenceable(16) %2, i32 noundef %7)
          to label %3 unwind label %.loopexit.split-lp, !llvm.loop !5

.loopexit:                                        ; preds = %12, %14
  %lpad.loopexit = landingpad { ptr, i32 }
          cleanup
  br label %10

.loopexit.split-lp:                               ; preds = %3, %6, %8
  %lpad.loopexit.split-lp = landingpad { ptr, i32 }
          cleanup
  br label %10

10:                                               ; preds = %.loopexit.split-lp, %.loopexit
  %lpad.phi = phi { ptr, i32 } [ %lpad.loopexit, %.loopexit ], [ %lpad.loopexit.split-lp, %.loopexit.split-lp ]
  call void @_ZN5Stack5StackIiED2Ev(ptr noundef nonnull align 8 dereferenceable(16) %2) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #3
  resume { ptr, i32 } %lpad.phi

.preheader:                                       ; preds = %5, %14
  %11 = call noundef zeroext i1 @_ZN5Stack5StackIiE7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
  br i1 %11, label %16, label %12

12:                                               ; preds = %.preheader
  %13 = invoke noundef i32 @_ZN5Stack5StackIiE3popEv(ptr noundef nonnull align 8 dereferenceable(16) %2)
          to label %14 unwind label %.loopexit

14:                                               ; preds = %12
  %15 = invoke noundef i32 @_ZN5Queue5QueueIiE7enqueueEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %13)
          to label %.preheader unwind label %.loopexit, !llvm.loop !7

16:                                               ; preds = %.preheader
  call void @_ZN5Stack5StackIiED2Ev(ptr noundef nonnull align 8 dereferenceable(16) %2) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #3
  ret void
}

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN5Stack5StackIiEC2Ei(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN5Queue5QueueIiE7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #0 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN5Stack5StackIiE4pushEi(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN5Queue5QueueIiE7dequeueEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN5Stack5StackIiE7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN5Queue5QueueIiE7enqueueEi(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN5Stack5StackIiE3popEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN5Stack5StackIiED2Ev(ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #2 align 2

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

attributes #0 = { mustprogress 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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 #3 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass