; LLVM IR from kazunetakahashi/atcoder :: 2018/0421_ARC096/E.ll (Z1fx)

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

@MOD = external dso_local global i64, align 8
@N = external dso_local global i64, align 8

; 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 uwtable
declare dso_local noundef i64 @_Z5powerxx(i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z13power_2_powerx(i64 noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z4calcxx(i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z1fx(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %6 = load i64, ptr @N, align 8, !tbaa !5
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %8 = sub nsw i64 %6, %7
  %9 = call noundef i64 @_Z13power_2_powerx(i64 noundef %8)
  store i64 %9, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 0, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %36, %1
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = icmp sle i64 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %39

16:                                               ; preds = %10
  %17 = load i64, ptr %2, align 8, !tbaa !5
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = call noundef i64 @_Z4calcxx(i64 noundef %17, i64 noundef %19)
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = load i64, ptr @N, align 8, !tbaa !5
  %24 = load i64, ptr %2, align 8, !tbaa !5
  %25 = sub nsw i64 %23, %24
  %26 = mul nsw i64 %22, %25
  %27 = call noundef i64 @_Z5powerxx(i64 noundef 2, i64 noundef %26)
  %28 = mul nsw i64 %20, %27
  %29 = load i64, ptr @MOD, align 8, !tbaa !5
  %30 = srem i64 %28, %29
  %31 = load i64, ptr %4, align 8, !tbaa !5
  %32 = add nsw i64 %31, %30
  store i64 %32, ptr %4, align 8, !tbaa !5
  %33 = load i64, ptr @MOD, align 8, !tbaa !5
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = srem i64 %34, %33
  store i64 %35, ptr %4, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %16
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %5, align 4, !tbaa !9
  br label %10, !llvm.loop !11

39:                                               ; preds = %15
  %40 = load i64, ptr %3, align 8, !tbaa !5
  %41 = load i64, ptr %4, align 8, !tbaa !5
  %42 = mul nsw i64 %40, %41
  %43 = load i64, ptr @MOD, align 8, !tbaa !5
  %44 = srem i64 %42, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i64 %44
}

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

@MOD = external dso_local local_unnamed_addr global i64, align 8
@N = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z5powerxx(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z13power_2_powerx(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z4calcxx(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z1fx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr @N, align 8, !tbaa !5
  %3 = sub nsw i64 %2, %0
  %4 = tail call noundef i64 @_Z13power_2_powerx(i64 noundef %3)
  %.not15 = icmp slt i64 %0, 0
  br i1 %.not15, label %.._crit_edge_crit_edge, label %.lr.ph

.._crit_edge_crit_edge:                           ; preds = %1
  %.pre = load i64, ptr @MOD, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.._crit_edge_crit_edge
  %5 = phi i64 [ %.pre, %.._crit_edge_crit_edge ], [ %14, %.lr.ph ]
  %.012.lcssa = phi i64 [ 0, %.._crit_edge_crit_edge ], [ %17, %.lr.ph ]
  %6 = mul nsw i64 %.012.lcssa, %4
  %7 = srem i64 %6, %5
  ret i64 %7

.lr.ph:                                           ; preds = %1, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %1 ]
  %.01216 = phi i64 [ %17, %.lr.ph ], [ 0, %1 ]
  %8 = tail call noundef i64 @_Z4calcxx(i64 noundef %0, i64 noundef %indvars.iv)
  %9 = load i64, ptr @N, align 8, !tbaa !5
  %10 = sub nsw i64 %9, %0
  %11 = mul nsw i64 %10, %indvars.iv
  %12 = tail call noundef i64 @_Z5powerxx(i64 noundef 2, i64 noundef %11)
  %13 = mul nsw i64 %12, %8
  %14 = load i64, ptr @MOD, align 8, !tbaa !5
  %15 = srem i64 %13, %14
  %16 = add nsw i64 %15, %.01216
  %17 = srem i64 %16, %14
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.not.not = icmp slt i64 %indvars.iv, %0
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !9
}

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

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass