; LLVM IR from SuprDewd/CompetitiveProgramming :: code/data-structures/persistent_segment_tree.ll (Z5buildii)

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

@segcnt = external dso_local global i32, align 4
@segs = external dso_local global [2000000 x %struct.segment], align 16

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

11:                                               ; preds = %2
  store i32 -1, ptr %3, align 4
  br label %63

12:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %13 = load i32, ptr @segcnt, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr @segcnt, align 4, !tbaa !5
  store i32 %13, ptr %6, align 4, !tbaa !5
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.segment, ptr %18, i32 0, i32 0
  store i32 %15, ptr %19, align 4, !tbaa !9
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.segment, ptr %23, i32 0, i32 1
  store i32 %20, ptr %24, align 4, !tbaa !11
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = icmp eq i32 %25, %26
  br i1 %27, label %28, label %37

28:                                               ; preds = %12
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %30
  %32 = getelementptr inbounds %struct.segment, ptr %31, i32 0, i32 2
  store i32 -1, ptr %32, align 4, !tbaa !12
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.segment, ptr %35, i32 0, i32 3
  store i32 -1, ptr %36, align 4, !tbaa !13
  br label %57

37:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = add nsw i32 %38, %39
  %41 = sdiv i32 %40, 2
  store i32 %41, ptr %7, align 4, !tbaa !5
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = call noundef i32 @_Z5buildii(i32 noundef %42, i32 noundef %43)
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %46
  %48 = getelementptr inbounds %struct.segment, ptr %47, i32 0, i32 2
  store i32 %44, ptr %48, align 4, !tbaa !12
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = call noundef i32 @_Z5buildii(i32 noundef %50, i32 noundef %51)
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %54
  %56 = getelementptr inbounds %struct.segment, ptr %55, i32 0, i32 3
  store i32 %52, ptr %56, align 4, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %57

57:                                               ; preds = %37, %28
  %58 = load i32, ptr %6, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %59
  %61 = getelementptr inbounds %struct.segment, ptr %60, i32 0, i32 4
  store i32 0, ptr %61, align 4, !tbaa !14
  %62 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %62, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %63

63:                                               ; preds = %57, %11
  %64 = load i32, ptr %3, align 4
  ret i32 %64
}

; 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTS7segment", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !6, i64 12}
!14 = !{!10, !6, i64 16}


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

@segcnt = external dso_local local_unnamed_addr global i32, align 4
@segs = external dso_local local_unnamed_addr global [2000000 x %struct.segment], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5buildii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %0, %1
  br i1 %3, label %23, label %4

4:                                                ; preds = %2
  %5 = load i32, ptr @segcnt, align 4, !tbaa !5
  %6 = add nsw i32 %5, 1
  store i32 %6, ptr @segcnt, align 4, !tbaa !5
  %7 = sext i32 %5 to i64
  %8 = getelementptr inbounds [2000000 x %struct.segment], ptr @segs, i64 0, i64 %7
  store i32 %0, ptr %8, align 4, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  store i32 %1, ptr %9, align 4, !tbaa !11
  %10 = icmp eq i32 %0, %1
  br i1 %10, label %11, label %13

11:                                               ; preds = %4
  %12 = getelementptr inbounds i8, ptr %8, i64 8
  store i32 -1, ptr %12, align 4, !tbaa !12
  br label %20

13:                                               ; preds = %4
  %14 = add nsw i32 %1, %0
  %15 = sdiv i32 %14, 2
  %16 = tail call noundef i32 @_Z5buildii(i32 noundef %0, i32 noundef %15)
  %17 = getelementptr inbounds i8, ptr %8, i64 8
  store i32 %16, ptr %17, align 4, !tbaa !12
  %18 = add nsw i32 %15, 1
  %19 = tail call noundef i32 @_Z5buildii(i32 noundef %18, i32 noundef %1)
  br label %20

20:                                               ; preds = %13, %11
  %.sink = phi i32 [ -1, %11 ], [ %19, %13 ]
  %21 = getelementptr inbounds i8, ptr %8, i64 12
  store i32 %.sink, ptr %21, align 4
  %22 = getelementptr inbounds i8, ptr %8, i64 16
  store i32 0, ptr %22, align 4, !tbaa !13
  br label %23

23:                                               ; preds = %2, %20
  %.0 = phi i32 [ %5, %20 ], [ -1, %2 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, 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"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTS7segment", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !6, i64 16}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass