; 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_SIGNATURE = type { [32 x i8], [32 x i8] }
%struct.SM2_SIGN_CTX = type { %struct.SM3_CTX, %struct.SM3_CTX, %struct.SM2_KEY, [4 x i64], [32 x %struct.SM2_SIGN_PRE_COMP], i32, [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] }
%struct.SM2_SIGN_PRE_COMP = type { [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_sign_finish = 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: nounwind uwtable
declare dso_local i32 @sm2_fast_sign_pre_compute(ptr noundef) #2

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sm2_sign_finish(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca [32 x i8], align 16
  %9 = alloca %struct.SM2_SIGNATURE, align 1
  %10 = 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 32, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 64, ptr %9) #4
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %19

13:                                               ; preds = %3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %19

16:                                               ; preds = %13
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %25, label %19

19:                                               ; preds = %16, %13, %3
  br label %20

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

23:                                               ; preds = %20
  br label %24

24:                                               ; preds = %23
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

25:                                               ; preds = %16
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %26, i32 0, i32 0
  %28 = getelementptr inbounds [32 x i8], ptr %8, i64 0, i64 0
  call void @sm3_finish(ptr noundef %27, ptr noundef %28)
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %29, i32 0, i32 5
  %31 = load i32, ptr %30, align 8, !tbaa !9
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %33, label %48

33:                                               ; preds = %25
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %34, i32 0, i32 4
  %36 = getelementptr inbounds [32 x %struct.SM2_SIGN_PRE_COMP], ptr %35, i64 0, i64 0
  %37 = call i32 @sm2_fast_sign_pre_compute(ptr noundef %36)
  %38 = icmp ne i32 %37, 1
  br i1 %38, label %39, label %45

39:                                               ; preds = %33
  br label %40

40:                                               ; preds = %39
  %41 = load ptr, ptr @stderr, align 8, !tbaa !5
  %42 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %41, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 572, ptr noundef @__FUNCTION__.sm2_sign_finish) #4
  br label %43

43:                                               ; preds = %40
  br label %44

44:                                               ; preds = %43
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

45:                                               ; preds = %33
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %46, i32 0, i32 5
  store i32 32, ptr %47, align 8, !tbaa !9
  br label %48

48:                                               ; preds = %45, %25
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %49, i32 0, i32 5
  %51 = load i32, ptr %50, align 8, !tbaa !9
  %52 = add i32 %51, -1
  store i32 %52, ptr %50, align 8, !tbaa !9
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %53, i32 0, i32 3
  %55 = getelementptr inbounds [4 x i64], ptr %54, i64 0, i64 0
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %56, i32 0, i32 4
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %58, i32 0, i32 5
  %60 = load i32, ptr %59, align 8, !tbaa !9
  %61 = zext i32 %60 to i64
  %62 = getelementptr inbounds [32 x %struct.SM2_SIGN_PRE_COMP], ptr %57, i64 0, i64 %61
  %63 = getelementptr inbounds [32 x i8], ptr %8, i64 0, i64 0
  %64 = call i32 @sm2_fast_sign(ptr noundef %55, ptr noundef %62, ptr noundef %63, ptr noundef %9)
  %65 = icmp ne i32 %64, 1
  br i1 %65, label %66, label %72

66:                                               ; preds = %48
  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, ptr noundef @.str.1, i32 noundef 581, ptr noundef @__FUNCTION__.sm2_sign_finish) #4
  br label %70

70:                                               ; preds = %67
  br label %71

71:                                               ; preds = %70
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

72:                                               ; preds = %48
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 0, ptr %73, align 8, !tbaa !16
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = call i32 @sm2_signature_to_der(ptr noundef %9, ptr noundef %6, ptr noundef %74)
  %76 = icmp ne i32 %75, 1
  br i1 %76, label %77, label %83

77:                                               ; preds = %72
  br label %78

78:                                               ; preds = %77
  %79 = load ptr, ptr @stderr, align 8, !tbaa !5
  %80 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %79, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 587, ptr noundef @__FUNCTION__.sm2_sign_finish) #4
  br label %81

81:                                               ; preds = %78
  br label %82

82:                                               ; preds = %81
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

83:                                               ; preds = %72
  store i32 1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %84

84:                                               ; preds = %83, %82, %71, %44, %24
  call void @llvm.lifetime.end.p0(i64 64, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %8) #4
  %85 = load i32, ptr %4, align 4
  ret i32 %85
}

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 = { 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 #3 = { "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 }

!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, !15, i64 2432}
!10 = !{!"", !11, i64 0, !11, i64 112, !13, i64 224, !7, i64 352, !7, i64 384, !15, i64 2432, !7, i64 2440}
!11 = !{!"", !7, i64 0, !12, i64 32, !7, i64 40, !12, i64 104}
!12 = !{!"long", !7, i64 0}
!13 = !{!"", !14, i64 0, !7, i64 96}
!14 = !{!"", !7, i64 0, !7, i64 32, !7, i64 64}
!15 = !{!"int", !7, i64 0}
!16 = !{!12, !12, 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_SIGNATURE = type { [32 x i8], [32 x i8] }
%struct.SM2_SIGN_PRE_COMP = type { [4 x i64], [4 x 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 [62 x i8], align 1
@__FUNCTION__.sm2_sign_finish = 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: nounwind uwtable
declare dso_local i32 @sm2_fast_sign_pre_compute(ptr noundef) local_unnamed_addr #2

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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @sm2_sign_finish(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #2 {
  %4 = alloca ptr, align 8
  %5 = alloca [32 x i8], align 16
  %6 = alloca %struct.SM2_SIGNATURE, align 1
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #4
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %6) #4
  %7 = icmp ne ptr %0, null
  %8 = icmp ne ptr %1, null
  %or.cond = and i1 %7, %8
  %9 = icmp ne ptr %2, null
  %or.cond3 = and i1 %or.cond, %9
  br i1 %or.cond3, label %13, label %10

10:                                               ; preds = %3
  %11 = load ptr, ptr @stderr, align 8, !tbaa !5
  %12 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %11, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 564, ptr noundef nonnull @__FUNCTION__.sm2_sign_finish) #5
  br label %39

13:                                               ; preds = %3
  call void @sm3_finish(ptr noundef nonnull %0, ptr noundef nonnull %5) #4
  %14 = getelementptr inbounds i8, ptr %0, i64 2432
  %15 = load i32, ptr %14, align 8, !tbaa !9
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %17, label %23

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %0, i64 384
  %19 = call i32 @sm2_fast_sign_pre_compute(ptr noundef nonnull %18)
  %.not = icmp eq i32 %19, 1
  br i1 %.not, label %23, label %20

20:                                               ; preds = %17
  %21 = load ptr, ptr @stderr, align 8, !tbaa !5
  %22 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %21, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 572, ptr noundef nonnull @__FUNCTION__.sm2_sign_finish) #5
  br label %39

23:                                               ; preds = %17, %13
  %24 = phi i32 [ %15, %13 ], [ 32, %17 ]
  %25 = add i32 %24, -1
  store i32 %25, ptr %14, align 8, !tbaa !9
  %26 = getelementptr inbounds i8, ptr %0, i64 352
  %27 = getelementptr inbounds i8, ptr %0, i64 384
  %28 = zext i32 %25 to i64
  %29 = getelementptr inbounds [32 x %struct.SM2_SIGN_PRE_COMP], ptr %27, i64 0, i64 %28
  %30 = call i32 @sm2_fast_sign(ptr noundef nonnull %26, ptr noundef nonnull %29, ptr noundef nonnull %5, ptr noundef nonnull %6)
  %.not17 = icmp eq i32 %30, 1
  br i1 %.not17, label %34, label %31

31:                                               ; preds = %23
  %32 = load ptr, ptr @stderr, align 8, !tbaa !5
  %33 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %32, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 581, ptr noundef nonnull @__FUNCTION__.sm2_sign_finish) #5
  br label %39

34:                                               ; preds = %23
  store i64 0, ptr %2, align 8, !tbaa !16
  %35 = call i32 @sm2_signature_to_der(ptr noundef nonnull %6, ptr noundef nonnull %4, ptr noundef nonnull %2)
  %.not18 = icmp eq i32 %35, 1
  br i1 %.not18, label %39, label %36

36:                                               ; preds = %34
  %37 = load ptr, ptr @stderr, align 8, !tbaa !5
  %38 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %37, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 587, ptr noundef nonnull @__FUNCTION__.sm2_sign_finish) #5
  br label %39

39:                                               ; preds = %34, %36, %31, %20, %10
  %.0 = phi i32 [ -1, %20 ], [ -1, %31 ], [ -1, %36 ], [ -1, %10 ], [ 1, %34 ]
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #4
  ret i32 %.0
}

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 = { 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 #3 = { "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 }
attributes #5 = { 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, !15, i64 2432}
!10 = !{!"", !11, i64 0, !11, i64 112, !13, i64 224, !7, i64 352, !7, i64 384, !15, i64 2432, !7, i64 2440}
!11 = !{!"", !7, i64 0, !12, i64 32, !7, i64 40, !12, i64 104}
!12 = !{!"long", !7, i64 0}
!13 = !{!"", !14, i64 0, !7, i64 96}
!14 = !{!"", !7, i64 0, !7, i64 32, !7, i64 64}
!15 = !{!"int", !7, i64 0}
!16 = !{!12, !12, i64 0}


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