; LLVM IR from tangjz/acm-icpc :: usaco/dec22-gold/B.ll (Z6segAskiii)

; 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.Point = type { i32, i32 }
%"struct.std::pair" = type { i32, %struct.Point }

@zero = external dso_local global %struct.Point, align 4
@seg = external dso_local global [4019 x %"struct.std::pair"], 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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6segIdxii(i32 noundef, i32 noundef) #1

; 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 nonnull align 4 dereferenceable(8) ptr @_Z6segAskiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %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
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = load i32, ptr %6, align 4, !tbaa !5
  %14 = call noundef i32 @_Z6segIdxii(i32 noundef %12, i32 noundef %13)
  store i32 %14, ptr %8, align 4, !tbaa !5
  %15 = load i32, ptr %8, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [4019 x %"struct.std::pair"], ptr @seg, i64 0, i64 %16
  %18 = getelementptr inbounds %"struct.std::pair", ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %22, label %21

21:                                               ; preds = %3
  store ptr @zero, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %61

22:                                               ; preds = %3
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = icmp sle i32 %23, %24
  br i1 %25, label %26, label %31

26:                                               ; preds = %22
  %27 = load i32, ptr %8, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [4019 x %"struct.std::pair"], ptr @seg, i64 0, i64 %28
  %30 = getelementptr inbounds %"struct.std::pair", ptr %29, i32 0, i32 1
  store ptr %30, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %61

31:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = add nsw i32 %32, %33
  %35 = ashr i32 %34, 1
  store i32 %35, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %36 = load i32, ptr %7, align 4, !tbaa !5
  %37 = load i32, ptr %10, align 4, !tbaa !5
  %38 = icmp sgt i32 %36, %37
  br i1 %38, label %39, label %45

39:                                               ; preds = %31
  %40 = load i32, ptr %10, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = call noundef nonnull align 4 dereferenceable(8) ptr @_Z6segAskiii(i32 noundef %41, i32 noundef %42, i32 noundef %43)
  br label %46

45:                                               ; preds = %31
  br label %46

46:                                               ; preds = %45, %39
  %47 = phi ptr [ %44, %39 ], [ @zero, %45 ]
  store ptr %47, ptr %11, align 8, !tbaa !12
  %48 = load ptr, ptr %11, align 8, !tbaa !12
  %49 = getelementptr inbounds %struct.Point, ptr %48, i32 0, i32 0
  %50 = load i32, ptr %49, align 4, !tbaa !14
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %57, label %52

52:                                               ; preds = %46
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = load i32, ptr %10, align 4, !tbaa !5
  %55 = load i32, ptr %7, align 4, !tbaa !5
  %56 = call noundef nonnull align 4 dereferenceable(8) ptr @_Z6segAskiii(i32 noundef %53, i32 noundef %54, i32 noundef %55)
  br label %59

57:                                               ; preds = %46
  %58 = load ptr, ptr %11, align 8, !tbaa !12
  br label %59

59:                                               ; preds = %57, %52
  %60 = phi ptr [ %56, %52 ], [ %58, %57 ]
  store ptr %60, ptr %4, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %61

61:                                               ; preds = %59, %26, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %62 = load ptr, ptr %4, align 8
  ret ptr %62
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { 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 = !{!"_ZTSSt4pairIi5PointE", !6, i64 0, !11, i64 4}
!11 = !{!"_ZTS5Point", !6, i64 0, !6, i64 4}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!11, !6, 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.Point = type { i32, i32 }
%"struct.std::pair" = type { i32, %struct.Point }

@zero = external dso_local global %struct.Point, align 4
@seg = external dso_local global [4019 x %"struct.std::pair"], align 16

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef nonnull align 4 dereferenceable(8) ptr @_Z6segAskiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %18, %3
  %.tr23 = phi i32 [ %1, %3 ], [ %13, %18 ]
  %4 = tail call noundef i32 @_Z6segIdxii(i32 noundef %0, i32 noundef %.tr23)
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [4019 x %"struct.std::pair"], ptr @seg, i64 0, i64 %5
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %.loopexit, label %8

8:                                                ; preds = %tailrecurse
  %.not21 = icmp sgt i32 %.tr23, %2
  br i1 %.not21, label %11, label %9

9:                                                ; preds = %8
  %10 = getelementptr inbounds i8, ptr %6, i64 4
  br label %.loopexit

11:                                               ; preds = %8
  %12 = add nsw i32 %.tr23, %0
  %13 = ashr i32 %12, 1
  %14 = icmp slt i32 %13, %2
  br i1 %14, label %15, label %18

15:                                               ; preds = %11
  %16 = add nsw i32 %13, 1
  %17 = tail call noundef nonnull align 4 dereferenceable(8) ptr @_Z6segAskiii(i32 noundef %16, i32 noundef %.tr23, i32 noundef %2)
  br label %18

18:                                               ; preds = %11, %15
  %19 = phi ptr [ %17, %15 ], [ @zero, %11 ]
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %.not22 = icmp eq i32 %20, 0
  br i1 %.not22, label %tailrecurse, label %.loopexit

.loopexit:                                        ; preds = %18, %tailrecurse, %9
  %.0 = phi ptr [ %10, %9 ], [ %19, %18 ], [ @zero, %tailrecurse ]
  ret ptr %.0
}

attributes #0 = { 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 #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" }

!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 = !{!"_ZTSSt4pairIi5PointE", !7, i64 0, !10, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTS5Point", !7, i64 0, !7, i64 4}
!11 = !{!10, !7, i64 0}


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