; 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_PRIVATE_KEY = type { %struct.KYBER_CPA_PRIVATE_KEY, %struct.KYBER_CPA_PUBLIC_KEY, [32 x i8], [32 x i8] }
%struct.KYBER_CPA_PRIVATE_KEY = type { [2 x [384 x i8]] }
%struct.KYBER_CPA_PUBLIC_KEY = type { [2 x [384 x i8]], [32 x i8] }

@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_decap = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_h_hash(ptr noundef, i64 noundef, ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_g_hash(ptr noundef, i64 noundef, ptr noundef) #0

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @kyber_decap(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca [64 x i8], align 16
  %9 = alloca [64 x i8], align 16
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca %struct.KYBER_CPA_CIPHERTEXT, align 1
  %15 = alloca [32 x i8], align 16
  %16 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 64, ptr %8) #6
  call void @llvm.lifetime.start.p0(i64 64, ptr %9) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  %17 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  store ptr %17, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %18 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  %19 = getelementptr inbounds i8, ptr %18, i64 32
  store ptr %19, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %20 = getelementptr inbounds [64 x i8], ptr %9, i64 0, i64 0
  store ptr %20, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #6
  %21 = getelementptr inbounds [64 x i8], ptr %9, i64 0, i64 0
  %22 = getelementptr inbounds i8, ptr %21, i64 32
  store ptr %22, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 768, ptr %14) #6
  call void @llvm.lifetime.start.p0(i64 32, ptr %15) #6
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.KYBER_PRIVATE_KEY, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  %27 = call i32 @kyber_cpa_decrypt(ptr noundef %24, ptr noundef %25, ptr noundef %26)
  %28 = icmp ne i32 %27, 1
  br i1 %28, label %29, label %35

29:                                               ; preds = %3
  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 1476, ptr noundef @__FUNCTION__.kyber_decap) #6
  br label %33

33:                                               ; preds = %30
  br label %34

34:                                               ; preds = %33
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %16, align 4
  br label %82

35:                                               ; preds = %3
  %36 = load ptr, ptr %11, align 8, !tbaa !5
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.KYBER_PRIVATE_KEY, ptr %37, i32 0, i32 2
  %39 = getelementptr inbounds [32 x i8], ptr %38, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %36, ptr align 1 %39, i64 32, i1 false)
  %40 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  %41 = getelementptr inbounds [64 x i8], ptr %9, i64 0, i64 0
  call void @kyber_g_hash(ptr noundef %40, i64 noundef 64, ptr noundef %41)
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.KYBER_PRIVATE_KEY, ptr %42, i32 0, i32 1
  %44 = load ptr, ptr %10, align 8, !tbaa !5
  %45 = load ptr, ptr %13, align 8, !tbaa !5
  %46 = call i32 @kyber_cpa_encrypt(ptr noundef %43, ptr noundef %44, ptr noundef %45, ptr noundef %14)
  %47 = icmp ne i32 %46, 1
  br i1 %47, label %48, label %56

48:                                               ; preds = %35
  %49 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  call void @gmssl_secure_clear(ptr noundef %49, i64 noundef 64)
  %50 = getelementptr inbounds [64 x i8], ptr %9, i64 0, i64 0
  call void @gmssl_secure_clear(ptr noundef %50, i64 noundef 64)
  br label %51

51:                                               ; preds = %48
  %52 = load ptr, ptr @stderr, align 8, !tbaa !5
  %53 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %52, ptr noundef @.str.4, ptr noundef @.str.5, i32 noundef 1490, ptr noundef @__FUNCTION__.kyber_decap) #6
  br label %54

54:                                               ; preds = %51
  br label %55

55:                                               ; preds = %54
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %16, align 4
  br label %82

56:                                               ; preds = %35
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = load ptr, ptr %13, align 8, !tbaa !5
  call void @kyber_h_hash(ptr noundef %57, i64 noundef 768, ptr noundef %58)
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = call i32 @memcmp(ptr noundef %59, ptr noundef %14, i64 noundef 768) #7
  %61 = icmp eq i32 %60, 0
  br i1 %61, label %62, label %66

62:                                               ; preds = %56
  %63 = getelementptr inbounds [64 x i8], ptr %9, i64 0, i64 0
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = call i32 @kyber_kdf(ptr noundef %63, ptr noundef %64)
  br label %79

66:                                               ; preds = %56
  br label %67

67:                                               ; preds = %66
  %68 = load ptr, ptr @stderr, align 8, !tbaa !5
  %69 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %68, ptr noundef @.str.4, ptr noundef @.str.5, i32 noundef 1501, ptr noundef @__FUNCTION__.kyber_decap) #6
  br label %70

70:                                               ; preds = %67
  br label %71

71:                                               ; preds = %70
  %72 = getelementptr inbounds [64 x i8], ptr %9, i64 0, i64 0
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.KYBER_PRIVATE_KEY, ptr %73, i32 0, i32 3
  %75 = getelementptr inbounds [32 x i8], ptr %74, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %72, ptr align 1 %75, i64 32, i1 false)
  %76 = getelementptr inbounds [64 x i8], ptr %9, i64 0, i64 0
  %77 = load ptr, ptr %7, align 8, !tbaa !5
  %78 = call i32 @kyber_kdf(ptr noundef %76, ptr noundef %77)
  br label %79

79:                                               ; preds = %71, %62
  %80 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  call void @gmssl_secure_clear(ptr noundef %80, i64 noundef 64)
  %81 = getelementptr inbounds [64 x i8], ptr %9, i64 0, i64 0
  call void @gmssl_secure_clear(ptr noundef %81, i64 noundef 64)
  store i32 1, ptr %4, align 4
  store i32 1, ptr %16, align 4
  br label %82

82:                                               ; preds = %79, %55, %34
  call void @llvm.lifetime.end.p0(i64 32, ptr %15) #6
  call void @llvm.lifetime.end.p0(i64 768, ptr %14) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 64, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 64, ptr %8) #6
  %83 = load i32, ptr %4, align 4
  ret i32 %83
}

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #5

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 #5 = { nounwind willreturn memory(read) "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 #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(read) }

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


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

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

@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_decap = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_h_hash(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @kyber_g_hash(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @kyber_decap(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca [64 x i8], align 16
  %5 = alloca [64 x i8], align 16
  %6 = alloca %struct.KYBER_CPA_CIPHERTEXT, align 1
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %4) #6
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #6
  %7 = getelementptr inbounds i8, ptr %5, i64 32
  call void @llvm.lifetime.start.p0(i64 768, ptr nonnull %6) #6
  %8 = call i32 @kyber_cpa_decrypt(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %4)
  %.not = icmp eq i32 %8, 1
  br i1 %.not, label %12, label %9

9:                                                ; preds = %3
  %10 = load ptr, ptr @stderr, align 8, !tbaa !5
  %11 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %10, ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.5, i32 noundef 1476, ptr noundef nonnull @__FUNCTION__.kyber_decap) #7
  br label %28

12:                                               ; preds = %3
  %13 = getelementptr inbounds i8, ptr %4, i64 32
  %14 = getelementptr inbounds i8, ptr %0, i64 1568
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %13, ptr noundef nonnull align 1 dereferenceable(32) %14, i64 32, i1 false)
  call void @kyber_g_hash(ptr noundef nonnull %4, i64 noundef 64, ptr noundef nonnull %5)
  %15 = getelementptr inbounds i8, ptr %0, i64 768
  %16 = call i32 @kyber_cpa_encrypt(ptr noundef nonnull %15, ptr noundef nonnull %4, ptr noundef nonnull %7, ptr noundef nonnull %6)
  %.not14 = icmp eq i32 %16, 1
  br i1 %.not14, label %20, label %17

17:                                               ; preds = %12
  call void @gmssl_secure_clear(ptr noundef nonnull %4, i64 noundef 64) #6
  call void @gmssl_secure_clear(ptr noundef nonnull %5, i64 noundef 64) #6
  %18 = load ptr, ptr @stderr, align 8, !tbaa !5
  %19 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.5, i32 noundef 1490, ptr noundef nonnull @__FUNCTION__.kyber_decap) #7
  br label %28

20:                                               ; preds = %12
  call void @kyber_h_hash(ptr noundef %1, i64 noundef 768, ptr noundef nonnull %7)
  %bcmp = call i32 @bcmp(ptr noundef nonnull dereferenceable(768) %1, ptr noundef nonnull dereferenceable(768) %6, i64 768)
  %21 = icmp eq i32 %bcmp, 0
  br i1 %21, label %26, label %22

22:                                               ; preds = %20
  %23 = load ptr, ptr @stderr, align 8, !tbaa !5
  %24 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %23, ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.5, i32 noundef 1501, ptr noundef nonnull @__FUNCTION__.kyber_decap) #7
  %25 = getelementptr inbounds i8, ptr %0, i64 1600
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %5, ptr noundef nonnull align 1 dereferenceable(32) %25, i64 32, i1 false)
  br label %26

26:                                               ; preds = %20, %22
  %27 = call i32 @kyber_kdf(ptr noundef nonnull %5, ptr noundef %2)
  call void @gmssl_secure_clear(ptr noundef nonnull %4, i64 noundef 64) #6
  call void @gmssl_secure_clear(ptr noundef nonnull %5, i64 noundef 64) #6
  br label %28

28:                                               ; preds = %26, %17, %9
  %.0 = phi i32 [ -1, %9 ], [ -1, %17 ], [ 1, %26 ]
  call void @llvm.lifetime.end.p0(i64 768, ptr nonnull %6) #6
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %5) #6
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %4) #6
  ret i32 %.0
}

; Function Attrs: nofree nounwind willreturn memory(argmem: read)
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64) local_unnamed_addr #5

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 #5 = { nofree nounwind willreturn memory(argmem: read) }
attributes #6 = { nounwind }
attributes #7 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass