; LLVM IR from eyangch/competitive-programming :: Codeforces/1543/d2.ll (Z4kxorxx)

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

@K = external dso_local global i64, align 8

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

11:                                               ; preds = %21, %2
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = icmp sle i64 %12, %13
  br i1 %14, label %19, label %15

15:                                               ; preds = %11
  %16 = load i64, ptr %5, align 8, !tbaa !5
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = icmp sle i64 %16, %17
  br label %19

19:                                               ; preds = %15, %11
  %20 = phi i1 [ true, %11 ], [ %18, %15 ]
  br i1 %20, label %21, label %25

21:                                               ; preds = %19
  %22 = load i64, ptr @K, align 8, !tbaa !5
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = mul nsw i64 %23, %22
  store i64 %24, ptr %5, align 8, !tbaa !5
  br label %11, !llvm.loop !9

25:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %26 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %26, ptr %7, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %54, %25
  %28 = load i64, ptr %7, align 8, !tbaa !5
  %29 = icmp sgt i64 %28, 0
  br i1 %29, label %31, label %30

30:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %58

31:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %33 = load i64, ptr %7, align 8, !tbaa !5
  %34 = sdiv i64 %32, %33
  store i64 %34, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %35 = load i64, ptr %4, align 8, !tbaa !5
  %36 = load i64, ptr %7, align 8, !tbaa !5
  %37 = sdiv i64 %35, %36
  store i64 %37, ptr %9, align 8, !tbaa !5
  %38 = load i64, ptr %7, align 8, !tbaa !5
  %39 = load i64, ptr %3, align 8, !tbaa !5
  %40 = srem i64 %39, %38
  store i64 %40, ptr %3, align 8, !tbaa !5
  %41 = load i64, ptr %7, align 8, !tbaa !5
  %42 = load i64, ptr %4, align 8, !tbaa !5
  %43 = srem i64 %42, %41
  store i64 %43, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %44 = load i64, ptr %8, align 8, !tbaa !5
  %45 = load i64, ptr %9, align 8, !tbaa !5
  %46 = add nsw i64 %44, %45
  %47 = load i64, ptr @K, align 8, !tbaa !5
  %48 = srem i64 %46, %47
  store i64 %48, ptr %10, align 8, !tbaa !5
  %49 = load i64, ptr %10, align 8, !tbaa !5
  %50 = load i64, ptr %7, align 8, !tbaa !5
  %51 = mul nsw i64 %49, %50
  %52 = load i64, ptr %6, align 8, !tbaa !5
  %53 = add nsw i64 %52, %51
  store i64 %53, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %54

54:                                               ; preds = %31
  %55 = load i64, ptr @K, align 8, !tbaa !5
  %56 = load i64, ptr %7, align 8, !tbaa !5
  %57 = sdiv i64 %56, %55
  store i64 %57, ptr %7, align 8, !tbaa !5
  br label %27, !llvm.loop !11

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

; 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"}
!11 = distinct !{!11, !10}


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

@K = 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 i64 @_Z4kxorxx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %invariant.smax = tail call i64 @llvm.smax.i64(i64 %0, i64 %1)
  %3 = icmp sgt i64 %invariant.smax, 0
  br i1 %3, label %.lr.ph, label %.lr.ph33

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

.preheader:                                       ; preds = %7
  %5 = icmp sgt i64 %8, 0
  br i1 %5, label %.lr.ph33, label %._crit_edge

.lr.ph33:                                         ; preds = %2, %.preheader
  %.023.lcssa37 = phi i64 [ %8, %.preheader ], [ 1, %2 ]
  %6 = load i64, ptr @K, align 8, !tbaa !5
  br label %9

7:                                                ; preds = %.lr.ph, %7
  %.02328 = phi i64 [ 1, %.lr.ph ], [ %8, %7 ]
  %8 = mul nsw i64 %4, %.02328
  %.not = icmp sgt i64 %8, %invariant.smax
  br i1 %.not, label %.preheader, label %7, !llvm.loop !9

._crit_edge:                                      ; preds = %9, %.preheader
  %.025.lcssa = phi i64 [ 0, %.preheader ], [ %17, %9 ]
  ret i64 %.025.lcssa

9:                                                ; preds = %.lr.ph33, %9
  %.032 = phi i64 [ %0, %.lr.ph33 ], [ %12, %9 ]
  %.02231 = phi i64 [ %1, %.lr.ph33 ], [ %13, %9 ]
  %.02430 = phi i64 [ %.023.lcssa37, %.lr.ph33 ], [ %18, %9 ]
  %.02529 = phi i64 [ 0, %.lr.ph33 ], [ %17, %9 ]
  %10 = sdiv i64 %.032, %.02430
  %11 = sdiv i64 %.02231, %.02430
  %12 = srem i64 %.032, %.02430
  %13 = srem i64 %.02231, %.02430
  %14 = add nsw i64 %11, %10
  %15 = srem i64 %14, %6
  %16 = mul nsw i64 %15, %.02430
  %17 = add nsw i64 %16, %.02529
  %18 = sdiv i64 %.02430, %6
  %19 = icmp sgt i64 %18, 0
  br i1 %19, label %9, label %._crit_edge, !llvm.loop !11
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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"}
!11 = distinct !{!11, !10}


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