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

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

@rearrangeOrder = external hidden constant [16 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local i64 @mbx_sm4_gcm_init_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 i16, align 2
  %14 = 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
  call void @llvm.lifetime.start.p0(i64 2, ptr %13) #3
  store i16 -1, ptr %13, align 2, !tbaa !11
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp eq ptr null, %15
  br i1 %16, label %26, label %17

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

20:                                               ; preds = %17
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = icmp eq ptr null, %21
  br i1 %22, label %26, label %23

23:                                               ; preds = %20
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = icmp eq ptr null, %24
  br i1 %25, label %26, label %29

26:                                               ; preds = %23, %20, %17, %4
  %27 = call i64 @MBX_SET_STS16_ALL(i64 noundef 2)
  store i64 %27, ptr %11, align 8, !tbaa !9
  %28 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %28, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %102

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

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

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

40:                                               ; preds = %33
  %41 = load i64, ptr %11, align 8, !tbaa !9
  %42 = load i32, ptr %10, align 4, !tbaa !13
  %43 = call i64 @MBX_SET_STS16(i64 noundef %41, i32 noundef %42, i64 noundef 2)
  store i64 %43, ptr %11, align 8, !tbaa !9
  %44 = load i32, ptr %10, align 4, !tbaa !13
  %45 = shl i32 1, %44
  %46 = xor i32 %45, -1
  %47 = load i16, ptr %12, align 2, !tbaa !11
  %48 = zext i16 %47 to i32
  %49 = and i32 %48, %46
  %50 = trunc i32 %49 to i16
  store i16 %50, ptr %12, align 2, !tbaa !11
  br label %51

51:                                               ; preds = %40, %33
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = load i32, ptr %10, align 4, !tbaa !13
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds ptr, ptr %52, i64 %54
  %56 = load ptr, ptr %55, align 8, !tbaa !5
  %57 = icmp eq ptr %56, null
  br i1 %57, label %58, label %72

58:                                               ; preds = %51
  %59 = load i64, ptr %11, align 8, !tbaa !9
  %60 = load i32, ptr %10, align 4, !tbaa !13
  %61 = call i64 @MBX_SET_STS16(i64 noundef %59, i32 noundef %60, i64 noundef 2)
  store i64 %61, ptr %11, align 8, !tbaa !9
  %62 = load i32, ptr %10, align 4, !tbaa !13
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [16 x i32], ptr @rearrangeOrder, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !13
  %66 = shl i32 1, %65
  %67 = xor i32 %66, -1
  %68 = load i16, ptr %13, align 2, !tbaa !11
  %69 = zext i16 %68 to i32
  %70 = and i32 %69, %67
  %71 = trunc i32 %70 to i16
  store i16 %71, ptr %13, align 2, !tbaa !11
  br label %72

72:                                               ; preds = %58, %51
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  %74 = load i32, ptr %10, align 4, !tbaa !13
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds i32, ptr %73, i64 %75
  %77 = load i32, ptr %76, align 4, !tbaa !13
  %78 = icmp sle i32 %77, 0
  br i1 %78, label %79, label %93

79:                                               ; preds = %72
  %80 = load i64, ptr %11, align 8, !tbaa !9
  %81 = load i32, ptr %10, align 4, !tbaa !13
  %82 = call i64 @MBX_SET_STS16(i64 noundef %80, i32 noundef %81, i64 noundef 1)
  store i64 %82, ptr %11, align 8, !tbaa !9
  %83 = load i32, ptr %10, align 4, !tbaa !13
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [16 x i32], ptr @rearrangeOrder, i64 0, i64 %84
  %86 = load i32, ptr %85, align 4, !tbaa !13
  %87 = shl i32 1, %86
  %88 = xor i32 %87, -1
  %89 = load i16, ptr %13, align 2, !tbaa !11
  %90 = zext i16 %89 to i32
  %91 = and i32 %90, %88
  %92 = trunc i32 %91 to i16
  store i16 %92, ptr %13, align 2, !tbaa !11
  br label %93

93:                                               ; preds = %79, %72
  br label %94

94:                                               ; preds = %93
  %95 = load i32, ptr %10, align 4, !tbaa !13
  %96 = add nsw i32 %95, 1
  store i32 %96, ptr %10, align 4, !tbaa !13
  br label %30, !llvm.loop !15

97:                                               ; preds = %30
  %98 = load i16, ptr %12, align 2, !tbaa !11
  %99 = load i16, ptr %13, align 2, !tbaa !11
  %100 = call i64 @MBX_SET_STS16_ALL(i64 noundef 10)
  store i64 %100, ptr %11, align 8, !tbaa !9
  %101 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %101, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %102

102:                                              ; preds = %97, %26
  call void @llvm.lifetime.end.p0(i64 2, ptr %13) #3
  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
  %103 = load i64, ptr %5, align 8
  ret i64 %103
}

; 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_gcm_init_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, %31
  %indvars.iv = phi i64 [ %indvars.iv.next, %31 ], [ 0, %4 ]
  %.03544 = phi i64 [ %.3, %31 ], [ 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 %14, label %17

14:                                               ; preds = %.preheader
  %15 = trunc nuw nsw i64 %indvars.iv to i32
  %16 = tail call i64 @MBX_SET_STS16(i64 noundef %.03544, i32 noundef %15, i64 noundef 2)
  br label %17

17:                                               ; preds = %14, %.preheader
  %.136 = phi i64 [ %16, %14 ], [ %.03544, %.preheader ]
  %18 = getelementptr inbounds ptr, ptr %1, i64 %indvars.iv
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %24

21:                                               ; preds = %17
  %22 = trunc nuw nsw i64 %indvars.iv to i32
  %23 = tail call i64 @MBX_SET_STS16(i64 noundef %.136, i32 noundef %22, i64 noundef 2)
  br label %24

24:                                               ; preds = %21, %17
  %.237 = phi i64 [ %23, %21 ], [ %.136, %17 ]
  %25 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = icmp slt i32 %26, 1
  br i1 %27, label %28, label %31

28:                                               ; preds = %24
  %29 = trunc nuw nsw i64 %indvars.iv to i32
  %30 = tail call i64 @MBX_SET_STS16(i64 noundef %.237, i32 noundef %29, i64 noundef 1)
  br label %31

31:                                               ; preds = %24, %28
  %.3 = phi i64 [ %30, %28 ], [ %.237, %24 ]
  %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 = %31, %4
  %.sink = phi i64 [ 2, %4 ], [ 10, %31 ]
  %32 = tail call i64 @MBX_SET_STS16_ALL(i64 noundef %.sink)
  ret i64 %32
}

; 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,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass