; LLVM IR from guanzhi/GmSSL :: src (sm2_enc)

; 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.SM2_Z256_POINT = type { [4 x i64], [4 x i64], [4 x i64] }
%struct.SM3_CTX = type { [8 x i32], i64, [64 x i8], i64 }
%struct.SM2_CIPHERTEXT = type { %struct.SM2_POINT, [32 x i8], i8, [255 x i8] }
%struct.SM2_POINT = type { [32 x i8], [32 x i8] }
%struct.SM2_KEY = type { %struct.SM2_Z256_POINT, [4 x i64] }

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [61 x i8], align 1
@__FUNCTION__.sm2_do_decrypt = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @sm2_kdf(ptr noundef, i64 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

declare void @sm3_init(ptr noundef) #2

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

declare void @sm3_finish(ptr noundef, ptr noundef) #2

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

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

declare void @sm2_z256_point_mul(ptr noundef, ptr noundef, ptr noundef) #2

declare i32 @sm2_z256_point_to_bytes(ptr noundef, ptr noundef) #2

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sm2_do_decrypt(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca %struct.SM2_Z256_POINT, align 8
  %12 = alloca [64 x i8], align 16
  %13 = alloca %struct.SM3_CTX, align 8
  %14 = alloca [32 x i8], align 16
  %15 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  store i32 -1, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 96, ptr %11) #5
  call void @llvm.lifetime.start.p0(i64 64, ptr %12) #5
  call void @llvm.lifetime.start.p0(i64 112, ptr %13) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr %14) #5
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %16, i32 0, i32 0
  %18 = call i32 @sm2_z256_point_from_bytes(ptr noundef %11, ptr noundef %17)
  %19 = icmp ne i32 %18, 1
  br i1 %19, label %20, label %26

20:                                               ; preds = %4
  br label %21

21:                                               ; preds = %20
  %22 = load ptr, ptr @stderr, align 8, !tbaa !5
  %23 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %22, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 328, ptr noundef @__FUNCTION__.sm2_do_decrypt) #5
  br label %24

24:                                               ; preds = %21
  br label %25

25:                                               ; preds = %24
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %92

26:                                               ; preds = %4
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.SM2_KEY, ptr %27, i32 0, i32 1
  %29 = getelementptr inbounds [4 x i64], ptr %28, i64 0, i64 0
  call void @sm2_z256_point_mul(ptr noundef %11, ptr noundef %29, ptr noundef %11)
  %30 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  %31 = call i32 @sm2_z256_point_to_bytes(ptr noundef %11, ptr noundef %30)
  %32 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %33, i32 0, i32 2
  %35 = load i8, ptr %34, align 1, !tbaa !11
  %36 = zext i8 %35 to i64
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = call i32 @sm2_kdf(ptr noundef %32, i64 noundef 64, i64 noundef %36, ptr noundef %37)
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %40, i32 0, i32 2
  %42 = load i8, ptr %41, align 1, !tbaa !11
  %43 = zext i8 %42 to i64
  %44 = call i32 @all_zero(ptr noundef %39, i64 noundef %43)
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %52

46:                                               ; preds = %26
  br label %47

47:                                               ; preds = %46
  %48 = load ptr, ptr @stderr, align 8, !tbaa !5
  %49 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %48, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 339, ptr noundef @__FUNCTION__.sm2_do_decrypt) #5
  br label %50

50:                                               ; preds = %47
  br label %51

51:                                               ; preds = %50
  br label %89

52:                                               ; preds = %26
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %55, i32 0, i32 3
  %57 = getelementptr inbounds [255 x i8], ptr %56, i64 0, i64 0
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %58, i32 0, i32 2
  %60 = load i8, ptr %59, align 1, !tbaa !11
  %61 = zext i8 %60 to i64
  call void @gmssl_memxor(ptr noundef %53, ptr noundef %54, ptr noundef %57, i64 noundef %61)
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %62, i32 0, i32 2
  %64 = load i8, ptr %63, align 1, !tbaa !11
  %65 = zext i8 %64 to i64
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  store i64 %65, ptr %66, align 8, !tbaa !14
  call void @sm3_init(ptr noundef %13)
  %67 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  call void @sm3_update(ptr noundef %13, ptr noundef %67, i64 noundef 32)
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %69, i32 0, i32 2
  %71 = load i8, ptr %70, align 1, !tbaa !11
  %72 = zext i8 %71 to i64
  call void @sm3_update(ptr noundef %13, ptr noundef %68, i64 noundef %72)
  %73 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  %74 = getelementptr inbounds i8, ptr %73, i64 32
  call void @sm3_update(ptr noundef %13, ptr noundef %74, i64 noundef 32)
  %75 = getelementptr inbounds [32 x i8], ptr %14, i64 0, i64 0
  call void @sm3_finish(ptr noundef %13, ptr noundef %75)
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %76, i32 0, i32 1
  %78 = getelementptr inbounds [32 x i8], ptr %77, i64 0, i64 0
  %79 = getelementptr inbounds [32 x i8], ptr %14, i64 0, i64 0
  %80 = call i32 @memcmp(ptr noundef %78, ptr noundef %79, i64 noundef 32) #6
  %81 = icmp ne i32 %80, 0
  br i1 %81, label %82, label %88

82:                                               ; preds = %52
  br label %83

83:                                               ; preds = %82
  %84 = load ptr, ptr @stderr, align 8, !tbaa !5
  %85 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %84, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 356, ptr noundef @__FUNCTION__.sm2_do_decrypt) #5
  br label %86

86:                                               ; preds = %83
  br label %87

87:                                               ; preds = %86
  br label %89

88:                                               ; preds = %52
  store i32 1, ptr %10, align 4, !tbaa !9
  br label %89

89:                                               ; preds = %88, %87, %51
  call void @gmssl_secure_clear(ptr noundef %11, i64 noundef 96)
  %90 = getelementptr inbounds [64 x i8], ptr %12, i64 0, i64 0
  call void @gmssl_secure_clear(ptr noundef %90, i64 noundef 64)
  %91 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %91, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %92

92:                                               ; preds = %89, %25
  call void @llvm.lifetime.end.p0(i64 32, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 112, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 96, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %93 = load i32, ptr %5, align 4
  ret i32 %93
}

declare i32 @sm2_z256_point_from_bytes(ptr noundef, ptr noundef) #2

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

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 = { "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 "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 #4 = { 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 #5 = { nounwind }
attributes #6 = { 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"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !7, i64 96}
!12 = !{!"", !13, i64 0, !7, i64 64, !7, i64 96, !7, i64 97}
!13 = !{!"", !7, i64 0, !7, i64 32}
!14 = !{!15, !15, i64 0}
!15 = !{!"long", !7, i64 0}


; 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.SM2_Z256_POINT = type { [4 x i64], [4 x i64], [4 x i64] }
%struct.SM3_CTX = type { [8 x i32], i64, [64 x i8], i64 }

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [61 x i8], align 1
@__FUNCTION__.sm2_do_decrypt = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @sm2_kdf(ptr noundef, i64 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

declare void @sm3_init(ptr noundef) local_unnamed_addr #2

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

declare void @sm3_finish(ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

declare void @sm2_z256_point_mul(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @sm2_z256_point_to_bytes(ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @sm2_do_decrypt(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = alloca %struct.SM2_Z256_POINT, align 8
  %6 = alloca [64 x i8], align 16
  %7 = alloca %struct.SM3_CTX, align 8
  %8 = alloca [32 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 96, ptr nonnull %5) #5
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %6) #5
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %7) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %8) #5
  %9 = call i32 @sm2_z256_point_from_bytes(ptr noundef nonnull %5, ptr noundef %1) #5
  %.not = icmp eq i32 %9, 1
  br i1 %.not, label %13, label %10

10:                                               ; preds = %4
  %11 = load ptr, ptr @stderr, align 8, !tbaa !5
  %12 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %11, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 328, ptr noundef nonnull @__FUNCTION__.sm2_do_decrypt) #6
  br label %36

13:                                               ; preds = %4
  %14 = getelementptr inbounds i8, ptr %0, i64 96
  call void @sm2_z256_point_mul(ptr noundef nonnull %5, ptr noundef nonnull %14, ptr noundef nonnull %5) #5
  %15 = call i32 @sm2_z256_point_to_bytes(ptr noundef nonnull %5, ptr noundef nonnull %6) #5
  %16 = getelementptr inbounds i8, ptr %1, i64 96
  %17 = load i8, ptr %16, align 1, !tbaa !9
  %18 = zext i8 %17 to i64
  %19 = call i32 @sm2_kdf(ptr noundef nonnull %6, i64 noundef 64, i64 noundef %18, ptr noundef %2)
  %20 = load i8, ptr %16, align 1, !tbaa !9
  %21 = zext i8 %20 to i64
  %22 = call i32 @all_zero(ptr noundef %2, i64 noundef %21)
  %.not17 = icmp eq i32 %22, 0
  br i1 %.not17, label %23, label %.sink.split

23:                                               ; preds = %13
  %24 = getelementptr inbounds i8, ptr %1, i64 97
  %25 = load i8, ptr %16, align 1, !tbaa !9
  %26 = zext i8 %25 to i64
  call void @gmssl_memxor(ptr noundef %2, ptr noundef %2, ptr noundef nonnull %24, i64 noundef %26) #5
  %27 = load i8, ptr %16, align 1, !tbaa !9
  %28 = zext i8 %27 to i64
  store i64 %28, ptr %3, align 8, !tbaa !12
  call void @sm3_init(ptr noundef nonnull %7) #5
  call void @sm3_update(ptr noundef nonnull %7, ptr noundef nonnull %6, i64 noundef 32) #5
  %29 = load i8, ptr %16, align 1, !tbaa !9
  %30 = zext i8 %29 to i64
  call void @sm3_update(ptr noundef nonnull %7, ptr noundef %2, i64 noundef %30) #5
  %31 = getelementptr inbounds i8, ptr %6, i64 32
  call void @sm3_update(ptr noundef nonnull %7, ptr noundef nonnull %31, i64 noundef 32) #5
  call void @sm3_finish(ptr noundef nonnull %7, ptr noundef nonnull %8) #5
  %32 = getelementptr inbounds i8, ptr %1, i64 64
  %bcmp = call i32 @bcmp(ptr noundef nonnull dereferenceable(32) %32, ptr noundef nonnull dereferenceable(32) %8, i64 32)
  %.not18 = icmp eq i32 %bcmp, 0
  br i1 %.not18, label %35, label %.sink.split

.sink.split:                                      ; preds = %23, %13
  %.sink19 = phi i32 [ 339, %13 ], [ 356, %23 ]
  %33 = load ptr, ptr @stderr, align 8, !tbaa !5
  %34 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %33, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef %.sink19, ptr noundef nonnull @__FUNCTION__.sm2_do_decrypt) #6
  br label %35

35:                                               ; preds = %.sink.split, %23
  %.0 = phi i32 [ 1, %23 ], [ -1, %.sink.split ]
  call void @gmssl_secure_clear(ptr noundef nonnull %5, i64 noundef 96) #5
  call void @gmssl_secure_clear(ptr noundef nonnull %6, i64 noundef 64) #5
  br label %36

36:                                               ; preds = %35, %10
  %.016 = phi i32 [ -1, %10 ], [ %.0, %35 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %8) #5
  call void @llvm.lifetime.end.p0(i64 112, ptr nonnull %7) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %6) #5
  call void @llvm.lifetime.end.p0(i64 96, ptr nonnull %5) #5
  ret i32 %.016
}

declare i32 @sm2_z256_point_from_bytes(ptr noundef, ptr noundef) local_unnamed_addr #2

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

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 = { "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 = { 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 #4 = { nofree nounwind willreturn memory(argmem: read) }
attributes #5 = { nounwind }
attributes #6 = { 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"}
!9 = !{!10, !7, i64 96}
!10 = !{!"", !11, i64 0, !7, i64 64, !7, i64 96, !7, i64 97}
!11 = !{!"", !7, i64 0, !7, i64 32}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}


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