; LLVM IR from guanzhi/GmSSL :: src (sm4_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"

; Function Attrs: nounwind uwtable
define dso_local i32 @sm4_cbc_padding_encrypt(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, ptr noundef %5) #0 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca [16 x i8], align 16
  %14 = alloca [16 x i8], align 16
  %15 = alloca i64, align 8
  %16 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i64 %3, ptr %10, align 8, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !5
  store ptr %5, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %13) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr %14) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  %17 = load i64, ptr %10, align 8, !tbaa !9
  %18 = urem i64 %17, 16
  store i64 %18, ptr %15, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #5
  %19 = load i64, ptr %10, align 8, !tbaa !9
  %20 = urem i64 %19, 16
  %21 = sub i64 16, %20
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %16, align 4, !tbaa !11
  %23 = getelementptr inbounds [16 x i8], ptr %13, i64 0, i64 0
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %23, ptr align 1 %24, i64 16, i1 false)
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %36

27:                                               ; preds = %6
  %28 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = load i64, ptr %10, align 8, !tbaa !9
  %31 = getelementptr inbounds i8, ptr %29, i64 %30
  %32 = load i64, ptr %15, align 8, !tbaa !9
  %33 = sub i64 0, %32
  %34 = getelementptr inbounds i8, ptr %31, i64 %33
  %35 = load i64, ptr %15, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %28, ptr align 1 %34, i64 %35, i1 false)
  br label %36

36:                                               ; preds = %27, %6
  %37 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %38 = load i64, ptr %15, align 8, !tbaa !9
  %39 = getelementptr inbounds i8, ptr %37, i64 %38
  %40 = load i32, ptr %16, align 4, !tbaa !11
  %41 = trunc i32 %40 to i8
  %42 = load i32, ptr %16, align 4, !tbaa !11
  %43 = sext i32 %42 to i64
  call void @llvm.memset.p0.i64(ptr align 1 %39, i8 %41, i64 %43, i1 false)
  %44 = load i64, ptr %10, align 8, !tbaa !9
  %45 = udiv i64 %44, 16
  %46 = icmp ne i64 %45, 0
  br i1 %46, label %47, label %59

47:                                               ; preds = %36
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds [16 x i8], ptr %13, i64 0, i64 0
  %50 = load ptr, ptr %9, align 8, !tbaa !5
  %51 = load i64, ptr %10, align 8, !tbaa !9
  %52 = udiv i64 %51, 16
  %53 = load ptr, ptr %11, align 8, !tbaa !5
  call void @sm4_cbc_encrypt_blocks(ptr noundef %48, ptr noundef %49, ptr noundef %50, i64 noundef %52, ptr noundef %53)
  %54 = load i64, ptr %10, align 8, !tbaa !9
  %55 = load i64, ptr %15, align 8, !tbaa !9
  %56 = sub i64 %54, %55
  %57 = load ptr, ptr %11, align 8, !tbaa !5
  %58 = getelementptr inbounds i8, ptr %57, i64 %56
  store ptr %58, ptr %11, align 8, !tbaa !5
  br label %59

59:                                               ; preds = %47, %36
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = getelementptr inbounds [16 x i8], ptr %13, i64 0, i64 0
  %62 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %63 = load ptr, ptr %11, align 8, !tbaa !5
  call void @sm4_cbc_encrypt_blocks(ptr noundef %60, ptr noundef %61, ptr noundef %62, i64 noundef 1, ptr noundef %63)
  %64 = load i64, ptr %10, align 8, !tbaa !9
  %65 = load i64, ptr %15, align 8, !tbaa !9
  %66 = sub i64 %64, %65
  %67 = add i64 %66, 16
  %68 = load ptr, ptr %12, align 8, !tbaa !5
  store i64 %67, ptr %68, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %13) #5
  ret i32 1
}

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

declare void @sm4_cbc_encrypt_blocks(ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef) #4

; 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { "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 #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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}


; 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 noundef i32 @sm4_cbc_padding_encrypt(ptr noundef %0, ptr nocapture noundef readonly %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, ptr nocapture noundef writeonly %5) local_unnamed_addr #0 {
  %7 = alloca [16 x i8], align 16
  %8 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %7) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %8) #5
  %9 = and i64 %3, 15
  %10 = trunc nuw nsw i64 %9 to i32
  %11 = sub nuw nsw i32 16, %10
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %7, ptr noundef nonnull align 1 dereferenceable(16) %1, i64 16, i1 false)
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %16, label %12

12:                                               ; preds = %6
  %13 = getelementptr inbounds i8, ptr %2, i64 %3
  %14 = sub nsw i64 0, %9
  %15 = getelementptr inbounds i8, ptr %13, i64 %14
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %8, ptr nonnull align 1 %15, i64 %9, i1 false)
  br label %16

16:                                               ; preds = %12, %6
  %17 = getelementptr inbounds i8, ptr %8, i64 %9
  %18 = trunc nuw nsw i32 %11 to i8
  %19 = zext nneg i32 %11 to i64
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %17, i8 %18, i64 %19, i1 false)
  %.not25 = icmp ult i64 %3, 16
  br i1 %.not25, label %._crit_edge, label %20

20:                                               ; preds = %16
  %21 = lshr i64 %3, 4
  call void @sm4_cbc_encrypt_blocks(ptr noundef %0, ptr noundef nonnull %7, ptr noundef %2, i64 noundef %21, ptr noundef %4) #5
  %22 = and i64 %3, -16
  %23 = getelementptr inbounds i8, ptr %4, i64 %22
  %24 = add i64 %22, 16
  br label %._crit_edge

._crit_edge:                                      ; preds = %16, %20
  %.pre-phi = phi i64 [ %24, %20 ], [ 16, %16 ]
  %.0 = phi ptr [ %23, %20 ], [ %4, %16 ]
  call void @sm4_cbc_encrypt_blocks(ptr noundef %0, ptr noundef nonnull %7, ptr noundef nonnull %8, i64 noundef 1, ptr noundef %.0) #5
  store i64 %.pre-phi, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %8) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %7) #5
  ret i32 1
}

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

declare void @sm4_cbc_encrypt_blocks(ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #4

; 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { "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 #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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass