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

; Function Attrs: nounwind uwtable
define dso_local i32 @sm2_kdf(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca %struct.SM3_CTX, align 8
  %10 = alloca [4 x i8], align 1
  %11 = alloca [32 x i8], align 16
  %12 = alloca i32, align 4
  %13 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 112, ptr %9) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr %11) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  store i32 1, ptr %12, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  br label %14

14:                                               ; preds = %44, %4
  %15 = load i64, ptr %7, align 8, !tbaa !9
  %16 = icmp ne i64 %15, 0
  br i1 %16, label %17, label %55

17:                                               ; preds = %14
  %18 = load i32, ptr %12, align 4, !tbaa !11
  %19 = lshr i32 %18, 24
  %20 = trunc i32 %19 to i8
  %21 = getelementptr inbounds [4 x i8], ptr %10, i64 0, i64 0
  store i8 %20, ptr %21, align 1, !tbaa !13
  %22 = load i32, ptr %12, align 4, !tbaa !11
  %23 = lshr i32 %22, 16
  %24 = trunc i32 %23 to i8
  %25 = getelementptr inbounds [4 x i8], ptr %10, i64 0, i64 1
  store i8 %24, ptr %25, align 1, !tbaa !13
  %26 = load i32, ptr %12, align 4, !tbaa !11
  %27 = lshr i32 %26, 8
  %28 = trunc i32 %27 to i8
  %29 = getelementptr inbounds [4 x i8], ptr %10, i64 0, i64 2
  store i8 %28, ptr %29, align 1, !tbaa !13
  %30 = load i32, ptr %12, align 4, !tbaa !11
  %31 = trunc i32 %30 to i8
  %32 = getelementptr inbounds [4 x i8], ptr %10, i64 0, i64 3
  store i8 %31, ptr %32, align 1, !tbaa !13
  %33 = load i32, ptr %12, align 4, !tbaa !11
  %34 = add i32 %33, 1
  store i32 %34, ptr %12, align 4, !tbaa !11
  call void @sm3_init(ptr noundef %9)
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = load i64, ptr %6, align 8, !tbaa !9
  call void @sm3_update(ptr noundef %9, ptr noundef %35, i64 noundef %36)
  %37 = getelementptr inbounds [4 x i8], ptr %10, i64 0, i64 0
  call void @sm3_update(ptr noundef %9, ptr noundef %37, i64 noundef 4)
  %38 = getelementptr inbounds [32 x i8], ptr %11, i64 0, i64 0
  call void @sm3_finish(ptr noundef %9, ptr noundef %38)
  %39 = load i64, ptr %7, align 8, !tbaa !9
  %40 = icmp ult i64 %39, 32
  br i1 %40, label %41, label %43

41:                                               ; preds = %17
  %42 = load i64, ptr %7, align 8, !tbaa !9
  br label %44

43:                                               ; preds = %17
  br label %44

44:                                               ; preds = %43, %41
  %45 = phi i64 [ %42, %41 ], [ 32, %43 ]
  store i64 %45, ptr %13, align 8, !tbaa !9
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = getelementptr inbounds [32 x i8], ptr %11, i64 0, i64 0
  %48 = load i64, ptr %13, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %46, ptr align 16 %47, i64 %48, i1 false)
  %49 = load i64, ptr %13, align 8, !tbaa !9
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = getelementptr inbounds i8, ptr %50, i64 %49
  store ptr %51, ptr %8, align 8, !tbaa !5
  %52 = load i64, ptr %13, align 8, !tbaa !9
  %53 = load i64, ptr %7, align 8, !tbaa !9
  %54 = sub i64 %53, %52
  store i64 %54, ptr %7, align 8, !tbaa !9
  br label %14, !llvm.loop !14

55:                                               ; preds = %14
  call void @llvm.memset.p0.i64(ptr align 8 %9, i8 0, i64 112, i1 false)
  %56 = getelementptr inbounds [32 x i8], ptr %11, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %56, i8 0, i64 32, i1 false)
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 112, ptr %9) #5
  ret i32 1
}

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

declare void @sm3_init(ptr noundef) #2

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

declare void @sm3_finish(ptr noundef, ptr noundef) #2

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

; 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 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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_CTX = type { [8 x i32], i64, [64 x i8], i64 }

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @sm2_kdf(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = alloca %struct.SM3_CTX, align 8
  %6 = alloca [4 x i8], align 1
  %7 = alloca [32 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %5) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %7) #5
  %.not17 = icmp eq i64 %2, 0
  br i1 %.not17, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %8 = getelementptr inbounds i8, ptr %6, i64 1
  %9 = getelementptr inbounds i8, ptr %6, i64 2
  %10 = getelementptr inbounds i8, ptr %6, i64 3
  br label %11

11:                                               ; preds = %.lr.ph, %11
  %.020 = phi i64 [ %2, %.lr.ph ], [ %22, %11 ]
  %.01519 = phi i32 [ 1, %.lr.ph ], [ %19, %11 ]
  %.01618 = phi ptr [ %3, %.lr.ph ], [ %21, %11 ]
  %12 = lshr i32 %.01519, 24
  %13 = trunc nuw i32 %12 to i8
  store i8 %13, ptr %6, align 1, !tbaa !5
  %14 = lshr i32 %.01519, 16
  %15 = trunc i32 %14 to i8
  store i8 %15, ptr %8, align 1, !tbaa !5
  %16 = lshr i32 %.01519, 8
  %17 = trunc i32 %16 to i8
  store i8 %17, ptr %9, align 1, !tbaa !5
  %18 = trunc i32 %.01519 to i8
  store i8 %18, ptr %10, align 1, !tbaa !5
  %19 = add i32 %.01519, 1
  call void @sm3_init(ptr noundef nonnull %5) #5
  call void @sm3_update(ptr noundef nonnull %5, ptr noundef %0, i64 noundef %1) #5
  call void @sm3_update(ptr noundef nonnull %5, ptr noundef nonnull %6, i64 noundef 4) #5
  call void @sm3_finish(ptr noundef nonnull %5, ptr noundef nonnull %7) #5
  %20 = call i64 @llvm.umin.i64(i64 %.020, i64 32)
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.01618, ptr nonnull align 16 %7, i64 %20, i1 false)
  %21 = getelementptr inbounds i8, ptr %.01618, i64 %20
  %22 = sub i64 %.020, %20
  %.not = icmp eq i64 %22, 0
  br i1 %.not, label %._crit_edge, label %11, !llvm.loop !8

._crit_edge:                                      ; preds = %11, %4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #5
  call void @llvm.lifetime.end.p0(i64 112, ptr nonnull %5) #5
  ret i32 1
}

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

declare void @sm3_init(ptr noundef) local_unnamed_addr #2

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

declare void @sm3_finish(ptr noundef, ptr noundef) local_unnamed_addr #2

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #4

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass