; LLVM IR from guanzhi/GmSSL :: src (sm3_lms)

; 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.SM3_LMS_SIGNATURE = type { [4 x i8], [4 x i8], %struct.anon, [25 x [32 x i8]] }
%struct.anon = type { [4 x i8], [34 x [32 x i8]], [32 x i8] }
%struct.SM3_HSS_KEY = type { %struct.SM3_HSS_PUBLIC_KEY, [5 x %struct.SM3_LMS_KEY], [4 x %struct.SM3_LMS_SIGNATURE] }
%struct.SM3_HSS_PUBLIC_KEY = type { i32, %struct.SM3_LMS_PUBLIC_KEY }
%struct.SM3_LMS_PUBLIC_KEY = type { [4 x i8], [4 x i8], [16 x i8], [32 x i8] }
%struct.SM3_LMS_KEY = type { %struct.SM3_LMS_PUBLIC_KEY, ptr, [32 x i8], i32 }

@stderr = external global ptr, align 8
@.str.14 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [61 x i8], align 1
@__FUNCTION__.sm3_hss_sign = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr 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

; 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) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sm3_hss_sign(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #3 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = alloca %struct.SM3_LMS_SIGNATURE, align 1
  %17 = 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 ptr %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %18 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %18, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 1932, ptr %16) #4
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.SM3_HSS_KEY, ptr %19, i32 0, i32 0
  %21 = getelementptr inbounds %struct.SM3_HSS_PUBLIC_KEY, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !9
  %23 = sub nsw i32 %22, 1
  store i32 %23, ptr %12, align 4, !tbaa !14
  %24 = load i32, ptr %12, align 4, !tbaa !14
  %25 = ashr i32 %24, 24
  %26 = trunc i32 %25 to i8
  %27 = load ptr, ptr %14, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %27, i64 0
  store i8 %26, ptr %28, align 1, !tbaa !15
  %29 = load i32, ptr %12, align 4, !tbaa !14
  %30 = ashr i32 %29, 16
  %31 = trunc i32 %30 to i8
  %32 = load ptr, ptr %14, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 1
  store i8 %31, ptr %33, align 1, !tbaa !15
  %34 = load i32, ptr %12, align 4, !tbaa !14
  %35 = ashr i32 %34, 8
  %36 = trunc i32 %35 to i8
  %37 = load ptr, ptr %14, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i64 2
  store i8 %36, ptr %38, align 1, !tbaa !15
  %39 = load i32, ptr %12, align 4, !tbaa !14
  %40 = trunc i32 %39 to i8
  %41 = load ptr, ptr %14, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i64 3
  store i8 %40, ptr %42, align 1, !tbaa !15
  %43 = load ptr, ptr %14, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i64 4
  store ptr %44, ptr %14, align 8, !tbaa !5
  store i32 0, ptr %13, align 4, !tbaa !14
  br label %45

45:                                               ; preds = %70, %5
  %46 = load i32, ptr %13, align 4, !tbaa !14
  %47 = load i32, ptr %12, align 4, !tbaa !14
  %48 = icmp slt i32 %46, %47
  br i1 %48, label %49, label %73

49:                                               ; preds = %45
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.SM3_HSS_KEY, ptr %50, i32 0, i32 2
  %52 = load i32, ptr %13, align 4, !tbaa !14
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [4 x %struct.SM3_LMS_SIGNATURE], ptr %51, i64 0, i64 %53
  %55 = load ptr, ptr %14, align 8, !tbaa !5
  %56 = call i32 @sm3_lms_signature_to_bytes(ptr noundef %54, ptr noundef %55, ptr noundef %15)
  %57 = load i64, ptr %15, align 8, !tbaa !16
  %58 = load ptr, ptr %14, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i64 %57
  store ptr %59, ptr %14, align 8, !tbaa !5
  %60 = load ptr, ptr %14, align 8, !tbaa !5
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.SM3_HSS_KEY, ptr %61, i32 0, i32 1
  %63 = load i32, ptr %13, align 4, !tbaa !14
  %64 = add nsw i32 %63, 1
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [5 x %struct.SM3_LMS_KEY], ptr %62, i64 0, i64 %65
  %67 = getelementptr inbounds %struct.SM3_LMS_KEY, ptr %66, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %60, ptr align 8 %67, i64 56, i1 false)
  %68 = load ptr, ptr %14, align 8, !tbaa !5
  %69 = getelementptr inbounds i8, ptr %68, i64 56
  store ptr %69, ptr %14, align 8, !tbaa !5
  br label %70

70:                                               ; preds = %49
  %71 = load i32, ptr %13, align 4, !tbaa !14
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %13, align 4, !tbaa !14
  br label %45, !llvm.loop !18

73:                                               ; preds = %45
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.SM3_HSS_KEY, ptr %74, i32 0, i32 1
  %76 = load i32, ptr %13, align 4, !tbaa !14
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds [5 x %struct.SM3_LMS_KEY], ptr %75, i64 0, i64 %77
  %79 = load ptr, ptr %8, align 8, !tbaa !5
  %80 = load ptr, ptr %9, align 8, !tbaa !5
  %81 = call i32 @sm3_lms_do_sign(ptr noundef %78, ptr noundef %79, ptr noundef %80, ptr noundef %16)
  %82 = icmp ne i32 %81, 1
  br i1 %82, label %83, label %89

83:                                               ; preds = %73
  br label %84

84:                                               ; preds = %83
  %85 = load ptr, ptr @stderr, align 8, !tbaa !5
  %86 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %85, ptr noundef @.str.14, ptr noundef @.str.15, i32 noundef 1057, ptr noundef @__FUNCTION__.sm3_hss_sign) #4
  br label %87

87:                                               ; preds = %84
  br label %88

88:                                               ; preds = %87
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %101

89:                                               ; preds = %73
  %90 = load ptr, ptr %14, align 8, !tbaa !5
  %91 = call i32 @sm3_lms_signature_to_bytes(ptr noundef %16, ptr noundef %90, ptr noundef %15)
  %92 = load i64, ptr %15, align 8, !tbaa !16
  %93 = load ptr, ptr %14, align 8, !tbaa !5
  %94 = getelementptr inbounds i8, ptr %93, i64 %92
  store ptr %94, ptr %14, align 8, !tbaa !5
  %95 = load ptr, ptr %14, align 8, !tbaa !5
  %96 = load ptr, ptr %10, align 8, !tbaa !5
  %97 = ptrtoint ptr %95 to i64
  %98 = ptrtoint ptr %96 to i64
  %99 = sub i64 %97, %98
  %100 = load ptr, ptr %11, align 8, !tbaa !5
  store i64 %99, ptr %100, align 8, !tbaa !16
  store i32 1, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %101

101:                                              ; preds = %89, %88
  call void @llvm.lifetime.end.p0(i64 1932, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  %102 = load i32, ptr %6, align 4
  ret i32 %102
}

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !12, i64 0}
!10 = !{!"", !11, i64 0, !7, i64 64, !7, i64 584}
!11 = !{!"", !12, i64 0, !13, i64 4}
!12 = !{!"int", !7, i64 0}
!13 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 24}
!14 = !{!12, !12, i64 0}
!15 = !{!7, !7, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"long", !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; 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.SM3_LMS_SIGNATURE = type { [4 x i8], [4 x i8], %struct.anon, [25 x [32 x i8]] }
%struct.anon = type { [4 x i8], [34 x [32 x i8]], [32 x i8] }
%struct.SM3_LMS_KEY = type { %struct.SM3_LMS_PUBLIC_KEY, ptr, [32 x i8], i32 }
%struct.SM3_LMS_PUBLIC_KEY = type { [4 x i8], [4 x i8], [16 x i8], [32 x i8] }

@stderr = external local_unnamed_addr global ptr, align 8
@.str.14 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [61 x i8], align 1
@__FUNCTION__.sm3_hss_sign = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) 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

; 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) #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @sm3_hss_sign(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #3 {
  %6 = alloca i64, align 8
  %7 = alloca %struct.SM3_LMS_SIGNATURE, align 1
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #4
  call void @llvm.lifetime.start.p0(i64 1932, ptr nonnull %7) #4
  %8 = load i32, ptr %0, align 8, !tbaa !5
  %9 = add i32 %8, -1
  %10 = lshr i32 %9, 24
  %11 = trunc nuw i32 %10 to i8
  store i8 %11, ptr %3, align 1, !tbaa !12
  %12 = lshr i32 %9, 16
  %13 = trunc i32 %12 to i8
  %14 = getelementptr inbounds i8, ptr %3, i64 1
  store i8 %13, ptr %14, align 1, !tbaa !12
  %15 = lshr i32 %9, 8
  %16 = trunc i32 %15 to i8
  %17 = getelementptr inbounds i8, ptr %3, i64 2
  store i8 %16, ptr %17, align 1, !tbaa !12
  %18 = trunc i32 %9 to i8
  %19 = getelementptr inbounds i8, ptr %3, i64 3
  store i8 %18, ptr %19, align 1, !tbaa !12
  %20 = getelementptr inbounds i8, ptr %3, i64 4
  %21 = icmp sgt i32 %8, 1
  br i1 %21, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %5
  %22 = getelementptr inbounds i8, ptr %0, i64 584
  %23 = getelementptr inbounds i8, ptr %0, i64 64
  %wide.trip.count = zext nneg i32 %9 to i64
  br label %24

24:                                               ; preds = %.lr.ph, %24
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %24 ]
  %.034 = phi ptr [ %20, %.lr.ph ], [ %30, %24 ]
  %25 = getelementptr inbounds [4 x %struct.SM3_LMS_SIGNATURE], ptr %22, i64 0, i64 %indvars.iv
  %26 = call i32 @sm3_lms_signature_to_bytes(ptr noundef nonnull %25, ptr noundef nonnull %.034, ptr noundef nonnull %6)
  %27 = load i64, ptr %6, align 8, !tbaa !13
  %28 = getelementptr inbounds i8, ptr %.034, i64 %27
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %29 = getelementptr inbounds [5 x %struct.SM3_LMS_KEY], ptr %23, i64 0, i64 %indvars.iv.next
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(56) %28, ptr noundef nonnull align 8 dereferenceable(56) %29, i64 56, i1 false)
  %30 = getelementptr inbounds i8, ptr %28, i64 56
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %24, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %24
  %31 = zext nneg i32 %9 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %5, %._crit_edge.loopexit
  %.031.lcssa = phi i64 [ %31, %._crit_edge.loopexit ], [ 0, %5 ]
  %.0.lcssa = phi ptr [ %30, %._crit_edge.loopexit ], [ %20, %5 ]
  %32 = getelementptr inbounds i8, ptr %0, i64 64
  %33 = getelementptr inbounds [5 x %struct.SM3_LMS_KEY], ptr %32, i64 0, i64 %.031.lcssa
  %34 = call i32 @sm3_lms_do_sign(ptr noundef nonnull %33, ptr noundef %1, ptr noundef %2, ptr noundef nonnull %7)
  %.not = icmp eq i32 %34, 1
  br i1 %.not, label %38, label %35

35:                                               ; preds = %._crit_edge
  %36 = load ptr, ptr @stderr, align 8, !tbaa !17
  %37 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %36, ptr noundef nonnull @.str.14, ptr noundef nonnull @.str.15, i32 noundef 1057, ptr noundef nonnull @__FUNCTION__.sm3_hss_sign) #5
  br label %45

38:                                               ; preds = %._crit_edge
  %39 = call i32 @sm3_lms_signature_to_bytes(ptr noundef nonnull %7, ptr noundef nonnull %.0.lcssa, ptr noundef nonnull %6)
  %40 = load i64, ptr %6, align 8, !tbaa !13
  %41 = getelementptr inbounds i8, ptr %.0.lcssa, i64 %40
  %42 = ptrtoint ptr %41 to i64
  %43 = ptrtoint ptr %3 to i64
  %44 = sub i64 %42, %43
  store i64 %44, ptr %4, align 8, !tbaa !13
  br label %45

45:                                               ; preds = %38, %35
  %.032 = phi i32 [ -1, %35 ], [ 1, %38 ]
  call void @llvm.lifetime.end.p0(i64 1932, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #4
  ret i32 %.032
}

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !8, i64 0}
!6 = !{!"", !7, i64 0, !9, i64 64, !9, i64 584}
!7 = !{!"", !8, i64 0, !11, i64 4}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"", !9, i64 0, !9, i64 4, !9, i64 8, !9, i64 24}
!12 = !{!9, !9, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !9, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!18, !18, i64 0}
!18 = !{!"any pointer", !9, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass