; LLVM IR from tangjz/acm-icpc :: 51nod/1394.ll (Z3addi)

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

@tot = external dso_local global i32, align 4
@que = external dso_local global [200002 x i32], align 16
@cnt = external dso_local global [200002 x i32], align 16
@sum = external dso_local global [200002 x i64], align 16
@ans = external dso_local global i64, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3addi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %8 = load i32, ptr @tot, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds i32, ptr @que, i64 %9
  %11 = call noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef @que, ptr noundef %10, ptr noundef nonnull align 4 dereferenceable(4) %2)
  %12 = ptrtoint ptr %11 to i64
  %13 = sub i64 %12, ptrtoint (ptr @que to i64)
  %14 = sdiv exact i64 %13, 4
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = call noundef i32 @_Z7bit_sumIiET_PS0_i(ptr noundef @cnt, i32 noundef %16)
  store i32 %17, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %18 = load i32, ptr @tot, align 4, !tbaa !5
  %19 = sub nsw i32 %18, 1
  %20 = call noundef i32 @_Z7bit_sumIiET_PS0_i(ptr noundef @cnt, i32 noundef %19)
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sub nsw i32 %20, %21
  store i32 %22, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = call noundef i64 @_Z7bit_sumIxET_PS0_i(ptr noundef @sum, i32 noundef %23)
  store i64 %24, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %25 = load i32, ptr @tot, align 4, !tbaa !5
  %26 = sub nsw i32 %25, 1
  %27 = call noundef i64 @_Z7bit_sumIxET_PS0_i(ptr noundef @sum, i32 noundef %26)
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = sub nsw i64 %27, %28
  store i64 %29, ptr %7, align 8, !tbaa !9
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = load i32, ptr %2, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = mul nsw i64 %31, %33
  %35 = load i64, ptr %6, align 8, !tbaa !9
  %36 = sub nsw i64 %34, %35
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = add nsw i64 %36, %37
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = mul nsw i64 %40, %42
  %44 = sub nsw i64 %38, %43
  %45 = load i64, ptr @ans, align 8, !tbaa !9
  %46 = add nsw i64 %45, %44
  store i64 %46, ptr @ans, align 8, !tbaa !9
  %47 = load i32, ptr %3, align 4, !tbaa !5
  call void @_Z7bit_addIiEvPT_ii(ptr noundef @cnt, i32 noundef %47, i32 noundef 1)
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = load i32, ptr %2, align 4, !tbaa !5
  call void @_Z7bit_addIxEvPT_ii(ptr noundef @sum, i32 noundef %48, i32 noundef %49)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  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: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef, ptr noundef, ptr noundef nonnull align 4 dereferenceable(4)) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7bit_sumIiET_PS0_i(ptr noundef, i32 noundef) #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z7bit_sumIxET_PS0_i(ptr noundef, i32 noundef) #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z7bit_addIiEvPT_ii(ptr noundef, i32 noundef, i32 noundef) #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z7bit_addIxEvPT_ii(ptr noundef, i32 noundef, i32 noundef) #3

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

@tot = external dso_local local_unnamed_addr global i32, align 4
@que = external dso_local global [200002 x i32], align 16
@cnt = external dso_local global [200002 x i32], align 16
@sum = external dso_local global [200002 x i64], align 16
@ans = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3addi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %3 = load i32, ptr @tot, align 4, !tbaa !5
  %4 = sext i32 %3 to i64
  %5 = getelementptr inbounds i32, ptr @que, i64 %4
  %6 = call noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef nonnull @que, ptr noundef nonnull %5, ptr noundef nonnull align 4 dereferenceable(4) %2)
  %7 = ptrtoint ptr %6 to i64
  %8 = sub i64 %7, ptrtoint (ptr @que to i64)
  %9 = lshr exact i64 %8, 2
  %10 = trunc i64 %9 to i32
  %11 = call noundef i32 @_Z7bit_sumIiET_PS0_i(ptr noundef nonnull @cnt, i32 noundef %10)
  %12 = load i32, ptr @tot, align 4, !tbaa !5
  %13 = add nsw i32 %12, -1
  %14 = call noundef i32 @_Z7bit_sumIiET_PS0_i(ptr noundef nonnull @cnt, i32 noundef %13)
  %15 = sub nsw i32 %14, %11
  %16 = call noundef i64 @_Z7bit_sumIxET_PS0_i(ptr noundef nonnull @sum, i32 noundef %10)
  %17 = load i32, ptr @tot, align 4, !tbaa !5
  %18 = add nsw i32 %17, -1
  %19 = call noundef i64 @_Z7bit_sumIxET_PS0_i(ptr noundef nonnull @sum, i32 noundef %18)
  %20 = sext i32 %11 to i64
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = sext i32 %15 to i64
  %24 = load i64, ptr @ans, align 8, !tbaa !9
  %reass.add11 = sub nsw i64 %20, %23
  %reass.mul = mul nsw i64 %reass.add11, %22
  %25 = shl i64 %16, 1
  %26 = sub i64 %19, %25
  %27 = add i64 %26, %24
  %28 = add i64 %27, %reass.mul
  store i64 %28, ptr @ans, align 8, !tbaa !9
  call void @_Z7bit_addIiEvPT_ii(ptr noundef nonnull @cnt, i32 noundef %10, i32 noundef 1)
  %29 = load i32, ptr %2, align 4, !tbaa !5
  call void @_Z7bit_addIxEvPT_ii(ptr noundef nonnull @sum, i32 noundef %10, i32 noundef %29)
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef, ptr noundef, ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z7bit_sumIiET_PS0_i(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z7bit_sumIxET_PS0_i(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z7bit_addIiEvPT_ii(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z7bit_addIxEvPT_ii(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

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" }
attributes #2 = { 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,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SCCPPass,SROAPass