; LLVM IR from sunkafei/xcpc-algorithm-templates :: 算法/数据结构/线段树（历史最大值）.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, i64, i64, i64, i64, i64, i64 }

@A = external dso_local global [510000 x i64], align 16
@cur = external dso_local global i32, align 4
@t = external dso_local global [2040000 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
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %7 = load i32, ptr @cur, align 4, !tbaa !5
  %8 = add nsw i32 %7, 1
  store i32 %8, ptr @cur, align 4, !tbaa !5
  store i32 %8, ptr %5, align 4, !tbaa !5
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %11
  %13 = getelementptr inbounds %struct.segment, ptr %12, i32 0, i32 0
  store i32 %9, ptr %13, align 16, !tbaa !9
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %16
  %18 = getelementptr inbounds %struct.segment, ptr %17, i32 0, i32 1
  store i32 %14, ptr %18, align 4, !tbaa !12
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %20
  %22 = getelementptr inbounds %struct.segment, ptr %21, i32 0, i32 6
  store i64 0, ptr %22, align 16, !tbaa !13
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %24
  %26 = getelementptr inbounds %struct.segment, ptr %25, i32 0, i32 8
  store i64 0, ptr %26, align 16, !tbaa !14
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %28
  %30 = getelementptr inbounds %struct.segment, ptr %29, i32 0, i32 7
  store i64 -1125899906842624, ptr %30, align 8, !tbaa !15
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %32
  %34 = getelementptr inbounds %struct.segment, ptr %33, i32 0, i32 9
  store i64 -1125899906842624, ptr %34, align 8, !tbaa !16
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.segment, ptr %37, i32 0, i32 5
  store i64 -1125899906842624, ptr %38, align 8, !tbaa !17
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %40
  %42 = getelementptr inbounds %struct.segment, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 16, !tbaa !9
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.segment, ptr %46, i32 0, i32 1
  %48 = load i32, ptr %47, align 4, !tbaa !12
  %49 = icmp eq i32 %43, %48
  br i1 %49, label %50, label %63

50:                                               ; preds = %2
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [510000 x i64], ptr @A, i64 0, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !18
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.segment, ptr %57, i32 0, i32 4
  store i64 %54, ptr %58, align 16, !tbaa !19
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %60
  %62 = getelementptr inbounds %struct.segment, ptr %61, i32 0, i32 5
  store i64 %54, ptr %62, align 8, !tbaa !17
  br label %92

63:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %65
  %67 = getelementptr inbounds %struct.segment, ptr %66, i32 0, i32 0
  %68 = load i32, ptr %67, align 16, !tbaa !9
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %70
  %72 = getelementptr inbounds %struct.segment, ptr %71, i32 0, i32 1
  %73 = load i32, ptr %72, align 4, !tbaa !12
  %74 = add nsw i32 %68, %73
  %75 = ashr i32 %74, 1
  store i32 %75, ptr %6, align 4, !tbaa !5
  %76 = load i32, ptr %3, align 4, !tbaa !5
  %77 = load i32, ptr %6, align 4, !tbaa !5
  %78 = call noundef i32 @_Z5buildii(i32 noundef %76, i32 noundef %77)
  %79 = load i32, ptr %5, align 4, !tbaa !5
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %80
  %82 = getelementptr inbounds %struct.segment, ptr %81, i32 0, i32 2
  store i32 %78, ptr %82, align 8, !tbaa !20
  %83 = load i32, ptr %6, align 4, !tbaa !5
  %84 = add nsw i32 %83, 1
  %85 = load i32, ptr %4, align 4, !tbaa !5
  %86 = call noundef i32 @_Z5buildii(i32 noundef %84, i32 noundef %85)
  %87 = load i32, ptr %5, align 4, !tbaa !5
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %88
  %90 = getelementptr inbounds %struct.segment, ptr %89, i32 0, i32 3
  store i32 %86, ptr %90, align 4, !tbaa !21
  %91 = load i32, ptr %5, align 4, !tbaa !5
  call void @_Z8maintaini(i32 noundef %91)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %92

92:                                               ; preds = %63, %50
  %93 = load i32, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret i32 %93
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z8maintaini(i32 noundef) #2

; 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 = { inlinehint 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 = !{!"_ZTS7segment", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !11, i64 56}
!11 = !{!"long long", !7, i64 0}
!12 = !{!10, !6, i64 4}
!13 = !{!10, !11, i64 32}
!14 = !{!10, !11, i64 48}
!15 = !{!10, !11, i64 40}
!16 = !{!10, !11, i64 56}
!17 = !{!10, !11, i64 24}
!18 = !{!11, !11, i64 0}
!19 = !{!10, !11, i64 16}
!20 = !{!10, !6, i64 8}
!21 = !{!10, !6, i64 12}


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

@A = external dso_local local_unnamed_addr global [510000 x i64], align 16
@cur = external dso_local local_unnamed_addr global i32, align 4
@t = external dso_local local_unnamed_addr global [2040000 x %struct.segment], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 -2147483647, -2147483648) i32 @_Z5buildii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @cur, align 4, !tbaa !5
  %4 = add nsw i32 %3, 1
  store i32 %4, ptr @cur, align 4, !tbaa !5
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [2040000 x %struct.segment], ptr @t, i64 0, i64 %5
  store i32 %0, ptr %6, align 16, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %6, i64 4
  store i32 %1, ptr %7, align 4, !tbaa !12
  %8 = getelementptr inbounds i8, ptr %6, i64 32
  store i64 0, ptr %8, align 16, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %6, i64 48
  store i64 0, ptr %9, align 16, !tbaa !14
  %10 = getelementptr inbounds i8, ptr %6, i64 40
  store i64 -1125899906842624, ptr %10, align 8, !tbaa !15
  %11 = getelementptr inbounds i8, ptr %6, i64 56
  store i64 -1125899906842624, ptr %11, align 8, !tbaa !16
  %12 = getelementptr inbounds i8, ptr %6, i64 24
  store i64 -1125899906842624, ptr %12, align 8, !tbaa !17
  %13 = icmp eq i32 %0, %1
  br i1 %13, label %common.ret, label %18

common.ret:                                       ; preds = %2
  %14 = sext i32 %0 to i64
  %15 = getelementptr inbounds [510000 x i64], ptr @A, i64 0, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !18
  %17 = getelementptr inbounds i8, ptr %6, i64 16
  store i64 %16, ptr %17, align 16, !tbaa !19
  store i64 %16, ptr %12, align 8, !tbaa !17
  br label %common.ret25

common.ret25:                                     ; preds = %18, %common.ret
  ret i32 %4

18:                                               ; preds = %2
  %19 = add nsw i32 %1, %0
  %20 = ashr i32 %19, 1
  %21 = tail call noundef i32 @_Z5buildii(i32 noundef %0, i32 noundef %20)
  %22 = getelementptr inbounds i8, ptr %6, i64 8
  store i32 %21, ptr %22, align 8, !tbaa !20
  %23 = add nsw i32 %20, 1
  %24 = tail call noundef i32 @_Z5buildii(i32 noundef %23, i32 noundef %1)
  %25 = getelementptr inbounds i8, ptr %6, i64 12
  store i32 %24, ptr %25, align 4, !tbaa !21
  tail call void @_Z8maintaini(i32 noundef %4)
  br label %common.ret25
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z8maintaini(i32 noundef) local_unnamed_addr #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 = { inlinehint 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"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTS7segment", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !11, i64 56}
!11 = !{!"long long", !7, i64 0}
!12 = !{!10, !6, i64 4}
!13 = !{!10, !11, i64 32}
!14 = !{!10, !11, i64 48}
!15 = !{!10, !11, i64 40}
!16 = !{!10, !11, i64 56}
!17 = !{!10, !11, i64 24}
!18 = !{!11, !11, i64 0}
!19 = !{!10, !11, i64 16}
!20 = !{!10, !6, i64 8}
!21 = !{!10, !6, i64 12}


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