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

%struct.SM2_Z256_POINT = type { [4 x i64], [4 x i64], [4 x i64] }

; 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @sm2_z256_get_booth(ptr noundef, i32 noundef, i32 noundef) #2

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @sm2_z256_point_mul_ex(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = 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 4, ptr %7) #4
  store i32 5, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 1, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = add nsw i32 256, %12
  %14 = sub nsw i32 %13, 1
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = sdiv i32 %14, %15
  store i32 %16, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %17 = load i32, ptr %9, align 4, !tbaa !9
  %18 = sub nsw i32 %17, 1
  store i32 %18, ptr %10, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %76, %3
  %20 = load i32, ptr %10, align 4, !tbaa !9
  %21 = icmp sge i32 %20, 0
  br i1 %21, label %22, label %79

22:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = load i32, ptr %10, align 4, !tbaa !9
  %26 = call i32 @sm2_z256_get_booth(ptr noundef %23, i32 noundef %24, i32 noundef %25)
  store i32 %26, ptr %11, align 4, !tbaa !9
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %40

29:                                               ; preds = %22
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %39

32:                                               ; preds = %29
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load i32, ptr %11, align 4, !tbaa !9
  %36 = sub nsw i32 %35, 1
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds %struct.SM2_Z256_POINT, ptr %34, i64 %37
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %33, ptr align 8 %38, i64 96, i1 false), !tbaa.struct !11
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %32, %29
  br label %75

40:                                               ; preds = %22
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sm2_z256_point_dbl(ptr noundef %41, ptr noundef %42)
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sm2_z256_point_dbl(ptr noundef %43, ptr noundef %44)
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sm2_z256_point_dbl(ptr noundef %45, ptr noundef %46)
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sm2_z256_point_dbl(ptr noundef %47, ptr noundef %48)
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sm2_z256_point_dbl(ptr noundef %49, ptr noundef %50)
  %51 = load i32, ptr %11, align 4, !tbaa !9
  %52 = icmp sgt i32 %51, 0
  br i1 %52, label %53, label %61

53:                                               ; preds = %40
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load i32, ptr %11, align 4, !tbaa !9
  %58 = sub nsw i32 %57, 1
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds %struct.SM2_Z256_POINT, ptr %56, i64 %59
  call void @sm2_z256_point_add(ptr noundef %54, ptr noundef %55, ptr noundef %60)
  br label %74

61:                                               ; preds = %40
  %62 = load i32, ptr %11, align 4, !tbaa !9
  %63 = icmp slt i32 %62, 0
  br i1 %63, label %64, label %73

64:                                               ; preds = %61
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = load i32, ptr %11, align 4, !tbaa !9
  %69 = sub nsw i32 0, %68
  %70 = sub nsw i32 %69, 1
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds %struct.SM2_Z256_POINT, ptr %67, i64 %71
  call void @sm2_z256_point_sub(ptr noundef %65, ptr noundef %66, ptr noundef %72)
  br label %73

73:                                               ; preds = %64, %61
  br label %74

74:                                               ; preds = %73, %53
  br label %75

75:                                               ; preds = %74, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %10, align 4, !tbaa !9
  %78 = add nsw i32 %77, -1
  store i32 %78, ptr %10, align 4, !tbaa !9
  br label %19, !llvm.loop !13

79:                                               ; preds = %19
  %80 = load i32, ptr %8, align 4, !tbaa !9
  %81 = icmp ne i32 %80, 0
  br i1 %81, label %82, label %84

82:                                               ; preds = %79
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %83, i8 0, i64 96, i1 false)
  br label %84

84:                                               ; preds = %82, %79
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{i64 0, i64 32, !12, i64 32, i64 32, !12, i64 64, i64 32, !12}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

%struct.SM2_Z256_POINT = type { [4 x i64], [4 x i64], [4 x i64] }

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @sm2_z256_point_add(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #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: nounwind uwtable
declare dso_local void @sm2_z256_point_sub(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @sm2_z256_point_mul_ex(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %invariant.gep = getelementptr i8, ptr %2, i64 -96
  br label %4

4:                                                ; preds = %.backedge, %3
  %.042 = phi i32 [ 1, %3 ], [ %.042.be, %.backedge ]
  %.03441 = phi i32 [ 51, %3 ], [ %.03441.be, %.backedge ]
  %5 = tail call i32 @sm2_z256_get_booth(ptr noundef %1, i32 noundef 5, i32 noundef %.03441)
  %.not37 = icmp eq i32 %.042, 0
  br i1 %.not37, label %9, label %6

6:                                                ; preds = %4
  %.not38 = icmp eq i32 %5, 0
  br i1 %.not38, label %.thread, label %7

7:                                                ; preds = %6
  %8 = sext i32 %5 to i64
  %gep = getelementptr %struct.SM2_Z256_POINT, ptr %invariant.gep, i64 %8
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(96) %0, ptr noundef nonnull align 8 dereferenceable(96) %gep, i64 96, i1 false), !tbaa.struct !5
  br label %19

9:                                                ; preds = %4
  tail call void @sm2_z256_point_dbl(ptr noundef %0, ptr noundef %0)
  tail call void @sm2_z256_point_dbl(ptr noundef %0, ptr noundef %0)
  tail call void @sm2_z256_point_dbl(ptr noundef %0, ptr noundef %0)
  tail call void @sm2_z256_point_dbl(ptr noundef %0, ptr noundef %0)
  tail call void @sm2_z256_point_dbl(ptr noundef %0, ptr noundef %0)
  %10 = icmp sgt i32 %5, 0
  br i1 %10, label %11, label %13

11:                                               ; preds = %9
  %12 = zext nneg i32 %5 to i64
  %gep40 = getelementptr %struct.SM2_Z256_POINT, ptr %invariant.gep, i64 %12
  tail call void @sm2_z256_point_add(ptr noundef %0, ptr noundef %0, ptr noundef %gep40)
  br label %19

13:                                               ; preds = %9
  %14 = icmp slt i32 %5, 0
  br i1 %14, label %15, label %19

15:                                               ; preds = %13
  %16 = xor i32 %5, -1
  %17 = zext nneg i32 %16 to i64
  %18 = getelementptr inbounds %struct.SM2_Z256_POINT, ptr %2, i64 %17
  tail call void @sm2_z256_point_sub(ptr noundef %0, ptr noundef %0, ptr noundef %18)
  br label %19

19:                                               ; preds = %11, %15, %13, %7
  %.not43 = icmp eq i32 %.03441, 0
  br i1 %.not43, label %.loopexit, label %.backedge

.backedge:                                        ; preds = %19, %.thread
  %.042.be = phi i32 [ 0, %19 ], [ 1, %.thread ]
  %.03441.be = add nsw i32 %.03441, -1
  br label %4, !llvm.loop !9

.thread:                                          ; preds = %6
  %.not4346 = icmp eq i32 %.03441, 0
  br i1 %.not4346, label %20, label %.backedge

20:                                               ; preds = %.thread
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(96) %0, i8 0, i64 96, i1 false)
  br label %.loopexit

.loopexit:                                        ; preds = %19, %20
  ret void
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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 = !{i64 0, i64 32, !6, i64 32, i64 32, !6, i64 64, i64 32, !6}
!6 = !{!7, !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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass