; LLVM IR from brpapa/algorithms :: solutions/uva/10405.ll (Z5lcsTDii)

; 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::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }

@memo = external dso_local global [1010 x [1010 x i32]], align 16
@_Z1aB5cxx11 = external dso_local global %"class.std::__cxx11::basic_string", align 8
@_Z1bB5cxx11 = external dso_local global %"class.std::__cxx11::basic_string", align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5lcsTDii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) @_Z1aB5cxx11) #4
  %13 = icmp eq i64 %11, %12
  br i1 %13, label %19, label %14

14:                                               ; preds = %2
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) @_Z1bB5cxx11) #4
  %18 = icmp eq i64 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %14, %2
  store i32 0, ptr %3, align 4
  br label %66

20:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [1010 x [1010 x i32]], ptr @memo, i64 0, i64 %22
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [1010 x i32], ptr %23, i64 0, i64 %25
  store ptr %26, ptr %6, align 8, !tbaa !9
  %27 = load ptr, ptr %6, align 8, !tbaa !9
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = icmp ne i32 %28, -1
  br i1 %29, label %30, label %33

30:                                               ; preds = %20
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  %32 = load i32, ptr %31, align 4, !tbaa !5
  store i32 %32, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %65

33:                                               ; preds = %20
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) @_Z1aB5cxx11, i64 noundef %35)
  %37 = load i8, ptr %36, align 1, !tbaa !11
  %38 = sext i8 %37 to i32
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) @_Z1bB5cxx11, i64 noundef %40)
  %42 = load i8, ptr %41, align 1, !tbaa !11
  %43 = sext i8 %42 to i32
  %44 = icmp eq i32 %38, %43
  br i1 %44, label %45, label %53

45:                                               ; preds = %33
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  %50 = call noundef i32 @_Z5lcsTDii(i32 noundef %47, i32 noundef %49)
  %51 = add nsw i32 1, %50
  %52 = load ptr, ptr %6, align 8, !tbaa !9
  store i32 %51, ptr %52, align 4, !tbaa !5
  store i32 %51, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %65

53:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  %56 = load i32, ptr %5, align 4, !tbaa !5
  %57 = call noundef i32 @_Z5lcsTDii(i32 noundef %55, i32 noundef %56)
  store i32 %57, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %58 = load i32, ptr %4, align 4, !tbaa !5
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  %61 = call noundef i32 @_Z5lcsTDii(i32 noundef %58, i32 noundef %60)
  store i32 %61, ptr %9, align 4, !tbaa !5
  %62 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %8, ptr noundef nonnull align 4 dereferenceable(4) %9)
  %63 = load i32, ptr %62, align 4, !tbaa !5
  %64 = load ptr, ptr %6, align 8, !tbaa !9
  store i32 %63, ptr %64, align 4, !tbaa !5
  store i32 %63, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  store i32 1, ptr %7, align 4
  br label %65

65:                                               ; preds = %53, %45, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %66

66:                                               ; preds = %65, %19
  %67 = load i32, ptr %3, align 4
  ret i32 %67
}

; Function Attrs: mustprogress nounwind uwtable
declare noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32)) #1 align 2

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

; Function Attrs: mustprogress nounwind uwtable
declare noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) #1 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { 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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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 = !{!7, !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::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }

@memo = external dso_local local_unnamed_addr global [1010 x [1010 x i32]], align 16
@_Z1aB5cxx11 = external dso_local global %"class.std::__cxx11::basic_string", align 8
@_Z1bB5cxx11 = external dso_local global %"class.std::__cxx11::basic_string", align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5lcsTDii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = sext i32 %0 to i64
  %6 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) @_Z1aB5cxx11) #3
  %7 = icmp eq i64 %6, %5
  br i1 %7, label %common.ret19, label %8

8:                                                ; preds = %2
  %9 = sext i32 %1 to i64
  %10 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32) @_Z1bB5cxx11) #3
  %11 = icmp eq i64 %10, %9
  br i1 %11, label %common.ret19, label %12

12:                                               ; preds = %8
  %13 = getelementptr inbounds [1010 x [1010 x i32]], ptr @memo, i64 0, i64 %5, i64 %9
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %.not = icmp eq i32 %14, -1
  br i1 %.not, label %15, label %common.ret19

15:                                               ; preds = %12
  %16 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) @_Z1aB5cxx11, i64 noundef %5)
  %17 = load i8, ptr %16, align 1, !tbaa !9
  %18 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) @_Z1bB5cxx11, i64 noundef %9)
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %20 = icmp eq i8 %17, %19
  br i1 %20, label %21, label %26

common.ret19:                                     ; preds = %12, %2, %8, %26, %21
  %common.ret19.op = phi i32 [ %25, %21 ], [ %32, %26 ], [ 0, %8 ], [ 0, %2 ], [ %14, %12 ]
  ret i32 %common.ret19.op

21:                                               ; preds = %15
  %22 = add nsw i32 %0, 1
  %23 = add nsw i32 %1, 1
  %24 = tail call noundef i32 @_Z5lcsTDii(i32 noundef %22, i32 noundef %23)
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %13, align 4, !tbaa !5
  br label %common.ret19

26:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  %27 = add nsw i32 %0, 1
  %28 = tail call noundef i32 @_Z5lcsTDii(i32 noundef %27, i32 noundef %1)
  store i32 %28, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %29 = add nsw i32 %1, 1
  %30 = tail call noundef i32 @_Z5lcsTDii(i32 noundef %0, i32 noundef %29)
  store i32 %30, ptr %4, align 4, !tbaa !5
  %31 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %3, ptr noundef nonnull align 4 dereferenceable(4) %4)
  %32 = load i32, ptr %31, align 4, !tbaa !5
  store i32 %32, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  br label %common.ret19
}

; Function Attrs: mustprogress nounwind uwtable
declare noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(32)) local_unnamed_addr #0 align 2

; 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 nounwind uwtable
declare noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}


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