; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2016-17/Feb 2017/Plat/17 Feb P3 (Segtree).ll (Z6updateiiii)

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

@segbit = external dso_local global [20000000 x %"struct.std::pair.0"], align 16

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6updateiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %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
  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
  store i32 %3, ptr %8, align 4, !tbaa !5
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %11
  %13 = getelementptr inbounds %"struct.std::pair.0", ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %13, align 4, !tbaa !9
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = load i32, ptr %8, align 4, !tbaa !5
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %24

19:                                               ; preds = %4
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = load i32, ptr %8, align 4, !tbaa !5
  %22 = icmp eq i32 %20, %21
  br i1 %22, label %23, label %24

23:                                               ; preds = %19
  br label %46

24:                                               ; preds = %19, %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = add nsw i32 %25, %26
  %28 = sdiv i32 %27, 2
  store i32 %28, ptr %9, align 4, !tbaa !5
  %29 = load i32, ptr %9, align 4, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = icmp sge i32 %29, %30
  br i1 %31, label %32, label %38

32:                                               ; preds = %24
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = call noundef i32 @_Z7getnodeii(i32 noundef %33, i32 noundef 0)
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = load i32, ptr %9, align 4, !tbaa !5
  %37 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z6updateiiii(i32 noundef %34, i32 noundef %35, i32 noundef %36, i32 noundef %37)
  br label %45

38:                                               ; preds = %24
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = call noundef i32 @_Z7getnodeii(i32 noundef %39, i32 noundef 1)
  %41 = load i32, ptr %9, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z6updateiiii(i32 noundef %40, i32 noundef %42, i32 noundef %43, i32 noundef %44)
  br label %45

45:                                               ; preds = %38, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %46

46:                                               ; preds = %45, %23
  ret void
}

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

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

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 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"_ZTSSt4pairIiS_IiiEE", !6, i64 0, !11, i64 4}
!11 = !{!"_ZTSSt4pairIiiE", !6, i64 0, !6, i64 4}


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

@segbit = external dso_local local_unnamed_addr global [20000000 x %"struct.std::pair.0"], align 16

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z6updateiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i32 %1, %3
  %6 = sext i32 %0 to i64
  %7 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr %7, align 4, !tbaa !5
  %10 = icmp eq i32 %2, %3
  %or.cond2433 = and i1 %5, %10
  br i1 %or.cond2433, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %tailrecurse.outer
  %11 = phi i1 [ %32, %tailrecurse.outer ], [ %5, %4 ]
  %.tr20.ph36 = phi i32 [ %.us-phi28, %tailrecurse.outer ], [ %2, %4 ]
  %.tr19.ph35 = phi i32 [ %31, %tailrecurse.outer ], [ %1, %4 ]
  %.tr.ph34 = phi i32 [ %30, %tailrecurse.outer ], [ %0, %4 ]
  br i1 %11, label %.lr.ph.split, label %.lr.ph.split.us

.lr.ph.split.us:                                  ; preds = %.lr.ph
  %12 = add i32 %.tr20.ph36, %.tr19.ph35
  %13 = sdiv i32 %12, 2
  %.not.us29 = icmp slt i32 %13, %3
  br i1 %.not.us29, label %tailrecurse.outer, label %tailrecurse.us

tailrecurse.us:                                   ; preds = %.lr.ph.split.us, %tailrecurse.us
  %14 = phi i32 [ %21, %tailrecurse.us ], [ %13, %.lr.ph.split.us ]
  %.tr25.us30 = phi i32 [ %15, %tailrecurse.us ], [ %.tr.ph34, %.lr.ph.split.us ]
  %15 = tail call noundef i32 @_Z7getnodeii(i32 noundef %.tr25.us30, i32 noundef 0)
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %17, align 4, !tbaa !5
  %20 = add i32 %14, %.tr19.ph35
  %21 = sdiv i32 %20, 2
  %.not.us = icmp slt i32 %21, %3
  br i1 %.not.us, label %tailrecurse.outer, label %tailrecurse.us

.lr.ph.split:                                     ; preds = %.lr.ph, %tailrecurse
  %.tr2026 = phi i32 [ %23, %tailrecurse ], [ %.tr20.ph36, %.lr.ph ]
  %.tr25 = phi i32 [ %24, %tailrecurse ], [ %.tr.ph34, %.lr.ph ]
  %22 = add i32 %.tr2026, %.tr19.ph35
  %23 = sdiv i32 %22, 2
  %.not = icmp slt i32 %23, %3
  br i1 %.not, label %tailrecurse.outer, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph.split
  %24 = tail call noundef i32 @_Z7getnodeii(i32 noundef %.tr25, i32 noundef 0)
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %26, align 4, !tbaa !5
  %29 = icmp eq i32 %23, %3
  br i1 %29, label %tailrecurse.outer._crit_edge, label %.lr.ph.split

tailrecurse.outer:                                ; preds = %tailrecurse.us, %.lr.ph.split, %.lr.ph.split.us
  %.us-phi = phi i32 [ %13, %.lr.ph.split.us ], [ %23, %.lr.ph.split ], [ %21, %tailrecurse.us ]
  %.us-phi27 = phi i32 [ %.tr.ph34, %.lr.ph.split.us ], [ %.tr25, %.lr.ph.split ], [ %15, %tailrecurse.us ]
  %.us-phi28 = phi i32 [ %.tr20.ph36, %.lr.ph.split.us ], [ %.tr2026, %.lr.ph.split ], [ %14, %tailrecurse.us ]
  %30 = tail call noundef i32 @_Z7getnodeii(i32 noundef %.us-phi27, i32 noundef 1)
  %31 = add nsw i32 %.us-phi, 1
  %32 = icmp eq i32 %31, %3
  %33 = sext i32 %30 to i64
  %34 = getelementptr inbounds [20000000 x %"struct.std::pair.0"], ptr @segbit, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %34, align 4, !tbaa !5
  %37 = icmp eq i32 %.us-phi28, %3
  %or.cond24 = and i1 %32, %37
  br i1 %or.cond24, label %tailrecurse.outer._crit_edge, label %.lr.ph

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse, %4
  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" }

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


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