; LLVM IR from brpapa/algorithms :: solutions/uva/10131.ll (Z3ldsi)

; 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.T = type { i32, i32, i32 }
%"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 }

@e = external dso_local global [1010 x %struct.T], align 16
@memo = external dso_local global %"class.std::vector", align 8
@p = external dso_local global %"class.std::vector", align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z3ldsi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %1
  store i32 1, ptr %2, align 4
  br label %75

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @memo, i64 noundef %12) #3
  store ptr %13, ptr %4, align 8, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !9
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp ne i32 %15, -1
  br i1 %16, label %17, label %20

17:                                               ; preds = %10
  %18 = load ptr, ptr %4, align 8, !tbaa !9
  %19 = load i32, ptr %18, align 4, !tbaa !5
  store i32 %19, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %74

20:                                               ; preds = %10
  %21 = load ptr, ptr %4, align 8, !tbaa !9
  store i32 1, ptr %21, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %68, %20
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %22
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %71

27:                                               ; preds = %22
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [1010 x %struct.T], ptr @e, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.T, ptr %30, i32 0, i32 2
  %32 = load i32, ptr %31, align 4, !tbaa !11
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [1010 x %struct.T], ptr @e, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.T, ptr %35, i32 0, i32 2
  %37 = load i32, ptr %36, align 4, !tbaa !11
  %38 = icmp sgt i32 %32, %37
  br i1 %38, label %39, label %67

39:                                               ; preds = %27
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [1010 x %struct.T], ptr @e, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.T, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !13
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1010 x %struct.T], ptr @e, i64 0, i64 %46
  %48 = getelementptr inbounds %struct.T, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 4, !tbaa !13
  %50 = icmp slt i32 %44, %49
  br i1 %50, label %51, label %67

51:                                               ; preds = %39
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = call noundef i32 @_Z3ldsi(i32 noundef %52)
  %54 = add nsw i32 1, %53
  %55 = load ptr, ptr %4, align 8, !tbaa !9
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = icmp sgt i32 %54, %56
  br i1 %57, label %58, label %67

58:                                               ; preds = %51
  %59 = load i32, ptr %6, align 4, !tbaa !5
  %60 = call noundef i32 @_Z3ldsi(i32 noundef %59)
  %61 = add nsw i32 1, %60
  %62 = load ptr, ptr %4, align 8, !tbaa !9
  store i32 %61, ptr %62, align 4, !tbaa !5
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = load i32, ptr %3, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @p, i64 noundef %65) #3
  store i32 %63, ptr %66, align 4, !tbaa !5
  br label %67

67:                                               ; preds = %58, %51, %39, %27
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %6, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %6, align 4, !tbaa !5
  br label %22, !llvm.loop !14

71:                                               ; preds = %26
  %72 = load ptr, ptr %4, align 8, !tbaa !9
  %73 = load i32, ptr %72, align 4, !tbaa !5
  store i32 %73, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %74

74:                                               ; preds = %71, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %75

75:                                               ; preds = %74, %9
  %76 = load i32, ptr %2, align 4
  ret i32 %76
}

; Function Attrs: 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 dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #2 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 = { 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS1T", !6, i64 0, !6, i64 4, !6, i64 8}
!13 = !{!12, !6, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; 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.T = type { i32, i32, i32 }
%"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 }

@e = external dso_local local_unnamed_addr global [1010 x %struct.T], align 16
@memo = external dso_local global %"class.std::vector", align 8
@p = external dso_local global %"class.std::vector", align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z3ldsi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq i32 %0, 0
  br i1 %2, label %._crit_edge, label %3

3:                                                ; preds = %1
  %4 = sext i32 %0 to i64
  %5 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @memo, i64 noundef %4) #1
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not = icmp eq i32 %6, -1
  br i1 %.not, label %7, label %._crit_edge

7:                                                ; preds = %3
  store i32 1, ptr %5, align 4, !tbaa !5
  %8 = icmp sgt i32 %0, 0
  br i1 %8, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %7
  %9 = getelementptr inbounds [1010 x %struct.T], ptr @e, i64 0, i64 %4
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  %11 = getelementptr inbounds i8, ptr %9, i64 4
  %wide.trip.count = zext nneg i32 %0 to i64
  br label %12

._crit_edge.loopexit:                             ; preds = %31
  %.pre = load i32, ptr %5, align 4, !tbaa !5
  br label %._crit_edge

12:                                               ; preds = %.lr.ph, %31
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %31 ]
  %13 = getelementptr inbounds [1010 x %struct.T], ptr @e, i64 0, i64 %indvars.iv
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = load i32, ptr %14, align 4, !tbaa !9
  %16 = load i32, ptr %10, align 4, !tbaa !9
  %17 = icmp sgt i32 %15, %16
  br i1 %17, label %18, label %31

18:                                               ; preds = %12
  %19 = getelementptr inbounds i8, ptr %13, i64 4
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = load i32, ptr %11, align 4, !tbaa !11
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %23, label %31

23:                                               ; preds = %18
  %24 = trunc nuw nsw i64 %indvars.iv to i32
  %25 = tail call noundef i32 @_Z3ldsi(i32 noundef %24)
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %.not22 = icmp slt i32 %25, %26
  br i1 %.not22, label %31, label %27

27:                                               ; preds = %23
  %28 = tail call noundef i32 @_Z3ldsi(i32 noundef %24)
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %5, align 4, !tbaa !5
  %30 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) @p, i64 noundef %4) #1
  store i32 %24, ptr %30, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %12, %18, %23, %27
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %12, !llvm.loop !12

._crit_edge:                                      ; preds = %7, %._crit_edge.loopexit, %3, %1
  %.1 = phi i32 [ 1, %1 ], [ %6, %3 ], [ %.pre, %._crit_edge.loopexit ], [ 1, %7 ]
  ret i32 %.1
}

; 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

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 = { 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, !6, i64 8}
!10 = !{!"_ZTS1T", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = !{!10, !6, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass