; LLVM IR from tangjz/acm-icpc :: codeforces/complete/1030F.ll (Z7seg_updiiii)

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

@a = external dso_local global [200001 x i32], align 16
@seg = external dso_local global [400003 x %struct.Segment], align 16

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z9seg_mergeR7SegmentS0_S0_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7seg_updiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #3 {
  %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
  %10 = alloca %struct.Segment, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = call noundef i32 @_Z7seg_idxii(i32 noundef %15, i32 noundef %16)
  store i32 %17, ptr %9, align 4, !tbaa !5
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = icmp eq i32 %18, %19
  br i1 %20, label %21, label %39

21:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 16, ptr %10) #4
  %22 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 0
  %23 = load i32, ptr %8, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  store i64 %24, ptr %22, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.Segment, ptr %10, i32 0, i32 1
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = load i32, ptr %8, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = mul nsw i64 %30, %32
  %34 = srem i64 %33, 1000000007
  %35 = trunc i64 %34 to i32
  store i32 %35, ptr %25, align 8, !tbaa !12
  %36 = load i32, ptr %9, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [400003 x %struct.Segment], ptr @seg, i64 0, i64 %37
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %38, ptr align 8 %10, i64 16, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 16, ptr %10) #4
  store i32 1, ptr %11, align 4
  br label %75

39:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = load i32, ptr %6, align 4, !tbaa !5
  %42 = add nsw i32 %40, %41
  %43 = ashr i32 %42, 1
  store i32 %43, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = load i32, ptr %12, align 4, !tbaa !5
  %46 = call noundef i32 @_Z7seg_idxii(i32 noundef %44, i32 noundef %45)
  store i32 %46, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %47 = load i32, ptr %12, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = call noundef i32 @_Z7seg_idxii(i32 noundef %48, i32 noundef %49)
  store i32 %50, ptr %14, align 4, !tbaa !5
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = load i32, ptr %12, align 4, !tbaa !5
  %53 = icmp sle i32 %51, %52
  br i1 %53, label %54, label %59

54:                                               ; preds = %39
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = load i32, ptr %12, align 4, !tbaa !5
  %57 = load i32, ptr %7, align 4, !tbaa !5
  %58 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z7seg_updiiii(i32 noundef %55, i32 noundef %56, i32 noundef %57, i32 noundef %58)
  br label %65

59:                                               ; preds = %39
  %60 = load i32, ptr %12, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = load i32, ptr %7, align 4, !tbaa !5
  %64 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z7seg_updiiii(i32 noundef %61, i32 noundef %62, i32 noundef %63, i32 noundef %64)
  br label %65

65:                                               ; preds = %59, %54
  %66 = load i32, ptr %9, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [400003 x %struct.Segment], ptr @seg, i64 0, i64 %67
  %69 = load i32, ptr %13, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [400003 x %struct.Segment], ptr @seg, i64 0, i64 %70
  %72 = load i32, ptr %14, align 4, !tbaa !5
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [400003 x %struct.Segment], ptr @seg, i64 0, i64 %73
  call void @_Z9seg_mergeR7SegmentS0_S0_(ptr noundef nonnull align 8 dereferenceable(16) %68, ptr noundef nonnull align 8 dereferenceable(16) %71, ptr noundef nonnull align 8 dereferenceable(16) %74)
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  store i32 0, ptr %11, align 4
  br label %75

75:                                               ; preds = %65, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  %76 = load i32, ptr %11, align 4
  switch i32 %76, label %78 [
    i32 0, label %77
    i32 1, label %77
  ]

77:                                               ; preds = %75, %75
  ret void

78:                                               ; preds = %75
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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" }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { 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, !11, i64 0}
!10 = !{!"_ZTS7Segment", !11, i64 0, !6, i64 8}
!11 = !{!"long long", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{i64 0, i64 8, !14, i64 8, i64 4, !5}
!14 = !{!11, !11, 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"

%struct.Segment = type { i64, i32 }

@a = external dso_local local_unnamed_addr global [200001 x i32], align 16
@seg = external dso_local global [400003 x %struct.Segment], align 16

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7seg_idxii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z9seg_mergeR7SegmentS0_S0_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z7seg_updiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  %5 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %0, i32 noundef %1)
  %6 = icmp eq i32 %0, %1
  br i1 %6, label %7, label %18

7:                                                ; preds = %4
  %8 = sext i32 %3 to i64
  %9 = sext i32 %0 to i64
  %10 = getelementptr inbounds [200001 x i32], ptr @a, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = mul nsw i64 %12, %8
  %14 = srem i64 %13, 1000000007
  %15 = trunc nsw i64 %14 to i32
  %16 = sext i32 %5 to i64
  %17 = getelementptr inbounds [400003 x %struct.Segment], ptr @seg, i64 0, i64 %16
  store i64 %8, ptr %17, align 16, !tbaa !9
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %17, i64 8
  store i32 %15, ptr %.sroa.4.0..sroa_idx, align 8, !tbaa !5
  br label %33

18:                                               ; preds = %4
  %19 = add nsw i32 %1, %0
  %20 = ashr i32 %19, 1
  %21 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %0, i32 noundef %20)
  %22 = add nsw i32 %20, 1
  %23 = tail call noundef i32 @_Z7seg_idxii(i32 noundef %22, i32 noundef %1)
  %.not = icmp slt i32 %20, %2
  br i1 %.not, label %25, label %24

24:                                               ; preds = %18
  tail call void @_Z7seg_updiiii(i32 noundef %0, i32 noundef %20, i32 noundef %2, i32 noundef %3)
  br label %26

25:                                               ; preds = %18
  tail call void @_Z7seg_updiiii(i32 noundef %22, i32 noundef %1, i32 noundef %2, i32 noundef %3)
  br label %26

26:                                               ; preds = %25, %24
  %27 = sext i32 %5 to i64
  %28 = getelementptr inbounds [400003 x %struct.Segment], ptr @seg, i64 0, i64 %27
  %29 = sext i32 %21 to i64
  %30 = getelementptr inbounds [400003 x %struct.Segment], ptr @seg, i64 0, i64 %29
  %31 = sext i32 %23 to i64
  %32 = getelementptr inbounds [400003 x %struct.Segment], ptr @seg, i64 0, i64 %31
  tail call void @_Z9seg_mergeR7SegmentS0_S0_(ptr noundef nonnull align 8 dereferenceable(16) %28, ptr noundef nonnull align 8 dereferenceable(16) %30, ptr noundef nonnull align 8 dereferenceable(16) %32)
  br label %33

33:                                               ; preds = %26, %7
  ret void
}

attributes #0 = { inlinehint 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" }
attributes #1 = { 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, !10, i64 0}
!10 = !{!"long long", !7, i64 0}


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