; LLVM IR from upupming/algorithm :: kick-start/2021/RoundE/b.ll (Z4calcv)

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

@r = external dso_local global i32, align 4
@c = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@r1 = external dso_local global i32, align 4
@r2 = external dso_local global i32, align 4
@c1 = external dso_local global i32, align 4
@c2 = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z3addRxi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4calcv() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  store i64 0, ptr %1, align 8, !tbaa !5
  %5 = load i32, ptr @r1, align 4, !tbaa !9
  %6 = icmp sgt i32 %5, 1
  br i1 %6, label %7, label %9

7:                                                ; preds = %0
  %8 = load i32, ptr @c2, align 4, !tbaa !9
  call void @_Z3addRxi(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef %8)
  br label %9

9:                                                ; preds = %7, %0
  %10 = load i32, ptr @c2, align 4, !tbaa !9
  %11 = load i32, ptr @c, align 4, !tbaa !9
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %18

13:                                               ; preds = %9
  %14 = load i32, ptr @r2, align 4, !tbaa !9
  %15 = load i32, ptr @r1, align 4, !tbaa !9
  %16 = sub nsw i32 %14, %15
  %17 = add nsw i32 %16, 1
  call void @_Z3addRxi(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef %17)
  br label %18

18:                                               ; preds = %13, %9
  %19 = load i32, ptr @r2, align 4, !tbaa !9
  %20 = load i32, ptr @r, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %22, label %27

22:                                               ; preds = %18
  %23 = load i32, ptr @c2, align 4, !tbaa !9
  %24 = load i32, ptr @c1, align 4, !tbaa !9
  %25 = sub nsw i32 %23, %24
  %26 = add nsw i32 %25, 1
  call void @_Z3addRxi(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef %26)
  br label %27

27:                                               ; preds = %22, %18
  %28 = load i32, ptr @c1, align 4, !tbaa !9
  %29 = icmp sgt i32 %28, 1
  br i1 %29, label %30, label %35

30:                                               ; preds = %27
  %31 = load i32, ptr @r2, align 4, !tbaa !9
  %32 = load i32, ptr @r1, align 4, !tbaa !9
  %33 = sub nsw i32 %31, %32
  %34 = add nsw i32 %33, 1
  call void @_Z3addRxi(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef %34)
  br label %35

35:                                               ; preds = %30, %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  %36 = load i32, ptr @r2, align 4, !tbaa !9
  %37 = load i32, ptr @r1, align 4, !tbaa !9
  %38 = sub nsw i32 %36, %37
  %39 = sub nsw i32 %38, 1
  store i32 %39, ptr %2, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %40 = load i32, ptr @c2, align 4, !tbaa !9
  %41 = load i32, ptr @c1, align 4, !tbaa !9
  %42 = sub nsw i32 %40, %41
  %43 = add nsw i32 %42, 1
  %44 = load i32, ptr @k, align 4, !tbaa !9
  %45 = add nsw i32 %43, %44
  %46 = sub nsw i32 %45, 1
  %47 = load i32, ptr @k, align 4, !tbaa !9
  %48 = sdiv i32 %46, %47
  %49 = sext i32 %48 to i64
  store i64 %49, ptr %3, align 8, !tbaa !5
  %50 = load i64, ptr %3, align 8, !tbaa !5
  %51 = load i32, ptr %2, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = mul nsw i64 %50, %52
  %54 = load i64, ptr %1, align 8, !tbaa !5
  %55 = add nsw i64 %54, %53
  store i64 %55, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %56 = load i32, ptr @c2, align 4, !tbaa !9
  %57 = load i32, ptr @c1, align 4, !tbaa !9
  %58 = sub nsw i32 %56, %57
  %59 = sub nsw i32 %58, 1
  store i32 %59, ptr %4, align 4, !tbaa !9
  %60 = load i32, ptr @r2, align 4, !tbaa !9
  %61 = load i32, ptr @r1, align 4, !tbaa !9
  %62 = sub nsw i32 %60, %61
  %63 = add nsw i32 %62, 1
  %64 = load i32, ptr @k, align 4, !tbaa !9
  %65 = add nsw i32 %63, %64
  %66 = sub nsw i32 %65, 1
  %67 = load i32, ptr @k, align 4, !tbaa !9
  %68 = sdiv i32 %66, %67
  %69 = sext i32 %68 to i64
  store i64 %69, ptr %3, align 8, !tbaa !5
  %70 = load i64, ptr %3, align 8, !tbaa !5
  %71 = load i32, ptr %4, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = mul nsw i64 %70, %72
  %74 = load i64, ptr %1, align 8, !tbaa !5
  %75 = add nsw i64 %74, %73
  store i64 %75, ptr %1, align 8, !tbaa !5
  %76 = load i64, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret i64 %76
}

; 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 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 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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"

@r = external dso_local local_unnamed_addr global i32, align 4
@c = external dso_local local_unnamed_addr global i32, align 4
@k = external dso_local local_unnamed_addr global i32, align 4
@r1 = external dso_local local_unnamed_addr global i32, align 4
@r2 = external dso_local local_unnamed_addr global i32, align 4
@c1 = external dso_local local_unnamed_addr global i32, align 4
@c2 = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4calcv() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #2
  store i64 0, ptr %1, align 8, !tbaa !5
  %2 = load i32, ptr @r1, align 4, !tbaa !9
  %3 = icmp sgt i32 %2, 1
  br i1 %3, label %4, label %6

4:                                                ; preds = %0
  %5 = load i32, ptr @c2, align 4, !tbaa !9
  call void @_Z3addRxi(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef %5)
  br label %6

6:                                                ; preds = %4, %0
  %7 = load i32, ptr @c2, align 4, !tbaa !9
  %8 = load i32, ptr @c, align 4, !tbaa !9
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %10, label %15

10:                                               ; preds = %6
  %11 = load i32, ptr @r2, align 4, !tbaa !9
  %12 = load i32, ptr @r1, align 4, !tbaa !9
  %13 = add i32 %11, 1
  %14 = sub i32 %13, %12
  call void @_Z3addRxi(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef %14)
  br label %15

15:                                               ; preds = %10, %6
  %16 = load i32, ptr @r2, align 4, !tbaa !9
  %17 = load i32, ptr @r, align 4, !tbaa !9
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %19, label %24

19:                                               ; preds = %15
  %20 = load i32, ptr @c2, align 4, !tbaa !9
  %21 = load i32, ptr @c1, align 4, !tbaa !9
  %22 = add i32 %20, 1
  %23 = sub i32 %22, %21
  call void @_Z3addRxi(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef %23)
  %.pre8.pre = load i32, ptr @r2, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %19, %15
  %.pre8 = phi i32 [ %.pre8.pre, %19 ], [ %16, %15 ]
  %25 = load i32, ptr @c1, align 4, !tbaa !9
  %26 = icmp sgt i32 %25, 1
  %.pre10 = load i32, ptr @r1, align 4, !tbaa !9
  br i1 %26, label %27, label %30

27:                                               ; preds = %24
  %28 = add i32 %.pre8, 1
  %29 = sub i32 %28, %.pre10
  call void @_Z3addRxi(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef %29)
  %.pre = load i32, ptr @r2, align 4, !tbaa !9
  %.pre9 = load i32, ptr @r1, align 4, !tbaa !9
  %.pre11 = load i32, ptr @c1, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %27, %24
  %31 = phi i32 [ %.pre11, %27 ], [ %25, %24 ]
  %32 = phi i32 [ %.pre9, %27 ], [ %.pre10, %24 ]
  %33 = phi i32 [ %.pre, %27 ], [ %.pre8, %24 ]
  %34 = xor i32 %32, -1
  %35 = add i32 %33, %34
  %36 = load i32, ptr @c2, align 4, !tbaa !9
  %37 = sub i32 %36, %31
  %38 = load i32, ptr @k, align 4, !tbaa !9
  %39 = add i32 %37, %38
  %40 = sdiv i32 %39, %38
  %41 = sext i32 %40 to i64
  %42 = sext i32 %35 to i64
  %43 = mul nsw i64 %41, %42
  %44 = load i64, ptr %1, align 8, !tbaa !5
  %45 = add nsw i64 %43, %44
  %46 = xor i32 %31, -1
  %47 = add i32 %36, %46
  %48 = sub i32 %33, %32
  %49 = add i32 %48, %38
  %50 = sdiv i32 %49, %38
  %51 = sext i32 %50 to i64
  %52 = sext i32 %47 to i64
  %53 = mul nsw i64 %51, %52
  %54 = add nsw i64 %45, %53
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #2
  ret i64 %54
}

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

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

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" }
attributes #1 = { mustprogress 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass