; LLVM IR from sp-nitech/SPTK :: third_party/SWIPE/vector.ll (ZN4sptk5swipe6primesEi)

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

%"struct.sptk::swipe::intvector" = type { i32, ptr }

; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local { i32, ptr } @_ZN4sptk5swipe6makeivEi(i32 noundef) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local { i32, ptr } @_ZN4sptk5swipe6onesivEi(i32 noundef) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN4sptk5swipe6freeivENS0_9intvectorE(i32, ptr) #2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN4sptk5swipe5sieveENS0_9intvectorE(i32, ptr) #3

; Function Attrs: mustprogress uwtable
define dso_local { i32, ptr } @_ZN4sptk5swipe6primesEi(i32 noundef %0) #3 {
  %2 = alloca %"struct.sptk::swipe::intvector", align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %"struct.sptk::swipe::intvector", align 8
  %7 = alloca %"struct.sptk::swipe::intvector", align 8
  %8 = alloca %"struct.sptk::swipe::intvector", align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #4
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = call { i32, ptr } @_ZN4sptk5swipe6onesivEi(i32 noundef %9)
  %11 = getelementptr inbounds { i32, ptr }, ptr %6, i32 0, i32 0
  %12 = extractvalue { i32, ptr } %10, 0
  store i32 %12, ptr %11, align 8
  %13 = getelementptr inbounds { i32, ptr }, ptr %6, i32 0, i32 1
  %14 = extractvalue { i32, ptr } %10, 1
  store ptr %14, ptr %13, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %7, ptr align 8 %6, i64 16, i1 false), !tbaa.struct !9
  %15 = getelementptr inbounds { i32, ptr }, ptr %7, i32 0, i32 0
  %16 = load i32, ptr %15, align 8
  %17 = getelementptr inbounds { i32, ptr }, ptr %7, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8
  %19 = call noundef i32 @_ZN4sptk5swipe5sieveENS0_9intvectorE(i32 %16, ptr %18)
  %20 = call { i32, ptr } @_ZN4sptk5swipe6makeivEi(i32 noundef %19)
  %21 = getelementptr inbounds { i32, ptr }, ptr %2, i32 0, i32 0
  %22 = extractvalue { i32, ptr } %20, 0
  store i32 %22, ptr %21, align 8
  %23 = getelementptr inbounds { i32, ptr }, ptr %2, i32 0, i32 1
  %24 = extractvalue { i32, ptr } %20, 1
  store ptr %24, ptr %23, align 8
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %48, %1
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = getelementptr inbounds %"struct.sptk::swipe::intvector", ptr %6, i32 0, i32 0
  %28 = load i32, ptr %27, align 8, !tbaa !12
  %29 = icmp slt i32 %26, %28
  br i1 %29, label %30, label %51

30:                                               ; preds = %25
  %31 = getelementptr inbounds %"struct.sptk::swipe::intvector", ptr %6, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !14
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = icmp eq i32 %36, 1
  br i1 %37, label %38, label %47

38:                                               ; preds = %30
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  %41 = getelementptr inbounds %"struct.sptk::swipe::intvector", ptr %2, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !14
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %5, align 4, !tbaa !5
  %45 = sext i32 %43 to i64
  %46 = getelementptr inbounds i32, ptr %42, i64 %45
  store i32 %40, ptr %46, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %38, %30
  br label %48

48:                                               ; preds = %47
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %4, align 4, !tbaa !5
  br label %25, !llvm.loop !15

51:                                               ; preds = %25
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %8, ptr align 8 %6, i64 16, i1 false), !tbaa.struct !9
  %52 = getelementptr inbounds { i32, ptr }, ptr %8, i32 0, i32 0
  %53 = load i32, ptr %52, align 8
  %54 = getelementptr inbounds { i32, ptr }, ptr %8, i32 0, i32 1
  %55 = load ptr, ptr %54, align 8
  call void @_ZN4sptk5swipe6freeivENS0_9intvectorE(i32 %53, ptr %55)
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %56 = load { i32, ptr }, ptr %2, align 8
  ret { i32, ptr } %56
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 #4 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i64 0, i64 4, !5, i64 8, i64 8, !10}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!13, !6, i64 0}
!13 = !{!"_ZTSN4sptk5swipe9intvectorE", !6, i64 0, !11, i64 8}
!14 = !{!13, !11, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"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 nounwind uwtable
declare dso_local { i32, ptr } @_ZN4sptk5swipe6makeivEi(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local { i32, ptr } @_ZN4sptk5swipe6onesivEi(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN4sptk5swipe6freeivENS0_9intvectorE(i32, ptr) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN4sptk5swipe5sieveENS0_9intvectorE(i32, ptr) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local { i32, ptr } @_ZN4sptk5swipe6primesEi(i32 noundef %0) local_unnamed_addr #1 {
  %2 = tail call { i32, ptr } @_ZN4sptk5swipe6onesivEi(i32 noundef %0)
  %3 = extractvalue { i32, ptr } %2, 0
  %4 = extractvalue { i32, ptr } %2, 1
  %5 = tail call noundef i32 @_ZN4sptk5swipe5sieveENS0_9intvectorE(i32 %3, ptr %4)
  %6 = tail call { i32, ptr } @_ZN4sptk5swipe6makeivEi(i32 noundef %5)
  %7 = extractvalue { i32, ptr } %6, 1
  %8 = icmp sgt i32 %3, 0
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %wide.trip.count = zext nneg i32 %3 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %9 = icmp eq i32 %3, 1
  br i1 %9, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph._crit_edge.1, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %22, %.lr.ph._crit_edge.1 ]
  %.01618 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.1.1, %.lr.ph._crit_edge.1 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph._crit_edge.1 ]
  %10 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp eq i32 %11, 1
  %13 = or disjoint i64 %indvars.iv, 1
  br i1 %12, label %14, label %.lr.ph._crit_edge

14:                                               ; preds = %.lr.ph
  %15 = add nsw i32 %.01618, 1
  %16 = sext i32 %.01618 to i64
  %17 = getelementptr inbounds i32, ptr %7, i64 %16
  %18 = trunc nuw nsw i64 %13 to i32
  store i32 %18, ptr %17, align 4, !tbaa !5
  br label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph, %14
  %.1 = phi i32 [ %15, %14 ], [ %.01618, %.lr.ph ]
  %19 = getelementptr inbounds i32, ptr %4, i64 %13
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = icmp eq i32 %20, 1
  %22 = add nuw nsw i64 %indvars.iv, 2
  br i1 %21, label %23, label %.lr.ph._crit_edge.1

23:                                               ; preds = %.lr.ph._crit_edge
  %24 = add nsw i32 %.1, 1
  %25 = sext i32 %.1 to i64
  %26 = getelementptr inbounds i32, ptr %7, i64 %25
  %27 = trunc nuw nsw i64 %22 to i32
  store i32 %27, ptr %26, align 4, !tbaa !5
  br label %.lr.ph._crit_edge.1

.lr.ph._crit_edge.1:                              ; preds = %23, %.lr.ph._crit_edge
  %.1.1 = phi i32 [ %24, %23 ], [ %.1, %.lr.ph._crit_edge ]
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa.loopexit, label %.lr.ph, !llvm.loop !9

._crit_edge.loopexit.unr-lcssa.loopexit:          ; preds = %.lr.ph._crit_edge.1
  %28 = sext i32 %.1.1 to i64
  br label %._crit_edge.loopexit.unr-lcssa

._crit_edge.loopexit.unr-lcssa:                   ; preds = %._crit_edge.loopexit.unr-lcssa.loopexit, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %22, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.01618.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %28, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %29 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv.unr
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = icmp eq i32 %30, 1
  br i1 %31, label %32, label %._crit_edge

32:                                               ; preds = %.lr.ph.epil
  %33 = getelementptr inbounds i32, ptr %7, i64 %.01618.unr
  %34 = trunc i64 %indvars.iv.unr to i32
  %35 = add i32 %34, 1
  store i32 %35, ptr %33, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %32, %.lr.ph.epil, %1
  tail call void @_ZN4sptk5swipe6freeivENS0_9intvectorE(i32 %3, ptr %4)
  ret { i32, ptr } %6
}

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 = { 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" }

!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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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