; LLVM IR from guanzhi/GmSSL :: src (kyber)

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

7:                                                ; preds = %36, %2
  %8 = load i32, ptr %5, align 4, !tbaa !9
  %9 = icmp slt i32 %8, 32
  br i1 %9, label %10, label %39

10:                                               ; preds = %7
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %32, %10
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = icmp slt i32 %12, 8
  br i1 %13, label %14, label %35

14:                                               ; preds = %11
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i8, ptr %15, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !11
  %20 = zext i8 %19 to i32
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = ashr i32 %20, %21
  %23 = and i32 %22, 1
  %24 = trunc i32 %23 to i16
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = mul nsw i32 8, %26
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = add nsw i32 %27, %28
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i16, ptr %25, i64 %30
  store i16 %24, ptr %31, align 2, !tbaa !12
  br label %32

32:                                               ; preds = %14
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %6, align 4, !tbaa !9
  br label %11, !llvm.loop !14

35:                                               ; preds = %11
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %5, align 4, !tbaa !9
  br label %7, !llvm.loop !16

39:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret void
}

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 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @kyber_poly_decode1(ptr nocapture noundef writeonly %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
vector.memcheck:
  %scevgep = getelementptr i8, ptr %0, i64 512
  %scevgep14 = getelementptr i8, ptr %1, i64 32
  %bound0 = icmp ugt ptr %scevgep14, %0
  %bound1 = icmp ugt ptr %scevgep, %1
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.preheader, label %vector.body.preheader

vector.body.preheader:                            ; preds = %vector.memcheck
  %invariant.gep = getelementptr i8, ptr %0, i64 -14
  br label %vector.body

vector.body:                                      ; preds = %vector.body.preheader, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %vector.body.preheader ]
  %2 = getelementptr inbounds i8, ptr %1, i64 %index
  %3 = shl nuw nsw i64 %index, 3
  %wide.load = load <8 x i8>, ptr %2, align 1, !tbaa !5, !alias.scope !8
  %4 = and <8 x i8> %wide.load, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
  %5 = zext nneg <8 x i8> %4 to <8 x i16>
  %6 = lshr <8 x i8> %wide.load, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
  %7 = and <8 x i8> %6, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
  %8 = zext nneg <8 x i8> %7 to <8 x i16>
  %9 = lshr <8 x i8> %wide.load, <i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2, i8 2>
  %10 = and <8 x i8> %9, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
  %11 = zext nneg <8 x i8> %10 to <8 x i16>
  %wide.load17 = load <8 x i8>, ptr %2, align 1, !tbaa !5, !alias.scope !8
  %12 = lshr <8 x i8> %wide.load17, <i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3>
  %13 = and <8 x i8> %12, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
  %14 = zext nneg <8 x i8> %13 to <8 x i16>
  %15 = lshr <8 x i8> %wide.load17, <i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4>
  %16 = and <8 x i8> %15, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
  %17 = zext nneg <8 x i8> %16 to <8 x i16>
  %wide.load19 = load <8 x i8>, ptr %2, align 1, !tbaa !5, !alias.scope !8
  %18 = lshr <8 x i8> %wide.load19, <i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5, i8 5>
  %19 = and <8 x i8> %18, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
  %20 = zext nneg <8 x i8> %19 to <8 x i16>
  %21 = lshr <8 x i8> %wide.load19, <i8 6, i8 6, i8 6, i8 6, i8 6, i8 6, i8 6, i8 6>
  %22 = and <8 x i8> %21, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
  %23 = zext nneg <8 x i8> %22 to <8 x i16>
  %wide.load21 = load <8 x i8>, ptr %2, align 1, !tbaa !5, !alias.scope !8
  %24 = lshr <8 x i8> %wide.load21, <i8 7, i8 7, i8 7, i8 7, i8 7, i8 7, i8 7, i8 7>
  %25 = zext nneg <8 x i8> %24 to <8 x i16>
  %26 = or disjoint i64 %3, 7
  %gep = getelementptr i16, ptr %invariant.gep, i64 %26
  %27 = shufflevector <8 x i16> %5, <8 x i16> %8, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
  %28 = shufflevector <8 x i16> %11, <8 x i16> %14, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
  %29 = shufflevector <8 x i16> %17, <8 x i16> %20, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
  %30 = shufflevector <8 x i16> %23, <8 x i16> %25, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
  %31 = shufflevector <16 x i16> %27, <16 x i16> %28, <32 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31>
  %32 = shufflevector <16 x i16> %29, <16 x i16> %30, <32 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31>
  %interleaved.vec = shufflevector <32 x i16> %31, <32 x i16> %32, <64 x i32> <i32 0, i32 8, i32 16, i32 24, i32 32, i32 40, i32 48, i32 56, i32 1, i32 9, i32 17, i32 25, i32 33, i32 41, i32 49, i32 57, i32 2, i32 10, i32 18, i32 26, i32 34, i32 42, i32 50, i32 58, i32 3, i32 11, i32 19, i32 27, i32 35, i32 43, i32 51, i32 59, i32 4, i32 12, i32 20, i32 28, i32 36, i32 44, i32 52, i32 60, i32 5, i32 13, i32 21, i32 29, i32 37, i32 45, i32 53, i32 61, i32 6, i32 14, i32 22, i32 30, i32 38, i32 46, i32 54, i32 62, i32 7, i32 15, i32 23, i32 31, i32 39, i32 47, i32 55, i32 63>
  store <64 x i16> %interleaved.vec, ptr %gep, align 2, !tbaa !11
  %index.next = add nuw i64 %index, 8
  %33 = icmp eq i64 %index.next, 32
  br i1 %33, label %middle.block, label %vector.body, !llvm.loop !13

.preheader:                                       ; preds = %vector.memcheck, %.preheader
  %indvars.iv = phi i64 [ %indvars.iv.next, %.preheader ], [ 0, %vector.memcheck ]
  %34 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv
  %35 = shl nuw nsw i64 %indvars.iv, 3
  %36 = load i8, ptr %34, align 1, !tbaa !5
  %37 = and i8 %36, 1
  %38 = zext nneg i8 %37 to i16
  %39 = getelementptr inbounds i16, ptr %0, i64 %35
  store i16 %38, ptr %39, align 2, !tbaa !11
  %40 = load i8, ptr %34, align 1, !tbaa !5
  %41 = lshr i8 %40, 1
  %42 = and i8 %41, 1
  %43 = zext nneg i8 %42 to i16
  %44 = or disjoint i64 %35, 1
  %45 = getelementptr inbounds i16, ptr %0, i64 %44
  store i16 %43, ptr %45, align 2, !tbaa !11
  %46 = load i8, ptr %34, align 1, !tbaa !5
  %47 = lshr i8 %46, 2
  %48 = and i8 %47, 1
  %49 = zext nneg i8 %48 to i16
  %50 = or disjoint i64 %35, 2
  %51 = getelementptr inbounds i16, ptr %0, i64 %50
  store i16 %49, ptr %51, align 2, !tbaa !11
  %52 = load i8, ptr %34, align 1, !tbaa !5
  %53 = lshr i8 %52, 3
  %54 = and i8 %53, 1
  %55 = zext nneg i8 %54 to i16
  %56 = or disjoint i64 %35, 3
  %57 = getelementptr inbounds i16, ptr %0, i64 %56
  store i16 %55, ptr %57, align 2, !tbaa !11
  %58 = load i8, ptr %34, align 1, !tbaa !5
  %59 = lshr i8 %58, 4
  %60 = and i8 %59, 1
  %61 = zext nneg i8 %60 to i16
  %62 = or disjoint i64 %35, 4
  %63 = getelementptr inbounds i16, ptr %0, i64 %62
  store i16 %61, ptr %63, align 2, !tbaa !11
  %64 = load i8, ptr %34, align 1, !tbaa !5
  %65 = lshr i8 %64, 5
  %66 = and i8 %65, 1
  %67 = zext nneg i8 %66 to i16
  %68 = or disjoint i64 %35, 5
  %69 = getelementptr inbounds i16, ptr %0, i64 %68
  store i16 %67, ptr %69, align 2, !tbaa !11
  %70 = load i8, ptr %34, align 1, !tbaa !5
  %71 = lshr i8 %70, 6
  %72 = and i8 %71, 1
  %73 = zext nneg i8 %72 to i16
  %74 = or disjoint i64 %35, 6
  %75 = getelementptr inbounds i16, ptr %0, i64 %74
  store i16 %73, ptr %75, align 2, !tbaa !11
  %76 = load i8, ptr %34, align 1, !tbaa !5
  %77 = lshr i8 %76, 7
  %78 = zext nneg i8 %77 to i16
  %79 = or disjoint i64 %35, 7
  %80 = getelementptr inbounds i16, ptr %0, i64 %79
  store i16 %78, ptr %80, align 2, !tbaa !11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 32
  br i1 %exitcond.not, label %middle.block, label %.preheader, !llvm.loop !17

middle.block:                                     ; preds = %vector.body, %.preheader
  ret void
}

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


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass