; LLVM IR from imeplusplus/icpc-notebook :: data-structures/seg_tree2d.ll (Z6countxiiii)

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

@st = external dso_local global [100000005 x i32], align 16
@lc = external dso_local global [100000005 x i32], align 16
@rc = external dso_local global [100000005 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z6countxiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %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 %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
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %16

12:                                               ; preds = %4
  %13 = load i32, ptr %6, align 4, !tbaa !5
  %14 = load i32, ptr %7, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %16, label %17

16:                                               ; preds = %12, %4
  store i32 0, ptr %5, align 4
  br label %51

17:                                               ; preds = %12
  %18 = load i32, ptr %8, align 4, !tbaa !5
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = icmp sle i32 %18, %19
  br i1 %20, label %21, label %26

21:                                               ; preds = %17
  %22 = load i32, ptr %9, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [100000005 x i32], ptr @st, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  store i32 %25, ptr %5, align 4
  br label %51

26:                                               ; preds = %17
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = load i32, ptr %7, align 4, !tbaa !5
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = add nsw i32 %29, %30
  %32 = sdiv i32 %31, 2
  %33 = load i32, ptr %9, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [100000005 x i32], ptr @lc, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = call noundef i32 @_Z6countxiiii(i32 noundef %27, i32 noundef %28, i32 noundef %32, i32 noundef %36)
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = load i32, ptr %7, align 4, !tbaa !5
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = add nsw i32 %39, %40
  %42 = sdiv i32 %41, 2
  %43 = add nsw i32 %42, 1
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = load i32, ptr %9, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [100000005 x i32], ptr @rc, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = call noundef i32 @_Z6countxiiii(i32 noundef %38, i32 noundef %43, i32 noundef %44, i32 noundef %48)
  %50 = add nsw i32 %37, %49
  store i32 %50, ptr %5, align 4
  br label %51

51:                                               ; preds = %26, %21, %16
  %52 = load i32, ptr %5, align 4
  ret i32 %52
}

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

!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"}


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

@st = external dso_local local_unnamed_addr global [100000005 x i32], align 16
@lc = external dso_local local_unnamed_addr global [100000005 x i32], align 16
@rc = external dso_local local_unnamed_addr global [100000005 x i32], align 16

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z6countxiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %.not28 = icmp eq i32 %3, 0
  %5 = icmp slt i32 %0, %1
  %or.cond29 = or i1 %5, %.not28
  br i1 %or.cond29, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %.not21 = icmp sgt i32 %2, %0
  br i1 %.not21, label %tailrecurse.us, label %.lr.ph.split

tailrecurse.us:                                   ; preds = %.lr.ph, %tailrecurse.us
  %.tr2432.us = phi i32 [ %14, %tailrecurse.us ], [ %3, %.lr.ph ]
  %.tr2231.us = phi i32 [ %12, %tailrecurse.us ], [ %1, %.lr.ph ]
  %accumulator.tr30.us = phi i32 [ %15, %tailrecurse.us ], [ 0, %.lr.ph ]
  %6 = add nsw i32 %.tr2231.us, %2
  %7 = sdiv i32 %6, 2
  %8 = sext i32 %.tr2432.us to i64
  %9 = getelementptr inbounds [100000005 x i32], ptr @lc, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = tail call noundef i32 @_Z6countxiiii(i32 noundef %0, i32 noundef %.tr2231.us, i32 noundef %7, i32 noundef %10)
  %12 = add nsw i32 %7, 1
  %13 = getelementptr inbounds [100000005 x i32], ptr @rc, i64 0, i64 %8
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = add nsw i32 %11, %accumulator.tr30.us
  %.not.us = icmp eq i32 %14, 0
  %16 = icmp sge i32 %7, %0
  %or.cond.us = or i1 %16, %.not.us
  br i1 %or.cond.us, label %.loopexit, label %tailrecurse.us

.lr.ph.split:                                     ; preds = %.lr.ph
  %17 = sext i32 %3 to i64
  %18 = getelementptr inbounds [100000005 x i32], ptr @st, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %tailrecurse.us, %4, %.lr.ph.split
  %accumulator.ret.tr = phi i32 [ %19, %.lr.ph.split ], [ 0, %4 ], [ %15, %tailrecurse.us ]
  ret i32 %accumulator.ret.tr
}

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


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