; LLVM IR from brpapa/algorithms :: solutions/uva/10261.ll (Z2dpiii)

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

@cars = external dso_local global [202 x i32], align 16
@C = external dso_local global i32, align 4
@memo = external dso_local global [202 x [10001 x i32]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z2dpiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = load i32, ptr @C, align 4, !tbaa !5
  %14 = icmp eq i32 %12, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  br label %80

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

26:                                               ; preds = %16
  %27 = load ptr, ptr %8, align 8, !tbaa !9
  %28 = load i32, ptr %27, align 4, !tbaa !5
  store i32 %28, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %79

29:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 -1, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 -1, ptr %11, align 4, !tbaa !5
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [202 x i32], ptr @cars, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = icmp sge i32 %30, %34
  br i1 %35, label %36, label %47

36:                                               ; preds = %29
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [202 x i32], ptr @cars, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = sub nsw i32 %39, %43
  %45 = load i32, ptr %7, align 4, !tbaa !5
  %46 = call noundef i32 @_Z2dpiii(i32 noundef %38, i32 noundef %44, i32 noundef %45)
  store i32 %46, ptr %10, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %36, %29
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [202 x i32], ptr @cars, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = icmp sge i32 %48, %52
  br i1 %53, label %54, label %65

54:                                               ; preds = %47
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [202 x i32], ptr @cars, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = sub nsw i32 %58, %62
  %64 = call noundef i32 @_Z2dpiii(i32 noundef %56, i32 noundef %57, i32 noundef %63)
  store i32 %64, ptr %11, align 4, !tbaa !5
  br label %65

65:                                               ; preds = %54, %47
  %66 = load i32, ptr %10, align 4, !tbaa !5
  %67 = icmp eq i32 %66, -1
  br i1 %67, label %68, label %72

68:                                               ; preds = %65
  %69 = load i32, ptr %11, align 4, !tbaa !5
  %70 = icmp eq i32 %69, -1
  br i1 %70, label %71, label %72

71:                                               ; preds = %68
  br label %76

72:                                               ; preds = %68, %65
  %73 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %10, ptr noundef nonnull align 4 dereferenceable(4) %11)
  %74 = load i32, ptr %73, align 4, !tbaa !5
  %75 = add nsw i32 1, %74
  br label %76

76:                                               ; preds = %72, %71
  %77 = phi i32 [ 0, %71 ], [ %75, %72 ]
  %78 = load ptr, ptr %8, align 8, !tbaa !9
  store i32 %77, ptr %78, align 4, !tbaa !5
  store i32 %77, ptr %4, align 4
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %79

79:                                               ; preds = %76, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %80

80:                                               ; preds = %79, %15
  %81 = load i32, ptr %4, align 4
  ret i32 %81
}

; 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 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #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 = { 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}


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

@cars = external dso_local local_unnamed_addr global [202 x i32], align 16
@C = external dso_local local_unnamed_addr global i32, align 4
@memo = external dso_local local_unnamed_addr global [202 x [10001 x i32]], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z2dpiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = load i32, ptr @C, align 4, !tbaa !5
  %7 = icmp eq i32 %6, %0
  br i1 %7, label %37, label %8

8:                                                ; preds = %3
  %9 = sext i32 %0 to i64
  %10 = sext i32 %1 to i64
  %11 = getelementptr inbounds [202 x [10001 x i32]], ptr @memo, i64 0, i64 %9, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %.not = icmp eq i32 %12, -1
  br i1 %.not, label %13, label %37

13:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  store i32 -1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #3
  store i32 -1, ptr %5, align 4, !tbaa !5
  %14 = getelementptr inbounds [202 x i32], ptr @cars, i64 0, i64 %9
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %.not25 = icmp sgt i32 %15, %1
  br i1 %.not25, label %21, label %16

16:                                               ; preds = %13
  %17 = add nsw i32 %0, 1
  %18 = sub nsw i32 %1, %15
  %19 = tail call noundef i32 @_Z2dpiii(i32 noundef %17, i32 noundef %18, i32 noundef %2)
  store i32 %19, ptr %4, align 4, !tbaa !5
  %.pre = load i32, ptr %14, align 4, !tbaa !5
  %20 = icmp eq i32 %19, -1
  br label %21

21:                                               ; preds = %16, %13
  %22 = phi i1 [ %20, %16 ], [ true, %13 ]
  %23 = phi i32 [ %.pre, %16 ], [ %15, %13 ]
  %.not26 = icmp sgt i32 %23, %2
  br i1 %.not26, label %29, label %24

24:                                               ; preds = %21
  %25 = add nsw i32 %0, 1
  %26 = sub nsw i32 %2, %23
  %27 = tail call noundef i32 @_Z2dpiii(i32 noundef %25, i32 noundef %1, i32 noundef %26)
  store i32 %27, ptr %5, align 4, !tbaa !5
  %28 = icmp eq i32 %27, -1
  br label %29

29:                                               ; preds = %24, %21
  %30 = phi i1 [ %28, %24 ], [ true, %21 ]
  %or.cond = and i1 %22, %30
  br i1 %or.cond, label %35, label %31

31:                                               ; preds = %29
  %32 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %4, ptr noundef nonnull align 4 dereferenceable(4) %5)
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  br label %35

35:                                               ; preds = %29, %31
  %36 = phi i32 [ %34, %31 ], [ 0, %29 ]
  store i32 %36, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  br label %37

37:                                               ; preds = %35, %8, %3
  %.1 = phi i32 [ 0, %3 ], [ %36, %35 ], [ %12, %8 ]
  ret i32 %.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 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"}


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