; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x43-线段树/243. 一个简单的整数问题2.ll (Z6spreadi)

; 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.SegmentTree = type { i32, i32, i64, i64 }

@tree = external dso_local global [400040 x %struct.SegmentTree], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z6spreadi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = sext i32 %3 to i64
  %5 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %4
  %6 = getelementptr inbounds %struct.SegmentTree, ptr %5, i32 0, i32 3
  %7 = load i64, ptr %6, align 8, !tbaa !9
  %8 = icmp ne i64 %7, 0
  br i1 %8, label %9, label %98

9:                                                ; preds = %1
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %11
  %13 = getelementptr inbounds %struct.SegmentTree, ptr %12, i32 0, i32 3
  %14 = load i64, ptr %13, align 8, !tbaa !9
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = mul nsw i32 2, %15
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.SegmentTree, ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 4, !tbaa !12
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = mul nsw i32 2, %21
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %23
  %25 = getelementptr inbounds %struct.SegmentTree, ptr %24, i32 0, i32 0
  %26 = load i32, ptr %25, align 8, !tbaa !13
  %27 = sub nsw i32 %20, %26
  %28 = add nsw i32 %27, 1
  %29 = sext i32 %28 to i64
  %30 = mul nsw i64 %14, %29
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = mul nsw i32 2, %31
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.SegmentTree, ptr %34, i32 0, i32 2
  %36 = load i64, ptr %35, align 8, !tbaa !14
  %37 = add nsw i64 %36, %30
  store i64 %37, ptr %35, align 8, !tbaa !14
  %38 = load i32, ptr %2, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %39
  %41 = getelementptr inbounds %struct.SegmentTree, ptr %40, i32 0, i32 3
  %42 = load i64, ptr %41, align 8, !tbaa !9
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = mul nsw i32 2, %43
  %45 = add nsw i32 %44, 1
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %46
  %48 = getelementptr inbounds %struct.SegmentTree, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 4, !tbaa !12
  %50 = load i32, ptr %2, align 4, !tbaa !5
  %51 = mul nsw i32 2, %50
  %52 = add nsw i32 %51, 1
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.SegmentTree, ptr %54, i32 0, i32 0
  %56 = load i32, ptr %55, align 8, !tbaa !13
  %57 = sub nsw i32 %49, %56
  %58 = add nsw i32 %57, 1
  %59 = sext i32 %58 to i64
  %60 = mul nsw i64 %42, %59
  %61 = load i32, ptr %2, align 4, !tbaa !5
  %62 = mul nsw i32 2, %61
  %63 = add nsw i32 %62, 1
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %64
  %66 = getelementptr inbounds %struct.SegmentTree, ptr %65, i32 0, i32 2
  %67 = load i64, ptr %66, align 8, !tbaa !14
  %68 = add nsw i64 %67, %60
  store i64 %68, ptr %66, align 8, !tbaa !14
  %69 = load i32, ptr %2, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %70
  %72 = getelementptr inbounds %struct.SegmentTree, ptr %71, i32 0, i32 3
  %73 = load i64, ptr %72, align 8, !tbaa !9
  %74 = load i32, ptr %2, align 4, !tbaa !5
  %75 = mul nsw i32 2, %74
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %76
  %78 = getelementptr inbounds %struct.SegmentTree, ptr %77, i32 0, i32 3
  %79 = load i64, ptr %78, align 8, !tbaa !9
  %80 = add nsw i64 %79, %73
  store i64 %80, ptr %78, align 8, !tbaa !9
  %81 = load i32, ptr %2, align 4, !tbaa !5
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %82
  %84 = getelementptr inbounds %struct.SegmentTree, ptr %83, i32 0, i32 3
  %85 = load i64, ptr %84, align 8, !tbaa !9
  %86 = load i32, ptr %2, align 4, !tbaa !5
  %87 = mul nsw i32 2, %86
  %88 = add nsw i32 %87, 1
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %89
  %91 = getelementptr inbounds %struct.SegmentTree, ptr %90, i32 0, i32 3
  %92 = load i64, ptr %91, align 8, !tbaa !9
  %93 = add nsw i64 %92, %85
  store i64 %93, ptr %91, align 8, !tbaa !9
  %94 = load i32, ptr %2, align 4, !tbaa !5
  %95 = sext i32 %94 to i64
  %96 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %95
  %97 = getelementptr inbounds %struct.SegmentTree, ptr %96, i32 0, i32 3
  store i64 0, ptr %97, align 8, !tbaa !9
  br label %98

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


; 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.SegmentTree = type { i32, i32, i64, i64 }

@tree = external dso_local local_unnamed_addr global [400040 x %struct.SegmentTree], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z6spreadi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %2, i32 3
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %.not = icmp eq i64 %4, 0
  br i1 %.not, label %39, label %5

5:                                                ; preds = %1
  %6 = shl nsw i32 %0, 1
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %7
  %9 = getelementptr inbounds i8, ptr %8, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = load i32, ptr %8, align 16, !tbaa !12
  %12 = add i32 %10, 1
  %13 = sub i32 %12, %11
  %14 = sext i32 %13 to i64
  %15 = mul nsw i64 %4, %14
  %16 = getelementptr inbounds i8, ptr %8, i64 8
  %17 = load i64, ptr %16, align 8, !tbaa !13
  %18 = add nsw i64 %15, %17
  store i64 %18, ptr %16, align 8, !tbaa !13
  %19 = or disjoint i32 %6, 1
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [400040 x %struct.SegmentTree], ptr @tree, i64 0, i64 %20
  %22 = getelementptr inbounds i8, ptr %21, i64 4
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = load i32, ptr %21, align 8, !tbaa !12
  %25 = add i32 %23, 1
  %26 = sub i32 %25, %24
  %27 = sext i32 %26 to i64
  %28 = mul nsw i64 %4, %27
  %29 = getelementptr inbounds i8, ptr %21, i64 8
  %30 = load i64, ptr %29, align 16, !tbaa !13
  %31 = add nsw i64 %28, %30
  store i64 %31, ptr %29, align 16, !tbaa !13
  %32 = getelementptr inbounds i8, ptr %8, i64 16
  %33 = load i64, ptr %32, align 16, !tbaa !5
  %34 = add nsw i64 %33, %4
  store i64 %34, ptr %32, align 16, !tbaa !5
  %35 = load i64, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %21, i64 16
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = add nsw i64 %37, %35
  store i64 %38, ptr %36, align 8, !tbaa !5
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %5, %1
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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, !10, i64 16}
!6 = !{!"_ZTS11SegmentTree", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 8}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferAlignmentPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass