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

@stderr = external global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [59 x i8], align 1
@__FUNCTION__.kyber_poly_encode4 = external hidden unnamed_addr constant [19 x i8], align 1

; 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
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #1

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

8:                                                ; preds = %34, %2
  %9 = load i32, ptr %6, align 4, !tbaa !9
  %10 = icmp slt i32 %9, 256
  br i1 %10, label %11, label %37

11:                                               ; preds = %8
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i16, ptr %12, i64 %14
  %16 = load i16, ptr %15, align 2, !tbaa !11
  %17 = sext i16 %16 to i32
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %27, label %19

19:                                               ; preds = %11
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i16, ptr %20, i64 %22
  %24 = load i16, ptr %23, align 2, !tbaa !11
  %25 = sext i16 %24 to i32
  %26 = icmp sge i32 %25, 16
  br i1 %26, label %27, label %33

27:                                               ; preds = %19, %11
  br label %28

28:                                               ; preds = %27
  %29 = load ptr, ptr @stderr, align 8, !tbaa !5
  %30 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %29, ptr noundef @.str.4, ptr noundef @.str.5, i32 noundef 620, ptr noundef @__FUNCTION__.kyber_poly_encode4) #3
  br label %31

31:                                               ; preds = %28
  br label %32

32:                                               ; preds = %31
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %64

33:                                               ; preds = %19
  br label %34

34:                                               ; preds = %33
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %6, align 4, !tbaa !9
  br label %8, !llvm.loop !13

37:                                               ; preds = %8
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %60, %37
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = icmp slt i32 %39, 256
  br i1 %40, label %41, label %63

41:                                               ; preds = %38
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i16, ptr %42, i64 %44
  %46 = load i16, ptr %45, align 2, !tbaa !11
  %47 = sext i16 %46 to i32
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i16, ptr %48, i64 %51
  %53 = load i16, ptr %52, align 2, !tbaa !11
  %54 = sext i16 %53 to i32
  %55 = shl i32 %54, 4
  %56 = or i32 %47, %55
  %57 = trunc i32 %56 to i8
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr %5, align 8, !tbaa !5
  store i8 %57, ptr %58, align 1, !tbaa !15
  br label %60

60:                                               ; preds = %41
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = add nsw i32 %61, 2
  store i32 %62, ptr %6, align 4, !tbaa !9
  br label %38, !llvm.loop !16

63:                                               ; preds = %38
  store i32 1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %64

64:                                               ; preds = %63, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %65 = load i32, ptr %3, align 4
  ret i32 %65
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 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 = { 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 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !14}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [59 x i8], align 1
@__FUNCTION__.kyber_poly_encode4 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nofree nounwind uwtable
define dso_local range(i32 -1, 2) i32 @kyber_poly_encode4(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  br label %29

3:                                                ; preds = %29
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %4 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv.next
  %5 = load i16, ptr %4, align 2, !tbaa !5
  %6 = icmp ugt i16 %5, 15
  br i1 %6, label %33, label %7

7:                                                ; preds = %3
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %8 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv.next.1
  %9 = load i16, ptr %8, align 2, !tbaa !5
  %10 = icmp ugt i16 %9, 15
  br i1 %10, label %33, label %11

11:                                               ; preds = %7
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %12 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv.next.2
  %13 = load i16, ptr %12, align 2, !tbaa !5
  %14 = icmp ugt i16 %13, 15
  br i1 %14, label %33, label %15

15:                                               ; preds = %11
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, 256
  br i1 %exitcond.not.3, label %vector.memcheck, label %29, !llvm.loop !9

vector.memcheck:                                  ; preds = %15
  %scevgep = getelementptr i8, ptr %1, i64 128
  %scevgep23 = getelementptr i8, ptr %0, i64 512
  %bound0 = icmp ugt ptr %scevgep23, %1
  %bound1 = icmp ugt ptr %scevgep, %0
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.preheader, label %vector.body

vector.body:                                      ; preds = %vector.memcheck, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %vector.memcheck ]
  %offset.idx = shl i64 %index, 1
  %16 = or disjoint i64 %offset.idx, 8
  %next.gep = getelementptr i8, ptr %1, i64 %index
  %17 = getelementptr inbounds i16, ptr %0, i64 %offset.idx
  %18 = getelementptr inbounds i16, ptr %0, i64 %16
  %wide.vec = load <8 x i16>, ptr %17, align 2, !tbaa !5
  %wide.vec26 = load <8 x i16>, ptr %18, align 2, !tbaa !5
  %strided.vec = shufflevector <8 x i16> %wide.vec, <8 x i16> poison, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
  %strided.vec27 = shufflevector <8 x i16> %wide.vec26, <8 x i16> poison, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
  %strided.vec28 = shufflevector <8 x i16> %wide.vec, <8 x i16> poison, <4 x i32> <i32 1, i32 3, i32 5, i32 7>
  %strided.vec29 = shufflevector <8 x i16> %wide.vec26, <8 x i16> poison, <4 x i32> <i32 1, i32 3, i32 5, i32 7>
  %19 = trunc <4 x i16> %strided.vec to <4 x i8>
  %20 = trunc <4 x i16> %strided.vec27 to <4 x i8>
  %21 = trunc <4 x i16> %strided.vec28 to <4 x i8>
  %22 = trunc <4 x i16> %strided.vec29 to <4 x i8>
  %23 = shl <4 x i8> %21, <i8 4, i8 4, i8 4, i8 4>
  %24 = shl <4 x i8> %22, <i8 4, i8 4, i8 4, i8 4>
  %25 = or <4 x i8> %23, %19
  %26 = or <4 x i8> %24, %20
  %27 = getelementptr i8, ptr %next.gep, i64 4
  store <4 x i8> %25, ptr %next.gep, align 1, !tbaa !11, !alias.scope !12, !noalias !15
  store <4 x i8> %26, ptr %27, align 1, !tbaa !11, !alias.scope !12, !noalias !15
  %index.next = add nuw i64 %index, 8
  %28 = icmp eq i64 %index.next, 128
  br i1 %28, label %.loopexit, label %vector.body, !llvm.loop !17

29:                                               ; preds = %15, %2
  %indvars.iv = phi i64 [ 0, %2 ], [ %indvars.iv.next.3, %15 ]
  %30 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv
  %31 = load i16, ptr %30, align 2, !tbaa !5
  %32 = icmp ugt i16 %31, 15
  br i1 %32, label %33, label %3

33:                                               ; preds = %11, %7, %3, %29
  %34 = load ptr, ptr @stderr, align 8, !tbaa !20
  %35 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %34, ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.5, i32 noundef 620, ptr noundef nonnull @__FUNCTION__.kyber_poly_encode4) #2
  br label %.loopexit

.preheader:                                       ; preds = %vector.memcheck, %.preheader
  %indvars.iv20 = phi i64 [ %indvars.iv.next21.1, %.preheader ], [ 0, %vector.memcheck ]
  %.01317 = phi ptr [ %55, %.preheader ], [ %1, %vector.memcheck ]
  %36 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv20
  %37 = load i16, ptr %36, align 2, !tbaa !5
  %38 = trunc i16 %37 to i8
  %39 = or disjoint i64 %indvars.iv20, 1
  %40 = getelementptr inbounds i16, ptr %0, i64 %39
  %41 = load i16, ptr %40, align 2, !tbaa !5
  %42 = trunc i16 %41 to i8
  %43 = shl i8 %42, 4
  %44 = or i8 %43, %38
  %45 = getelementptr inbounds i8, ptr %.01317, i64 1
  store i8 %44, ptr %.01317, align 1, !tbaa !11
  %indvars.iv.next21 = or disjoint i64 %indvars.iv20, 2
  %46 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv.next21
  %47 = load i16, ptr %46, align 2, !tbaa !5
  %48 = trunc i16 %47 to i8
  %49 = or disjoint i64 %indvars.iv20, 3
  %50 = getelementptr inbounds i16, ptr %0, i64 %49
  %51 = load i16, ptr %50, align 2, !tbaa !5
  %52 = trunc i16 %51 to i8
  %53 = shl i8 %52, 4
  %54 = or i8 %53, %48
  %55 = getelementptr inbounds i8, ptr %.01317, i64 2
  store i8 %54, ptr %45, align 1, !tbaa !11
  %indvars.iv.next21.1 = add nuw nsw i64 %indvars.iv20, 4
  %56 = icmp ult i64 %indvars.iv.next21, 254
  br i1 %56, label %.preheader, label %.loopexit, !llvm.loop !22

.loopexit:                                        ; preds = %vector.body, %.preheader, %33
  %.014 = phi i32 [ -1, %33 ], [ 1, %.preheader ], [ 1, %vector.body ]
  ret i32 %.014
}

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass