; 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_CIPHERTEXT = type { %struct.SM2_POINT, [32 x i8], i8, [255 x i8] }
%struct.SM2_POINT = type { [32 x i8], [32 x i8] }

@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_ciphertext_print = external hidden unnamed_addr constant [21 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [12 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [12 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [11 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

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

declare i32 @asn1_length_is_zero(i64 noundef) #4

; Function Attrs: nounwind uwtable
define dso_local i32 @sm2_ciphertext_print(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5) #3 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca [512 x i8], align 16
  %15 = alloca ptr, align 8
  %16 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i32 %1, ptr %9, align 4, !tbaa !9
  store i32 %2, ptr %10, align 4, !tbaa !9
  store ptr %3, ptr %11, align 8, !tbaa !5
  store ptr %4, ptr %12, align 8, !tbaa !5
  store i64 %5, ptr %13, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 512, ptr %14) #5
  call void @llvm.memset.p0.i64(ptr align 16 %14, i8 0, i64 512, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  %17 = getelementptr inbounds [512 x i8], ptr %14, i64 0, i64 0
  store ptr %17, ptr %15, align 8, !tbaa !5
  %18 = load ptr, ptr %15, align 8, !tbaa !5
  %19 = call i32 @sm2_ciphertext_from_der(ptr noundef %18, ptr noundef %12, ptr noundef %13)
  %20 = icmp ne i32 %19, 1
  br i1 %20, label %25, label %21

21:                                               ; preds = %6
  %22 = load i64, ptr %13, align 8, !tbaa !11
  %23 = call i32 @asn1_length_is_zero(i64 noundef %22)
  %24 = icmp ne i32 %23, 1
  br i1 %24, label %25, label %31

25:                                               ; preds = %21, %6
  br label %26

26:                                               ; preds = %25
  %27 = load ptr, ptr @stderr, align 8, !tbaa !5
  %28 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %27, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 445, ptr noundef @__FUNCTION__.sm2_ciphertext_print) #5
  br label %29

29:                                               ; preds = %26
  br label %30

30:                                               ; preds = %29
  store i32 -1, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %73

31:                                               ; preds = %21
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = load i32, ptr %9, align 4, !tbaa !9
  %34 = load i32, ptr %10, align 4, !tbaa !9
  %35 = load ptr, ptr %11, align 8, !tbaa !5
  %36 = call i32 (ptr, i32, i32, ptr, ...) @format_print(ptr noundef %32, i32 noundef %33, i32 noundef %34, ptr noundef @.str.2, ptr noundef %35)
  %37 = load i32, ptr %10, align 4, !tbaa !9
  %38 = add nsw i32 %37, 4
  store i32 %38, ptr %10, align 4, !tbaa !9
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = load i32, ptr %10, align 4, !tbaa !9
  %42 = load ptr, ptr %15, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %42, i32 0, i32 0
  %44 = getelementptr inbounds %struct.SM2_POINT, ptr %43, i32 0, i32 0
  %45 = getelementptr inbounds [32 x i8], ptr %44, i64 0, i64 0
  %46 = call i32 @format_bytes(ptr noundef %39, i32 noundef %40, i32 noundef %41, ptr noundef @.str.3, ptr noundef %45, i64 noundef 32)
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = load i32, ptr %9, align 4, !tbaa !9
  %49 = load i32, ptr %10, align 4, !tbaa !9
  %50 = load ptr, ptr %15, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %50, i32 0, i32 0
  %52 = getelementptr inbounds %struct.SM2_POINT, ptr %51, i32 0, i32 1
  %53 = getelementptr inbounds [32 x i8], ptr %52, i64 0, i64 0
  %54 = call i32 @format_bytes(ptr noundef %47, i32 noundef %48, i32 noundef %49, ptr noundef @.str.4, ptr noundef %53, i64 noundef 32)
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = load i32, ptr %9, align 4, !tbaa !9
  %57 = load i32, ptr %10, align 4, !tbaa !9
  %58 = load ptr, ptr %15, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %58, i32 0, i32 1
  %60 = getelementptr inbounds [32 x i8], ptr %59, i64 0, i64 0
  %61 = call i32 @format_bytes(ptr noundef %55, i32 noundef %56, i32 noundef %57, ptr noundef @.str.5, ptr noundef %60, i64 noundef 32)
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = load i32, ptr %9, align 4, !tbaa !9
  %64 = load i32, ptr %10, align 4, !tbaa !9
  %65 = load ptr, ptr %15, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %65, i32 0, i32 3
  %67 = getelementptr inbounds [255 x i8], ptr %66, i64 0, i64 0
  %68 = load ptr, ptr %15, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.SM2_CIPHERTEXT, ptr %68, i32 0, i32 2
  %70 = load i8, ptr %69, align 1, !tbaa !13
  %71 = zext i8 %70 to i64
  %72 = call i32 @format_bytes(ptr noundef %62, i32 noundef %63, i32 noundef %64, ptr noundef @.str.6, ptr noundef %67, i64 noundef %71)
  store i32 1, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %73

73:                                               ; preds = %31, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 512, ptr %14) #5
  %74 = load i32, ptr %7, align 4
  ret i32 %74
}

declare i32 @format_print(ptr noundef, i32 noundef, i32 noundef, ptr noundef, ...) #4

declare i32 @format_bytes(ptr noundef, i32 noundef, i32 noundef, ptr noundef, ptr noundef, i64 noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 #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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !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"

@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_ciphertext_print = external hidden unnamed_addr constant [21 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [12 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [12 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [11 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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

declare i32 @asn1_length_is_zero(i64 noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @sm2_ciphertext_print(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5) local_unnamed_addr #3 {
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca [512 x i8], align 16
  store ptr %4, ptr %7, align 8, !tbaa !5
  store i64 %5, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 512, ptr nonnull %9) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(512) %9, i8 0, i64 512, i1 false)
  %10 = call i32 @sm2_ciphertext_from_der(ptr noundef nonnull %9, ptr noundef nonnull %7, ptr noundef nonnull %8)
  %.not = icmp eq i32 %10, 1
  br i1 %.not, label %11, label %14

11:                                               ; preds = %6
  %12 = load i64, ptr %8, align 8, !tbaa !9
  %13 = call i32 @asn1_length_is_zero(i64 noundef %12) #5
  %.not23 = icmp eq i32 %13, 1
  br i1 %.not23, label %17, label %14

14:                                               ; preds = %6, %11
  %15 = load ptr, ptr @stderr, align 8, !tbaa !5
  %16 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 445, ptr noundef nonnull @__FUNCTION__.sm2_ciphertext_print) #6
  br label %30

17:                                               ; preds = %11
  %18 = call i32 (ptr, i32, i32, ptr, ...) @format_print(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef nonnull @.str.2, ptr noundef %3) #5
  %19 = add nsw i32 %2, 4
  %20 = call i32 @format_bytes(ptr noundef %0, i32 noundef %1, i32 noundef %19, ptr noundef nonnull @.str.3, ptr noundef nonnull %9, i64 noundef 32) #5
  %21 = getelementptr inbounds i8, ptr %9, i64 32
  %22 = call i32 @format_bytes(ptr noundef %0, i32 noundef %1, i32 noundef %19, ptr noundef nonnull @.str.4, ptr noundef nonnull %21, i64 noundef 32) #5
  %23 = getelementptr inbounds i8, ptr %9, i64 64
  %24 = call i32 @format_bytes(ptr noundef %0, i32 noundef %1, i32 noundef %19, ptr noundef nonnull @.str.5, ptr noundef nonnull %23, i64 noundef 32) #5
  %25 = getelementptr inbounds i8, ptr %9, i64 97
  %26 = getelementptr inbounds i8, ptr %9, i64 96
  %27 = load i8, ptr %26, align 16, !tbaa !11
  %28 = zext i8 %27 to i64
  %29 = call i32 @format_bytes(ptr noundef %0, i32 noundef %1, i32 noundef %19, ptr noundef nonnull @.str.6, ptr noundef nonnull %25, i64 noundef %28) #5
  br label %30

30:                                               ; preds = %17, %14
  %.0 = phi i32 [ -1, %14 ], [ 1, %17 ]
  call void @llvm.lifetime.end.p0(i64 512, ptr nonnull %9) #5
  ret i32 %.0
}

declare i32 @format_print(ptr noundef, i32 noundef, i32 noundef, ptr noundef, ...) local_unnamed_addr #4

declare i32 @format_bytes(ptr noundef, i32 noundef, i32 noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 #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 }
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 = !{!10, !10, i64 0}
!10 = !{!"long", !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,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass