; LLVM IR from tangjz/acm-icpc :: codeforces/complete/547A.ll (Z3CRTxxxxRx)

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z5exgcdxxRxS_(i64 noundef, i64 noundef, ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #0

; 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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z3CRTxxxxRx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, ptr noundef nonnull align 8 dereferenceable(8) %4) #0 {
  %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
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  %15 = alloca i64, align 8
  %16 = alloca i32, align 4
  store i64 %0, ptr %7, align 8, !tbaa !5
  store i64 %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !5
  store i64 %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %17 = load i64, ptr %7, align 8, !tbaa !5
  store i64 %17, ptr %12, align 8, !tbaa !5
  %18 = load i64, ptr %8, align 8, !tbaa !5
  %19 = load ptr, ptr %11, align 8, !tbaa !9
  store i64 %18, ptr %19, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #2
  %20 = load ptr, ptr %11, align 8, !tbaa !9
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = load i64, ptr %10, align 8, !tbaa !5
  %23 = call noundef i64 @_Z5exgcdxxRxS_(i64 noundef %21, i64 noundef %22, ptr noundef nonnull align 8 dereferenceable(8) %13, ptr noundef nonnull align 8 dereferenceable(8) %14)
  store i64 %23, ptr %15, align 8, !tbaa !5
  %24 = load i64, ptr %9, align 8, !tbaa !5
  %25 = load i64, ptr %12, align 8, !tbaa !5
  %26 = sub nsw i64 %24, %25
  %27 = load i64, ptr %15, align 8, !tbaa !5
  %28 = srem i64 %26, %27
  %29 = icmp ne i64 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %5
  store i64 -1, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %65

31:                                               ; preds = %5
  %32 = load i64, ptr %9, align 8, !tbaa !5
  %33 = load i64, ptr %12, align 8, !tbaa !5
  %34 = sub nsw i64 %32, %33
  %35 = load i64, ptr %15, align 8, !tbaa !5
  %36 = sdiv i64 %34, %35
  %37 = load i64, ptr %13, align 8, !tbaa !5
  %38 = mul nsw i64 %36, %37
  %39 = load i64, ptr %10, align 8, !tbaa !5
  %40 = load i64, ptr %15, align 8, !tbaa !5
  %41 = sdiv i64 %39, %40
  %42 = srem i64 %38, %41
  store i64 %42, ptr %13, align 8, !tbaa !5
  %43 = load i64, ptr %13, align 8, !tbaa !5
  %44 = icmp slt i64 %43, 0
  br i1 %44, label %45, label %51

45:                                               ; preds = %31
  %46 = load i64, ptr %10, align 8, !tbaa !5
  %47 = load i64, ptr %15, align 8, !tbaa !5
  %48 = sdiv i64 %46, %47
  %49 = load i64, ptr %13, align 8, !tbaa !5
  %50 = add nsw i64 %49, %48
  store i64 %50, ptr %13, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %45, %31
  %52 = load ptr, ptr %11, align 8, !tbaa !9
  %53 = load i64, ptr %52, align 8, !tbaa !5
  %54 = load i64, ptr %13, align 8, !tbaa !5
  %55 = mul nsw i64 %53, %54
  %56 = load i64, ptr %12, align 8, !tbaa !5
  %57 = add nsw i64 %56, %55
  store i64 %57, ptr %12, align 8, !tbaa !5
  %58 = load i64, ptr %10, align 8, !tbaa !5
  %59 = load i64, ptr %15, align 8, !tbaa !5
  %60 = sdiv i64 %58, %59
  %61 = load ptr, ptr %11, align 8, !tbaa !9
  %62 = load i64, ptr %61, align 8, !tbaa !5
  %63 = mul nsw i64 %62, %60
  store i64 %63, ptr %61, align 8, !tbaa !5
  %64 = load i64, ptr %12, align 8, !tbaa !5
  store i64 %64, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %65

65:                                               ; preds = %51, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  %66 = load i64, ptr %6, align 8
  ret i64 %66
}

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" }
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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !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"

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z5exgcdxxRxS_(i64 noundef, i64 noundef, ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z3CRTxxxxRx(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, ptr nocapture noundef nonnull align 8 dereferenceable(8) %4) local_unnamed_addr #0 {
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store i64 %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #2
  %8 = call noundef i64 @_Z5exgcdxxRxS_(i64 noundef %1, i64 noundef %3, ptr noundef nonnull align 8 dereferenceable(8) %6, ptr noundef nonnull align 8 dereferenceable(8) %7)
  %9 = sub nsw i64 %2, %0
  %10 = srem i64 %9, %8
  %11 = sdiv i64 %9, %8
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %12, label %23

12:                                               ; preds = %5
  %13 = load i64, ptr %6, align 8, !tbaa !5
  %14 = mul nsw i64 %13, %11
  %15 = sdiv i64 %3, %8
  %16 = srem i64 %14, %15
  %17 = icmp slt i64 %16, 0
  %18 = select i1 %17, i64 %15, i64 0
  %spec.select = add nsw i64 %18, %16
  %19 = load i64, ptr %4, align 8, !tbaa !5
  %20 = mul nsw i64 %spec.select, %19
  %21 = add nsw i64 %20, %0
  %22 = mul nsw i64 %19, %15
  store i64 %22, ptr %4, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %5, %12
  %.0 = phi i64 [ %21, %12 ], [ -1, %5 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #2
  ret i64 %.0
}

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" }
attributes #1 = { mustprogress 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"}


; Effective O3 passes: DSEPass,DivRemPairsPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass