; LLVM IR from kazunetakahashi/atcoder :: 2017/0812_ABC080/D.ll (Z4disti)

; 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::tuple" = type { %"struct.std::_Tuple_impl.base", [4 x i8] }
%"struct.std::_Tuple_impl.base" = type <{ %"struct.std::_Tuple_impl.0", %"struct.std::_Head_base.1" }>
%"struct.std::_Tuple_impl.0" = type { %"struct.std::_Head_base" }
%"struct.std::_Head_base" = type { i64 }
%"struct.std::_Head_base.1" = type { i32 }

@K = external dso_local global i32, align 4
@parent = external dso_local global [100010 x %"class.std::tuple"], align 16
@memo = external dso_local global [100010 x i64], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z4disti(i32 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %5 = load i32, ptr %3, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [100010 x i64], ptr @memo, i64 0, i64 %6
  %8 = load i64, ptr %7, align 8, !tbaa !9
  %9 = icmp ne i64 %8, -1
  br i1 %9, label %10, label %15

10:                                               ; preds = %1
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100010 x i64], ptr @memo, i64 0, i64 %12
  %14 = load i64, ptr %13, align 8, !tbaa !9
  store i64 %14, ptr %2, align 8
  br label %40

15:                                               ; preds = %1
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = load i32, ptr @K, align 4, !tbaa !5
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %23

19:                                               ; preds = %15
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [100010 x i64], ptr @memo, i64 0, i64 %21
  store i64 0, ptr %22, align 8, !tbaa !9
  store i64 0, ptr %2, align 8
  br label %40

23:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [100010 x %"class.std::tuple"], ptr @parent, i64 0, i64 %25
  %27 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3getILm0EJixEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERS4_(ptr noundef nonnull align 8 dereferenceable(12) %26) #3
  %28 = load i32, ptr %27, align 4, !tbaa !5
  store i32 %28, ptr %4, align 4, !tbaa !5
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = call noundef i64 @_Z4disti(i32 noundef %29)
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100010 x %"class.std::tuple"], ptr @parent, i64 0, i64 %32
  %34 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3getILm1EJixEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERS4_(ptr noundef nonnull align 8 dereferenceable(12) %33) #3
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = add nsw i64 %30, %35
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [100010 x i64], ptr @memo, i64 0, i64 %38
  store i64 %36, ptr %39, align 8, !tbaa !9
  store i64 %36, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %40

40:                                               ; preds = %23, %19, %10
  %41 = load i64, ptr %2, align 8
  ret i64 %41
}

; 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 @_ZSt3getILm0EJixEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERS4_(ptr noundef nonnull align 8 dereferenceable(12)) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3getILm1EJixEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERS4_(ptr noundef nonnull align 8 dereferenceable(12)) #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 = !{!"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"

%"class.std::tuple" = type { %"struct.std::_Tuple_impl.base", [4 x i8] }
%"struct.std::_Tuple_impl.base" = type <{ %"struct.std::_Tuple_impl.0", %"struct.std::_Head_base.1" }>
%"struct.std::_Tuple_impl.0" = type { %"struct.std::_Head_base" }
%"struct.std::_Head_base" = type { i64 }
%"struct.std::_Head_base.1" = type { i32 }

@K = external dso_local local_unnamed_addr global i32, align 4
@parent = external dso_local global [100010 x %"class.std::tuple"], align 16
@memo = external dso_local local_unnamed_addr global [100010 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z4disti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [100010 x i64], ptr @memo, i64 0, i64 %2
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %.not = icmp eq i64 %4, -1
  br i1 %.not, label %5, label %common.ret

5:                                                ; preds = %1
  %6 = load i32, ptr @K, align 4, !tbaa !9
  %7 = icmp eq i32 %6, %0
  br i1 %7, label %common.ret.sink.split, label %8

common.ret.sink.split:                            ; preds = %5, %8
  %.sink = phi i64 [ %15, %8 ], [ 0, %5 ]
  store i64 %.sink, ptr %3, align 8, !tbaa !5
  br label %common.ret

common.ret:                                       ; preds = %common.ret.sink.split, %1
  %common.ret.op = phi i64 [ %4, %1 ], [ %.sink, %common.ret.sink.split ]
  ret i64 %common.ret.op

8:                                                ; preds = %5
  %9 = getelementptr inbounds [100010 x %"class.std::tuple"], ptr @parent, i64 0, i64 %2
  %10 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3getILm0EJixEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERS4_(ptr noundef nonnull align 8 dereferenceable(12) %9) #1
  %11 = load i32, ptr %10, align 4, !tbaa !9
  %12 = tail call noundef i64 @_Z4disti(i32 noundef %11)
  %13 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3getILm1EJixEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERS4_(ptr noundef nonnull align 8 dereferenceable(12) %9) #1
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = add nsw i64 %14, %12
  br label %common.ret.sink.split
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3getILm0EJixEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERS4_(ptr noundef nonnull align 8 dereferenceable(12)) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3getILm1EJixEERNSt13tuple_elementIXT_ESt5tupleIJDpT0_EEE4typeERS4_(ptr noundef nonnull align 8 dereferenceable(12)) local_unnamed_addr #0

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 = !{!"long long", !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,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass