; LLVM IR from tfhe/spqlios-arithmetic :: test/testlib/mod_q120.ll (ZNK8mod_q1209to_int128Ev)

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

%class.mod_q120 = type { [4 x i64] }

@_ZN8mod_q1202QiE = external dso_local constant [4 x i64], align 16
@_ZZNK8mod_q1209to_int128EvE2qm = external hidden constant [4 x i128], align 16
@_ZZNK8mod_q1209to_int128EvE4CRTi = external hidden constant [4 x i64], align 16

; 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
define dso_local noundef { i64, i64 } @_ZNK8mod_q1209to_int128Ev(ptr noundef nonnull align 8 dereferenceable(32) %0) #1 align 2 {
  %2 = alloca i128, align 16
  %3 = alloca ptr, align 8
  %4 = alloca i128, align 16
  %5 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr %4) #2
  store i128 0, ptr %4, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !11
  br label %7

7:                                                ; preds = %31, %1
  %8 = load i64, ptr %5, align 8, !tbaa !11
  %9 = icmp ult i64 %8, 4
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %7
  %12 = getelementptr inbounds %class.mod_q120, ptr %6, i32 0, i32 0
  %13 = load i64, ptr %5, align 8, !tbaa !11
  %14 = getelementptr inbounds [4 x i64], ptr %12, i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !11
  %16 = load i64, ptr %5, align 8, !tbaa !11
  %17 = getelementptr inbounds [4 x i64], ptr @_ZZNK8mod_q1209to_int128EvE4CRTi, i64 0, i64 %16
  %18 = load i64, ptr %17, align 8, !tbaa !11
  %19 = mul nsw i64 %15, %18
  %20 = load i64, ptr %5, align 8, !tbaa !11
  %21 = getelementptr inbounds [4 x i64], ptr @_ZN8mod_q1202QiE, i64 0, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = srem i64 %19, %22
  %24 = sext i64 %23 to i128
  %25 = load i64, ptr %5, align 8, !tbaa !11
  %26 = getelementptr inbounds [4 x i128], ptr @_ZZNK8mod_q1209to_int128EvE2qm, i64 0, i64 %25
  %27 = load i128, ptr %26, align 16, !tbaa !9
  %28 = mul nsw i128 %24, %27
  %29 = load i128, ptr %4, align 16, !tbaa !9
  %30 = add nsw i128 %29, %28
  store i128 %30, ptr %4, align 16, !tbaa !9
  br label %31

31:                                               ; preds = %11
  %32 = load i64, ptr %5, align 8, !tbaa !11
  %33 = add i64 %32, 1
  store i64 %33, ptr %5, align 8, !tbaa !11
  br label %7, !llvm.loop !13

34:                                               ; preds = %10
  %35 = load i128, ptr %4, align 16, !tbaa !9
  %36 = srem i128 %35, 1315642440469820935610546842527858689
  %37 = add nsw i128 %36, 1315642440469820935610546842527858689
  %38 = add nsw i128 %37, 657821220234910467805273421263929344
  %39 = srem i128 %38, 1315642440469820935610546842527858689
  %40 = sub nsw i128 %39, 657821220234910467805273421263929344
  store i128 %40, ptr %4, align 16, !tbaa !9
  %41 = load i128, ptr %4, align 16, !tbaa !9
  store i128 %41, ptr %2, align 16
  call void @llvm.lifetime.end.p0(i64 16, ptr %4) #2
  %42 = load { i64, i64 }, ptr %2, align 16
  ret { i64, i64 } %42
}

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

@_ZN8mod_q1202QiE = external dso_local local_unnamed_addr constant [4 x i64], align 16
@_ZZNK8mod_q1209to_int128EvE2qm = external hidden local_unnamed_addr constant [4 x i128], align 16
@_ZZNK8mod_q1209to_int128EvE4CRTi = external hidden local_unnamed_addr constant [4 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef { i64, i64 } @_ZNK8mod_q1209to_int128Ev(ptr nocapture noundef nonnull readonly align 8 dereferenceable(32) %0) local_unnamed_addr #0 align 2 {
  %2 = load i64, ptr %0, align 8, !tbaa !5
  %3 = load i64, ptr @_ZZNK8mod_q1209to_int128EvE4CRTi, align 16, !tbaa !5
  %4 = mul nsw i64 %3, %2
  %5 = load i64, ptr @_ZN8mod_q1202QiE, align 16, !tbaa !5
  %6 = srem i64 %4, %5
  %7 = sext i64 %6 to i128
  %8 = load i128, ptr @_ZZNK8mod_q1209to_int128EvE2qm, align 16, !tbaa !9
  %9 = mul nsw i128 %8, %7
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %12 = load i64, ptr getelementptr inbounds (i8, ptr @_ZZNK8mod_q1209to_int128EvE4CRTi, i64 8), align 8, !tbaa !5
  %13 = mul nsw i64 %12, %11
  %14 = load i64, ptr getelementptr inbounds (i8, ptr @_ZN8mod_q1202QiE, i64 8), align 8, !tbaa !5
  %15 = srem i64 %13, %14
  %16 = sext i64 %15 to i128
  %17 = load i128, ptr getelementptr inbounds (i8, ptr @_ZZNK8mod_q1209to_int128EvE2qm, i64 16), align 16, !tbaa !9
  %18 = mul nsw i128 %17, %16
  %19 = add nsw i128 %18, %9
  %20 = getelementptr inbounds i8, ptr %0, i64 16
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = load i64, ptr getelementptr inbounds (i8, ptr @_ZZNK8mod_q1209to_int128EvE4CRTi, i64 16), align 16, !tbaa !5
  %23 = mul nsw i64 %22, %21
  %24 = load i64, ptr getelementptr inbounds (i8, ptr @_ZN8mod_q1202QiE, i64 16), align 16, !tbaa !5
  %25 = srem i64 %23, %24
  %26 = sext i64 %25 to i128
  %27 = load i128, ptr getelementptr inbounds (i8, ptr @_ZZNK8mod_q1209to_int128EvE2qm, i64 32), align 16, !tbaa !9
  %28 = mul nsw i128 %27, %26
  %29 = add nsw i128 %28, %19
  %30 = getelementptr inbounds i8, ptr %0, i64 24
  %31 = load i64, ptr %30, align 8, !tbaa !5
  %32 = load i64, ptr getelementptr inbounds (i8, ptr @_ZZNK8mod_q1209to_int128EvE4CRTi, i64 24), align 8, !tbaa !5
  %33 = mul nsw i64 %32, %31
  %34 = load i64, ptr getelementptr inbounds (i8, ptr @_ZN8mod_q1202QiE, i64 24), align 8, !tbaa !5
  %35 = srem i64 %33, %34
  %36 = sext i64 %35 to i128
  %37 = load i128, ptr getelementptr inbounds (i8, ptr @_ZZNK8mod_q1209to_int128EvE2qm, i64 48), align 16, !tbaa !9
  %38 = mul nsw i128 %37, %36
  %39 = add nsw i128 %38, %29
  %40 = srem i128 %39, 1315642440469820935610546842527858689
  %41 = add nsw i128 %40, 1973463660704731403415820263791788033
  %42 = urem i128 %41, 1315642440469820935610546842527858689
  %43 = add nsw i128 %42, -657821220234910467805273421263929344
  %.sroa.0.0.extract.trunc = trunc i128 %43 to i64
  %.sroa.2.0.extract.shift = lshr i128 %43, 64
  %.sroa.2.0.extract.trunc = trunc nuw i128 %.sroa.2.0.extract.shift to i64
  %.fca.0.insert = insertvalue { i64, i64 } poison, i64 %.sroa.0.0.extract.trunc, 0
  %.fca.1.insert = insertvalue { i64, i64 } %.fca.0.insert, i64 %.sroa.2.0.extract.trunc, 1
  ret { i64, i64 } %.fca.1.insert
}

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass