; LLVM IR from imteekay/algorithms :: competitive-programming/acm-icpc-br/regionals_2017_1/A.ll (Z13build_segtreePiiii)

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

@segtree = external dso_local global [1048576 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z13build_segtreePiiii(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  %10 = load i32, ptr %7, align 4, !tbaa !9
  %11 = load i32, ptr %8, align 4, !tbaa !9
  %12 = icmp eq i32 %10, %11
  br i1 %12, label %13, label %22

13:                                               ; preds = %4
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds i32, ptr %14, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !9
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1048576 x i32], ptr @segtree, i64 0, i64 %20
  store i32 %18, ptr %21, align 4, !tbaa !9
  br label %39

22:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = load i32, ptr %8, align 4, !tbaa !9
  %25 = add nsw i32 %23, %24
  %26 = sdiv i32 %25, 2
  store i32 %26, ptr %9, align 4, !tbaa !9
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = mul nsw i32 %28, 2
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = load i32, ptr %9, align 4, !tbaa !9
  call void @_Z13build_segtreePiiii(ptr noundef %27, i32 noundef %29, i32 noundef %30, i32 noundef %31)
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = mul nsw i32 %33, 2
  %35 = add nsw i32 %34, 1
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  %38 = load i32, ptr %8, align 4, !tbaa !9
  call void @_Z13build_segtreePiiii(ptr noundef %32, i32 noundef %35, i32 noundef %37, i32 noundef %38)
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %39

39:                                               ; preds = %22, %13
  ret void
}

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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"

@segtree = external dso_local local_unnamed_addr global [1048576 x i32], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(write, argmem: read, inaccessiblemem: none) uwtable
define dso_local void @_Z13build_segtreePiiii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i32 %2, %3
  br i1 %5, label %tailrecurse._crit_edge, label %tailrecurse

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %4
  %.tr15.lcssa = phi i32 [ %1, %4 ], [ %14, %tailrecurse ]
  %.tr16.lcssa = phi i32 [ %2, %4 ], [ %3, %tailrecurse ]
  %6 = sext i32 %.tr16.lcssa to i64
  %7 = getelementptr inbounds i32, ptr %0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = sext i32 %.tr15.lcssa to i64
  %10 = getelementptr inbounds [1048576 x i32], ptr @segtree, i64 0, i64 %9
  store i32 %8, ptr %10, align 4, !tbaa !5
  ret void

tailrecurse:                                      ; preds = %4, %tailrecurse
  %.tr1619 = phi i32 [ %15, %tailrecurse ], [ %2, %4 ]
  %.tr1518 = phi i32 [ %14, %tailrecurse ], [ %1, %4 ]
  %11 = add nsw i32 %.tr1619, %3
  %12 = sdiv i32 %11, 2
  %13 = shl nsw i32 %.tr1518, 1
  tail call void @_Z13build_segtreePiiii(ptr noundef %0, i32 noundef %13, i32 noundef %.tr1619, i32 noundef %12)
  %14 = or disjoint i32 %13, 1
  %15 = add nsw i32 %12, 1
  %16 = icmp eq i32 %15, %3
  br i1 %16, label %tailrecurse._crit_edge, label %tailrecurse
}

attributes #0 = { mustprogress nofree nosync nounwind memory(write, argmem: read, 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,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass