; LLVM IR from guanzhi/GmSSL :: src (sm9_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.SM9_Z256_AFFINE_POINT = type { [4 x i64], [4 x i64] }

@g_pre_comp = external hidden 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 i32 @sm9_z256_get_booth(ptr noundef, i64 noundef, i32 noundef) #1

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @sm9_z256_point_mul_generator(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 7, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %10 = load i64, ptr %5, align 8, !tbaa !9
  %11 = add i64 256, %10
  %12 = sub i64 %11, 1
  %13 = trunc i64 %12 to i32
  %14 = sext i32 %13 to i64
  %15 = load i64, ptr %5, align 8, !tbaa !9
  %16 = udiv i64 %14, %15
  %17 = trunc i64 %16 to i32
  store i32 %17, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %18 = load i32, ptr %7, align 4, !tbaa !11
  %19 = sub nsw i32 %18, 1
  store i32 %19, ptr %8, align 4, !tbaa !11
  br label %20

20:                                               ; preds = %76, %2
  %21 = load i32, ptr %8, align 4, !tbaa !11
  %22 = icmp sge i32 %21, 0
  br i1 %22, label %23, label %79

23:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load i64, ptr %5, align 8, !tbaa !9
  %26 = load i32, ptr %8, align 4, !tbaa !11
  %27 = call i32 @sm9_z256_get_booth(ptr noundef %24, i64 noundef %25, i32 noundef %26)
  store i32 %27, ptr %9, align 4, !tbaa !11
  %28 = load i32, ptr %6, align 4, !tbaa !11
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %44

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

33:                                               ; preds = %30
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load ptr, ptr @g_pre_comp, align 8, !tbaa !5
  %36 = load i32, ptr %8, align 4, !tbaa !11
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [64 x %struct.SM9_Z256_AFFINE_POINT], ptr %35, i64 %37
  %39 = load i32, ptr %9, align 4, !tbaa !11
  %40 = sub nsw i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [64 x %struct.SM9_Z256_AFFINE_POINT], ptr %38, i64 0, i64 %41
  call void @sm9_z256_point_copy_affine(ptr noundef %34, ptr noundef %42)
  store i32 0, ptr %6, align 4, !tbaa !11
  br label %43

43:                                               ; preds = %33, %30
  br label %75

44:                                               ; preds = %23
  %45 = load i32, ptr %9, align 4, !tbaa !11
  %46 = icmp sgt i32 %45, 0
  br i1 %46, label %47, label %58

47:                                               ; preds = %44
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = load ptr, ptr @g_pre_comp, align 8, !tbaa !5
  %51 = load i32, ptr %8, align 4, !tbaa !11
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [64 x %struct.SM9_Z256_AFFINE_POINT], ptr %50, i64 %52
  %54 = load i32, ptr %9, align 4, !tbaa !11
  %55 = sub nsw i32 %54, 1
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [64 x %struct.SM9_Z256_AFFINE_POINT], ptr %53, i64 0, i64 %56
  call void @sm9_z256_point_add_affine(ptr noundef %48, ptr noundef %49, ptr noundef %57)
  br label %74

58:                                               ; preds = %44
  %59 = load i32, ptr %9, align 4, !tbaa !11
  %60 = icmp slt i32 %59, 0
  br i1 %60, label %61, label %73

61:                                               ; preds = %58
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = load ptr, ptr @g_pre_comp, align 8, !tbaa !5
  %65 = load i32, ptr %8, align 4, !tbaa !11
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [64 x %struct.SM9_Z256_AFFINE_POINT], ptr %64, i64 %66
  %68 = load i32, ptr %9, align 4, !tbaa !11
  %69 = sub nsw i32 0, %68
  %70 = sub nsw i32 %69, 1
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [64 x %struct.SM9_Z256_AFFINE_POINT], ptr %67, i64 0, i64 %71
  call void @sm9_z256_point_sub_affine(ptr noundef %62, ptr noundef %63, ptr noundef %72)
  br label %73

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

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

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

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

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

82:                                               ; preds = %79
  %83 = load ptr, ptr %3, align 8, !tbaa !5
  call void @sm9_z256_point_set_infinity(ptr noundef %83)
  br label %84

84:                                               ; preds = %82, %79
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !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.SM9_Z256_AFFINE_POINT = type { [4 x i64], [4 x i64] }

@g_pre_comp = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @sm9_z256_get_booth(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @sm9_z256_point_set_infinity(ptr noundef) local_unnamed_addr #0

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @sm9_z256_point_mul_generator(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  br label %3

3:                                                ; preds = %.backedge, %2
  %indvars.iv = phi i64 [ 36, %2 ], [ %indvars.iv.be, %.backedge ]
  %.030 = phi i32 [ 1, %2 ], [ %.030.be, %.backedge ]
  %4 = trunc nuw nsw i64 %indvars.iv to i32
  %5 = tail call i32 @sm9_z256_get_booth(ptr noundef %1, i64 noundef 7, i32 noundef %4)
  %.not27 = icmp eq i32 %.030, 0
  br i1 %.not27, label %12, label %6

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

7:                                                ; preds = %6
  %8 = load ptr, ptr @g_pre_comp, align 8, !tbaa !5
  %9 = add nsw i32 %5, -1
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [64 x %struct.SM9_Z256_AFFINE_POINT], ptr %8, i64 %indvars.iv, i64 %10
  tail call void @sm9_z256_point_copy_affine(ptr noundef %0, ptr noundef %11)
  br label %26

12:                                               ; preds = %3
  %13 = icmp sgt i32 %5, 0
  br i1 %13, label %14, label %19

14:                                               ; preds = %12
  %15 = load ptr, ptr @g_pre_comp, align 8, !tbaa !5
  %16 = add nsw i32 %5, -1
  %17 = zext nneg i32 %16 to i64
  %18 = getelementptr inbounds [64 x %struct.SM9_Z256_AFFINE_POINT], ptr %15, i64 %indvars.iv, i64 %17
  tail call void @sm9_z256_point_add_affine(ptr noundef %0, ptr noundef %0, ptr noundef %18)
  br label %26

19:                                               ; preds = %12
  %20 = icmp slt i32 %5, 0
  br i1 %20, label %21, label %26

21:                                               ; preds = %19
  %22 = load ptr, ptr @g_pre_comp, align 8, !tbaa !5
  %23 = xor i32 %5, -1
  %24 = zext nneg i32 %23 to i64
  %25 = getelementptr inbounds [64 x %struct.SM9_Z256_AFFINE_POINT], ptr %22, i64 %indvars.iv, i64 %24
  tail call void @sm9_z256_point_sub_affine(ptr noundef %0, ptr noundef %0, ptr noundef %25)
  br label %26

26:                                               ; preds = %14, %21, %19, %7
  %.not32 = icmp eq i64 %indvars.iv, 0
  br i1 %.not32, label %.loopexit, label %.backedge

.backedge:                                        ; preds = %26, %.thread
  %.030.be = phi i32 [ 0, %26 ], [ 1, %.thread ]
  %indvars.iv.be = add nsw i64 %indvars.iv, -1
  br label %3, !llvm.loop !9

.thread:                                          ; preds = %6
  %.not3236 = icmp eq i64 %indvars.iv, 0
  br i1 %.not3236, label %27, label %.backedge

27:                                               ; preds = %.thread
  tail call void @sm9_z256_point_set_infinity(ptr noundef %0)
  br label %.loopexit

.loopexit:                                        ; preds = %26, %27
  ret void
}

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

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass