; LLVM IR from intel/cryptography-primitives :: sources/ippcp/crypto_mb/src/sm4 (sm4_ecb_dec_mb16)

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

; Function Attrs: nounwind uwtable
define dso_local i64 @mbx_sm4_decrypt_ecb_mb16(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca i16, align 2
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  store i64 0, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %12) #3
  store i16 -1, ptr %12, align 2, !tbaa !11
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = icmp eq ptr null, %14
  br i1 %15, label %25, label %16

16:                                               ; preds = %4
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = icmp eq ptr null, %17
  br i1 %18, label %25, label %19

19:                                               ; preds = %16
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  %21 = icmp eq ptr null, %20
  br i1 %21, label %25, label %22

22:                                               ; preds = %19
  %23 = load ptr, ptr %9, align 8, !tbaa !5
  %24 = icmp eq ptr null, %23
  br i1 %24, label %25, label %28

25:                                               ; preds = %22, %19, %16, %4
  %26 = call i64 @MBX_SET_STS16_ALL(i64 noundef 2)
  store i64 %26, ptr %11, align 8, !tbaa !9
  %27 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %27, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %91

28:                                               ; preds = %22
  store i32 0, ptr %10, align 4, !tbaa !13
  br label %29

29:                                               ; preds = %84, %28
  %30 = load i32, ptr %10, align 4, !tbaa !13
  %31 = icmp slt i32 %30, 16
  br i1 %31, label %32, label %87

32:                                               ; preds = %29
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load i32, ptr %10, align 4, !tbaa !13
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds ptr, ptr %33, i64 %35
  %37 = load ptr, ptr %36, align 8, !tbaa !5
  %38 = icmp eq ptr %37, null
  br i1 %38, label %46, label %39

39:                                               ; preds = %32
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = load i32, ptr %10, align 4, !tbaa !13
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds ptr, ptr %40, i64 %42
  %44 = load ptr, ptr %43, align 8, !tbaa !5
  %45 = icmp eq ptr %44, null
  br i1 %45, label %46, label %57

46:                                               ; preds = %39, %32
  %47 = load i64, ptr %11, align 8, !tbaa !9
  %48 = load i32, ptr %10, align 4, !tbaa !13
  %49 = call i64 @MBX_SET_STS16(i64 noundef %47, i32 noundef %48, i64 noundef 2)
  store i64 %49, ptr %11, align 8, !tbaa !9
  %50 = load i32, ptr %10, align 4, !tbaa !13
  %51 = shl i32 1, %50
  %52 = xor i32 %51, -1
  %53 = load i16, ptr %12, align 2, !tbaa !11
  %54 = zext i16 %53 to i32
  %55 = and i32 %54, %52
  %56 = trunc i32 %55 to i16
  store i16 %56, ptr %12, align 2, !tbaa !11
  br label %57

57:                                               ; preds = %46, %39
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = load i32, ptr %10, align 4, !tbaa !13
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i32, ptr %58, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !13
  %63 = icmp slt i32 %62, 0
  br i1 %63, label %72, label %64

64:                                               ; preds = %57
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = load i32, ptr %10, align 4, !tbaa !13
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds i32, ptr %65, i64 %67
  %69 = load i32, ptr %68, align 4, !tbaa !13
  %70 = and i32 %69, 15
  %71 = icmp ne i32 %70, 0
  br i1 %71, label %72, label %83

72:                                               ; preds = %64, %57
  %73 = load i64, ptr %11, align 8, !tbaa !9
  %74 = load i32, ptr %10, align 4, !tbaa !13
  %75 = call i64 @MBX_SET_STS16(i64 noundef %73, i32 noundef %74, i64 noundef 1)
  store i64 %75, ptr %11, align 8, !tbaa !9
  %76 = load i32, ptr %10, align 4, !tbaa !13
  %77 = shl i32 1, %76
  %78 = xor i32 %77, -1
  %79 = load i16, ptr %12, align 2, !tbaa !11
  %80 = zext i16 %79 to i32
  %81 = and i32 %80, %78
  %82 = trunc i32 %81 to i16
  store i16 %82, ptr %12, align 2, !tbaa !11
  br label %83

83:                                               ; preds = %72, %64
  br label %84

84:                                               ; preds = %83
  %85 = load i32, ptr %10, align 4, !tbaa !13
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %10, align 4, !tbaa !13
  br label %29, !llvm.loop !15

87:                                               ; preds = %29
  %88 = load i16, ptr %12, align 2, !tbaa !11
  %89 = call i64 @MBX_SET_STS16_ALL(i64 noundef 10)
  store i64 %89, ptr %11, align 8, !tbaa !9
  %90 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %90, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %91

91:                                               ; preds = %87, %25
  call void @llvm.lifetime.end.p0(i64 2, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %92 = load i64, ptr %5, align 8
  ret i64 %92
}

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

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i64 @MBX_SET_STS16_ALL(i64 noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i64 @MBX_SET_STS16(i64 noundef, i32 noundef, i64 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 = { alwaysinline 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 #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 long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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 i64 @mbx_sm4_decrypt_ecb_mb16(ptr noundef readonly %0, ptr noundef readonly %1, ptr noundef readonly %2, ptr noundef readnone %3) local_unnamed_addr #0 {
  %5 = insertelement <4 x ptr> poison, ptr %0, i64 0
  %6 = insertelement <4 x ptr> %5, ptr %1, i64 1
  %7 = insertelement <4 x ptr> %6, ptr %2, i64 2
  %8 = insertelement <4 x ptr> %7, ptr %3, i64 3
  %9 = icmp eq <4 x ptr> %8, zeroinitializer
  %10 = bitcast <4 x i1> %9 to i4
  %.not = icmp eq i4 %10, 0
  br i1 %.not, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %4, %28
  %indvars.iv = phi i64 [ %indvars.iv.next, %28 ], [ 0, %4 ]
  %.03041 = phi i64 [ %.232, %28 ], [ 0, %4 ]
  %11 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %18, label %14

14:                                               ; preds = %.preheader
  %15 = getelementptr inbounds ptr, ptr %1, i64 %indvars.iv
  %16 = load ptr, ptr %15, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %21

18:                                               ; preds = %14, %.preheader
  %19 = trunc nuw nsw i64 %indvars.iv to i32
  %20 = tail call i64 @MBX_SET_STS16(i64 noundef %.03041, i32 noundef %19, i64 noundef 2)
  br label %21

21:                                               ; preds = %18, %14
  %.131 = phi i64 [ %20, %18 ], [ %.03041, %14 ]
  %22 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv
  %23 = load i32, ptr %22, align 4, !tbaa !9
  %24 = and i32 %23, -2147483633
  %or.cond39 = icmp eq i32 %24, 0
  br i1 %or.cond39, label %28, label %25

25:                                               ; preds = %21
  %26 = trunc nuw nsw i64 %indvars.iv to i32
  %27 = tail call i64 @MBX_SET_STS16(i64 noundef %.131, i32 noundef %26, i64 noundef 1)
  br label %28

28:                                               ; preds = %21, %25
  %.232 = phi i64 [ %27, %25 ], [ %.131, %21 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 16
  br i1 %exitcond.not, label %.loopexit, label %.preheader, !llvm.loop !11

.loopexit:                                        ; preds = %28, %4
  %.sink = phi i64 [ 2, %4 ], [ 10, %28 ]
  %29 = tail call i64 @MBX_SET_STS16_ALL(i64 noundef %.sink)
  ret i64 %29
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i64 @MBX_SET_STS16_ALL(i64 noundef) local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i64 @MBX_SET_STS16(i64 noundef, i32 noundef, i64 noundef) local_unnamed_addr #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 = { alwaysinline 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" }

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


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalDCEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass