; 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_encrypt = 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

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

declare ptr @sm2_z256_order() #2

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

declare i64 @sm2_z256_is_zero(ptr noundef) #2

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

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_encrypt(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca [4 x i64], align 16
  %11 = alloca %struct.SM2_Z256_POINT, align 8
  %12 = alloca %struct.SM2_Z256_POINT, align 8
  %13 = alloca [64 x i8], align 16
  %14 = alloca %struct.SM3_CTX, align 8
  %15 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 96, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 96, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 64, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 112, ptr %14) #4
  %16 = load i64, ptr %8, align 8, !tbaa !9
  %17 = icmp ult i64 %16, 1
  br i1 %17, label %21, label %18

18:                                               ; preds = %4
  %19 = load i64, ptr %8, align 8, !tbaa !9
  %20 = icmp ugt i64 %19, 255
  br i1 %20, label %21, label %27

21:                                               ; preds = %18, %4
  br label %22

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

25:                                               ; preds = %22
  br label %26

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

27:                                               ; preds = %18
  br label %28

28:                                               ; preds = %67, %27
  br label %29

29:                                               ; preds = %41, %28
  %30 = getelementptr inbounds [4 x i64], ptr %10, i64 0, i64 0
  %31 = call ptr @sm2_z256_order()
  %32 = call i32 @sm2_z256_rand_range(ptr noundef %30, ptr noundef %31)
  %33 = icmp ne i32 %32, 1
  br i1 %33, label %34, label %40

34:                                               ; preds = %29
  br label %35

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

38:                                               ; preds = %35
  br label %39

39:                                               ; preds = %38
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %91

40:                                               ; preds = %29
  br label %41

41:                                               ; preds = %40
  %42 = getelementptr inbounds [4 x i64], ptr %10, i64 0, i64 0
  %43 = call i64 @sm2_z256_is_zero(ptr noundef %42)
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %29, label %45, !llvm.loop !11

45:                                               ; preds = %41
  %46 = getelementptr inbounds [4 x i64], ptr %10, i64 0, i64 0
  call void @sm2_z256_point_mul_generator(ptr noundef %11, ptr noundef %46)
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %47, i32 0, i32 0
  %49 = call i32 @sm2_z256_point_to_bytes(ptr noundef %11, ptr noundef %48)
  %50 = getelementptr inbounds [4 x i64], ptr %10, i64 0, i64 0
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.SM2_KEY, ptr %51, i32 0, i32 0
  call void @sm2_z256_point_mul(ptr noundef %12, ptr noundef %50, ptr noundef %52)
  %53 = getelementptr inbounds [64 x i8], ptr %13, i64 0, i64 0
  %54 = call i32 @sm2_z256_point_to_bytes(ptr noundef %12, ptr noundef %53)
  %55 = getelementptr inbounds [64 x i8], ptr %13, i64 0, i64 0
  %56 = load i64, ptr %8, align 8, !tbaa !9
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %57, i32 0, i32 3
  %59 = getelementptr inbounds [255 x i8], ptr %58, i64 0, i64 0
  %60 = call i32 @sm2_kdf(ptr noundef %55, i64 noundef 64, i64 noundef %56, ptr noundef %59)
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %61, i32 0, i32 3
  %63 = getelementptr inbounds [255 x i8], ptr %62, i64 0, i64 0
  %64 = load i64, ptr %8, align 8, !tbaa !9
  %65 = call i32 @all_zero(ptr noundef %63, i64 noundef %64)
  %66 = icmp ne i32 %65, 0
  br i1 %66, label %67, label %68

67:                                               ; preds = %45
  br label %28

68:                                               ; preds = %45
  %69 = load ptr, ptr %9, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %69, i32 0, i32 3
  %71 = getelementptr inbounds [255 x i8], ptr %70, i64 0, i64 0
  %72 = load ptr, ptr %9, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %72, i32 0, i32 3
  %74 = getelementptr inbounds [255 x i8], ptr %73, i64 0, i64 0
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  %76 = load i64, ptr %8, align 8, !tbaa !9
  call void @gmssl_memxor(ptr noundef %71, ptr noundef %74, ptr noundef %75, i64 noundef %76)
  %77 = load i64, ptr %8, align 8, !tbaa !9
  %78 = trunc i64 %77 to i8
  %79 = load ptr, ptr %9, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %79, i32 0, i32 2
  store i8 %78, ptr %80, align 1, !tbaa !13
  call void @sm3_init(ptr noundef %14)
  %81 = getelementptr inbounds [64 x i8], ptr %13, i64 0, i64 0
  call void @sm3_update(ptr noundef %14, ptr noundef %81, i64 noundef 32)
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = load i64, ptr %8, align 8, !tbaa !9
  call void @sm3_update(ptr noundef %14, ptr noundef %82, i64 noundef %83)
  %84 = getelementptr inbounds [64 x i8], ptr %13, i64 0, i64 0
  %85 = getelementptr inbounds i8, ptr %84, i64 32
  call void @sm3_update(ptr noundef %14, ptr noundef %85, i64 noundef 32)
  %86 = load ptr, ptr %9, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %86, i32 0, i32 1
  %88 = getelementptr inbounds [32 x i8], ptr %87, i64 0, i64 0
  call void @sm3_finish(ptr noundef %14, ptr noundef %88)
  %89 = getelementptr inbounds [4 x i64], ptr %10, i64 0, i64 0
  call void @gmssl_secure_clear(ptr noundef %89, i64 noundef 32)
  call void @gmssl_secure_clear(ptr noundef %12, i64 noundef 96)
  %90 = getelementptr inbounds [64 x i8], ptr %13, i64 0, i64 0
  call void @gmssl_secure_clear(ptr noundef %90, i64 noundef 64)
  store i32 1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %91

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

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 }

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !7, i64 96}
!14 = !{!"", !15, i64 0, !7, i64 64, !7, i64 96, !7, i64 97}
!15 = !{!"", !7, i64 0, !7, i64 32}


; 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_encrypt = 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

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

declare ptr @sm2_z256_order() local_unnamed_addr #2

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

declare i64 @sm2_z256_is_zero(ptr noundef) local_unnamed_addr #2

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

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_encrypt(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = alloca [4 x i64], align 16
  %6 = alloca %struct.SM2_Z256_POINT, align 8
  %7 = alloca %struct.SM2_Z256_POINT, align 8
  %8 = alloca [64 x i8], align 16
  %9 = alloca %struct.SM3_CTX, align 8
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #4
  call void @llvm.lifetime.start.p0(i64 96, ptr nonnull %6) #4
  call void @llvm.lifetime.start.p0(i64 96, ptr nonnull %7) #4
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %8) #4
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %9) #4
  %10 = add i64 %2, -256
  %or.cond = icmp ult i64 %10, -255
  br i1 %or.cond, label %12, label %.preheader

.preheader:                                       ; preds = %4
  %11 = getelementptr inbounds i8, ptr %3, i64 97
  br label %15

12:                                               ; preds = %4
  %13 = load ptr, ptr @stderr, align 8, !tbaa !5
  %14 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %13, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 191, ptr noundef nonnull @__FUNCTION__.sm2_do_encrypt) #5
  br label %33

15:                                               ; preds = %.backedge, %.preheader
  %16 = call ptr @sm2_z256_order() #4
  %17 = call i32 @sm2_z256_rand_range(ptr noundef nonnull %5, ptr noundef %16) #4
  %.not = icmp eq i32 %17, 1
  br i1 %.not, label %21, label %18

18:                                               ; preds = %15
  %19 = load ptr, ptr @stderr, align 8, !tbaa !5
  %20 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %19, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 199, ptr noundef nonnull @__FUNCTION__.sm2_do_encrypt) #5
  br label %33

21:                                               ; preds = %15
  %22 = call i64 @sm2_z256_is_zero(ptr noundef nonnull %5) #4
  %.not18 = icmp eq i64 %22, 0
  br i1 %.not18, label %23, label %.backedge

.backedge:                                        ; preds = %21, %23
  br label %15, !llvm.loop !9

23:                                               ; preds = %21
  call void @sm2_z256_point_mul_generator(ptr noundef nonnull %6, ptr noundef nonnull %5) #4
  %24 = call i32 @sm2_z256_point_to_bytes(ptr noundef nonnull %6, ptr noundef %3) #4
  call void @sm2_z256_point_mul(ptr noundef nonnull %7, ptr noundef nonnull %5, ptr noundef %0) #4
  %25 = call i32 @sm2_z256_point_to_bytes(ptr noundef nonnull %7, ptr noundef nonnull %8) #4
  %26 = call i32 @sm2_kdf(ptr noundef nonnull %8, i64 noundef 64, i64 noundef %2, ptr noundef nonnull %11)
  %27 = call i32 @all_zero(ptr noundef nonnull %11, i64 noundef %2)
  %.not19 = icmp eq i32 %27, 0
  br i1 %.not19, label %28, label %.backedge

28:                                               ; preds = %23
  call void @gmssl_memxor(ptr noundef nonnull %11, ptr noundef nonnull %11, ptr noundef %1, i64 noundef %2) #4
  %29 = trunc nuw i64 %2 to i8
  %30 = getelementptr inbounds i8, ptr %3, i64 96
  store i8 %29, ptr %30, align 1, !tbaa !11
  call void @sm3_init(ptr noundef nonnull %9) #4
  call void @sm3_update(ptr noundef nonnull %9, ptr noundef nonnull %8, i64 noundef 32) #4
  call void @sm3_update(ptr noundef nonnull %9, ptr noundef %1, i64 noundef %2) #4
  %31 = getelementptr inbounds i8, ptr %8, i64 32
  call void @sm3_update(ptr noundef nonnull %9, ptr noundef nonnull %31, i64 noundef 32) #4
  %32 = getelementptr inbounds i8, ptr %3, i64 64
  call void @sm3_finish(ptr noundef nonnull %9, ptr noundef nonnull %32) #4
  call void @gmssl_secure_clear(ptr noundef nonnull %5, i64 noundef 32) #4
  call void @gmssl_secure_clear(ptr noundef nonnull %7, i64 noundef 96) #4
  call void @gmssl_secure_clear(ptr noundef nonnull %8, i64 noundef 64) #4
  br label %33

33:                                               ; preds = %28, %18, %12
  %.0 = phi i32 [ -1, %12 ], [ -1, %18 ], [ 1, %28 ]
  call void @llvm.lifetime.end.p0(i64 112, ptr nonnull %9) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %8) #4
  call void @llvm.lifetime.end.p0(i64 96, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 96, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #4
  ret i32 %.0
}

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 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!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}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass