; LLVM IR from tangjz/acm-icpc :: 51nod/1382.ll (ZN7Segment5queryEii)

; 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.Segment = type { i32, i32, i32, [4 x i64], [2 x ptr] }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN7Segment5mergeES_S_(ptr noundef nonnull align 8 dereferenceable(64), ptr noundef byval(%struct.Segment) align 8, ptr noundef byval(%struct.Segment) align 8) #0 align 2

; 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) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7Segment5queryEii(ptr dead_on_unwind noalias writable sret(%struct.Segment) align 8 %0, ptr noundef nonnull align 8 dereferenceable(64) %1, i32 noundef %2, i32 noundef %3) #2 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca %struct.Segment, align 8
  %9 = alloca %struct.Segment, align 8
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  store i32 %3, ptr %7, align 4, !tbaa !9
  %10 = load ptr, ptr %5, align 8
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = icmp sle i32 %11, %13
  br i1 %14, label %15, label %21

15:                                               ; preds = %4
  %16 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 1
  %17 = load i32, ptr %16, align 4, !tbaa !13
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = icmp sle i32 %17, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %15
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 8 %10, i64 64, i1 false), !tbaa.struct !14
  br label %54

21:                                               ; preds = %15, %4
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 2
  %24 = load i32, ptr %23, align 8, !tbaa !16
  %25 = icmp sle i32 %22, %24
  br i1 %25, label %26, label %32

26:                                               ; preds = %21
  %27 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 4
  %28 = getelementptr inbounds [2 x ptr], ptr %27, i64 0, i64 0
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = load i32, ptr %7, align 4, !tbaa !9
  call void @_ZN7Segment5queryEii(ptr dead_on_unwind writable sret(%struct.Segment) align 8 %0, ptr noundef nonnull align 8 dereferenceable(64) %29, i32 noundef %30, i32 noundef %31)
  br label %54

32:                                               ; preds = %21
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 2
  %35 = load i32, ptr %34, align 8, !tbaa !16
  %36 = icmp sgt i32 %33, %35
  br i1 %36, label %37, label %43

37:                                               ; preds = %32
  %38 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 4
  %39 = getelementptr inbounds [2 x ptr], ptr %38, i64 0, i64 1
  %40 = load ptr, ptr %39, align 8, !tbaa !5
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = load i32, ptr %7, align 4, !tbaa !9
  call void @_ZN7Segment5queryEii(ptr dead_on_unwind writable sret(%struct.Segment) align 8 %0, ptr noundef nonnull align 8 dereferenceable(64) %40, i32 noundef %41, i32 noundef %42)
  br label %54

43:                                               ; preds = %32
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 8 %10, i64 64, i1 false), !tbaa.struct !14
  %44 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 4
  %45 = getelementptr inbounds [2 x ptr], ptr %44, i64 0, i64 0
  %46 = load ptr, ptr %45, align 8, !tbaa !5
  %47 = load i32, ptr %6, align 4, !tbaa !9
  %48 = load i32, ptr %7, align 4, !tbaa !9
  call void @_ZN7Segment5queryEii(ptr dead_on_unwind writable sret(%struct.Segment) align 8 %8, ptr noundef nonnull align 8 dereferenceable(64) %46, i32 noundef %47, i32 noundef %48)
  %49 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 4
  %50 = getelementptr inbounds [2 x ptr], ptr %49, i64 0, i64 1
  %51 = load ptr, ptr %50, align 8, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = load i32, ptr %7, align 4, !tbaa !9
  call void @_ZN7Segment5queryEii(ptr dead_on_unwind writable sret(%struct.Segment) align 8 %9, ptr noundef nonnull align 8 dereferenceable(64) %51, i32 noundef %52, i32 noundef %53)
  call void @_ZN7Segment5mergeES_S_(ptr noundef nonnull align 8 dereferenceable(64) %0, ptr noundef byval(%struct.Segment) align 8 %8, ptr noundef byval(%struct.Segment) align 8 %9)
  br label %54

54:                                               ; preds = %43, %37, %26, %20
  ret void
}

attributes #0 = { 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 #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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" }

!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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS7Segment", !10, i64 0, !10, i64 4, !10, i64 8, !7, i64 16, !7, i64 48}
!13 = !{!12, !10, i64 4}
!14 = !{i64 0, i64 4, !9, i64 4, i64 4, !9, i64 8, i64 4, !9, i64 16, i64 32, !15, i64 48, i64 16, !15}
!15 = !{!7, !7, i64 0}
!16 = !{!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"

%struct.Segment = type { i32, i32, i32, [4 x i64], [2 x ptr] }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN7Segment5mergeES_S_(ptr noundef nonnull align 8 dereferenceable(64), ptr noundef byval(%struct.Segment) align 8, ptr noundef byval(%struct.Segment) align 8) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress 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) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN7Segment5queryEii(ptr dead_on_unwind noalias writable sret(%struct.Segment) align 8 %0, ptr nocapture noundef nonnull readonly align 8 dereferenceable(64) %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 align 2 {
  %5 = alloca %struct.Segment, align 8
  %6 = alloca %struct.Segment, align 8
  %7 = load i32, ptr %1, align 8, !tbaa !5
  %.not22 = icmp slt i32 %7, %2
  %8 = getelementptr inbounds i8, ptr %1, i64 4
  %9 = load i32, ptr %8, align 4
  %.not1523 = icmp sgt i32 %9, %3
  %or.cond24 = select i1 %.not22, i1 true, i1 %.not1523
  br i1 %or.cond24, label %.lr.ph, label %tailrecurse._crit_edge

tailrecurse._crit_edge:                           ; preds = %tailrecurse.backedge, %4
  %.tr18.lcssa = phi ptr [ %1, %4 ], [ %.tr18.be, %tailrecurse.backedge ]
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(64) %0, ptr noundef nonnull align 8 dereferenceable(64) %.tr18.lcssa, i64 64, i1 false), !tbaa.struct !10
  br label %23

.lr.ph:                                           ; preds = %4, %tailrecurse.backedge
  %.tr1825 = phi ptr [ %.tr18.be, %tailrecurse.backedge ], [ %1, %4 ]
  %10 = getelementptr inbounds i8, ptr %.tr1825, i64 8
  %11 = load i32, ptr %10, align 8, !tbaa !13
  %.not16 = icmp slt i32 %11, %3
  br i1 %.not16, label %16, label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %.lr.ph, %16
  %.sink = phi i64 [ 56, %16 ], [ 48, %.lr.ph ]
  %12 = getelementptr inbounds i8, ptr %.tr1825, i64 %.sink
  %.tr18.be = load ptr, ptr %12, align 8, !tbaa !14
  %13 = load i32, ptr %.tr18.be, align 8, !tbaa !5
  %.not = icmp slt i32 %13, %2
  %14 = getelementptr inbounds i8, ptr %.tr18.be, i64 4
  %15 = load i32, ptr %14, align 4
  %.not15 = icmp sgt i32 %15, %3
  %or.cond = select i1 %.not, i1 true, i1 %.not15
  br i1 %or.cond, label %.lr.ph, label %tailrecurse._crit_edge

16:                                               ; preds = %.lr.ph
  %17 = icmp slt i32 %11, %2
  br i1 %17, label %tailrecurse.backedge, label %18

18:                                               ; preds = %16
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(64) %0, ptr noundef nonnull align 8 dereferenceable(64) %.tr1825, i64 64, i1 false), !tbaa.struct !10
  %19 = getelementptr inbounds i8, ptr %.tr1825, i64 48
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  call void @_ZN7Segment5queryEii(ptr dead_on_unwind nonnull writable sret(%struct.Segment) align 8 %5, ptr noundef nonnull align 8 dereferenceable(64) %20, i32 noundef %2, i32 noundef %3)
  %21 = getelementptr inbounds i8, ptr %.tr1825, i64 56
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  call void @_ZN7Segment5queryEii(ptr dead_on_unwind nonnull writable sret(%struct.Segment) align 8 %6, ptr noundef nonnull align 8 dereferenceable(64) %22, i32 noundef %2, i32 noundef %3)
  call void @_ZN7Segment5mergeES_S_(ptr noundef nonnull align 8 dereferenceable(64) %0, ptr noundef nonnull byval(%struct.Segment) align 8 %5, ptr noundef nonnull byval(%struct.Segment) align 8 %6)
  br label %23

23:                                               ; preds = %18, %tailrecurse._crit_edge
  ret void
}

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !7, i64 0}
!6 = !{!"_ZTS7Segment", !7, i64 0, !7, i64 4, !7, i64 8, !8, i64 16, !8, i64 48}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{i64 0, i64 4, !11, i64 4, i64 4, !11, i64 8, i64 4, !11, i64 16, i64 32, !12, i64 48, i64 16, !12}
!11 = !{!7, !7, i64 0}
!12 = !{!8, !8, i64 0}
!13 = !{!6, !7, i64 8}
!14 = !{!15, !15, i64 0}
!15 = !{!"any pointer", !8, i64 0}


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