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

@__const.sm3_lmots_derive_secrets.jbytes = external hidden unnamed_addr constant [1 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @sm3_lmots_derive_secrets(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca %struct.SHA256_CTX, align 8
  %10 = alloca [4 x i8], align 1
  %11 = alloca [2 x i8], align 1
  %12 = alloca [1 x i8], align 1
  %13 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 112, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %12, ptr align 1 @__const.sm3_lmots_derive_secrets.jbytes, i64 1, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = ashr i32 %14, 24
  %16 = trunc i32 %15 to i8
  %17 = getelementptr inbounds [4 x i8], ptr %10, i64 0, i64 0
  store i8 %16, ptr %17, align 1, !tbaa !11
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = ashr i32 %18, 16
  %20 = trunc i32 %19 to i8
  %21 = getelementptr inbounds [4 x i8], ptr %10, i64 0, i64 1
  store i8 %20, ptr %21, align 1, !tbaa !11
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = ashr i32 %22, 8
  %24 = trunc i32 %23 to i8
  %25 = getelementptr inbounds [4 x i8], ptr %10, i64 0, i64 2
  store i8 %24, ptr %25, align 1, !tbaa !11
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = trunc i32 %26 to i8
  %28 = getelementptr inbounds [4 x i8], ptr %10, i64 0, i64 3
  store i8 %27, ptr %28, align 1, !tbaa !11
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %50, %4
  %30 = load i32, ptr %13, align 4, !tbaa !9
  %31 = icmp slt i32 %30, 34
  br i1 %31, label %32, label %53

32:                                               ; preds = %29
  %33 = load i32, ptr %13, align 4, !tbaa !9
  %34 = ashr i32 %33, 8
  %35 = trunc i32 %34 to i8
  %36 = getelementptr inbounds [2 x i8], ptr %11, i64 0, i64 0
  store i8 %35, ptr %36, align 1, !tbaa !11
  %37 = load i32, ptr %13, align 4, !tbaa !9
  %38 = trunc i32 %37 to i8
  %39 = getelementptr inbounds [2 x i8], ptr %11, i64 0, i64 1
  store i8 %38, ptr %39, align 1, !tbaa !11
  call void @sha256_init(ptr noundef %9)
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  call void @sha256_update(ptr noundef %9, ptr noundef %40, i64 noundef 16)
  %41 = getelementptr inbounds [4 x i8], ptr %10, i64 0, i64 0
  call void @sha256_update(ptr noundef %9, ptr noundef %41, i64 noundef 4)
  %42 = getelementptr inbounds [2 x i8], ptr %11, i64 0, i64 0
  call void @sha256_update(ptr noundef %9, ptr noundef %42, i64 noundef 2)
  %43 = getelementptr inbounds [1 x i8], ptr %12, i64 0, i64 0
  call void @sha256_update(ptr noundef %9, ptr noundef %43, i64 noundef 1)
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  call void @sha256_update(ptr noundef %9, ptr noundef %44, i64 noundef 32)
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = load i32, ptr %13, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [32 x i8], ptr %45, i64 %47
  %49 = getelementptr inbounds [32 x i8], ptr %48, i64 0, i64 0
  call void @sha256_finish(ptr noundef %9, ptr noundef %49)
  br label %50

50:                                               ; preds = %32
  %51 = load i32, ptr %13, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %13, align 4, !tbaa !9
  br label %29, !llvm.loop !12

53:                                               ; preds = %29
  call void @gmssl_secure_clear(ptr noundef %9, i64 noundef 112)
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 112, ptr %9) #4
  ret void
}

; 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

declare void @sha256_init(ptr noundef) #3

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

declare void @sha256_finish(ptr noundef, ptr noundef) #3

declare void @gmssl_secure_clear(ptr noundef, i64 noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 }

@__const.sm3_lmots_derive_secrets.jbytes = external hidden unnamed_addr constant [1 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @sm3_lmots_derive_secrets(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = alloca %struct.SHA256_CTX, align 8
  %6 = alloca [4 x i8], align 1
  %7 = alloca [2 x i8], align 1
  %8 = alloca [1 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %7) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %8) #3
  %9 = load i8, ptr @__const.sm3_lmots_derive_secrets.jbytes, align 1
  store i8 %9, ptr %8, align 1
  %10 = lshr i32 %2, 24
  %11 = trunc nuw i32 %10 to i8
  store i8 %11, ptr %6, align 1, !tbaa !5
  %12 = lshr i32 %2, 16
  %13 = trunc i32 %12 to i8
  %14 = getelementptr inbounds i8, ptr %6, i64 1
  store i8 %13, ptr %14, align 1, !tbaa !5
  %15 = lshr i32 %2, 8
  %16 = trunc i32 %15 to i8
  %17 = getelementptr inbounds i8, ptr %6, i64 2
  store i8 %16, ptr %17, align 1, !tbaa !5
  %18 = trunc i32 %2 to i8
  %19 = getelementptr inbounds i8, ptr %6, i64 3
  store i8 %18, ptr %19, align 1, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %7, i64 1
  br label %21

21:                                               ; preds = %4, %21
  %indvars.iv = phi i64 [ 0, %4 ], [ %indvars.iv.next, %21 ]
  store i8 0, ptr %7, align 1, !tbaa !5
  %22 = trunc i64 %indvars.iv to i8
  store i8 %22, ptr %20, align 1, !tbaa !5
  call void @sha256_init(ptr noundef nonnull %5) #3
  call void @sha256_update(ptr noundef nonnull %5, ptr noundef %1, i64 noundef 16) #3
  call void @sha256_update(ptr noundef nonnull %5, ptr noundef nonnull %6, i64 noundef 4) #3
  call void @sha256_update(ptr noundef nonnull %5, ptr noundef nonnull %7, i64 noundef 2) #3
  call void @sha256_update(ptr noundef nonnull %5, ptr noundef nonnull %8, i64 noundef 1) #3
  call void @sha256_update(ptr noundef nonnull %5, ptr noundef %0, i64 noundef 32) #3
  %23 = getelementptr inbounds [32 x i8], ptr %3, i64 %indvars.iv
  call void @sha256_finish(ptr noundef nonnull %5, ptr noundef %23) #3
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 34
  br i1 %exitcond.not, label %24, label %21, !llvm.loop !8

24:                                               ; preds = %21
  call void @gmssl_secure_clear(ptr noundef nonnull %5, i64 noundef 112) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %8) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 112, ptr nonnull %5) #3
  ret void
}

; 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

declare void @gmssl_secure_clear(ptr noundef, i64 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

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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,SROAPass,SimplifyCFGPass