; LLVM IR from openssl/openssl :: crypto/sm2 (sm2_crypt)

; 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"

; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

21:                                               ; preds = %4
  %22 = load i32, ptr %11, align 4, !tbaa !11
  %23 = icmp sle i32 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %21, %4
  store i32 0, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %44

25:                                               ; preds = %21
  %26 = load i64, ptr %10, align 8, !tbaa !9
  %27 = add i64 %26, 1
  %28 = trunc i64 %27 to i32
  %29 = call i32 @ASN1_object_size(i32 noundef 0, i32 noundef %28, i32 noundef 2)
  %30 = mul nsw i32 2, %29
  %31 = load i32, ptr %11, align 4, !tbaa !11
  %32 = call i32 @ASN1_object_size(i32 noundef 0, i32 noundef %31, i32 noundef 4)
  %33 = add nsw i32 %30, %32
  %34 = load i64, ptr %8, align 8, !tbaa !9
  %35 = trunc i64 %34 to i32
  %36 = call i32 @ASN1_object_size(i32 noundef 0, i32 noundef %35, i32 noundef 4)
  %37 = add nsw i32 %33, %36
  %38 = sext i32 %37 to i64
  store i64 %38, ptr %12, align 8, !tbaa !9
  %39 = load i64, ptr %12, align 8, !tbaa !9
  %40 = trunc i64 %39 to i32
  %41 = call i32 @ASN1_object_size(i32 noundef 1, i32 noundef %40, i32 noundef 16)
  %42 = sext i32 %41 to i64
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  store i64 %42, ptr %43, align 8, !tbaa !9
  store i32 1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %44

44:                                               ; preds = %25, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %45 = load i32, ptr %5, align 4
  ret i32 %45
}

; Function Attrs: nounwind uwtable
declare hidden i64 @ec_field_size(ptr noundef) #1

declare ptr @EC_KEY_get0_group(ptr noundef) #2

declare i32 @EVP_MD_get_size(ptr noundef) #2

declare i32 @ASN1_object_size(i32 noundef, i32 noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 }

!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, !12, i64 0}
!12 = !{!"int", !7, 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"

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @ossl_sm2_ciphertext_size(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = tail call ptr @EC_KEY_get0_group(ptr noundef %0) #2
  %6 = tail call i64 @ec_field_size(ptr noundef %5)
  %7 = tail call i32 @EVP_MD_get_size(ptr noundef %1) #2
  %8 = icmp eq i64 %6, 0
  %9 = icmp slt i32 %7, 1
  %or.cond = select i1 %8, i1 true, i1 %9
  br i1 %or.cond, label %22, label %10

10:                                               ; preds = %4
  %11 = trunc i64 %6 to i32
  %12 = add i32 %11, 1
  %13 = tail call i32 @ASN1_object_size(i32 noundef 0, i32 noundef %12, i32 noundef 2) #2
  %14 = shl nsw i32 %13, 1
  %15 = tail call i32 @ASN1_object_size(i32 noundef 0, i32 noundef %7, i32 noundef 4) #2
  %16 = add nsw i32 %14, %15
  %17 = trunc i64 %2 to i32
  %18 = tail call i32 @ASN1_object_size(i32 noundef 0, i32 noundef %17, i32 noundef 4) #2
  %19 = add nsw i32 %16, %18
  %20 = tail call i32 @ASN1_object_size(i32 noundef 1, i32 noundef %19, i32 noundef 16) #2
  %21 = sext i32 %20 to i64
  store i64 %21, ptr %3, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %4, %10
  %.0 = phi i32 [ 1, %10 ], [ 0, %4 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i64 @ec_field_size(ptr noundef) local_unnamed_addr #0

declare ptr @EC_KEY_get0_group(ptr noundef) local_unnamed_addr #1

declare i32 @EVP_MD_get_size(ptr noundef) local_unnamed_addr #1

declare i32 @ASN1_object_size(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

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

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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