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

%struct.KYBER_CPA_CIPHERTEXT = type { [2 x [320 x i8]], [128 x i8] }
%struct.KYBER_CPA_PRIVATE_KEY = type { [2 x [384 x i8]] }

; 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 dso_local void @kyber_poly_set_zero(ptr noundef) #1

declare void @gmssl_secure_clear(ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_poly_add(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_poly_sub(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @kyber_poly_ntt(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @kyber_poly_inv_ntt(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @kyber_poly_ntt_mul(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @kyber_poly_compress(ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @kyber_poly_decompress(ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @kyber_poly_decode12(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @kyber_poly_decode10(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_poly_decode4(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @kyber_poly_encode1(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @kyber_cpa_decrypt(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 [2 x [256 x i16]], align 16
  %8 = alloca [2 x [256 x i16]], align 16
  %9 = alloca [256 x i16], align 16
  %10 = alloca [256 x i16], align 16
  %11 = alloca i32, align 4
  %12 = alloca [256 x i16], align 16
  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 1024, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 1024, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 512, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 512, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %37, %3
  %14 = load i32, ptr %11, align 4, !tbaa !9
  %15 = icmp slt i32 %14, 2
  br i1 %15, label %16, label %40

16:                                               ; preds = %13
  %17 = load i32, ptr %11, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [2 x [256 x i16]], ptr %7, i64 0, i64 %18
  %20 = getelementptr inbounds [256 x i16], ptr %19, i64 0, i64 0
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.KYBER_CPA_CIPHERTEXT, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %11, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [2 x [320 x i8]], ptr %22, i64 0, i64 %24
  %26 = getelementptr inbounds [320 x i8], ptr %25, i64 0, i64 0
  %27 = call i32 @kyber_poly_decode10(ptr noundef %20, ptr noundef %26)
  %28 = load i32, ptr %11, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [2 x [256 x i16]], ptr %7, i64 0, i64 %29
  %31 = getelementptr inbounds [256 x i16], ptr %30, i64 0, i64 0
  %32 = load i32, ptr %11, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [2 x [256 x i16]], ptr %7, i64 0, i64 %33
  %35 = getelementptr inbounds [256 x i16], ptr %34, i64 0, i64 0
  %36 = call i32 @kyber_poly_decompress(ptr noundef %31, i32 noundef 10, ptr noundef %35)
  br label %37

37:                                               ; preds = %16
  %38 = load i32, ptr %11, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %11, align 4, !tbaa !9
  br label %13, !llvm.loop !11

40:                                               ; preds = %13
  %41 = getelementptr inbounds [256 x i16], ptr %9, i64 0, i64 0
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.KYBER_CPA_CIPHERTEXT, ptr %42, i32 0, i32 1
  %44 = getelementptr inbounds [128 x i8], ptr %43, i64 0, i64 0
  call void @kyber_poly_decode4(ptr noundef %41, ptr noundef %44)
  %45 = getelementptr inbounds [256 x i16], ptr %9, i64 0, i64 0
  %46 = getelementptr inbounds [256 x i16], ptr %9, i64 0, i64 0
  %47 = call i32 @kyber_poly_decompress(ptr noundef %45, i32 noundef 4, ptr noundef %46)
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %63, %40
  %49 = load i32, ptr %11, align 4, !tbaa !9
  %50 = icmp slt i32 %49, 2
  br i1 %50, label %51, label %66

51:                                               ; preds = %48
  %52 = load i32, ptr %11, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [2 x [256 x i16]], ptr %8, i64 0, i64 %53
  %55 = getelementptr inbounds [256 x i16], ptr %54, i64 0, i64 0
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.KYBER_CPA_PRIVATE_KEY, ptr %56, i32 0, i32 0
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [2 x [384 x i8]], ptr %57, i64 0, i64 %59
  %61 = getelementptr inbounds [384 x i8], ptr %60, i64 0, i64 0
  %62 = call i32 @kyber_poly_decode12(ptr noundef %55, ptr noundef %61)
  br label %63

63:                                               ; preds = %51
  %64 = load i32, ptr %11, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %11, align 4, !tbaa !9
  br label %48, !llvm.loop !13

66:                                               ; preds = %48
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %67

67:                                               ; preds = %76, %66
  %68 = load i32, ptr %11, align 4, !tbaa !9
  %69 = icmp slt i32 %68, 2
  br i1 %69, label %70, label %79

70:                                               ; preds = %67
  %71 = load i32, ptr %11, align 4, !tbaa !9
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [2 x [256 x i16]], ptr %7, i64 0, i64 %72
  %74 = getelementptr inbounds [256 x i16], ptr %73, i64 0, i64 0
  %75 = call i32 @kyber_poly_ntt(ptr noundef %74)
  br label %76

76:                                               ; preds = %70
  %77 = load i32, ptr %11, align 4, !tbaa !9
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %11, align 4, !tbaa !9
  br label %67, !llvm.loop !14

79:                                               ; preds = %67
  %80 = getelementptr inbounds [256 x i16], ptr %10, i64 0, i64 0
  call void @kyber_poly_set_zero(ptr noundef %80)
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %81

81:                                               ; preds = %98, %79
  %82 = load i32, ptr %11, align 4, !tbaa !9
  %83 = icmp slt i32 %82, 2
  br i1 %83, label %84, label %101

84:                                               ; preds = %81
  call void @llvm.lifetime.start.p0(i64 512, ptr %12) #3
  %85 = getelementptr inbounds [256 x i16], ptr %12, i64 0, i64 0
  %86 = load i32, ptr %11, align 4, !tbaa !9
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds [2 x [256 x i16]], ptr %8, i64 0, i64 %87
  %89 = getelementptr inbounds [256 x i16], ptr %88, i64 0, i64 0
  %90 = load i32, ptr %11, align 4, !tbaa !9
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds [2 x [256 x i16]], ptr %7, i64 0, i64 %91
  %93 = getelementptr inbounds [256 x i16], ptr %92, i64 0, i64 0
  %94 = call i32 @kyber_poly_ntt_mul(ptr noundef %85, ptr noundef %89, ptr noundef %93)
  %95 = getelementptr inbounds [256 x i16], ptr %10, i64 0, i64 0
  %96 = getelementptr inbounds [256 x i16], ptr %10, i64 0, i64 0
  %97 = getelementptr inbounds [256 x i16], ptr %12, i64 0, i64 0
  call void @kyber_poly_add(ptr noundef %95, ptr noundef %96, ptr noundef %97)
  call void @llvm.lifetime.end.p0(i64 512, ptr %12) #3
  br label %98

98:                                               ; preds = %84
  %99 = load i32, ptr %11, align 4, !tbaa !9
  %100 = add nsw i32 %99, 1
  store i32 %100, ptr %11, align 4, !tbaa !9
  br label %81, !llvm.loop !15

101:                                              ; preds = %81
  %102 = getelementptr inbounds [256 x i16], ptr %10, i64 0, i64 0
  %103 = call i32 @kyber_poly_inv_ntt(ptr noundef %102)
  %104 = getelementptr inbounds [256 x i16], ptr %10, i64 0, i64 0
  %105 = getelementptr inbounds [256 x i16], ptr %9, i64 0, i64 0
  %106 = getelementptr inbounds [256 x i16], ptr %10, i64 0, i64 0
  call void @kyber_poly_sub(ptr noundef %104, ptr noundef %105, ptr noundef %106)
  %107 = getelementptr inbounds [256 x i16], ptr %10, i64 0, i64 0
  %108 = getelementptr inbounds [256 x i16], ptr %10, i64 0, i64 0
  %109 = call i32 @kyber_poly_compress(ptr noundef %107, i32 noundef 1, ptr noundef %108)
  %110 = getelementptr inbounds [256 x i16], ptr %10, i64 0, i64 0
  %111 = load ptr, ptr %6, align 8, !tbaa !5
  %112 = call i32 @kyber_poly_encode1(ptr noundef %110, ptr noundef %111)
  %113 = getelementptr inbounds [2 x [256 x i16]], ptr %8, i64 0, i64 0
  call void @gmssl_secure_clear(ptr noundef %113, i64 noundef 1024)
  %114 = getelementptr inbounds [256 x i16], ptr %10, i64 0, i64 0
  call void @gmssl_secure_clear(ptr noundef %114, i64 noundef 512)
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 512, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 512, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 1024, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 1024, ptr %7) #3
  ret i32 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 = { "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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !12}


; 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_poly_set_zero(ptr noundef) local_unnamed_addr #1

declare void @gmssl_secure_clear(ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_poly_add(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_poly_sub(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @kyber_poly_ntt(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @kyber_poly_inv_ntt(ptr noundef) local_unnamed_addr #1

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

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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_poly_decode4(ptr noundef, ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @kyber_cpa_decrypt(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #1 {
.preheader.preheader:
  %3 = alloca [2 x [256 x i16]], align 16
  %4 = alloca [2 x [256 x i16]], align 16
  %5 = alloca [256 x i16], align 16
  %6 = alloca [256 x i16], align 16
  %7 = alloca [256 x i16], align 16
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 512, ptr nonnull %5) #3
  call void @llvm.lifetime.start.p0(i64 512, ptr nonnull %6) #3
  %8 = call i32 @kyber_poly_decode10(ptr noundef nonnull %3, ptr noundef %1)
  %9 = call i32 @kyber_poly_decompress(ptr noundef nonnull %3, i32 noundef 10, ptr noundef nonnull %3)
  %10 = getelementptr inbounds i8, ptr %3, i64 512
  %11 = getelementptr inbounds i8, ptr %1, i64 320
  %12 = call i32 @kyber_poly_decode10(ptr noundef nonnull %10, ptr noundef nonnull %11)
  %13 = call i32 @kyber_poly_decompress(ptr noundef nonnull %10, i32 noundef 10, ptr noundef nonnull %10)
  %14 = getelementptr inbounds i8, ptr %1, i64 640
  call void @kyber_poly_decode4(ptr noundef nonnull %5, ptr noundef nonnull %14)
  %15 = call i32 @kyber_poly_decompress(ptr noundef nonnull %5, i32 noundef 4, ptr noundef nonnull %5)
  %16 = call i32 @kyber_poly_decode12(ptr noundef nonnull %4, ptr noundef %0)
  %17 = getelementptr inbounds i8, ptr %4, i64 512
  %18 = getelementptr inbounds i8, ptr %0, i64 384
  %19 = call i32 @kyber_poly_decode12(ptr noundef nonnull %17, ptr noundef nonnull %18)
  %20 = call i32 @kyber_poly_ntt(ptr noundef nonnull %3)
  %21 = call i32 @kyber_poly_ntt(ptr noundef nonnull %10)
  call void @kyber_poly_set_zero(ptr noundef nonnull %6)
  call void @llvm.lifetime.start.p0(i64 512, ptr nonnull %7) #3
  %22 = call i32 @kyber_poly_ntt_mul(ptr noundef nonnull %7, ptr noundef nonnull %4, ptr noundef nonnull %3)
  call void @kyber_poly_add(ptr noundef nonnull %6, ptr noundef nonnull %6, ptr noundef nonnull %7)
  call void @llvm.lifetime.end.p0(i64 512, ptr nonnull %7) #3
  call void @llvm.lifetime.start.p0(i64 512, ptr nonnull %7) #3
  %23 = call i32 @kyber_poly_ntt_mul(ptr noundef nonnull %7, ptr noundef nonnull %17, ptr noundef nonnull %10)
  call void @kyber_poly_add(ptr noundef nonnull %6, ptr noundef nonnull %6, ptr noundef nonnull %7)
  call void @llvm.lifetime.end.p0(i64 512, ptr nonnull %7) #3
  %24 = call i32 @kyber_poly_inv_ntt(ptr noundef nonnull %6)
  call void @kyber_poly_sub(ptr noundef nonnull %6, ptr noundef nonnull %5, ptr noundef nonnull %6)
  %25 = call i32 @kyber_poly_compress(ptr noundef nonnull %6, i32 noundef 1, ptr noundef nonnull %6)
  %26 = call i32 @kyber_poly_encode1(ptr noundef nonnull %6, ptr noundef %2)
  call void @gmssl_secure_clear(ptr noundef nonnull %4, i64 noundef 1024) #3
  call void @gmssl_secure_clear(ptr noundef nonnull %6, i64 noundef 512) #3
  call void @llvm.lifetime.end.p0(i64 512, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 512, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %3) #3
  ret i32 1
}

attributes #0 = { mustprogress 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 = { "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)"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass