; LLVM IR from tangjz/acm-icpc :: codeforces/gym/102012E.ll (Z7seg_queiiiiRx)

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

@seg = external dso_local global ptr, align 8

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z7seg_queiix(i32 noundef, i32 noundef, i64 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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z8seg_downii(i32 noundef, i32 noundef) #3

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7seg_queiiiiRx(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, ptr noundef nonnull align 8 dereferenceable(8) %4) #0 {
  %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 ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = 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 ptr %4, ptr %11, align 8, !tbaa !9
  %15 = load i32, ptr %9, align 4, !tbaa !5
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = icmp sle i32 %15, %16
  br i1 %17, label %18, label %40

18:                                               ; preds = %5
  %19 = load i32, ptr %8, align 4, !tbaa !5
  %20 = load i32, ptr %10, align 4, !tbaa !5
  %21 = icmp sle i32 %19, %20
  br i1 %21, label %22, label %40

22:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = load i32, ptr %8, align 4, !tbaa !5
  %25 = load ptr, ptr %11, align 8, !tbaa !9
  %26 = load i64, ptr %25, align 8, !tbaa !11
  %27 = call noundef i32 @_Z7seg_queiix(i32 noundef %23, i32 noundef %24, i64 noundef %26)
  store i32 %27, ptr %12, align 4, !tbaa !5
  %28 = load ptr, ptr %11, align 8, !tbaa !9
  %29 = load ptr, ptr @seg, align 8, !tbaa !9
  %30 = load i32, ptr %7, align 4, !tbaa !5
  %31 = load i32, ptr %8, align 4, !tbaa !5
  %32 = call noundef i32 @_Z7seg_idxii(i32 noundef %30, i32 noundef %31)
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.Segment, ptr %29, i64 %33
  %35 = getelementptr inbounds %struct.Segment, ptr %34, i32 0, i32 2
  %36 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %28, ptr noundef nonnull align 8 dereferenceable(8) %35)
  %37 = load i64, ptr %36, align 8, !tbaa !11
  %38 = load ptr, ptr %11, align 8, !tbaa !9
  store i64 %37, ptr %38, align 8, !tbaa !11
  %39 = load i32, ptr %12, align 4, !tbaa !5
  store i32 %39, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %75

40:                                               ; preds = %18, %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = add nsw i32 %41, %42
  %44 = ashr i32 %43, 1
  store i32 %44, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  store i32 0, ptr %14, align 4, !tbaa !5
  %45 = load i32, ptr %7, align 4, !tbaa !5
  %46 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z8seg_downii(i32 noundef %45, i32 noundef %46)
  %47 = load i32, ptr %9, align 4, !tbaa !5
  %48 = load i32, ptr %13, align 4, !tbaa !5
  %49 = icmp sle i32 %47, %48
  br i1 %49, label %50, label %59

50:                                               ; preds = %40
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = load i32, ptr %13, align 4, !tbaa !5
  %53 = load i32, ptr %9, align 4, !tbaa !5
  %54 = load i32, ptr %10, align 4, !tbaa !5
  %55 = load ptr, ptr %11, align 8, !tbaa !9
  %56 = call noundef i32 @_Z7seg_queiiiiRx(i32 noundef %51, i32 noundef %52, i32 noundef %53, i32 noundef %54, ptr noundef nonnull align 8 dereferenceable(8) %55)
  %57 = load i32, ptr %14, align 4, !tbaa !5
  %58 = add nsw i32 %57, %56
  store i32 %58, ptr %14, align 4, !tbaa !5
  br label %59

59:                                               ; preds = %50, %40
  %60 = load i32, ptr %13, align 4, !tbaa !5
  %61 = load i32, ptr %10, align 4, !tbaa !5
  %62 = icmp slt i32 %60, %61
  br i1 %62, label %63, label %73

63:                                               ; preds = %59
  %64 = load i32, ptr %13, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  %66 = load i32, ptr %8, align 4, !tbaa !5
  %67 = load i32, ptr %9, align 4, !tbaa !5
  %68 = load i32, ptr %10, align 4, !tbaa !5
  %69 = load ptr, ptr %11, align 8, !tbaa !9
  %70 = call noundef i32 @_Z7seg_queiiiiRx(i32 noundef %65, i32 noundef %66, i32 noundef %67, i32 noundef %68, ptr noundef nonnull align 8 dereferenceable(8) %69)
  %71 = load i32, ptr %14, align 4, !tbaa !5
  %72 = add nsw i32 %71, %70
  store i32 %72, ptr %14, align 4, !tbaa !5
  br label %73

73:                                               ; preds = %63, %59
  %74 = load i32, ptr %14, align 4, !tbaa !5
  store i32 %74, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  br label %75

75:                                               ; preds = %73, %22
  %76 = load i32, ptr %6, align 4
  ret i32 %76
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

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 = { inlinehint 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 = { inlinehint 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 #4 = { 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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}


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

@seg = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z7seg_queiix(i32 noundef, i32 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z8seg_downii(i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7seg_queiiiiRx(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, ptr noundef nonnull align 8 dereferenceable(8) %4) local_unnamed_addr #0 {
  %.not40 = icmp sgt i32 %1, %3
  %.not49 = icmp sgt i32 %2, %0
  %or.cond50 = or i1 %.not40, %.not49
  br i1 %or.cond50, label %.lr.ph, label %tailrecurse._crit_edge

.lr.ph:                                           ; preds = %5
  br i1 %.not40, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %tailrecurse.us
  %.tr52.us = phi i32 [ %12, %tailrecurse.us ], [ %0, %.lr.ph ]
  %accumulator.tr51.us = phi i32 [ %13, %tailrecurse.us ], [ 0, %.lr.ph ]
  %6 = add nsw i32 %.tr52.us, %1
  %7 = ashr i32 %6, 1
  tail call void @_Z8seg_downii(i32 noundef %.tr52.us, i32 noundef %1)
  %.not41.us = icmp slt i32 %7, %2
  br i1 %.not41.us, label %10, label %8

8:                                                ; preds = %.lr.ph.split.us
  %9 = tail call noundef i32 @_Z7seg_queiiiiRx(i32 noundef %.tr52.us, i32 noundef %7, i32 noundef %2, i32 noundef %3, ptr noundef nonnull align 8 dereferenceable(8) %4)
  br label %10

10:                                               ; preds = %8, %.lr.ph.split.us
  %.0.us = phi i32 [ %9, %8 ], [ 0, %.lr.ph.split.us ]
  %11 = icmp slt i32 %7, %3
  br i1 %11, label %tailrecurse.us, label %.loopexit

tailrecurse.us:                                   ; preds = %10
  %12 = add nsw i32 %7, 1
  %13 = add nsw i32 %.0.us, %accumulator.tr51.us
  br label %.lr.ph.split.us

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %5
  %accumulator.tr.lcssa = phi i32 [ 0, %5 ], [ %29, %tailrecurse ]
  %.tr.lcssa = phi i32 [ %0, %5 ], [ %28, %tailrecurse ]
  %14 = load i64, ptr %4, align 8, !tbaa !5
  %15 = tail call noundef i32 @_Z7seg_queiix(i32 noundef %.tr.lcssa, i32 noundef %1, i64 noundef %14)
  %16 = load ptr, ptr @seg, align 8, !tbaa !9
  %17 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %.tr.lcssa, i32 noundef %1)
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds %struct.Segment, ptr %16, i64 %18, i32 2
  %20 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %4, ptr noundef nonnull align 8 dereferenceable(8) %19)
  %21 = load i64, ptr %20, align 8, !tbaa !5
  store i64 %21, ptr %4, align 8, !tbaa !5
  br label %.loopexit

.lr.ph.split:                                     ; preds = %.lr.ph, %tailrecurse
  %.tr52 = phi i32 [ %28, %tailrecurse ], [ %0, %.lr.ph ]
  %accumulator.tr51 = phi i32 [ %29, %tailrecurse ], [ 0, %.lr.ph ]
  %22 = add nsw i32 %.tr52, %1
  %23 = ashr i32 %22, 1
  tail call void @_Z8seg_downii(i32 noundef %.tr52, i32 noundef %1)
  %.not41 = icmp slt i32 %23, %2
  br i1 %.not41, label %26, label %24

24:                                               ; preds = %.lr.ph.split
  %25 = tail call noundef i32 @_Z7seg_queiiiiRx(i32 noundef %.tr52, i32 noundef %23, i32 noundef %2, i32 noundef %3, ptr noundef nonnull align 8 dereferenceable(8) %4)
  br label %26

26:                                               ; preds = %24, %.lr.ph.split
  %.0 = phi i32 [ %25, %24 ], [ 0, %.lr.ph.split ]
  %27 = icmp slt i32 %23, %3
  br i1 %27, label %tailrecurse, label %.loopexit

tailrecurse:                                      ; preds = %26
  %28 = add nsw i32 %23, 1
  %29 = add nsw i32 %.0, %accumulator.tr51
  %.not = icmp slt i32 %28, %2
  br i1 %.not, label %.lr.ph.split, label %tailrecurse._crit_edge

.loopexit:                                        ; preds = %26, %10, %tailrecurse._crit_edge
  %accumulator.tr48 = phi i32 [ %accumulator.tr.lcssa, %tailrecurse._crit_edge ], [ %accumulator.tr51.us, %10 ], [ %accumulator.tr51, %26 ]
  %.033 = phi i32 [ %15, %tailrecurse._crit_edge ], [ %.0.us, %10 ], [ %.0, %26 ]
  %accumulator.ret.tr = add nsw i32 %.033, %accumulator.tr48
  ret i32 %accumulator.ret.tr
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #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 = { inlinehint 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 = { inlinehint 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}


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