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

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

@SM2_Z256_MODN_MONT_ONE = external dso_local global ptr, align 8

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @sm2_z256_copy(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @sm2_z256_modn_mont_mul(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @sm2_z256_modn_mont_sqr(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @sm2_z256_modn_mont_exp(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca [4 x i64], align 16
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %11 = getelementptr inbounds [4 x i64], ptr %7, i64 0, i64 0
  %12 = load ptr, ptr @SM2_Z256_MODN_MONT_ONE, align 8, !tbaa !5
  call void @sm2_z256_copy(ptr noundef %11, ptr noundef %12)
  store i32 3, ptr %9, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %42, %3
  %14 = load i32, ptr %9, align 4, !tbaa !9
  %15 = icmp sge i32 %14, 0
  br i1 %15, label %16, label %45

16:                                               ; preds = %13
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load i32, ptr %9, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i64, ptr %17, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !11
  store i64 %21, ptr %8, align 8, !tbaa !11
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %38, %16
  %23 = load i32, ptr %10, align 4, !tbaa !9
  %24 = icmp slt i32 %23, 64
  br i1 %24, label %25, label %41

25:                                               ; preds = %22
  %26 = getelementptr inbounds [4 x i64], ptr %7, i64 0, i64 0
  %27 = getelementptr inbounds [4 x i64], ptr %7, i64 0, i64 0
  call void @sm2_z256_modn_mont_sqr(ptr noundef %26, ptr noundef %27)
  %28 = load i64, ptr %8, align 8, !tbaa !11
  %29 = and i64 %28, -9223372036854775808
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %31, label %35

31:                                               ; preds = %25
  %32 = getelementptr inbounds [4 x i64], ptr %7, i64 0, i64 0
  %33 = getelementptr inbounds [4 x i64], ptr %7, i64 0, i64 0
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  call void @sm2_z256_modn_mont_mul(ptr noundef %32, ptr noundef %33, ptr noundef %34)
  br label %35

35:                                               ; preds = %31, %25
  %36 = load i64, ptr %8, align 8, !tbaa !11
  %37 = shl i64 %36, 1
  store i64 %37, ptr %8, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %35
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %10, align 4, !tbaa !9
  br label %22, !llvm.loop !13

41:                                               ; preds = %22
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = add nsw i32 %43, -1
  store i32 %44, ptr %9, align 4, !tbaa !9
  br label %13, !llvm.loop !15

45:                                               ; preds = %13
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds [4 x i64], ptr %7, i64 0, i64 0
  call void @sm2_z256_copy(ptr noundef %46, ptr noundef %47)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 32, ptr %7) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

@SM2_Z256_MODN_MONT_ONE = external dso_local local_unnamed_addr global ptr, align 8

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @sm2_z256_copy(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @sm2_z256_modn_mont_mul(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @sm2_z256_modn_mont_sqr(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @sm2_z256_modn_mont_exp(ptr noundef %0, ptr noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #1 {
  %4 = alloca [4 x i64], align 16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %4) #2
  %5 = load ptr, ptr @SM2_Z256_MODN_MONT_ONE, align 8, !tbaa !5
  call void @sm2_z256_copy(ptr noundef nonnull %4, ptr noundef %5)
  %6 = getelementptr inbounds i8, ptr %2, i64 24
  %7 = load i64, ptr %6, align 8, !tbaa !9
  br label %8

8:                                                ; preds = %3, %10
  %.012 = phi i32 [ 0, %3 ], [ %12, %10 ]
  %.01011 = phi i64 [ %7, %3 ], [ %11, %10 ]
  call void @sm2_z256_modn_mont_sqr(ptr noundef nonnull %4, ptr noundef nonnull %4)
  %.not = icmp sgt i64 %.01011, -1
  br i1 %.not, label %10, label %9

9:                                                ; preds = %8
  call void @sm2_z256_modn_mont_mul(ptr noundef nonnull %4, ptr noundef nonnull %4, ptr noundef %1)
  br label %10

10:                                               ; preds = %9, %8
  %11 = shl i64 %.01011, 1
  %12 = add nuw nsw i32 %.012, 1
  %exitcond.not = icmp eq i32 %12, 64
  br i1 %exitcond.not, label %13, label %8, !llvm.loop !11

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %2, i64 16
  %15 = load i64, ptr %14, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %18, %13
  %.012.1 = phi i32 [ 0, %13 ], [ %20, %18 ]
  %.01011.1 = phi i64 [ %15, %13 ], [ %19, %18 ]
  call void @sm2_z256_modn_mont_sqr(ptr noundef nonnull %4, ptr noundef nonnull %4)
  %.not.1 = icmp sgt i64 %.01011.1, -1
  br i1 %.not.1, label %18, label %17

17:                                               ; preds = %16
  call void @sm2_z256_modn_mont_mul(ptr noundef nonnull %4, ptr noundef nonnull %4, ptr noundef %1)
  br label %18

18:                                               ; preds = %17, %16
  %19 = shl i64 %.01011.1, 1
  %20 = add nuw nsw i32 %.012.1, 1
  %exitcond.1.not = icmp eq i32 %20, 64
  br i1 %exitcond.1.not, label %21, label %16, !llvm.loop !11

21:                                               ; preds = %18
  %22 = getelementptr inbounds i8, ptr %2, i64 8
  %23 = load i64, ptr %22, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %26, %21
  %.012.2 = phi i32 [ 0, %21 ], [ %28, %26 ]
  %.01011.2 = phi i64 [ %23, %21 ], [ %27, %26 ]
  call void @sm2_z256_modn_mont_sqr(ptr noundef nonnull %4, ptr noundef nonnull %4)
  %.not.2 = icmp sgt i64 %.01011.2, -1
  br i1 %.not.2, label %26, label %25

25:                                               ; preds = %24
  call void @sm2_z256_modn_mont_mul(ptr noundef nonnull %4, ptr noundef nonnull %4, ptr noundef %1)
  br label %26

26:                                               ; preds = %25, %24
  %27 = shl i64 %.01011.2, 1
  %28 = add nuw nsw i32 %.012.2, 1
  %exitcond.2.not = icmp eq i32 %28, 64
  br i1 %exitcond.2.not, label %29, label %24, !llvm.loop !11

29:                                               ; preds = %26
  %30 = load i64, ptr %2, align 8, !tbaa !9
  br label %31

31:                                               ; preds = %33, %29
  %.012.3 = phi i32 [ 0, %29 ], [ %35, %33 ]
  %.01011.3 = phi i64 [ %30, %29 ], [ %34, %33 ]
  call void @sm2_z256_modn_mont_sqr(ptr noundef nonnull %4, ptr noundef nonnull %4)
  %.not.3 = icmp sgt i64 %.01011.3, -1
  br i1 %.not.3, label %33, label %32

32:                                               ; preds = %31
  call void @sm2_z256_modn_mont_mul(ptr noundef nonnull %4, ptr noundef nonnull %4, ptr noundef %1)
  br label %33

33:                                               ; preds = %32, %31
  %34 = shl i64 %.01011.3, 1
  %35 = add nuw nsw i32 %.012.3, 1
  %exitcond.3.not = icmp eq i32 %35, 64
  br i1 %exitcond.3.not, label %36, label %31, !llvm.loop !11

36:                                               ; preds = %33
  call void @sm2_z256_copy(ptr noundef %0, ptr noundef nonnull %4)
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %4) #2
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass