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

; 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.SM4_CBC_MAC_CTX = type { %struct.SM4_KEY, [16 x i8], i64 }
%struct.SM4_KEY = type { [32 x i32] }

; Function Attrs: nounwind uwtable
define dso_local void @sm4_cbc_mac_update(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  br label %9

9:                                                ; preds = %62, %3
  %10 = load i64, ptr %6, align 8, !tbaa !9
  %11 = icmp ne i64 %10, 0
  br i1 %11, label %12, label %69

12:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %13, i32 0, i32 2
  %15 = load i64, ptr %14, align 8, !tbaa !11
  %16 = sub i64 16, %15
  store i64 %16, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %17 = load i64, ptr %6, align 8, !tbaa !9
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = icmp ult i64 %17, %18
  br i1 %19, label %20, label %22

20:                                               ; preds = %12
  %21 = load i64, ptr %6, align 8, !tbaa !9
  br label %24

22:                                               ; preds = %12
  %23 = load i64, ptr %7, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %22, %20
  %25 = phi i64 [ %21, %20 ], [ %23, %22 ]
  store i64 %25, ptr %8, align 8, !tbaa !9
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %26, i32 0, i32 1
  %28 = getelementptr inbounds [16 x i8], ptr %27, i64 0, i64 0
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %29, i32 0, i32 2
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = getelementptr inbounds i8, ptr %28, i64 %31
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %33, i32 0, i32 1
  %35 = getelementptr inbounds [16 x i8], ptr %34, i64 0, i64 0
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %36, i32 0, i32 2
  %38 = load i64, ptr %37, align 8, !tbaa !11
  %39 = getelementptr inbounds i8, ptr %35, i64 %38
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = load i64, ptr %8, align 8, !tbaa !9
  call void @gmssl_memxor(ptr noundef %32, ptr noundef %39, ptr noundef %40, i64 noundef %41)
  %42 = load i64, ptr %8, align 8, !tbaa !9
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %43, i32 0, i32 2
  %45 = load i64, ptr %44, align 8, !tbaa !11
  %46 = add i64 %45, %42
  store i64 %46, ptr %44, align 8, !tbaa !11
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %47, i32 0, i32 2
  %49 = load i64, ptr %48, align 8, !tbaa !11
  %50 = icmp uge i64 %49, 16
  br i1 %50, label %51, label %62

51:                                               ; preds = %24
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %54, i32 0, i32 1
  %56 = getelementptr inbounds [16 x i8], ptr %55, i64 0, i64 0
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %57, i32 0, i32 1
  %59 = getelementptr inbounds [16 x i8], ptr %58, i64 0, i64 0
  call void @sm4_encrypt(ptr noundef %53, ptr noundef %56, ptr noundef %59)
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.SM4_CBC_MAC_CTX, ptr %60, i32 0, i32 2
  store i64 0, ptr %61, align 8, !tbaa !11
  br label %62

62:                                               ; preds = %51, %24
  %63 = load i64, ptr %8, align 8, !tbaa !9
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = getelementptr inbounds i8, ptr %64, i64 %63
  store ptr %65, ptr %5, align 8, !tbaa !5
  %66 = load i64, ptr %8, align 8, !tbaa !9
  %67 = load i64, ptr %6, align 8, !tbaa !9
  %68 = sub i64 %67, %66
  store i64 %68, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %9, !llvm.loop !14

69:                                               ; preds = %9
  ret void
}

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

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

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

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 = { 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, !10, i64 144}
!12 = !{!"", !13, i64 0, !7, i64 128, !10, i64 144}
!13 = !{!"", !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"

; Function Attrs: nounwind uwtable
define dso_local void @sm4_cbc_mac_update(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not23 = icmp eq i64 %2, 0
  br i1 %.not23, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %4 = getelementptr inbounds i8, ptr %0, i64 144
  %5 = getelementptr inbounds i8, ptr %0, i64 128
  %.pre = load i64, ptr %4, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %.lr.ph, %15
  %7 = phi i64 [ %.pre, %.lr.ph ], [ %16, %15 ]
  %.025 = phi ptr [ %1, %.lr.ph ], [ %17, %15 ]
  %.02224 = phi i64 [ %2, %.lr.ph ], [ %18, %15 ]
  %8 = sub i64 16, %7
  %9 = tail call i64 @llvm.umin.i64(i64 %.02224, i64 %8)
  %10 = getelementptr inbounds i8, ptr %5, i64 %7
  tail call void @gmssl_memxor(ptr noundef nonnull %10, ptr noundef nonnull %10, ptr noundef %.025, i64 noundef %9) #3
  %11 = load i64, ptr %4, align 8, !tbaa !5
  %12 = add i64 %11, %9
  store i64 %12, ptr %4, align 8, !tbaa !5
  %13 = icmp ugt i64 %12, 15
  br i1 %13, label %14, label %15

14:                                               ; preds = %6
  tail call void @sm4_encrypt(ptr noundef nonnull %0, ptr noundef nonnull %5, ptr noundef nonnull %5) #3
  store i64 0, ptr %4, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %14, %6
  %16 = phi i64 [ 0, %14 ], [ %12, %6 ]
  %17 = getelementptr inbounds i8, ptr %.025, i64 %9
  %18 = sub i64 %.02224, %9
  %.not = icmp eq i64 %18, 0
  br i1 %.not, label %._crit_edge, label %6, !llvm.loop !11

._crit_edge:                                      ; preds = %15, %3
  ret void
}

declare void @gmssl_memxor(ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare void @sm4_encrypt(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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

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 = { "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 speculatable willreturn memory(none) }
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, !10, i64 144}
!6 = !{!"", !7, i64 0, !8, i64 128, !10, i64 144}
!7 = !{!"", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass