; LLVM IR from eyangch/competitive-programming :: USACOcontests/silver/2020.01/loan.ll (Z5worksx)

; 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
@K = external dso_local global i64, align 8
@M = external dso_local global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z5worksx(i64 noundef %0) #0 {
  %2 = alloca i1, align 1
  %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
  %9 = load i64, ptr @N, align 8, !tbaa !5
  store i64 %9, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %10 = load i64, ptr @K, align 8, !tbaa !5
  store i64 %10, ptr %5, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %53, %1
  %12 = load i64, ptr %4, align 8, !tbaa !5
  %13 = icmp sgt i64 %12, 0
  br i1 %13, label %14, label %17

14:                                               ; preds = %11
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = icmp sgt i64 %15, 0
  br label %17

17:                                               ; preds = %14, %11
  %18 = phi i1 [ false, %11 ], [ %16, %14 ]
  br i1 %18, label %19, label %54

19:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %20 = load i64, ptr %4, align 8, !tbaa !5
  %21 = load i64, ptr %3, align 8, !tbaa !5
  %22 = sdiv i64 %20, %21
  store i64 %22, ptr %6, align 8, !tbaa !5
  %23 = load i64, ptr %6, align 8, !tbaa !5
  %24 = load i64, ptr @M, align 8, !tbaa !5
  %25 = icmp sle i64 %23, %24
  br i1 %25, label %26, label %32

26:                                               ; preds = %19
  %27 = load i64, ptr %5, align 8, !tbaa !5
  %28 = load i64, ptr @M, align 8, !tbaa !5
  %29 = mul nsw i64 %27, %28
  %30 = load i64, ptr %4, align 8, !tbaa !5
  %31 = sub nsw i64 %30, %29
  store i64 %31, ptr %4, align 8, !tbaa !5
  store i32 3, ptr %7, align 4
  br label %51

32:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %33 = load i64, ptr %4, align 8, !tbaa !5
  %34 = load i64, ptr %6, align 8, !tbaa !5
  %35 = load i64, ptr %3, align 8, !tbaa !5
  %36 = mul nsw i64 %34, %35
  %37 = sub nsw i64 %33, %36
  store i64 %37, ptr %8, align 8, !tbaa !5
  %38 = load i64, ptr %6, align 8, !tbaa !5
  %39 = load i64, ptr %8, align 8, !tbaa !5
  %40 = sdiv i64 %39, %38
  store i64 %40, ptr %8, align 8, !tbaa !5
  %41 = load i64, ptr %8, align 8, !tbaa !5
  %42 = add nsw i64 %41, 1
  store i64 %42, ptr %8, align 8, !tbaa !5
  %43 = load i64, ptr %8, align 8, !tbaa !5
  %44 = load i64, ptr %6, align 8, !tbaa !5
  %45 = mul nsw i64 %43, %44
  %46 = load i64, ptr %4, align 8, !tbaa !5
  %47 = sub nsw i64 %46, %45
  store i64 %47, ptr %4, align 8, !tbaa !5
  %48 = load i64, ptr %8, align 8, !tbaa !5
  %49 = load i64, ptr %5, align 8, !tbaa !5
  %50 = sub nsw i64 %49, %48
  store i64 %50, 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 %51

51:                                               ; preds = %32, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %52 = load i32, ptr %7, align 4
  switch i32 %52, label %61 [
    i32 0, label %53
    i32 3, label %54
  ]

53:                                               ; preds = %51
  br label %11, !llvm.loop !9

54:                                               ; preds = %51, %17
  %55 = load i64, ptr %4, align 8, !tbaa !5
  %56 = icmp sle i64 %55, 0
  br i1 %56, label %57, label %58

57:                                               ; preds = %54
  store i1 true, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %59

58:                                               ; preds = %54
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %59

59:                                               ; preds = %58, %57
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %60 = load i1, ptr %2, align 1
  ret i1 %60

61:                                               ; preds = %51
  unreachable
}

; 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 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 = { 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 = !{!"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
@K = external dso_local local_unnamed_addr global i64, align 8
@M = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z5worksx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr @N, align 8, !tbaa !5
  %3 = load i64, ptr @K, align 8, !tbaa !5
  %4 = icmp sgt i64 %2, 0
  %5 = icmp sgt i64 %3, 0
  %6 = select i1 %4, i1 %5, i1 false
  br i1 %6, label %.lr.ph, label %.loopexit

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

8:                                                ; preds = %.lr.ph, %11
  %.02134 = phi i64 [ %2, %.lr.ph ], [ %.1, %11 ]
  %.02233 = phi i64 [ %3, %.lr.ph ], [ %16, %11 ]
  %9 = sdiv i64 %.02134, %0
  %.recomposed = srem i64 %.02134, %0
  %.not = icmp sgt i64 %9, %7
  br i1 %.not, label %11, label %.thread

.thread:                                          ; preds = %8
  %10 = mul nsw i64 %7, %.02233
  %.129 = sub nsw i64 %.02134, %10
  br label %.loopexit

11:                                               ; preds = %8
  %12 = mul nsw i64 %9, %0
  %13 = sdiv i64 %.recomposed, %9
  %14 = add nsw i64 %13, 1
  %15 = mul nsw i64 %14, %9
  %16 = sub nsw i64 %.02233, %14
  %.1 = sub nsw i64 %.02134, %15
  %17 = icmp sgt i64 %.1, 0
  %18 = icmp sgt i64 %16, 0
  %19 = select i1 %17, i1 %18, i1 false
  br i1 %19, label %8, label %.loopexit

.loopexit:                                        ; preds = %11, %1, %.thread
  %.2 = phi i64 [ %.129, %.thread ], [ %2, %1 ], [ %.1, %11 ]
  %20 = icmp slt i64 %.2, 1
  ret i1 %20
}

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