; 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_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_init = external hidden unnamed_addr constant [14 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_compute_key(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @sm2_fast_sign_pre_compute(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

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

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_sign_init(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #2 {
  %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 510, ptr noundef @__FUNCTION__.sm2_sign_init) #5
  br label %21

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

22:                                               ; preds = %14
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.SM2_SIGN_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 519, ptr noundef @__FUNCTION__.sm2_sign_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_SIGN_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 %80 [
    i32 0, label %51
    i32 1, label %78
  ]

51:                                               ; preds = %49
  br label %52

52:                                               ; preds = %51, %22
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %53, i32 0, i32 1
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.SM2_SIGN_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_SIGN_CTX, ptr %57, i32 0, i32 4
  %59 = getelementptr inbounds [32 x %struct.SM2_SIGN_PRE_COMP], ptr %58, i64 0, i64 0
  %60 = call i32 @sm2_fast_sign_pre_compute(ptr noundef %59)
  %61 = icmp ne i32 %60, 1
  br i1 %61, label %62, label %67

62:                                               ; preds = %52
  br label %63

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

66:                                               ; preds = %63
  store i32 -1, ptr %5, align 4
  br label %78

67:                                               ; preds = %52
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %68, i32 0, i32 5
  store i32 32, ptr %69, align 8, !tbaa !13
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %70, i32 0, i32 2
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %71, ptr align 8 %72, i64 128, i1 false), !tbaa.struct !19
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.SM2_SIGN_CTX, ptr %74, i32 0, i32 3
  %76 = getelementptr inbounds [4 x i64], ptr %75, i64 0, i64 0
  %77 = call i32 @sm2_fast_sign_compute_key(ptr noundef %73, ptr noundef %76)
  store i32 1, ptr %5, align 4
  br label %78

78:                                               ; preds = %67, %66, %49, %21
  %79 = load i32, ptr %5, align 4
  ret i32 %79

80:                                               ; preds = %49
  unreachable
}

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 = { 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 }

!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}
!13 = !{!14, !18, i64 2432}
!14 = !{!"", !15, i64 0, !15, i64 112, !16, i64 224, !7, i64 352, !7, i64 384, !18, i64 2432, !7, i64 2440}
!15 = !{!"", !7, i64 0, !10, i64 32, !7, i64 40, !10, i64 104}
!16 = !{!"", !17, i64 0, !7, i64 96}
!17 = !{!"", !7, i64 0, !7, i64 32, !7, i64 64}
!18 = !{!"int", !7, i64 0}
!19 = !{i64 0, i64 32, !12, i64 32, i64 32, !12, i64 64, i64 32, !12, i64 96, i64 32, !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"

@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_init = external hidden unnamed_addr constant [14 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_compute_key(ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

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_sign_init(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #2 {
  %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 510, ptr noundef nonnull @__FUNCTION__.sm2_sign_init) #5
  br label %30

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 519, ptr noundef nonnull @__FUNCTION__.sm2_sign_init) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #6
  br label %30

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 384
  %21 = call i32 @sm2_fast_sign_pre_compute(ptr noundef nonnull %20)
  %.not27 = icmp eq i32 %21, 1
  br i1 %.not27, 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 528, ptr noundef nonnull @__FUNCTION__.sm2_sign_init) #5
  br label %30

25:                                               ; preds = %18
  %26 = getelementptr inbounds i8, ptr %0, i64 2432
  store i32 32, ptr %26, align 8, !tbaa !13
  %27 = getelementptr inbounds i8, ptr %0, i64 224
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(128) %27, ptr noundef nonnull align 8 dereferenceable(128) %1, i64 128, i1 false), !tbaa.struct !19
  %28 = getelementptr inbounds i8, ptr %0, i64 352
  %29 = call i32 @sm2_fast_sign_compute_key(ptr noundef nonnull %1, ptr noundef nonnull %28)
  br label %30

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

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 = { 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 = { 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}
!13 = !{!14, !18, i64 2432}
!14 = !{!"", !15, i64 0, !15, i64 112, !16, i64 224, !7, i64 352, !7, i64 384, !18, i64 2432, !7, i64 2440}
!15 = !{!"", !7, i64 0, !12, i64 32, !7, i64 40, !12, i64 104}
!16 = !{!"", !17, i64 0, !7, i64 96}
!17 = !{!"", !7, i64 0, !7, i64 32, !7, i64 64}
!18 = !{!"int", !7, i64 0}
!19 = !{i64 0, i64 32, !10, i64 32, i64 32, !10, i64 64, i64 32, !10, i64 96, i64 32, !10}


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