; LLVM IR from tangjz/acm-icpc :: codeforces/complete/853C.ll (Z5queryiiiii)

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

@seg = external dso_local global [4200021 x %struct.Segment], align 16

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5queryiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i32 %0, ptr %7, align 4, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !5
  %14 = load i32, ptr %10, align 4, !tbaa !5
  %15 = load i32, ptr %8, align 4, !tbaa !5
  %16 = icmp sle i32 %14, %15
  br i1 %16, label %17, label %27

17:                                               ; preds = %5
  %18 = load i32, ptr %9, align 4, !tbaa !5
  %19 = load i32, ptr %11, align 4, !tbaa !5
  %20 = icmp sle i32 %18, %19
  br i1 %20, label %21, label %27

21:                                               ; preds = %17
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [4200021 x %struct.Segment], ptr @seg, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.Segment, ptr %24, i32 0, i32 0
  %26 = load i32, ptr %25, align 4, !tbaa !9
  store i32 %26, ptr %6, align 4
  br label %86

27:                                               ; preds = %17, %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %28 = load i32, ptr %8, align 4, !tbaa !5
  %29 = load i32, ptr %9, align 4, !tbaa !5
  %30 = add nsw i32 %28, %29
  %31 = ashr i32 %30, 1
  store i32 %31, ptr %12, align 4, !tbaa !5
  %32 = load i32, ptr %11, align 4, !tbaa !5
  %33 = load i32, ptr %12, align 4, !tbaa !5
  %34 = icmp sle i32 %32, %33
  br i1 %34, label %35, label %46

35:                                               ; preds = %27
  %36 = load i32, ptr %7, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [4200021 x %struct.Segment], ptr @seg, i64 0, i64 %37
  %39 = getelementptr inbounds %struct.Segment, ptr %38, i32 0, i32 1
  %40 = load i32, ptr %39, align 4, !tbaa !11
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = load i32, ptr %12, align 4, !tbaa !5
  %43 = load i32, ptr %10, align 4, !tbaa !5
  %44 = load i32, ptr %11, align 4, !tbaa !5
  %45 = call noundef i32 @_Z5queryiiiii(i32 noundef %40, i32 noundef %41, i32 noundef %42, i32 noundef %43, i32 noundef %44)
  store i32 %45, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %85

46:                                               ; preds = %27
  %47 = load i32, ptr %12, align 4, !tbaa !5
  %48 = load i32, ptr %10, align 4, !tbaa !5
  %49 = icmp slt i32 %47, %48
  br i1 %49, label %50, label %62

50:                                               ; preds = %46
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [4200021 x %struct.Segment], ptr @seg, i64 0, i64 %52
  %54 = getelementptr inbounds %struct.Segment, ptr %53, i32 0, i32 2
  %55 = load i32, ptr %54, align 4, !tbaa !12
  %56 = load i32, ptr %12, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  %58 = load i32, ptr %9, align 4, !tbaa !5
  %59 = load i32, ptr %10, align 4, !tbaa !5
  %60 = load i32, ptr %11, align 4, !tbaa !5
  %61 = call noundef i32 @_Z5queryiiiii(i32 noundef %55, i32 noundef %57, i32 noundef %58, i32 noundef %59, i32 noundef %60)
  store i32 %61, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %85

62:                                               ; preds = %46
  %63 = load i32, ptr %7, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [4200021 x %struct.Segment], ptr @seg, i64 0, i64 %64
  %66 = getelementptr inbounds %struct.Segment, ptr %65, i32 0, i32 1
  %67 = load i32, ptr %66, align 4, !tbaa !11
  %68 = load i32, ptr %8, align 4, !tbaa !5
  %69 = load i32, ptr %12, align 4, !tbaa !5
  %70 = load i32, ptr %10, align 4, !tbaa !5
  %71 = load i32, ptr %11, align 4, !tbaa !5
  %72 = call noundef i32 @_Z5queryiiiii(i32 noundef %67, i32 noundef %68, i32 noundef %69, i32 noundef %70, i32 noundef %71)
  %73 = load i32, ptr %7, align 4, !tbaa !5
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [4200021 x %struct.Segment], ptr @seg, i64 0, i64 %74
  %76 = getelementptr inbounds %struct.Segment, ptr %75, i32 0, i32 2
  %77 = load i32, ptr %76, align 4, !tbaa !12
  %78 = load i32, ptr %12, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  %80 = load i32, ptr %9, align 4, !tbaa !5
  %81 = load i32, ptr %10, align 4, !tbaa !5
  %82 = load i32, ptr %11, align 4, !tbaa !5
  %83 = call noundef i32 @_Z5queryiiiii(i32 noundef %77, i32 noundef %79, i32 noundef %80, i32 noundef %81, i32 noundef %82)
  %84 = add nsw i32 %72, %83
  store i32 %84, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %85

85:                                               ; preds = %62, %50, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %86

86:                                               ; preds = %85, %21
  %87 = load i32, ptr %6, align 4
  ret i32 %87
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTS7Segment", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, 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 }

@seg = external dso_local local_unnamed_addr global [4200021 x %struct.Segment], align 16

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryiiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %.not6577 = icmp sgt i32 %3, %1
  %.not38516678 = icmp sgt i32 %2, %4
  %or.cond526779 = or i1 %.not6577, %.not38516678
  br i1 %or.cond526779, label %.lr.ph.lr.ph, label %tailrecurse.outer44._crit_edge

.lr.ph.lr.ph:                                     ; preds = %5, %tailrecurse.outer
  %.not6584 = phi i1 [ %.not65, %tailrecurse.outer ], [ %.not6577, %5 ]
  %.tr41.ph83 = phi i32 [ %.us-phi59, %tailrecurse.outer ], [ %2, %5 ]
  %.tr40.ph82 = phi i32 [ %33, %tailrecurse.outer ], [ %1, %5 ]
  %.tr.ph81 = phi i32 [ %32, %tailrecurse.outer ], [ %0, %5 ]
  %accumulator.tr.ph80 = phi i32 [ %34, %tailrecurse.outer ], [ 0, %5 ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.lr.ph, %tailrecurse.outer44
  %.not72 = phi i1 [ %.not6584, %.lr.ph.lr.ph ], [ %.not, %tailrecurse.outer44 ]
  %.tr41.ph4771 = phi i32 [ %.tr41.ph83, %.lr.ph.lr.ph ], [ %.us-phi59, %tailrecurse.outer44 ]
  %.tr40.ph4669 = phi i32 [ %.tr40.ph82, %.lr.ph.lr.ph ], [ %26, %tailrecurse.outer44 ]
  %.tr.ph4568 = phi i32 [ %.tr.ph81, %.lr.ph.lr.ph ], [ %25, %tailrecurse.outer44 ]
  br i1 %.not72, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph
  %6 = add i32 %.tr41.ph4771, %.tr40.ph4669
  %7 = ashr i32 %6, 1
  %.not39.us60 = icmp slt i32 %7, %4
  br i1 %.not39.us60, label %.split.us, label %tailrecurse.us

tailrecurse.us:                                   ; preds = %.lr.ph.split.us, %tailrecurse.us
  %8 = phi i32 [ %13, %tailrecurse.us ], [ %7, %.lr.ph.split.us ]
  %.tr53.us61 = phi i32 [ %11, %tailrecurse.us ], [ %.tr.ph4568, %.lr.ph.split.us ]
  %9 = sext i32 %.tr53.us61 to i64
  %10 = getelementptr inbounds [4200021 x %struct.Segment], ptr @seg, i64 0, i64 %9, i32 1
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = add i32 %8, %.tr40.ph4669
  %13 = ashr i32 %12, 1
  %.not39.us = icmp slt i32 %13, %4
  br i1 %.not39.us, label %.split.us, label %tailrecurse.us

tailrecurse.outer44._crit_edge:                   ; preds = %tailrecurse.outer, %tailrecurse.outer44, %tailrecurse, %5
  %accumulator.tr.ph.lcssa = phi i32 [ 0, %5 ], [ %accumulator.tr.ph80, %tailrecurse ], [ %accumulator.tr.ph80, %tailrecurse.outer44 ], [ %34, %tailrecurse.outer ]
  %.tr.lcssa = phi i32 [ %0, %5 ], [ %21, %tailrecurse ], [ %25, %tailrecurse.outer44 ], [ %32, %tailrecurse.outer ]
  %14 = sext i32 %.tr.lcssa to i64
  %15 = getelementptr inbounds [4200021 x %struct.Segment], ptr @seg, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !10
  %accumulator.ret.tr = add nsw i32 %16, %accumulator.tr.ph.lcssa
  ret i32 %accumulator.ret.tr

.lr.ph.split:                                     ; preds = %.lr.ph, %tailrecurse
  %.tr4154 = phi i32 [ %18, %tailrecurse ], [ %.tr41.ph4771, %.lr.ph ]
  %.tr53 = phi i32 [ %21, %tailrecurse ], [ %.tr.ph4568, %.lr.ph ]
  %17 = add i32 %.tr4154, %.tr40.ph4669
  %18 = ashr i32 %17, 1
  %.not39 = icmp slt i32 %18, %4
  br i1 %.not39, label %.split.us, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph.split
  %19 = sext i32 %.tr53 to i64
  %20 = getelementptr inbounds [4200021 x %struct.Segment], ptr @seg, i64 0, i64 %19, i32 1
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %.not38 = icmp sgt i32 %18, %4
  br i1 %.not38, label %.lr.ph.split, label %tailrecurse.outer44._crit_edge

.split.us:                                        ; preds = %.lr.ph.split, %tailrecurse.us, %.lr.ph.split.us
  %.us-phi = phi i32 [ %7, %.lr.ph.split.us ], [ %13, %tailrecurse.us ], [ %18, %.lr.ph.split ]
  %.us-phi58 = phi i32 [ %.tr.ph4568, %.lr.ph.split.us ], [ %11, %tailrecurse.us ], [ %.tr53, %.lr.ph.split ]
  %.us-phi59 = phi i32 [ %.tr41.ph4771, %.lr.ph.split.us ], [ %8, %tailrecurse.us ], [ %.tr4154, %.lr.ph.split ]
  %22 = icmp slt i32 %.us-phi, %3
  %23 = sext i32 %.us-phi58 to i64
  br i1 %22, label %tailrecurse.outer44, label %tailrecurse.outer

tailrecurse.outer44:                              ; preds = %.split.us
  %24 = getelementptr inbounds [4200021 x %struct.Segment], ptr @seg, i64 0, i64 %23, i32 2
  %25 = load i32, ptr %24, align 4, !tbaa !11
  %26 = add nsw i32 %.us-phi, 1
  %.not = icmp slt i32 %26, %3
  %.not3851 = icmp sgt i32 %.us-phi59, %4
  %or.cond52 = or i1 %.not, %.not3851
  br i1 %or.cond52, label %.lr.ph, label %tailrecurse.outer44._crit_edge

tailrecurse.outer:                                ; preds = %.split.us
  %27 = getelementptr inbounds [4200021 x %struct.Segment], ptr @seg, i64 0, i64 %23
  %28 = getelementptr inbounds i8, ptr %27, i64 4
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = tail call noundef i32 @_Z5queryiiiii(i32 noundef %29, i32 noundef %.tr40.ph4669, i32 noundef %.us-phi, i32 noundef %3, i32 noundef %4)
  %31 = getelementptr inbounds i8, ptr %27, i64 8
  %32 = load i32, ptr %31, align 4, !tbaa !11
  %33 = add nsw i32 %.us-phi, 1
  %34 = add nsw i32 %30, %accumulator.tr.ph80
  %.not65 = icmp slt i32 %33, %3
  %.not385166 = icmp sgt i32 %.us-phi59, %4
  %or.cond5267 = or i1 %.not65, %.not385166
  br i1 %or.cond5267, label %.lr.ph.lr.ph, label %tailrecurse.outer44._crit_edge
}

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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, !7, i64 4}
!6 = !{!"_ZTS7Segment", !7, i64 0, !7, i64 4, !7, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!6, !7, i64 8}


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