; LLVM IR from AlgoGenesis/C :: Queue/MergingTwoSortedArraysUsingCircularQueue (mergeSortedArrays)

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

; Function Attrs: nounwind uwtable
declare dso_local void @enqueue(ptr noundef, i32 noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @mergeSortedArrays(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !9
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %51, %5
  %14 = load i32, ptr %11, align 4, !tbaa !9
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %17, label %21

17:                                               ; preds = %13
  %18 = load i32, ptr %12, align 4, !tbaa !9
  %19 = load i32, ptr %9, align 4, !tbaa !9
  %20 = icmp slt i32 %18, %19
  br label %21

21:                                               ; preds = %17, %13
  %22 = phi i1 [ false, %13 ], [ %20, %17 ]
  br i1 %22, label %23, label %52

23:                                               ; preds = %21
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load i32, ptr %11, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %24, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = load i32, ptr %12, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %29, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = icmp slt i32 %28, %33
  br i1 %34, label %35, label %43

35:                                               ; preds = %23
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load i32, ptr %11, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %11, align 4, !tbaa !9
  %40 = sext i32 %38 to i64
  %41 = getelementptr inbounds i32, ptr %37, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  call void @enqueue(ptr noundef %36, i32 noundef %42)
  br label %51

43:                                               ; preds = %23
  %44 = load ptr, ptr %10, align 8, !tbaa !5
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = load i32, ptr %12, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %12, align 4, !tbaa !9
  %48 = sext i32 %46 to i64
  %49 = getelementptr inbounds i32, ptr %45, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !9
  call void @enqueue(ptr noundef %44, i32 noundef %50)
  br label %51

51:                                               ; preds = %43, %35
  br label %13, !llvm.loop !11

52:                                               ; preds = %21
  br label %53

53:                                               ; preds = %57, %52
  %54 = load i32, ptr %11, align 4, !tbaa !9
  %55 = load i32, ptr %7, align 4, !tbaa !9
  %56 = icmp slt i32 %54, %55
  br i1 %56, label %57, label %65

57:                                               ; preds = %53
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = load i32, ptr %11, align 4, !tbaa !9
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %11, align 4, !tbaa !9
  %62 = sext i32 %60 to i64
  %63 = getelementptr inbounds i32, ptr %59, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !9
  call void @enqueue(ptr noundef %58, i32 noundef %64)
  br label %53, !llvm.loop !13

65:                                               ; preds = %53
  br label %66

66:                                               ; preds = %70, %65
  %67 = load i32, ptr %12, align 4, !tbaa !9
  %68 = load i32, ptr %9, align 4, !tbaa !9
  %69 = icmp slt i32 %67, %68
  br i1 %69, label %70, label %78

70:                                               ; preds = %66
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  %72 = load ptr, ptr %8, align 8, !tbaa !5
  %73 = load i32, ptr %12, align 4, !tbaa !9
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %12, align 4, !tbaa !9
  %75 = sext i32 %73 to i64
  %76 = getelementptr inbounds i32, ptr %72, i64 %75
  %77 = load i32, ptr %76, align 4, !tbaa !9
  call void @enqueue(ptr noundef %71, i32 noundef %77)
  br label %66, !llvm.loop !14

78:                                               ; preds = %66
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  ret void
}

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


; 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: nounwind uwtable
declare dso_local void @enqueue(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @mergeSortedArrays(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef readonly %2, i32 noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = icmp sgt i32 %1, 0
  %7 = icmp sgt i32 %3, 0
  %8 = and i1 %6, %7
  br i1 %8, label %.lr.ph, label %.preheader28

.preheader28:                                     ; preds = %.lr.ph, %5
  %.023.lcssa = phi i32 [ 0, %5 ], [ %.124, %.lr.ph ]
  %.0.lcssa = phi i32 [ 0, %5 ], [ %.1, %.lr.ph ]
  %9 = icmp slt i32 %.023.lcssa, %1
  br i1 %9, label %.lr.ph33.preheader, label %.preheader

.lr.ph33.preheader:                               ; preds = %.preheader28
  %10 = sext i32 %.023.lcssa to i64
  br label %.lr.ph33

.lr.ph:                                           ; preds = %5, %.lr.ph
  %.030 = phi i32 [ %.1, %.lr.ph ], [ 0, %5 ]
  %.02329 = phi i32 [ %.124, %.lr.ph ], [ 0, %5 ]
  %11 = zext nneg i32 %.02329 to i64
  %12 = getelementptr inbounds i32, ptr %0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = zext nneg i32 %.030 to i64
  %15 = getelementptr inbounds i32, ptr %2, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp slt i32 %13, %16
  %.sink = tail call i32 @llvm.smin.i32(i32 %13, i32 %16)
  %18 = zext i1 %17 to i32
  %.124 = add nuw nsw i32 %.02329, %18
  %not. = xor i1 %17, true
  %19 = zext i1 %not. to i32
  %.1 = add nuw nsw i32 %.030, %19
  tail call void @enqueue(ptr noundef %4, i32 noundef %.sink)
  %20 = icmp slt i32 %.124, %1
  %21 = icmp slt i32 %.1, %3
  %22 = select i1 %20, i1 %21, i1 false
  br i1 %22, label %.lr.ph, label %.preheader28, !llvm.loop !9

.preheader:                                       ; preds = %.lr.ph33, %.preheader28
  %23 = icmp slt i32 %.0.lcssa, %3
  br i1 %23, label %.lr.ph35.preheader, label %._crit_edge

.lr.ph35.preheader:                               ; preds = %.preheader
  %24 = sext i32 %.0.lcssa to i64
  br label %.lr.ph35

.lr.ph33:                                         ; preds = %.lr.ph33.preheader, %.lr.ph33
  %indvars.iv = phi i64 [ %10, %.lr.ph33.preheader ], [ %indvars.iv.next, %.lr.ph33 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %25 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %26 = load i32, ptr %25, align 4, !tbaa !5
  tail call void @enqueue(ptr noundef %4, i32 noundef %26)
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %lftr.wideiv, %1
  br i1 %exitcond.not, label %.preheader, label %.lr.ph33, !llvm.loop !11

.lr.ph35:                                         ; preds = %.lr.ph35.preheader, %.lr.ph35
  %indvars.iv37 = phi i64 [ %24, %.lr.ph35.preheader ], [ %indvars.iv.next38, %.lr.ph35 ]
  %indvars.iv.next38 = add nsw i64 %indvars.iv37, 1
  %27 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv37
  %28 = load i32, ptr %27, align 4, !tbaa !5
  tail call void @enqueue(ptr noundef %4, i32 noundef %28)
  %lftr.wideiv40 = trunc i64 %indvars.iv.next38 to i32
  %exitcond41.not = icmp eq i32 %lftr.wideiv40, %3
  br i1 %exitcond41.not, label %._crit_edge, label %.lr.ph35, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph35, %.preheader
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass