; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/SHORT/COOK70/Problem D/D.ll (Z7getHashii)

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

@p = external dso_local global i64, align 8
@h = external dso_local global [200100 x i64], align 16
@pwr = external dso_local global [200100 x i64], align 16

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z7getHashii(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %6 = load i32, ptr %4, align 4, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [200100 x i64], ptr @h, i64 0, i64 %7
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = sub nsw i32 %10, 1
  %12 = icmp sge i32 %11, 0
  br i1 %12, label %13, label %29

13:                                               ; preds = %2
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = sub nsw i32 %14, 1
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [200100 x i64], ptr @h, i64 0, i64 %16
  %18 = load i64, ptr %17, align 8, !tbaa !9
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = sub nsw i32 %19, %20
  %22 = add nsw i32 %21, 1
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [200100 x i64], ptr @pwr, i64 0, i64 %23
  %25 = load i64, ptr %24, align 8, !tbaa !9
  %26 = mul nsw i64 %18, %25
  %27 = load i64, ptr @p, align 8, !tbaa !9
  %28 = srem i64 %26, %27
  br label %30

29:                                               ; preds = %2
  br label %30

30:                                               ; preds = %29, %13
  %31 = phi i64 [ %28, %13 ], [ 0, %29 ]
  %32 = sub nsw i64 %9, %31
  store i64 %32, ptr %5, align 8, !tbaa !9
  %33 = load i64, ptr %5, align 8, !tbaa !9
  %34 = icmp slt i64 %33, 0
  br i1 %34, label %35, label %39

35:                                               ; preds = %30
  %36 = load i64, ptr @p, align 8, !tbaa !9
  %37 = load i64, ptr %5, align 8, !tbaa !9
  %38 = add nsw i64 %37, %36
  store i64 %38, ptr %5, align 8, !tbaa !9
  br label %39

39:                                               ; preds = %35, %30
  %40 = load i64, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i64 %40
}

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

@p = external dso_local local_unnamed_addr global i64, align 8
@h = external dso_local local_unnamed_addr global [200100 x i64], align 16
@pwr = external dso_local local_unnamed_addr global [200100 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z7getHashii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %1 to i64
  %4 = getelementptr inbounds [200100 x i64], ptr @h, i64 0, i64 %3
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp sgt i32 %0, 0
  %.pre = load i64, ptr @p, align 8
  br i1 %6, label %7, label %18

7:                                                ; preds = %2
  %8 = add nsw i32 %0, -1
  %9 = zext nneg i32 %8 to i64
  %10 = getelementptr inbounds [200100 x i64], ptr @h, i64 0, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %reass.sub = sub i32 %1, %0
  %12 = add i32 %reass.sub, 1
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [200100 x i64], ptr @pwr, i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = mul nsw i64 %15, %11
  %17 = srem i64 %16, %.pre
  br label %18

18:                                               ; preds = %2, %7
  %19 = phi i64 [ %17, %7 ], [ 0, %2 ]
  %20 = sub nsw i64 %5, %19
  %21 = icmp slt i64 %20, 0
  %22 = select i1 %21, i64 %.pre, i64 0
  %.0 = add nsw i64 %22, %20
  ret i64 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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