; LLVM IR from ShahjalalShohag/code-library :: Number Theory/CRT.ll (Z15extended_euclidnnRnS_)

; 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 nounwind uwtable
define dso_local noundef { i64, i64 } @_Z15extended_euclidnnRnS_(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, ptr noundef nonnull align 16 dereferenceable(16) %4, ptr noundef nonnull align 16 dereferenceable(16) %5) #0 {
  %7 = alloca i128, align 16
  %8 = alloca i128, align 16
  %9 = alloca i128, align 16
  %10 = alloca i128, align 16
  %11 = alloca i128, align 16
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i128, align 16
  %15 = alloca i128, align 16
  %16 = alloca i128, align 16
  %17 = alloca i128, align 16
  %18 = getelementptr inbounds { i64, i64 }, ptr %8, i32 0, i32 0
  store i64 %0, ptr %18, align 16
  %19 = getelementptr inbounds { i64, i64 }, ptr %8, i32 0, i32 1
  store i64 %1, ptr %19, align 8
  %20 = load i128, ptr %8, align 16, !tbaa !5
  %21 = getelementptr inbounds { i64, i64 }, ptr %9, i32 0, i32 0
  store i64 %2, ptr %21, align 16
  %22 = getelementptr inbounds { i64, i64 }, ptr %9, i32 0, i32 1
  store i64 %3, ptr %22, align 8
  %23 = load i128, ptr %9, align 16, !tbaa !5
  store i128 %20, ptr %10, align 16, !tbaa !5
  store i128 %23, ptr %11, align 16, !tbaa !5
  store ptr %4, ptr %12, align 8, !tbaa !9
  store ptr %5, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %14) #2
  %24 = load ptr, ptr %13, align 8, !tbaa !9
  store i128 0, ptr %24, align 16, !tbaa !5
  store i128 0, ptr %14, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %15) #2
  %25 = load ptr, ptr %12, align 8, !tbaa !9
  store i128 1, ptr %25, align 16, !tbaa !5
  store i128 1, ptr %15, align 16, !tbaa !5
  br label %26

26:                                               ; preds = %29, %6
  %27 = load i128, ptr %11, align 16, !tbaa !5
  %28 = icmp ne i128 %27, 0
  br i1 %28, label %29, label %56

29:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 16, ptr %16) #2
  %30 = load i128, ptr %10, align 16, !tbaa !5
  %31 = load i128, ptr %11, align 16, !tbaa !5
  %32 = sdiv i128 %30, %31
  store i128 %32, ptr %16, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %17) #2
  %33 = load i128, ptr %11, align 16, !tbaa !5
  store i128 %33, ptr %17, align 16, !tbaa !5
  %34 = load i128, ptr %10, align 16, !tbaa !5
  %35 = load i128, ptr %11, align 16, !tbaa !5
  %36 = srem i128 %34, %35
  store i128 %36, ptr %11, align 16, !tbaa !5
  %37 = load i128, ptr %17, align 16, !tbaa !5
  store i128 %37, ptr %10, align 16, !tbaa !5
  %38 = load i128, ptr %14, align 16, !tbaa !5
  store i128 %38, ptr %17, align 16, !tbaa !5
  %39 = load ptr, ptr %12, align 8, !tbaa !9
  %40 = load i128, ptr %39, align 16, !tbaa !5
  %41 = load i128, ptr %16, align 16, !tbaa !5
  %42 = load i128, ptr %14, align 16, !tbaa !5
  %43 = mul nsw i128 %41, %42
  %44 = sub nsw i128 %40, %43
  store i128 %44, ptr %14, align 16, !tbaa !5
  %45 = load i128, ptr %17, align 16, !tbaa !5
  %46 = load ptr, ptr %12, align 8, !tbaa !9
  store i128 %45, ptr %46, align 16, !tbaa !5
  %47 = load i128, ptr %15, align 16, !tbaa !5
  store i128 %47, ptr %17, align 16, !tbaa !5
  %48 = load ptr, ptr %13, align 8, !tbaa !9
  %49 = load i128, ptr %48, align 16, !tbaa !5
  %50 = load i128, ptr %16, align 16, !tbaa !5
  %51 = load i128, ptr %15, align 16, !tbaa !5
  %52 = mul nsw i128 %50, %51
  %53 = sub nsw i128 %49, %52
  store i128 %53, ptr %15, align 16, !tbaa !5
  %54 = load i128, ptr %17, align 16, !tbaa !5
  %55 = load ptr, ptr %13, align 8, !tbaa !9
  store i128 %54, ptr %55, align 16, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 16, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %16) #2
  br label %26, !llvm.loop !11

56:                                               ; preds = %26
  %57 = load i128, ptr %10, align 16, !tbaa !5
  store i128 %57, ptr %7, align 16
  call void @llvm.lifetime.end.p0(i64 16, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %14) #2
  %58 = load { i64, i64 }, ptr %7, align 16
  ret { i64, i64 } %58
}

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

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local noundef { i64, i64 } @_Z15extended_euclidnnRnS_(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, ptr nocapture noundef nonnull align 16 dereferenceable(16) %4, ptr nocapture noundef nonnull align 16 dereferenceable(16) %5) local_unnamed_addr #0 {
  %.sroa.2.0.insert.ext = zext i64 %3 to i128
  %.sroa.2.0.insert.shift = shl nuw i128 %.sroa.2.0.insert.ext, 64
  %.sroa.0.0.insert.ext = zext i64 %2 to i128
  %.sroa.0.0.insert.insert = or disjoint i128 %.sroa.2.0.insert.shift, %.sroa.0.0.insert.ext
  store i128 0, ptr %5, align 16, !tbaa !5
  store i128 1, ptr %4, align 16, !tbaa !5
  %.not28 = icmp eq i128 %.sroa.0.0.insert.insert, 0
  br i1 %.not28, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %6
  %.sroa.222.0.insert.ext = zext i64 %1 to i128
  %.sroa.222.0.insert.shift = shl nuw i128 %.sroa.222.0.insert.ext, 64
  %.sroa.021.0.insert.ext = zext i64 %0 to i128
  %.sroa.021.0.insert.insert = or disjoint i128 %.sroa.222.0.insert.shift, %.sroa.021.0.insert.ext
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.032 = phi i128 [ %.02531, %.lr.ph ], [ %.sroa.021.0.insert.insert, %.lr.ph.preheader ]
  %.02531 = phi i128 [ %.decomposed, %.lr.ph ], [ %.sroa.0.0.insert.insert, %.lr.ph.preheader ]
  %.02630 = phi i128 [ %14, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %.02729 = phi i128 [ %11, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %.032.frozen = freeze i128 %.032
  %.02531.frozen = freeze i128 %.02531
  %7 = sdiv i128 %.032.frozen, %.02531.frozen
  %8 = mul i128 %7, %.02531.frozen
  %.decomposed = sub i128 %.032.frozen, %8
  %9 = load i128, ptr %4, align 16, !tbaa !5
  %10 = mul nsw i128 %7, %.02729
  %11 = sub nsw i128 %9, %10
  store i128 %.02729, ptr %4, align 16, !tbaa !5
  %12 = load i128, ptr %5, align 16, !tbaa !5
  %13 = mul nsw i128 %7, %.02630
  %14 = sub nsw i128 %12, %13
  store i128 %.02630, ptr %5, align 16, !tbaa !5
  %.not = icmp eq i128 %.decomposed, 0
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !9

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %extract35 = lshr i128 %.02531, 64
  %extract.t36 = trunc nuw i128 %extract35 to i64
  %extract.t33 = trunc i128 %.02531 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %6
  %.0.lcssa.off0 = phi i64 [ %0, %6 ], [ %extract.t33, %._crit_edge.loopexit ]
  %.0.lcssa.off64 = phi i64 [ %1, %6 ], [ %extract.t36, %._crit_edge.loopexit ]
  %.fca.0.insert = insertvalue { i64, i64 } poison, i64 %.0.lcssa.off0, 0
  %.fca.1.insert = insertvalue { i64, i64 } %.fca.0.insert, i64 %.0.lcssa.off64, 1
  ret { i64, i64 } %.fca.1.insert
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) 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 = !{!"__int128", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: DivRemPairsPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass