; 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_encode12 = external hidden unnamed_addr constant [20 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_encode12(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = 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 8, ptr %6) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %9, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %36, %2
  %11 = load i32, ptr %7, align 4, !tbaa !9
  %12 = icmp slt i32 %11, 256
  br i1 %12, label %13, label %39

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

21:                                               ; preds = %13
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i16, ptr %22, i64 %24
  %26 = load i16, ptr %25, align 2, !tbaa !11
  %27 = sext i16 %26 to i32
  %28 = icmp sge i32 %27, 3329
  br i1 %28, label %29, label %35

29:                                               ; preds = %21, %13
  br label %30

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

33:                                               ; preds = %30
  br label %34

34:                                               ; preds = %33
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %80

35:                                               ; preds = %21
  br label %36

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

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

40:                                               ; preds = %76, %39
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = icmp slt i32 %41, 128
  br i1 %42, label %43, label %79

43:                                               ; preds = %40
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds i16, ptr %44, i64 0
  %46 = load i16, ptr %45, align 2, !tbaa !11
  %47 = trunc i16 %46 to i8
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %48, i64 0
  store i8 %47, ptr %49, align 1, !tbaa !15
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds i16, ptr %50, i64 1
  %52 = load i16, ptr %51, align 2, !tbaa !11
  %53 = sext i16 %52 to i32
  %54 = shl i32 %53, 4
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds i16, ptr %55, i64 0
  %57 = load i16, ptr %56, align 2, !tbaa !11
  %58 = sext i16 %57 to i32
  %59 = ashr i32 %58, 8
  %60 = or i32 %54, %59
  %61 = trunc i32 %60 to i8
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds i8, ptr %62, i64 1
  store i8 %61, ptr %63, align 1, !tbaa !15
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds i16, ptr %64, i64 1
  %66 = load i16, ptr %65, align 2, !tbaa !11
  %67 = sext i16 %66 to i32
  %68 = ashr i32 %67, 4
  %69 = trunc i32 %68 to i8
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds i8, ptr %70, i64 2
  store i8 %69, ptr %71, align 1, !tbaa !15
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds i16, ptr %72, i64 2
  store ptr %73, ptr %6, align 8, !tbaa !5
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds i8, ptr %74, i64 3
  store ptr %75, ptr %5, align 8, !tbaa !5
  br label %76

76:                                               ; preds = %43
  %77 = load i32, ptr %7, align 4, !tbaa !9
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %7, align 4, !tbaa !9
  br label %40, !llvm.loop !16

79:                                               ; preds = %40
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %80

80:                                               ; preds = %79, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %81 = load i32, ptr %3, align 4
  ret i32 %81
}

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_encode12 = external hidden unnamed_addr constant [20 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_encode12(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  br label %16

3:                                                ; preds = %16
  %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, 3328
  br i1 %6, label %20, 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, 3328
  br i1 %10, label %20, 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, 3328
  br i1 %14, label %20, 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 %.preheader, label %16, !llvm.loop !9

16:                                               ; preds = %15, %2
  %indvars.iv = phi i64 [ 0, %2 ], [ %indvars.iv.next.3, %15 ]
  %17 = getelementptr inbounds i16, ptr %0, i64 %indvars.iv
  %18 = load i16, ptr %17, align 2, !tbaa !5
  %19 = icmp ugt i16 %18, 3328
  br i1 %19, label %20, label %3

20:                                               ; preds = %11, %7, %3, %16
  %21 = load ptr, ptr @stderr, align 8, !tbaa !11
  %22 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %21, ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.5, i32 noundef 540, ptr noundef nonnull @__FUNCTION__.kyber_poly_encode12) #2
  br label %.loopexit

.preheader:                                       ; preds = %15, %.preheader
  %.125 = phi i32 [ %57, %.preheader ], [ 0, %15 ]
  %.01824 = phi ptr [ %55, %.preheader ], [ %0, %15 ]
  %.01923 = phi ptr [ %56, %.preheader ], [ %1, %15 ]
  %23 = load i16, ptr %.01824, align 2, !tbaa !5
  %24 = trunc i16 %23 to i8
  store i8 %24, ptr %.01923, align 1, !tbaa !13
  %25 = getelementptr inbounds i8, ptr %.01824, i64 2
  %26 = load i16, ptr %25, align 2, !tbaa !5
  %27 = trunc i16 %26 to i8
  %28 = shl i8 %27, 4
  %29 = load i16, ptr %.01824, align 2, !tbaa !5
  %30 = lshr i16 %29, 8
  %31 = trunc nuw i16 %30 to i8
  %32 = or i8 %28, %31
  %33 = getelementptr inbounds i8, ptr %.01923, i64 1
  store i8 %32, ptr %33, align 1, !tbaa !13
  %34 = load i16, ptr %25, align 2, !tbaa !5
  %35 = lshr i16 %34, 4
  %36 = trunc i16 %35 to i8
  %37 = getelementptr inbounds i8, ptr %.01923, i64 2
  store i8 %36, ptr %37, align 1, !tbaa !13
  %38 = getelementptr inbounds i8, ptr %.01824, i64 4
  %39 = getelementptr inbounds i8, ptr %.01923, i64 3
  %40 = load i16, ptr %38, align 2, !tbaa !5
  %41 = trunc i16 %40 to i8
  store i8 %41, ptr %39, align 1, !tbaa !13
  %42 = getelementptr inbounds i8, ptr %.01824, i64 6
  %43 = load i16, ptr %42, align 2, !tbaa !5
  %44 = trunc i16 %43 to i8
  %45 = shl i8 %44, 4
  %46 = load i16, ptr %38, align 2, !tbaa !5
  %47 = lshr i16 %46, 8
  %48 = trunc nuw i16 %47 to i8
  %49 = or i8 %45, %48
  %50 = getelementptr inbounds i8, ptr %.01923, i64 4
  store i8 %49, ptr %50, align 1, !tbaa !13
  %51 = load i16, ptr %42, align 2, !tbaa !5
  %52 = lshr i16 %51, 4
  %53 = trunc i16 %52 to i8
  %54 = getelementptr inbounds i8, ptr %.01923, i64 5
  store i8 %53, ptr %54, align 1, !tbaa !13
  %55 = getelementptr inbounds i8, ptr %.01824, i64 8
  %56 = getelementptr inbounds i8, ptr %.01923, i64 6
  %57 = add nuw nsw i32 %.125, 2
  %exitcond27.not.1 = icmp eq i32 %57, 128
  br i1 %exitcond27.not.1, label %.loopexit, label %.preheader, !llvm.loop !14

.loopexit:                                        ; preds = %.preheader, %20
  %.020 = phi i32 [ -1, %20 ], [ 1, %.preheader ]
  ret i32 %.020
}

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 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !10}


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