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

; 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_VERIFY_CTX = type { %struct.SM3_CTX, %struct.SM3_CTX, %struct.SM2_KEY, [16 x %struct.SM2_Z256_POINT] }
%struct.SM3_CTX = type { [8 x i32], i64, [64 x i8], i64 }
%struct.SM2_KEY = type { %struct.SM2_Z256_POINT, [4 x i64] }
%struct.SM2_Z256_POINT = type { [4 x i64], [4 x i64], [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 [62 x i8], align 1
@__FUNCTION__.sm2_verify_init = external hidden unnamed_addr constant [16 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: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #1

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

; 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) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @sm2_compute_z(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #3

declare void @sm3_init(ptr noundef) #4

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sm2_verify_init(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #3 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca [32 x i8], align 16
  %11 = 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 i64 %3, ptr %9, align 8, !tbaa !9
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %17

14:                                               ; preds = %4
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %22, label %17

17:                                               ; preds = %14, %4
  br label %18

18:                                               ; preds = %17
  %19 = load ptr, ptr @stderr, align 8, !tbaa !5
  %20 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %19, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 613, ptr noundef @__FUNCTION__.sm2_verify_init) #5
  br label %21

21:                                               ; preds = %18
  store i32 -1, ptr %5, align 4
  br label %74

22:                                               ; preds = %14
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.SM2_VERIFY_CTX, ptr %23, i32 0, i32 0
  call void @sm3_init(ptr noundef %24)
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %52

27:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 32, ptr %10) #5
  %28 = load i64, ptr %9, align 8, !tbaa !9
  %29 = icmp ule i64 %28, 0
  br i1 %29, label %33, label %30

30:                                               ; preds = %27
  %31 = load i64, ptr %9, align 8, !tbaa !9
  %32 = icmp ugt i64 %31, 8191
  br i1 %32, label %33, label %39

33:                                               ; preds = %30, %27
  br label %34

34:                                               ; preds = %33
  %35 = load ptr, ptr @stderr, align 8, !tbaa !5
  %36 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %35, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 622, ptr noundef @__FUNCTION__.sm2_verify_init) #5
  br label %37

37:                                               ; preds = %34
  br label %38

38:                                               ; preds = %37
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %49

39:                                               ; preds = %30
  %40 = getelementptr inbounds [32 x i8], ptr %10, i64 0, i64 0
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.SM2_KEY, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = load i64, ptr %9, align 8, !tbaa !9
  %45 = call i32 @sm2_compute_z(ptr noundef %40, ptr noundef %42, ptr noundef %43, i64 noundef %44)
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.SM2_VERIFY_CTX, ptr %46, i32 0, i32 0
  %48 = getelementptr inbounds [32 x i8], ptr %10, i64 0, i64 0
  call void @sm3_update(ptr noundef %47, ptr noundef %48, i64 noundef 32)
  store i32 0, ptr %11, align 4
  br label %49

49:                                               ; preds = %39, %38
  call void @llvm.lifetime.end.p0(i64 32, ptr %10) #5
  %50 = load i32, ptr %11, align 4
  switch i32 %50, label %76 [
    i32 0, label %51
    i32 1, label %74
  ]

51:                                               ; preds = %49
  br label %52

52:                                               ; preds = %51, %22
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.SM2_VERIFY_CTX, ptr %53, i32 0, i32 1
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.SM2_VERIFY_CTX, ptr %55, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %54, ptr align 8 %56, i64 112, i1 false), !tbaa.struct !11
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.SM2_VERIFY_CTX, ptr %57, i32 0, i32 2
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.SM2_KEY, ptr %59, i32 0, i32 0
  %61 = call i32 @sm2_key_set_public_key(ptr noundef %58, ptr noundef %60)
  %62 = icmp ne i32 %61, 1
  br i1 %62, label %63, label %68

63:                                               ; preds = %52
  br label %64

64:                                               ; preds = %63
  %65 = load ptr, ptr @stderr, align 8, !tbaa !5
  %66 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %65, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 631, ptr noundef @__FUNCTION__.sm2_verify_init) #5
  br label %67

67:                                               ; preds = %64
  store i32 -1, ptr %5, align 4
  br label %74

68:                                               ; preds = %52
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.SM2_KEY, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.SM2_VERIFY_CTX, ptr %71, i32 0, i32 3
  %73 = getelementptr inbounds [16 x %struct.SM2_Z256_POINT], ptr %72, i64 0, i64 0
  call void @sm2_z256_point_mul_pre_compute(ptr noundef %70, ptr noundef %73)
  store i32 1, ptr %5, align 4
  br label %74

74:                                               ; preds = %68, %67, %49, %21
  %75 = load i32, ptr %5, align 4
  ret i32 %75

76:                                               ; preds = %49
  unreachable
}

declare i32 @sm2_key_set_public_key(ptr noundef, ptr noundef) #4

declare void @sm2_z256_point_mul_pre_compute(ptr noundef, ptr noundef) #4

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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 }

!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 = !{!"long", !7, i64 0}
!11 = !{i64 0, i64 32, !12, i64 32, i64 8, !9, i64 40, i64 64, !12, i64 104, i64 8, !9}
!12 = !{!7, !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"

@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 [62 x i8], align 1
@__FUNCTION__.sm2_verify_init = external hidden unnamed_addr constant [16 x i8], align 1

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

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

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

; 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) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @sm2_compute_z(ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

declare void @sm3_init(ptr noundef) local_unnamed_addr #4

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @sm2_verify_init(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #3 {
  %5 = alloca [32 x i8], align 16
  %6 = icmp ne ptr %0, null
  %7 = icmp ne ptr %1, null
  %or.cond = and i1 %6, %7
  br i1 %or.cond, label %11, label %8

8:                                                ; preds = %4
  %9 = load ptr, ptr @stderr, align 8, !tbaa !5
  %10 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %9, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 613, ptr noundef nonnull @__FUNCTION__.sm2_verify_init) #5
  br label %27

11:                                               ; preds = %4
  tail call void @sm3_init(ptr noundef nonnull %0) #6
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %18, label %12

12:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #6
  %13 = add i64 %3, -8192
  %or.cond3 = icmp ult i64 %13, -8191
  br i1 %or.cond3, label %15, label %.thread

.thread:                                          ; preds = %12
  %14 = call i32 @sm2_compute_z(ptr noundef nonnull %5, ptr noundef nonnull %1, ptr noundef nonnull %2, i64 noundef %3)
  call void @sm3_update(ptr noundef nonnull %0, ptr noundef nonnull %5, i64 noundef 32) #6
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #6
  br label %18

15:                                               ; preds = %12
  %16 = load ptr, ptr @stderr, align 8, !tbaa !5
  %17 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %16, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 622, ptr noundef nonnull @__FUNCTION__.sm2_verify_init) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #6
  br label %27

18:                                               ; preds = %.thread, %11
  %19 = getelementptr inbounds i8, ptr %0, i64 112
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(112) %19, ptr noundef nonnull align 8 dereferenceable(112) %0, i64 112, i1 false), !tbaa.struct !9
  %20 = getelementptr inbounds i8, ptr %0, i64 224
  %21 = call i32 @sm2_key_set_public_key(ptr noundef nonnull %20, ptr noundef nonnull %1) #6
  %.not25 = icmp eq i32 %21, 1
  br i1 %.not25, label %25, label %22

22:                                               ; preds = %18
  %23 = load ptr, ptr @stderr, align 8, !tbaa !5
  %24 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %23, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 631, ptr noundef nonnull @__FUNCTION__.sm2_verify_init) #5
  br label %27

25:                                               ; preds = %18
  %26 = getelementptr inbounds i8, ptr %0, i64 352
  call void @sm2_z256_point_mul_pre_compute(ptr noundef nonnull %1, ptr noundef nonnull %26) #6
  br label %27

27:                                               ; preds = %15, %25, %22, %8
  %.1 = phi i32 [ -1, %15 ], [ -1, %22 ], [ 1, %25 ], [ -1, %8 ]
  ret i32 %.1
}

declare i32 @sm2_key_set_public_key(ptr noundef, ptr noundef) local_unnamed_addr #4

declare void @sm2_z256_point_mul_pre_compute(ptr noundef, ptr noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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 = { cold nounwind }
attributes #6 = { 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 = !{i64 0, i64 32, !10, i64 32, i64 8, !11, i64 40, i64 64, !10, i64 104, i64 8, !11}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}


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