; LLVM IR from intel/cryptography-primitives :: sources/ippcp/sms4 (pcpsms4_decrypt_cbc)

; 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._cpSMS4 = type { i32, [32 x i32], [32 x i32] }

; Function Attrs: nounwind uwtable
define dso_local void @cpDecryptSMS4_cbc(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca [8 x i32], align 16
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  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 i32 %3, ptr %9, align 4, !tbaa !9
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %15 = load ptr, ptr %10, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct._cpSMS4, ptr %15, i32 0, i32 2
  %17 = getelementptr inbounds [32 x i32], ptr %16, i64 0, i64 0
  store ptr %17, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %18 = getelementptr inbounds [8 x i32], ptr %12, i64 0, i64 0
  store ptr %18, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %19 = getelementptr inbounds [8 x i32], ptr %12, i64 0, i64 0
  %20 = getelementptr inbounds i32, ptr %19, i64 4
  store ptr %20, ptr %14, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = load ptr, ptr %13, align 8, !tbaa !5
  call void @CopyBlock16(ptr noundef %21, ptr noundef %22)
  br label %23

23:                                               ; preds = %62, %5
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = icmp sgt i32 %24, 0
  br i1 %25, label %26, label %69

26:                                               ; preds = %23
  %27 = load ptr, ptr %14, align 8, !tbaa !5
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = load ptr, ptr %11, align 8, !tbaa !5
  call void @cpSMS4_Cipher(ptr noundef %27, ptr noundef %28, ptr noundef %29)
  %30 = load ptr, ptr %13, align 8, !tbaa !5
  %31 = getelementptr inbounds i32, ptr %30, i64 0
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = load ptr, ptr %14, align 8, !tbaa !5
  %34 = getelementptr inbounds i32, ptr %33, i64 0
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = xor i32 %35, %32
  store i32 %36, ptr %34, align 4, !tbaa !9
  %37 = load ptr, ptr %13, align 8, !tbaa !5
  %38 = getelementptr inbounds i32, ptr %37, i64 1
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = load ptr, ptr %14, align 8, !tbaa !5
  %41 = getelementptr inbounds i32, ptr %40, i64 1
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = xor i32 %42, %39
  store i32 %43, ptr %41, align 4, !tbaa !9
  %44 = load ptr, ptr %13, align 8, !tbaa !5
  %45 = getelementptr inbounds i32, ptr %44, i64 2
  %46 = load i32, ptr %45, align 4, !tbaa !9
  %47 = load ptr, ptr %14, align 8, !tbaa !5
  %48 = getelementptr inbounds i32, ptr %47, i64 2
  %49 = load i32, ptr %48, align 4, !tbaa !9
  %50 = xor i32 %49, %46
  store i32 %50, ptr %48, align 4, !tbaa !9
  %51 = load ptr, ptr %13, align 8, !tbaa !5
  %52 = getelementptr inbounds i32, ptr %51, i64 3
  %53 = load i32, ptr %52, align 4, !tbaa !9
  %54 = load ptr, ptr %14, align 8, !tbaa !5
  %55 = getelementptr inbounds i32, ptr %54, i64 3
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = xor i32 %56, %53
  store i32 %57, ptr %55, align 4, !tbaa !9
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = load ptr, ptr %13, align 8, !tbaa !5
  call void @CopyBlock16(ptr noundef %58, ptr noundef %59)
  %60 = load ptr, ptr %14, align 8, !tbaa !5
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  call void @CopyBlock16(ptr noundef %60, ptr noundef %61)
  br label %62

62:                                               ; preds = %26
  %63 = load i32, ptr %9, align 4, !tbaa !9
  %64 = sub nsw i32 %63, 16
  store i32 %64, ptr %9, align 4, !tbaa !9
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = getelementptr inbounds i8, ptr %65, i64 16
  store ptr %66, ptr %7, align 8, !tbaa !5
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = getelementptr inbounds i8, ptr %67, i64 16
  store ptr %68, ptr %8, align 8, !tbaa !5
  br label %23, !llvm.loop !11

69:                                               ; preds = %23
  %70 = getelementptr inbounds [8 x i32], ptr %12, i64 0, i64 0
  call void @PurgeBlock(ptr noundef %70, i32 noundef 32)
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  ret void
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @CopyBlock16(ptr noundef, ptr noundef) #2

declare void @cpSMS4_Cipher(ptr noundef, ptr noundef, ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @PurgeBlock(ptr noundef, i32 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 = { inlinehint 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 = { "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 #4 = { 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 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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 @cpDecryptSMS4_cbc(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = alloca [8 x i32], align 16
  %7 = getelementptr inbounds i8, ptr %4, i64 132
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6) #4
  %8 = getelementptr inbounds i8, ptr %6, i64 16
  call void @CopyBlock16(ptr noundef %0, ptr noundef nonnull %6)
  %9 = icmp sgt i32 %3, 0
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %5, %.lr.ph
  %.025 = phi ptr [ %14, %.lr.ph ], [ %1, %5 ]
  %.02124 = phi ptr [ %15, %.lr.ph ], [ %2, %5 ]
  %.02223 = phi i32 [ %13, %.lr.ph ], [ %3, %5 ]
  call void @cpSMS4_Cipher(ptr noundef nonnull %8, ptr noundef %.025, ptr noundef nonnull %7) #4
  %10 = load <4 x i32>, ptr %6, align 16, !tbaa !5
  %11 = load <4 x i32>, ptr %8, align 16, !tbaa !5
  %12 = xor <4 x i32> %11, %10
  store <4 x i32> %12, ptr %8, align 16, !tbaa !5
  call void @CopyBlock16(ptr noundef %.025, ptr noundef nonnull %6)
  call void @CopyBlock16(ptr noundef nonnull %8, ptr noundef %.02124)
  %13 = add nsw i32 %.02223, -16
  %14 = getelementptr inbounds i8, ptr %.025, i64 16
  %15 = getelementptr inbounds i8, ptr %.02124, i64 16
  %16 = icmp ugt i32 %.02223, 16
  br i1 %16, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %5
  call void @PurgeBlock(ptr noundef nonnull %6, i32 noundef 32)
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #4
  ret void
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @CopyBlock16(ptr noundef, ptr noundef) local_unnamed_addr #2

declare void @cpSMS4_Cipher(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @PurgeBlock(ptr noundef, i32 noundef) local_unnamed_addr #2

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


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