; 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_ENC_PRE_COMP = type { [4 x i64], %struct.SM2_POINT }
%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_ex = external hidden unnamed_addr constant [18 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 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: 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, ...) #4

; Function Attrs: nounwind uwtable
define dso_local i32 @sm2_do_encrypt_ex(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, 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 %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i64 %3, ptr %10, align 8, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 96, ptr %12) #5
  call void @llvm.lifetime.start.p0(i64 64, ptr %13) #5
  call void @llvm.lifetime.start.p0(i64 112, ptr %14) #5
  %16 = load i64, ptr %10, align 8, !tbaa !9
  %17 = icmp ult i64 %16, 1
  br i1 %17, label %21, label %18

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

21:                                               ; preds = %18, %5
  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 146, ptr noundef @__FUNCTION__.sm2_do_encrypt_ex) #5
  br label %25

25:                                               ; preds = %22
  br label %26

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

27:                                               ; preds = %18
  %28 = load ptr, ptr %11, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.SM2_ENC_PRE_COMP, ptr %30, i32 0, i32 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %29, ptr align 8 %31, i64 64, i1 false), !tbaa.struct !11
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.SM2_ENC_PRE_COMP, ptr %32, i32 0, i32 0
  %34 = getelementptr inbounds [4 x i64], ptr %33, i64 0, i64 0
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.SM2_KEY, ptr %35, i32 0, i32 0
  call void @sm2_z256_point_mul(ptr noundef %12, ptr noundef %34, ptr noundef %36)
  %37 = getelementptr inbounds [64 x i8], ptr %13, i64 0, i64 0
  %38 = call i32 @sm2_z256_point_to_bytes(ptr noundef %12, ptr noundef %37)
  %39 = getelementptr inbounds [64 x i8], ptr %13, i64 0, i64 0
  %40 = load i64, ptr %10, align 8, !tbaa !9
  %41 = load ptr, ptr %11, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %41, i32 0, i32 3
  %43 = getelementptr inbounds [255 x i8], ptr %42, i64 0, i64 0
  %44 = call i32 @sm2_kdf(ptr noundef %39, i64 noundef 64, i64 noundef %40, ptr noundef %43)
  %45 = load ptr, ptr %11, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %45, i32 0, i32 3
  %47 = getelementptr inbounds [255 x i8], ptr %46, i64 0, i64 0
  %48 = load i64, ptr %10, align 8, !tbaa !9
  %49 = call i32 @all_zero(ptr noundef %47, i64 noundef %48)
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %52

51:                                               ; preds = %27
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %74

52:                                               ; preds = %27
  %53 = load ptr, ptr %11, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %53, i32 0, i32 3
  %55 = getelementptr inbounds [255 x i8], ptr %54, i64 0, i64 0
  %56 = load ptr, ptr %11, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %56, i32 0, i32 3
  %58 = getelementptr inbounds [255 x i8], ptr %57, i64 0, i64 0
  %59 = load ptr, ptr %9, align 8, !tbaa !5
  %60 = load i64, ptr %10, align 8, !tbaa !9
  call void @gmssl_memxor(ptr noundef %55, ptr noundef %58, ptr noundef %59, i64 noundef %60)
  %61 = load i64, ptr %10, align 8, !tbaa !9
  %62 = trunc i64 %61 to i8
  %63 = load ptr, ptr %11, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %63, i32 0, i32 2
  store i8 %62, ptr %64, align 1, !tbaa !13
  call void @sm3_init(ptr noundef %14)
  %65 = getelementptr inbounds [64 x i8], ptr %13, i64 0, i64 0
  call void @sm3_update(ptr noundef %14, ptr noundef %65, i64 noundef 32)
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = load i64, ptr %10, align 8, !tbaa !9
  call void @sm3_update(ptr noundef %14, ptr noundef %66, i64 noundef %67)
  %68 = getelementptr inbounds [64 x i8], ptr %13, i64 0, i64 0
  %69 = getelementptr inbounds i8, ptr %68, i64 32
  call void @sm3_update(ptr noundef %14, ptr noundef %69, i64 noundef 32)
  %70 = load ptr, ptr %11, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %70, i32 0, i32 1
  %72 = getelementptr inbounds [32 x i8], ptr %71, i64 0, i64 0
  call void @sm3_finish(ptr noundef %14, ptr noundef %72)
  call void @gmssl_secure_clear(ptr noundef %12, i64 noundef 96)
  %73 = getelementptr inbounds [64 x i8], ptr %13, i64 0, i64 0
  call void @gmssl_secure_clear(ptr noundef %73, i64 noundef 64)
  store i32 1, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %74

74:                                               ; preds = %52, %51, %26
  call void @llvm.lifetime.end.p0(i64 112, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 96, ptr %12) #5
  %75 = load i32, ptr %6, align 4
  ret i32 %75
}

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

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #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 32, !12}
!12 = !{!7, !7, i64 0}
!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_ex = external hidden unnamed_addr constant [18 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 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: 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 #4

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @sm2_do_encrypt_ex(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = alloca %struct.SM2_Z256_POINT, align 8
  %7 = alloca [64 x i8], align 16
  %8 = alloca %struct.SM3_CTX, align 8
  call void @llvm.lifetime.start.p0(i64 96, ptr nonnull %6) #5
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %7) #5
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %8) #5
  %9 = add i64 %3, -256
  %or.cond = icmp ult i64 %9, -255
  br i1 %or.cond, label %10, label %13

10:                                               ; preds = %5
  %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 146, ptr noundef nonnull @__FUNCTION__.sm2_do_encrypt_ex) #6
  br label %24

13:                                               ; preds = %5
  %14 = getelementptr inbounds i8, ptr %1, i64 32
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(64) %4, ptr noundef nonnull align 8 dereferenceable(64) %14, i64 64, i1 false), !tbaa.struct !9
  call void @sm2_z256_point_mul(ptr noundef nonnull %6, ptr noundef %1, ptr noundef %0) #5
  %15 = call i32 @sm2_z256_point_to_bytes(ptr noundef nonnull %6, ptr noundef nonnull %7) #5
  %16 = getelementptr inbounds i8, ptr %4, i64 97
  %17 = call i32 @sm2_kdf(ptr noundef nonnull %7, i64 noundef 64, i64 noundef %3, ptr noundef nonnull %16)
  %18 = call i32 @all_zero(ptr noundef nonnull %16, i64 noundef %3)
  %.not = icmp eq i32 %18, 0
  br i1 %.not, label %19, label %24

19:                                               ; preds = %13
  call void @gmssl_memxor(ptr noundef nonnull %16, ptr noundef nonnull %16, ptr noundef %2, i64 noundef %3) #5
  %20 = trunc nuw i64 %3 to i8
  %21 = getelementptr inbounds i8, ptr %4, i64 96
  store i8 %20, ptr %21, align 1, !tbaa !11
  call void @sm3_init(ptr noundef nonnull %8) #5
  call void @sm3_update(ptr noundef nonnull %8, ptr noundef nonnull %7, i64 noundef 32) #5
  call void @sm3_update(ptr noundef nonnull %8, ptr noundef %2, i64 noundef %3) #5
  %22 = getelementptr inbounds i8, ptr %7, i64 32
  call void @sm3_update(ptr noundef nonnull %8, ptr noundef nonnull %22, i64 noundef 32) #5
  %23 = getelementptr inbounds i8, ptr %4, i64 64
  call void @sm3_finish(ptr noundef nonnull %8, ptr noundef nonnull %23) #5
  call void @gmssl_secure_clear(ptr noundef nonnull %6, i64 noundef 96) #5
  call void @gmssl_secure_clear(ptr noundef nonnull %7, i64 noundef 64) #5
  br label %24

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

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

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #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 = !{i64 0, i64 32, !10, i64 32, i64 32, !10}
!10 = !{!7, !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}


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