; LLVM IR from brpapa/algorithms :: solutions/uva/10721.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"

@N = external dso_local global i32, align 4
@K = external dso_local global i32, align 4
@M = external dso_local global i32, align 4
@memo = external dso_local global [55 x [55 x [55 x i64]]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z2dpiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i64, align 8
  %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
  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
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = load i32, ptr @N, align 4, !tbaa !5
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %22

13:                                               ; preds = %3
  %14 = load i32, ptr %7, align 4, !tbaa !5
  %15 = load i32, ptr @M, align 4, !tbaa !5
  %16 = icmp eq i32 %14, %15
  br i1 %16, label %21, label %17

17:                                               ; preds = %13
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = load i32, ptr @K, align 4, !tbaa !5
  %20 = icmp eq i32 %18, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %17, %13
  store i64 0, ptr %4, align 8
  br label %62

22:                                               ; preds = %17, %3
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = load i32, ptr @N, align 4, !tbaa !5
  %25 = icmp eq i32 %23, %24
  br i1 %25, label %26, label %31

26:                                               ; preds = %22
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = load i32, ptr @K, align 4, !tbaa !5
  %29 = icmp eq i32 %27, %28
  %30 = zext i1 %29 to i64
  store i64 %30, ptr %4, align 8
  br label %62

31:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [55 x [55 x [55 x i64]]], ptr @memo, i64 0, i64 %33
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [55 x [55 x i64]], ptr %34, i64 0, i64 %36
  %38 = load i32, ptr %7, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [55 x i64], ptr %37, i64 0, i64 %39
  store ptr %40, ptr %8, align 8, !tbaa !9
  %41 = load ptr, ptr %8, align 8, !tbaa !9
  %42 = load i64, ptr %41, align 8, !tbaa !11
  %43 = icmp ne i64 %42, -1
  br i1 %43, label %44, label %47

44:                                               ; preds = %31
  %45 = load ptr, ptr %8, align 8, !tbaa !9
  %46 = load i64, ptr %45, align 8, !tbaa !11
  store i64 %46, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %61

47:                                               ; preds = %31
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  %50 = load i32, ptr %6, align 4, !tbaa !5
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  %53 = call noundef i64 @_Z2dpiii(i32 noundef %49, i32 noundef %50, i32 noundef %52)
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  %58 = call noundef i64 @_Z2dpiii(i32 noundef %55, i32 noundef %57, i32 noundef 0)
  %59 = add nsw i64 %53, %58
  %60 = load ptr, ptr %8, align 8, !tbaa !9
  store i64 %59, ptr %60, align 8, !tbaa !11
  store i64 %59, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %61

61:                                               ; preds = %47, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %62

62:                                               ; preds = %61, %26, %21
  %63 = load i64, ptr %4, align 8
  ret i64 %63
}

; 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 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 = !{!"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 = !{!"long long", !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"

@N = external dso_local local_unnamed_addr global i32, align 4
@K = external dso_local local_unnamed_addr global i32, align 4
@M = external dso_local local_unnamed_addr global i32, align 4
@memo = external dso_local local_unnamed_addr global [55 x [55 x [55 x i64]]], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z2dpiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = load i32, ptr @N, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, %0
  br i1 %5, label %6, label %11

6:                                                ; preds = %3
  %7 = load i32, ptr @M, align 4, !tbaa !5
  %8 = icmp eq i32 %7, %2
  %9 = load i32, ptr @K, align 4
  %10 = icmp eq i32 %9, %1
  %or.cond = select i1 %8, i1 true, i1 %10
  br i1 %or.cond, label %common.ret19, label %11

11:                                               ; preds = %6, %3
  %12 = icmp eq i32 %4, %0
  br i1 %12, label %13, label %17

13:                                               ; preds = %11
  %14 = load i32, ptr @K, align 4, !tbaa !5
  %15 = icmp eq i32 %14, %1
  %16 = zext i1 %15 to i64
  br label %common.ret19

17:                                               ; preds = %11
  %18 = sext i32 %0 to i64
  %19 = sext i32 %1 to i64
  %20 = sext i32 %2 to i64
  %21 = getelementptr inbounds [55 x [55 x [55 x i64]]], ptr @memo, i64 0, i64 %18, i64 %19, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !9
  %.not = icmp eq i64 %22, -1
  br i1 %.not, label %23, label %common.ret19

common.ret19:                                     ; preds = %17, %6, %13, %23
  %common.ret19.op = phi i64 [ %29, %23 ], [ %16, %13 ], [ 0, %6 ], [ %22, %17 ]
  ret i64 %common.ret19.op

23:                                               ; preds = %17
  %24 = add nsw i32 %0, 1
  %25 = add nsw i32 %2, 1
  %26 = tail call noundef i64 @_Z2dpiii(i32 noundef %24, i32 noundef %1, i32 noundef %25)
  %27 = add nsw i32 %1, 1
  %28 = tail call noundef i64 @_Z2dpiii(i32 noundef %24, i32 noundef %27, i32 noundef 0)
  %29 = add nsw i64 %28, %26
  store i64 %29, ptr %21, align 8, !tbaa !9
  br label %common.ret19
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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" }

!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 = !{!"long long", !7, i64 0}


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