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

; 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 }
%struct.SM3_KDF_CTX = type { %struct.SM3_CTX, i64 }

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

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

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

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

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

43:                                               ; preds = %17
  br label %44

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

55:                                               ; preds = %14
  call void @llvm.memset.p0.i64(ptr align 8 %5, i8 0, i64 112, i1 false)
  %56 = getelementptr inbounds [32 x i8], ptr %8, 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 %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 112, ptr %5) #5
  ret void
}

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

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

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

attributes #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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !12, i64 112}
!10 = !{!"", !11, i64 0, !12, i64 112}
!11 = !{!"", !7, i64 0, !12, i64 32, !7, i64 40, !12, i64 104}
!12 = !{!"long", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!7, !7, i64 0}
!17 = !{i64 0, i64 32, !16, i64 32, i64 8, !13, i64 40, i64 64, !16, i64 104, i64 8, !13}
!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_CTX = type { [8 x i32], i64, [64 x i8], i64 }

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

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

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

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

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #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

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

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

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

attributes #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 = { 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 112}
!6 = !{!"", !7, i64 0, !10, i64 112}
!7 = !{!"", !8, i64 0, !10, i64 32, !8, i64 40, !10, i64 104}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!8, !8, i64 0}
!12 = !{i64 0, i64 32, !11, i64 32, i64 8, !13, i64 40, i64 64, !11, i64 104, i64 8, !13}
!13 = !{!10, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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