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

@zeta = external dso_local global [256 x i16], align 16

; 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 hidden zeroext i8 @br7(i8 noundef zeroext) #1

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

8:                                                ; preds = %47, %3
  %9 = load i32, ptr %7, align 4, !tbaa !9
  %10 = icmp slt i32 %9, 64
  br i1 %10, label %11, label %50

11:                                               ; preds = %8
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = add nsw i32 64, %15
  %17 = trunc i32 %16 to i8
  %18 = call zeroext i8 @br7(i8 noundef zeroext %17)
  %19 = zext i8 %18 to i64
  %20 = getelementptr inbounds [256 x i16], ptr @zeta, i64 0, i64 %19
  %21 = load i16, ptr %20, align 2, !tbaa !11
  %22 = sext i16 %21 to i32
  call void @kyber_linear_poly_mul(ptr noundef %12, ptr noundef %13, ptr noundef %14, i32 noundef %22)
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds i16, ptr %23, i64 2
  store ptr %24, ptr %4, align 8, !tbaa !5
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds i16, ptr %25, i64 2
  store ptr %26, ptr %5, align 8, !tbaa !5
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds i16, ptr %27, i64 2
  store ptr %28, ptr %6, align 8, !tbaa !5
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = add nsw i32 64, %32
  %34 = trunc i32 %33 to i8
  %35 = call zeroext i8 @br7(i8 noundef zeroext %34)
  %36 = zext i8 %35 to i64
  %37 = getelementptr inbounds [256 x i16], ptr @zeta, i64 0, i64 %36
  %38 = load i16, ptr %37, align 2, !tbaa !11
  %39 = sext i16 %38 to i32
  %40 = sub nsw i32 3329, %39
  call void @kyber_linear_poly_mul(ptr noundef %29, ptr noundef %30, ptr noundef %31, i32 noundef %40)
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds i16, ptr %41, i64 2
  store ptr %42, ptr %4, align 8, !tbaa !5
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds i16, ptr %43, i64 2
  store ptr %44, ptr %5, align 8, !tbaa !5
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds i16, ptr %45, i64 2
  store ptr %46, ptr %6, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %11
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %7, align 4, !tbaa !9
  br label %8, !llvm.loop !13

50:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 1
}

; Function Attrs: nounwind uwtable
declare hidden void @kyber_linear_poly_mul(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #1

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

@zeta = external dso_local local_unnamed_addr global [256 x i16], align 16

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @br7(i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @kyber_poly_ntt_mul(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  br label %4

4:                                                ; preds = %3, %4
  %.021 = phi i32 [ 0, %3 ], [ %24, %4 ]
  %.01520 = phi ptr [ %2, %3 ], [ %23, %4 ]
  %.01619 = phi ptr [ %1, %3 ], [ %22, %4 ]
  %.01718 = phi ptr [ %0, %3 ], [ %21, %4 ]
  %5 = trunc nuw i32 %.021 to i8
  %6 = or disjoint i8 %5, 64
  %7 = tail call zeroext i8 @br7(i8 noundef zeroext %6)
  %8 = zext i8 %7 to i64
  %9 = getelementptr inbounds [256 x i16], ptr @zeta, i64 0, i64 %8
  %10 = load i16, ptr %9, align 2, !tbaa !5
  %11 = sext i16 %10 to i32
  tail call void @kyber_linear_poly_mul(ptr noundef %.01718, ptr noundef %.01619, ptr noundef %.01520, i32 noundef %11)
  %12 = getelementptr inbounds i8, ptr %.01718, i64 4
  %13 = getelementptr inbounds i8, ptr %.01619, i64 4
  %14 = getelementptr inbounds i8, ptr %.01520, i64 4
  %15 = tail call zeroext i8 @br7(i8 noundef zeroext %6)
  %16 = zext i8 %15 to i64
  %17 = getelementptr inbounds [256 x i16], ptr @zeta, i64 0, i64 %16
  %18 = load i16, ptr %17, align 2, !tbaa !5
  %19 = sext i16 %18 to i32
  %20 = sub nsw i32 3329, %19
  tail call void @kyber_linear_poly_mul(ptr noundef nonnull %12, ptr noundef nonnull %13, ptr noundef nonnull %14, i32 noundef %20)
  %21 = getelementptr inbounds i8, ptr %.01718, i64 8
  %22 = getelementptr inbounds i8, ptr %.01619, i64 8
  %23 = getelementptr inbounds i8, ptr %.01520, i64 8
  %24 = add nuw nsw i32 %.021, 1
  %exitcond.not = icmp eq i32 %24, 64
  br i1 %exitcond.not, label %25, label %4, !llvm.loop !9

25:                                               ; preds = %4
  ret i32 1
}

; Function Attrs: nounwind uwtable
declare hidden void @kyber_linear_poly_mul(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

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

!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"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass