; 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.SHA256_CTX = type { [8 x i32], i64, [64 x i8], i64 }

@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
@D_LEAF = external hidden constant [2 x i8], align 1
@D_INTR = external hidden constant [2 x i8], align 1
@__FUNCTION__.sm3_lms_signature_to_merkle_root = external hidden unnamed_addr constant [33 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

declare void @sha256_init(ptr noundef) #2

declare void @sha256_update(ptr noundef, ptr noundef, i64 noundef) #2

declare void @sha256_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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sm3_lms_signature_to_merkle_root(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5, ptr noundef %6) #3 {
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = alloca [4 x i8], align 1
  %19 = alloca %struct.SHA256_CTX, align 8
  %20 = alloca i32, align 4
  %21 = alloca i32, align 4
  store ptr %0, ptr %9, align 8, !tbaa !5
  store i32 %1, ptr %10, align 4, !tbaa !9
  store i32 %2, ptr %11, align 4, !tbaa !9
  store ptr %3, ptr %12, align 8, !tbaa !5
  store ptr %4, ptr %13, align 8, !tbaa !5
  store ptr %5, ptr %14, align 8, !tbaa !5
  store ptr %6, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %18) #4
  call void @llvm.lifetime.start.p0(i64 112, ptr %19) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %20) #4
  %22 = load i32, ptr %10, align 4, !tbaa !9
  %23 = shl i32 1, %22
  store i32 %23, ptr %16, align 4, !tbaa !9
  %24 = load i32, ptr %11, align 4, !tbaa !9
  %25 = load i32, ptr %16, align 4, !tbaa !9
  %26 = icmp sge i32 %24, %25
  br i1 %26, label %27, label %33

27:                                               ; preds = %7
  br label %28

28:                                               ; preds = %27
  %29 = load ptr, ptr @stderr, align 8, !tbaa !5
  %30 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %29, ptr noundef @.str.14, ptr noundef @.str.15, i32 noundef 763, ptr noundef @__FUNCTION__.sm3_lms_signature_to_merkle_root) #4
  br label %31

31:                                               ; preds = %28
  br label %32

32:                                               ; preds = %31
  store i32 -1, ptr %8, align 4
  store i32 1, ptr %21, align 4
  br label %112

33:                                               ; preds = %7
  %34 = load i32, ptr %16, align 4, !tbaa !9
  %35 = load i32, ptr %11, align 4, !tbaa !9
  %36 = add nsw i32 %34, %35
  store i32 %36, ptr %17, align 4, !tbaa !9
  %37 = load i32, ptr %17, align 4, !tbaa !9
  %38 = ashr i32 %37, 24
  %39 = trunc i32 %38 to i8
  %40 = getelementptr inbounds [4 x i8], ptr %18, i64 0, i64 0
  store i8 %39, ptr %40, align 1, !tbaa !11
  %41 = load i32, ptr %17, align 4, !tbaa !9
  %42 = ashr i32 %41, 16
  %43 = trunc i32 %42 to i8
  %44 = getelementptr inbounds [4 x i8], ptr %18, i64 0, i64 1
  store i8 %43, ptr %44, align 1, !tbaa !11
  %45 = load i32, ptr %17, align 4, !tbaa !9
  %46 = ashr i32 %45, 8
  %47 = trunc i32 %46 to i8
  %48 = getelementptr inbounds [4 x i8], ptr %18, i64 0, i64 2
  store i8 %47, ptr %48, align 1, !tbaa !11
  %49 = load i32, ptr %17, align 4, !tbaa !9
  %50 = trunc i32 %49 to i8
  %51 = getelementptr inbounds [4 x i8], ptr %18, i64 0, i64 3
  store i8 %50, ptr %51, align 1, !tbaa !11
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = load i32, ptr %11, align 4, !tbaa !9
  %54 = load ptr, ptr %12, align 8, !tbaa !5
  %55 = load ptr, ptr %14, align 8, !tbaa !5
  %56 = load ptr, ptr %15, align 8, !tbaa !5
  call void @sm3_lmots_signature_to_public_hash(ptr noundef %52, i32 noundef %53, ptr noundef %54, ptr noundef %55, ptr noundef %56)
  call void @sha256_init(ptr noundef %19)
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  call void @sha256_update(ptr noundef %19, ptr noundef %57, i64 noundef 16)
  %58 = getelementptr inbounds [4 x i8], ptr %18, i64 0, i64 0
  call void @sha256_update(ptr noundef %19, ptr noundef %58, i64 noundef 4)
  call void @sha256_update(ptr noundef %19, ptr noundef @D_LEAF, i64 noundef 2)
  %59 = load ptr, ptr %15, align 8, !tbaa !5
  call void @sha256_update(ptr noundef %19, ptr noundef %59, i64 noundef 32)
  %60 = load ptr, ptr %15, align 8, !tbaa !5
  call void @sha256_finish(ptr noundef %19, ptr noundef %60)
  store i32 0, ptr %20, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %108, %33
  %62 = load i32, ptr %20, align 4, !tbaa !9
  %63 = load i32, ptr %10, align 4, !tbaa !9
  %64 = icmp slt i32 %62, %63
  br i1 %64, label %65, label %111

65:                                               ; preds = %61
  %66 = load i32, ptr %17, align 4, !tbaa !9
  %67 = sdiv i32 %66, 2
  %68 = ashr i32 %67, 24
  %69 = trunc i32 %68 to i8
  %70 = getelementptr inbounds [4 x i8], ptr %18, i64 0, i64 0
  store i8 %69, ptr %70, align 1, !tbaa !11
  %71 = load i32, ptr %17, align 4, !tbaa !9
  %72 = sdiv i32 %71, 2
  %73 = ashr i32 %72, 16
  %74 = trunc i32 %73 to i8
  %75 = getelementptr inbounds [4 x i8], ptr %18, i64 0, i64 1
  store i8 %74, ptr %75, align 1, !tbaa !11
  %76 = load i32, ptr %17, align 4, !tbaa !9
  %77 = sdiv i32 %76, 2
  %78 = ashr i32 %77, 8
  %79 = trunc i32 %78 to i8
  %80 = getelementptr inbounds [4 x i8], ptr %18, i64 0, i64 2
  store i8 %79, ptr %80, align 1, !tbaa !11
  %81 = load i32, ptr %17, align 4, !tbaa !9
  %82 = sdiv i32 %81, 2
  %83 = trunc i32 %82 to i8
  %84 = getelementptr inbounds [4 x i8], ptr %18, i64 0, i64 3
  store i8 %83, ptr %84, align 1, !tbaa !11
  call void @sha256_init(ptr noundef %19)
  %85 = load ptr, ptr %9, align 8, !tbaa !5
  call void @sha256_update(ptr noundef %19, ptr noundef %85, i64 noundef 16)
  %86 = getelementptr inbounds [4 x i8], ptr %18, i64 0, i64 0
  call void @sha256_update(ptr noundef %19, ptr noundef %86, i64 noundef 4)
  call void @sha256_update(ptr noundef %19, ptr noundef @D_INTR, i64 noundef 2)
  %87 = load i32, ptr %17, align 4, !tbaa !9
  %88 = and i32 %87, 1
  %89 = icmp ne i32 %88, 0
  br i1 %89, label %90, label %97

90:                                               ; preds = %65
  %91 = load ptr, ptr %13, align 8, !tbaa !5
  %92 = load i32, ptr %20, align 4, !tbaa !9
  %93 = sext i32 %92 to i64
  %94 = getelementptr inbounds [32 x i8], ptr %91, i64 %93
  %95 = getelementptr inbounds [32 x i8], ptr %94, i64 0, i64 0
  call void @sha256_update(ptr noundef %19, ptr noundef %95, i64 noundef 32)
  %96 = load ptr, ptr %15, align 8, !tbaa !5
  call void @sha256_update(ptr noundef %19, ptr noundef %96, i64 noundef 32)
  br label %104

97:                                               ; preds = %65
  %98 = load ptr, ptr %15, align 8, !tbaa !5
  call void @sha256_update(ptr noundef %19, ptr noundef %98, i64 noundef 32)
  %99 = load ptr, ptr %13, align 8, !tbaa !5
  %100 = load i32, ptr %20, align 4, !tbaa !9
  %101 = sext i32 %100 to i64
  %102 = getelementptr inbounds [32 x i8], ptr %99, i64 %101
  %103 = getelementptr inbounds [32 x i8], ptr %102, i64 0, i64 0
  call void @sha256_update(ptr noundef %19, ptr noundef %103, i64 noundef 32)
  br label %104

104:                                              ; preds = %97, %90
  %105 = load ptr, ptr %15, align 8, !tbaa !5
  call void @sha256_finish(ptr noundef %19, ptr noundef %105)
  %106 = load i32, ptr %17, align 4, !tbaa !9
  %107 = sdiv i32 %106, 2
  store i32 %107, ptr %17, align 4, !tbaa !9
  br label %108

108:                                              ; preds = %104
  %109 = load i32, ptr %20, align 4, !tbaa !9
  %110 = add nsw i32 %109, 1
  store i32 %110, ptr %20, align 4, !tbaa !9
  br label %61, !llvm.loop !12

111:                                              ; preds = %61
  store i32 1, ptr %8, align 4
  store i32 1, ptr %21, align 4
  br label %112

112:                                              ; preds = %111, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #4
  call void @llvm.lifetime.end.p0(i64 112, ptr %19) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  %113 = load i32, ptr %8, align 4
  ret i32 %113
}

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 = { "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 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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.SHA256_CTX = type { [8 x i32], i64, [64 x i8], i64 }

@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
@D_LEAF = external hidden constant [2 x i8], align 1
@D_INTR = external hidden constant [2 x i8], align 1
@__FUNCTION__.sm3_lms_signature_to_merkle_root = external hidden unnamed_addr constant [33 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

declare void @sha256_init(ptr noundef) local_unnamed_addr #2

declare void @sha256_update(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare void @sha256_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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @sm3_lms_signature_to_merkle_root(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5, ptr noundef %6) local_unnamed_addr #3 {
  %8 = alloca [4 x i8], align 1
  %9 = alloca %struct.SHA256_CTX, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %8) #4
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %9) #4
  %10 = shl nuw i32 1, %1
  %.not = icmp sgt i32 %10, %2
  br i1 %.not, label %14, label %11

11:                                               ; preds = %7
  %12 = load ptr, ptr @stderr, align 8, !tbaa !5
  %13 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef nonnull @.str.14, ptr noundef nonnull @.str.15, i32 noundef 763, ptr noundef nonnull @__FUNCTION__.sm3_lms_signature_to_merkle_root) #5
  br label %.loopexit

14:                                               ; preds = %7
  %15 = add nsw i32 %10, %2
  %16 = lshr i32 %15, 24
  %17 = trunc nuw i32 %16 to i8
  store i8 %17, ptr %8, align 1, !tbaa !9
  %18 = lshr i32 %15, 16
  %19 = trunc i32 %18 to i8
  %20 = getelementptr inbounds i8, ptr %8, i64 1
  store i8 %19, ptr %20, align 1, !tbaa !9
  %21 = lshr i32 %15, 8
  %22 = trunc i32 %21 to i8
  %23 = getelementptr inbounds i8, ptr %8, i64 2
  store i8 %22, ptr %23, align 1, !tbaa !9
  %24 = trunc i32 %15 to i8
  %25 = getelementptr inbounds i8, ptr %8, i64 3
  store i8 %24, ptr %25, align 1, !tbaa !9
  tail call void @sm3_lmots_signature_to_public_hash(ptr noundef %0, i32 noundef %2, ptr noundef %3, ptr noundef %5, ptr noundef %6)
  call void @sha256_init(ptr noundef nonnull %9) #4
  call void @sha256_update(ptr noundef nonnull %9, ptr noundef %0, i64 noundef 16) #4
  call void @sha256_update(ptr noundef nonnull %9, ptr noundef nonnull %8, i64 noundef 4) #4
  call void @sha256_update(ptr noundef nonnull %9, ptr noundef nonnull @D_LEAF, i64 noundef 2) #4
  call void @sha256_update(ptr noundef nonnull %9, ptr noundef %6, i64 noundef 32) #4
  call void @sha256_finish(ptr noundef nonnull %9, ptr noundef %6) #4
  %26 = icmp sgt i32 %1, 0
  br i1 %26, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %14
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %40
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %40 ]
  %.03439 = phi i32 [ %15, %.lr.ph.preheader ], [ %27, %40 ]
  %27 = sdiv i32 %.03439, 2
  %28 = lshr i32 %27, 24
  %29 = trunc nuw i32 %28 to i8
  store i8 %29, ptr %8, align 1, !tbaa !9
  %30 = lshr i32 %27, 16
  %31 = trunc i32 %30 to i8
  store i8 %31, ptr %20, align 1, !tbaa !9
  %32 = lshr i32 %27, 8
  %33 = trunc i32 %32 to i8
  store i8 %33, ptr %23, align 1, !tbaa !9
  %34 = trunc i32 %27 to i8
  store i8 %34, ptr %25, align 1, !tbaa !9
  call void @sha256_init(ptr noundef nonnull %9) #4
  call void @sha256_update(ptr noundef nonnull %9, ptr noundef %0, i64 noundef 16) #4
  call void @sha256_update(ptr noundef nonnull %9, ptr noundef nonnull %8, i64 noundef 4) #4
  call void @sha256_update(ptr noundef nonnull %9, ptr noundef nonnull @D_INTR, i64 noundef 2) #4
  %35 = and i32 %.03439, 1
  %.not38 = icmp eq i32 %35, 0
  br i1 %.not38, label %38, label %36

36:                                               ; preds = %.lr.ph
  %37 = getelementptr inbounds [32 x i8], ptr %4, i64 %indvars.iv
  call void @sha256_update(ptr noundef nonnull %9, ptr noundef %37, i64 noundef 32) #4
  br label %40

38:                                               ; preds = %.lr.ph
  call void @sha256_update(ptr noundef nonnull %9, ptr noundef %6, i64 noundef 32) #4
  %39 = getelementptr inbounds [32 x i8], ptr %4, i64 %indvars.iv
  br label %40

40:                                               ; preds = %38, %36
  %.sink = phi ptr [ %39, %38 ], [ %6, %36 ]
  call void @sha256_update(ptr noundef nonnull %9, ptr noundef %.sink, i64 noundef 32) #4
  call void @sha256_finish(ptr noundef nonnull %9, ptr noundef %6) #4
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !10

.loopexit:                                        ; preds = %40, %14, %11
  %.035 = phi i32 [ -1, %11 ], [ 1, %14 ], [ 1, %40 ]
  call void @llvm.lifetime.end.p0(i64 112, ptr nonnull %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %8) #4
  ret i32 %.035
}

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass