; LLVM IR from Izaron/ACM-ICPC :: Timus Online Judge/Volume 5/1495. One-two, One-two 2.ll (Z4calcii)

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

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@calced = external dso_local global [1000005 x [31 x i16]], align 16
@gen = external dso_local global %"class.std::vector", align 8
@n = external dso_local global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef signext i16 @_Z4calcii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i16, align 2
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %12, label %18

12:                                               ; preds = %2
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = icmp eq i32 %13, 0
  %15 = zext i1 %14 to i64
  %16 = select i1 %14, i32 1, i32 0
  %17 = trunc i32 %16 to i16
  store i16 %17, ptr %3, align 2
  br label %69

18:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1000005 x [31 x i16]], ptr @calced, i64 0, i64 %20
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [31 x i16], ptr %21, i64 0, i64 %23
  store ptr %24, ptr %6, align 8, !tbaa !9
  %25 = load ptr, ptr %6, align 8, !tbaa !9
  %26 = load i16, ptr %25, align 2, !tbaa !11
  %27 = sext i16 %26 to i32
  %28 = icmp ne i32 %27, -1
  br i1 %28, label %29, label %32

29:                                               ; preds = %18
  %30 = load ptr, ptr %6, align 8, !tbaa !9
  %31 = load i16, ptr %30, align 2, !tbaa !11
  store i16 %31, ptr %3, align 2
  store i32 1, ptr %7, align 4
  br label %68

32:                                               ; preds = %18
  %33 = load ptr, ptr %6, align 8, !tbaa !9
  store i16 0, ptr %33, align 2, !tbaa !11
  %34 = load ptr, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #4
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @gen, i64 noundef %37) #4
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = mul nsw i32 %39, 1
  %41 = add nsw i32 %35, %40
  %42 = load i32, ptr @n, align 4, !tbaa !5
  %43 = srem i32 %41, %42
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = sub nsw i32 %44, 1
  %46 = call noundef signext i16 @_Z4calcii(i32 noundef %43, i32 noundef %45)
  store i16 %46, ptr %8, align 2, !tbaa !11
  %47 = call noundef nonnull align 2 dereferenceable(2) ptr @_ZSt3maxIsERKT_S2_S2_(ptr noundef nonnull align 2 dereferenceable(2) %34, ptr noundef nonnull align 2 dereferenceable(2) %8)
  %48 = load i16, ptr %47, align 2, !tbaa !11
  %49 = load ptr, ptr %6, align 8, !tbaa !9
  store i16 %48, ptr %49, align 2, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #4
  %50 = load ptr, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #4
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @gen, i64 noundef %53) #4
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = mul nsw i32 %55, 2
  %57 = add nsw i32 %51, %56
  %58 = load i32, ptr @n, align 4, !tbaa !5
  %59 = srem i32 %57, %58
  %60 = load i32, ptr %5, align 4, !tbaa !5
  %61 = sub nsw i32 %60, 1
  %62 = call noundef signext i16 @_Z4calcii(i32 noundef %59, i32 noundef %61)
  store i16 %62, ptr %9, align 2, !tbaa !11
  %63 = call noundef nonnull align 2 dereferenceable(2) ptr @_ZSt3maxIsERKT_S2_S2_(ptr noundef nonnull align 2 dereferenceable(2) %50, ptr noundef nonnull align 2 dereferenceable(2) %9)
  %64 = load i16, ptr %63, align 2, !tbaa !11
  %65 = load ptr, ptr %6, align 8, !tbaa !9
  store i16 %64, ptr %65, align 2, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #4
  %66 = load ptr, ptr %6, align 8, !tbaa !9
  %67 = load i16, ptr %66, align 2, !tbaa !11
  store i16 %67, ptr %3, align 2
  store i32 1, ptr %7, align 4
  br label %68

68:                                               ; preds = %32, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %69

69:                                               ; preds = %68, %12
  %70 = load i16, ptr %3, align 2
  ret i16 %70
}

; 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 nounwind uwtable
declare dso_local noundef nonnull align 2 dereferenceable(2) ptr @_ZSt3maxIsERKT_S2_S2_(ptr noundef nonnull align 2 dereferenceable(2), ptr noundef nonnull align 2 dereferenceable(2)) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #3 align 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 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 #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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !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"

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@calced = external dso_local global [1000005 x [31 x i16]], align 16
@gen = external dso_local global %"class.std::vector", align 8
@n = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef signext i16 @_Z4calcii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = icmp slt i32 %1, 0
  br i1 %5, label %6, label %9

6:                                                ; preds = %2
  %7 = icmp eq i32 %0, 0
  %8 = zext i1 %7 to i16
  br label %33

9:                                                ; preds = %2
  %10 = sext i32 %0 to i64
  %11 = zext nneg i32 %1 to i64
  %12 = getelementptr inbounds [1000005 x [31 x i16]], ptr @calced, i64 0, i64 %10, i64 %11
  %13 = load i16, ptr %12, align 2, !tbaa !5
  %.not = icmp eq i16 %13, -1
  br i1 %.not, label %14, label %33

14:                                               ; preds = %9
  store i16 0, ptr %12, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %3) #3
  %15 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @gen, i64 noundef %11) #3
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = add nsw i32 %16, %0
  %18 = load i32, ptr @n, align 4, !tbaa !9
  %19 = srem i32 %17, %18
  %20 = add nsw i32 %1, -1
  %21 = tail call noundef signext i16 @_Z4calcii(i32 noundef %19, i32 noundef %20)
  store i16 %21, ptr %3, align 2, !tbaa !5
  %22 = call noundef nonnull align 2 dereferenceable(2) ptr @_ZSt3maxIsERKT_S2_S2_(ptr noundef nonnull align 2 dereferenceable(2) %12, ptr noundef nonnull align 2 dereferenceable(2) %3)
  %23 = load i16, ptr %22, align 2, !tbaa !5
  store i16 %23, ptr %12, align 2, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %4) #3
  %24 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @gen, i64 noundef %11) #3
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = shl nsw i32 %25, 1
  %27 = add nsw i32 %26, %0
  %28 = load i32, ptr @n, align 4, !tbaa !9
  %29 = srem i32 %27, %28
  %30 = call noundef signext i16 @_Z4calcii(i32 noundef %29, i32 noundef %20)
  store i16 %30, ptr %4, align 2, !tbaa !5
  %31 = call noundef nonnull align 2 dereferenceable(2) ptr @_ZSt3maxIsERKT_S2_S2_(ptr noundef nonnull align 2 dereferenceable(2) %12, ptr noundef nonnull align 2 dereferenceable(2) %4)
  %32 = load i16, ptr %31, align 2, !tbaa !5
  store i16 %32, ptr %12, align 2, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %4) #3
  br label %33

33:                                               ; preds = %14, %9, %6
  %.1 = phi i16 [ %8, %6 ], [ %32, %14 ], [ %13, %9 ]
  ret i16 %.1
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 2 dereferenceable(2) ptr @_ZSt3maxIsERKT_S2_S2_(ptr noundef nonnull align 2 dereferenceable(2), ptr noundef nonnull align 2 dereferenceable(2)) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #0 align 2

; 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 = { 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 #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 = !{!"short", !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: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass