; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2019-20/Jan/Silver/loan.ll (Z2okx)

; 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 i64, align 8
@M = 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 nounwind uwtable
declare dso_local noundef i64 @_Z4cdivxx(i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z2okx(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  store i64 %0, 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 8, ptr %5) #2
  %9 = load i64, ptr @N, align 8, !tbaa !5
  store i64 %9, ptr %5, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %43, %1
  %11 = load i64, ptr %5, align 8, !tbaa !5
  %12 = icmp ne i64 %11, 0
  br i1 %12, label %13, label %44

13:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %14 = load i64, ptr %5, align 8, !tbaa !5
  %15 = load i64, ptr %3, align 8, !tbaa !5
  %16 = sdiv i64 %14, %15
  store i64 %16, ptr %6, align 8, !tbaa !5
  %17 = load i64, ptr %6, align 8, !tbaa !5
  %18 = load i64, ptr @M, align 8, !tbaa !5
  %19 = icmp sle i64 %17, %18
  br i1 %19, label %20, label %26

20:                                               ; preds = %13
  %21 = load i64, ptr %4, align 8, !tbaa !5
  %22 = load i64, ptr %5, align 8, !tbaa !5
  %23 = load i64, ptr @M, align 8, !tbaa !5
  %24 = call noundef i64 @_Z4cdivxx(i64 noundef %22, i64 noundef %23)
  %25 = add nsw i64 %21, %24
  store i64 %25, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %41

26:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %27 = load i64, ptr %5, align 8, !tbaa !5
  %28 = load i64, ptr %6, align 8, !tbaa !5
  %29 = sdiv i64 %27, %28
  %30 = load i64, ptr %3, align 8, !tbaa !5
  %31 = sub nsw i64 %29, %30
  %32 = add nsw i64 %31, 1
  store i64 %32, ptr %8, align 8, !tbaa !5
  %33 = load i64, ptr %8, align 8, !tbaa !5
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = add nsw i64 %34, %33
  store i64 %35, ptr %4, align 8, !tbaa !5
  %36 = load i64, ptr %6, align 8, !tbaa !5
  %37 = load i64, ptr %8, align 8, !tbaa !5
  %38 = mul nsw i64 %36, %37
  %39 = load i64, ptr %5, align 8, !tbaa !5
  %40 = sub nsw i64 %39, %38
  store i64 %40, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  store i32 0, ptr %7, align 4
  br label %41

41:                                               ; preds = %26, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %42 = load i32, ptr %7, align 4
  switch i32 %42, label %46 [
    i32 0, label %43
  ]

43:                                               ; preds = %41
  br label %10, !llvm.loop !9

44:                                               ; preds = %10
  %45 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %45, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %46

46:                                               ; preds = %44, %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %47 = load i64, ptr %2, align 8
  ret i64 %47
}

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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z2okx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr @N, align 8, !tbaa !5
  %.not30 = icmp eq i64 %2, 0
  br i1 %.not30, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %3 = load i64, ptr @M, align 8, !tbaa !5
  br label %4

4:                                                ; preds = %.lr.ph, %8
  %.01832 = phi i64 [ 0, %.lr.ph ], [ %12, %8 ]
  %.02031 = phi i64 [ %2, %.lr.ph ], [ %14, %8 ]
  %5 = sdiv i64 %.02031, %0
  %.not24 = icmp sgt i64 %5, %3
  br i1 %.not24, label %8, label %.thread

.thread:                                          ; preds = %4
  %6 = tail call noundef i64 @_Z4cdivxx(i64 noundef %.02031, i64 noundef %3)
  %7 = add nsw i64 %6, %.01832
  br label %.loopexit

8:                                                ; preds = %4
  %9 = sdiv i64 %.02031, %5
  %10 = sub nsw i64 %9, %0
  %11 = add nsw i64 %10, 1
  %12 = add nsw i64 %11, %.01832
  %13 = mul nsw i64 %11, %5
  %14 = sub nsw i64 %.02031, %13
  %.not = icmp eq i64 %14, 0
  br i1 %.not, label %.loopexit, label %4

.loopexit:                                        ; preds = %8, %1, %.thread
  %.2 = phi i64 [ %7, %.thread ], [ 0, %1 ], [ %12, %8 ]
  ret i64 %.2
}

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

!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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass