; LLVM IR from tangjz/acm-icpc :: 51nod/1175.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 [850017 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 nounwind 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
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  store i32 %4, ptr %10, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %72, %5
  %14 = load i32, ptr %8, align 4, !tbaa !5
  %15 = load i32, ptr %9, align 4, !tbaa !5
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %17, label %73

17:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %18 = load i32, ptr %8, align 4, !tbaa !5
  %19 = load i32, ptr %9, align 4, !tbaa !5
  %20 = add nsw i32 %18, %19
  %21 = ashr i32 %20, 1
  store i32 %21, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.Segment, ptr %24, i32 0, i32 2
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.Segment, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 4, !tbaa !11
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %32
  %34 = getelementptr inbounds %struct.Segment, ptr %33, i32 0, i32 2
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.Segment, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 4, !tbaa !11
  %40 = sub nsw i32 %30, %39
  store i32 %40, ptr %12, align 4, !tbaa !5
  %41 = load i32, ptr %12, align 4, !tbaa !5
  %42 = load i32, ptr %10, align 4, !tbaa !5
  %43 = icmp slt i32 %41, %42
  br i1 %43, label %44, label %59

44:                                               ; preds = %17
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %46
  %48 = getelementptr inbounds %struct.Segment, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 4, !tbaa !12
  store i32 %49, ptr %6, align 4, !tbaa !5
  %50 = load i32, ptr %7, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %51
  %53 = getelementptr inbounds %struct.Segment, ptr %52, i32 0, i32 1
  %54 = load i32, ptr %53, align 4, !tbaa !12
  store i32 %54, ptr %7, align 4, !tbaa !5
  %55 = load i32, ptr %11, align 4, !tbaa !5
  store i32 %55, ptr %9, align 4, !tbaa !5
  %56 = load i32, ptr %12, align 4, !tbaa !5
  %57 = load i32, ptr %10, align 4, !tbaa !5
  %58 = sub nsw i32 %57, %56
  store i32 %58, ptr %10, align 4, !tbaa !5
  br label %72

59:                                               ; preds = %17
  %60 = load i32, ptr %6, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %61
  %63 = getelementptr inbounds %struct.Segment, ptr %62, i32 0, i32 2
  %64 = load i32, ptr %63, align 4, !tbaa !9
  store i32 %64, ptr %6, align 4, !tbaa !5
  %65 = load i32, ptr %7, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %66
  %68 = getelementptr inbounds %struct.Segment, ptr %67, i32 0, i32 2
  %69 = load i32, ptr %68, align 4, !tbaa !9
  store i32 %69, ptr %7, align 4, !tbaa !5
  %70 = load i32, ptr %11, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %8, align 4, !tbaa !5
  br label %72

72:                                               ; preds = %59, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %13, !llvm.loop !13

73:                                               ; preds = %13
  %74 = load i32, ptr %8, align 4, !tbaa !5
  ret i32 %74
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 8}
!10 = !{!"_ZTS7Segment", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = !{!10, !6, i64 0}
!12 = !{!10, !6, i64 4}
!13 = distinct !{!13, !14}
!14 = !{!"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.Segment = type { i32, i32, i32 }

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

; Function Attrs: mustprogress nofree norecurse 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 {
  %6 = icmp slt i32 %2, %3
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %5, %33
  %.030 = phi i32 [ %.1, %33 ], [ %0, %5 ]
  %.01729 = phi i32 [ %.118, %33 ], [ %1, %5 ]
  %.01928 = phi i32 [ %.120, %33 ], [ %4, %5 ]
  %.02127 = phi i32 [ %.122, %33 ], [ %3, %5 ]
  %.02326 = phi i32 [ %.124, %33 ], [ %2, %5 ]
  %7 = add nsw i32 %.02127, %.02326
  %8 = ashr i32 %7, 1
  %9 = sext i32 %.01729 to i64
  %10 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %9
  %11 = getelementptr inbounds i8, ptr %10, i64 8
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !10
  %16 = sext i32 %.030 to i64
  %17 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %16
  %18 = getelementptr inbounds i8, ptr %17, i64 8
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [850017 x %struct.Segment], ptr @seg, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !10
  %23 = sub nsw i32 %15, %22
  %24 = icmp slt i32 %23, %.01928
  br i1 %24, label %25, label %31

25:                                               ; preds = %.lr.ph
  %26 = getelementptr inbounds i8, ptr %17, i64 4
  %27 = load i32, ptr %26, align 4, !tbaa !11
  %28 = getelementptr inbounds i8, ptr %10, i64 4
  %29 = load i32, ptr %28, align 4, !tbaa !11
  %30 = sub nsw i32 %.01928, %23
  br label %33

31:                                               ; preds = %.lr.ph
  %32 = add nsw i32 %8, 1
  br label %33

33:                                               ; preds = %31, %25
  %.124 = phi i32 [ %.02326, %25 ], [ %32, %31 ]
  %.122 = phi i32 [ %8, %25 ], [ %.02127, %31 ]
  %.120 = phi i32 [ %30, %25 ], [ %.01928, %31 ]
  %.118 = phi i32 [ %29, %25 ], [ %12, %31 ]
  %.1 = phi i32 [ %27, %25 ], [ %19, %31 ]
  %34 = icmp slt i32 %.124, %.122
  br i1 %34, label %.lr.ph, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %33, %5
  %.023.lcssa = phi i32 [ %2, %5 ], [ %.124, %33 ]
  ret i32 %.023.lcssa
}

attributes #0 = { mustprogress nofree norecurse 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 8}
!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 4}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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