; LLVM IR from AlgoGenesis/C :: Mathematical Algorithms/Chinese Remainder Theorem (program)

; 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: 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: nounwind uwtable
declare dso_local i32 @modInverse(i32 noundef, i32 noundef) #1

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

14:                                               ; preds = %28, %4
  %15 = load i32, ptr %9, align 4, !tbaa !9
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %31

19:                                               ; preds = %14
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = load i32, ptr %9, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i32, ptr %20, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = mul nsw i32 %26, %24
  store i32 %27, ptr %25, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %19
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %9, align 4, !tbaa !9
  br label %14, !llvm.loop !11

31:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %63, %31
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %66

37:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load i32, ptr %11, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i32, ptr %40, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = sdiv i32 %39, %44
  store i32 %45, ptr %12, align 4, !tbaa !9
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i32, ptr %11, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i32, ptr %46, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !9
  %51 = load i32, ptr %12, align 4, !tbaa !9
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = load i32, ptr %11, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds i32, ptr %52, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = call i32 @modInverse(i32 noundef %51, i32 noundef %56)
  %58 = mul nsw i32 %50, %57
  %59 = load i32, ptr %12, align 4, !tbaa !9
  %60 = mul nsw i32 %58, %59
  %61 = load i32, ptr %10, align 4, !tbaa !9
  %62 = add nsw i32 %61, %60
  store i32 %62, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %63

63:                                               ; preds = %37
  %64 = load i32, ptr %11, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %11, align 4, !tbaa !9
  br label %32, !llvm.loop !13

66:                                               ; preds = %36
  %67 = load i32, ptr %10, align 4, !tbaa !9
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  %69 = load i32, ptr %68, align 4, !tbaa !9
  %70 = srem i32 %67, %69
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  ret i32 %70
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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: nounwind uwtable
declare dso_local i32 @modInverse(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @chineseRemainder(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i32 noundef %2, ptr nocapture noundef %3) local_unnamed_addr #0 {
  store i32 1, ptr %3, align 4, !tbaa !5
  %5 = icmp sgt i32 %2, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4
  %wide.trip.count = zext nneg i32 %2 to i64
  %min.iters.check = icmp ult i32 %2, 12
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.memcheck

scalar.ph.preheader:                              ; preds = %middle.block, %vector.memcheck, %.lr.ph
  %indvars.iv.ph = phi i64 [ 0, %vector.memcheck ], [ 0, %.lr.ph ], [ %n.vec, %middle.block ]
  %.ph = phi i32 [ 1, %vector.memcheck ], [ 1, %.lr.ph ], [ %18, %middle.block ]
  %xtraiter = and i64 %wide.trip.count, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %scalar.ph.prol ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %6 = phi i32 [ %9, %scalar.ph.prol ], [ %.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %7 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.prol
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = mul nsw i32 %6, %8
  store i32 %9, ptr %3, align 4, !tbaa !5
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !9

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %.lcssa39.unr = phi i32 [ poison, %scalar.ph.preheader ], [ %9, %scalar.ph.prol ]
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %.unr = phi i32 [ %.ph, %scalar.ph.preheader ], [ %9, %scalar.ph.prol ]
  %10 = sub nsw i64 %indvars.iv.ph, %wide.trip.count
  %11 = icmp ugt i64 %10, -4
  br i1 %11, label %.preheader, label %scalar.ph.preheader.new

scalar.ph.preheader.new:                          ; preds = %scalar.ph.prol.loopexit
  %invariant.gep = getelementptr i8, ptr %0, i64 4
  %invariant.gep46 = getelementptr i8, ptr %0, i64 8
  %invariant.gep48 = getelementptr i8, ptr %0, i64 12
  br label %scalar.ph

vector.memcheck:                                  ; preds = %.lr.ph
  %scevgep = getelementptr i8, ptr %3, i64 4
  %12 = shl nuw nsw i64 %wide.trip.count, 2
  %scevgep36 = getelementptr i8, ptr %0, i64 %12
  %bound0 = icmp ugt ptr %scevgep36, %3
  %bound1 = icmp ugt ptr %scevgep, %0
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %wide.trip.count, 2147483640
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ <i32 1, i32 1, i32 1, i32 1>, %vector.ph ], [ %15, %vector.body ]
  %vec.phi37 = phi <4 x i32> [ <i32 1, i32 1, i32 1, i32 1>, %vector.ph ], [ %16, %vector.body ]
  %13 = getelementptr inbounds i32, ptr %0, i64 %index
  %14 = getelementptr inbounds i8, ptr %13, i64 16
  %wide.load = load <4 x i32>, ptr %13, align 4, !tbaa !5, !alias.scope !11
  %wide.load38 = load <4 x i32>, ptr %14, align 4, !tbaa !5, !alias.scope !11
  %15 = mul <4 x i32> %vec.phi, %wide.load
  %16 = mul <4 x i32> %vec.phi37, %wide.load38
  %index.next = add nuw i64 %index, 8
  %17 = icmp eq i64 %index.next, %n.vec
  br i1 %17, label %middle.block, label %vector.body, !llvm.loop !14

middle.block:                                     ; preds = %vector.body
  %bin.rdx = mul <4 x i32> %16, %15
  %18 = tail call i32 @llvm.vector.reduce.mul.v4i32(<4 x i32> %bin.rdx)
  store i32 %18, ptr %3, align 4, !tbaa !5
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %.preheader, label %scalar.ph.preheader

.preheader:                                       ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %.lcssa35 = phi i32 [ %18, %middle.block ], [ %.lcssa39.unr, %scalar.ph.prol.loopexit ], [ %28, %scalar.ph ]
  br i1 %5, label %.lr.ph27.preheader, label %._crit_edge

.lr.ph27.preheader:                               ; preds = %.preheader
  %wide.trip.count32 = zext nneg i32 %2 to i64
  br label %.lr.ph27

scalar.ph:                                        ; preds = %scalar.ph, %scalar.ph.preheader.new
  %indvars.iv = phi i64 [ %indvars.iv.unr, %scalar.ph.preheader.new ], [ %indvars.iv.next.3, %scalar.ph ]
  %19 = phi i32 [ %.unr, %scalar.ph.preheader.new ], [ %28, %scalar.ph ]
  %20 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = mul nsw i32 %19, %21
  store i32 %22, ptr %3, align 4, !tbaa !5
  %gep = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv
  %23 = load i32, ptr %gep, align 4, !tbaa !5
  %24 = mul nsw i32 %22, %23
  store i32 %24, ptr %3, align 4, !tbaa !5
  %gep47 = getelementptr i32, ptr %invariant.gep46, i64 %indvars.iv
  %25 = load i32, ptr %gep47, align 4, !tbaa !5
  %26 = mul nsw i32 %24, %25
  store i32 %26, ptr %3, align 4, !tbaa !5
  %gep49 = getelementptr i32, ptr %invariant.gep48, i64 %indvars.iv
  %27 = load i32, ptr %gep49, align 4, !tbaa !5
  %28 = mul nsw i32 %26, %27
  store i32 %28, ptr %3, align 4, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %.preheader, label %scalar.ph, !llvm.loop !18

._crit_edge.loopexit:                             ; preds = %.lr.ph27
  %.pre = load i32, ptr %3, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %4, %._crit_edge.loopexit, %.preheader
  %29 = phi i32 [ %.lcssa35, %.preheader ], [ %.pre, %._crit_edge.loopexit ], [ 1, %4 ]
  %.022.lcssa = phi i32 [ 0, %.preheader ], [ %40, %._crit_edge.loopexit ], [ 0, %4 ]
  %30 = srem i32 %.022.lcssa, %29
  ret i32 %30

.lr.ph27:                                         ; preds = %.lr.ph27.preheader, %.lr.ph27
  %indvars.iv29 = phi i64 [ 0, %.lr.ph27.preheader ], [ %indvars.iv.next30, %.lr.ph27 ]
  %.02225 = phi i32 [ 0, %.lr.ph27.preheader ], [ %40, %.lr.ph27 ]
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv29
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = sdiv i32 %31, %33
  %35 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv29
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = tail call i32 @modInverse(i32 noundef %34, i32 noundef %33)
  %38 = mul i32 %36, %34
  %39 = mul i32 %38, %37
  %40 = add nsw i32 %39, %.02225
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, 1
  %exitcond33.not = icmp eq i64 %indvars.iv.next30, %wide.trip.count32
  br i1 %exitcond33.not, label %._crit_edge.loopexit, label %.lr.ph27, !llvm.loop !19
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.mul.v4i32(<4 x i32>) #1

attributes #0 = { 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 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = !{!12}
!12 = distinct !{!12, !13}
!13 = distinct !{!13, !"LVerDomain"}
!14 = distinct !{!14, !15, !16, !17}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.isvectorized", i32 1}
!17 = !{!"llvm.loop.unroll.runtime.disable"}
!18 = distinct !{!18, !15, !16}
!19 = distinct !{!19, !15}


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