; 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_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 @sm2_z256_get_booth(ptr noundef, i32 noundef, i32 noundef) #1

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @sm2_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 = load i64, ptr %5, align 8, !tbaa !9
  %14 = udiv i64 %12, %13
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %16 = load i32, ptr %7, align 4, !tbaa !11
  %17 = sub nsw i32 %16, 1
  store i32 %17, ptr %8, align 4, !tbaa !11
  br label %18

18:                                               ; preds = %75, %2
  %19 = load i32, ptr %8, align 4, !tbaa !11
  %20 = icmp sge i32 %19, 0
  br i1 %20, label %21, label %78

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

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

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

42:                                               ; preds = %32, %29
  br label %74

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

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

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

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

72:                                               ; preds = %60, %57
  br label %73

73:                                               ; preds = %72, %46
  br label %74

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

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

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

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

83:                                               ; preds = %81, %78
  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.SM2_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 @sm2_z256_get_booth(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @sm2_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 @sm2_z256_get_booth(ptr noundef %1, i32 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.SM2_Z256_AFFINE_POINT], ptr %8, i64 %indvars.iv, i64 %10
  tail call void @sm2_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.SM2_Z256_AFFINE_POINT], ptr %15, i64 %indvars.iv, i64 %17
  tail call void @sm2_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.SM2_Z256_AFFINE_POINT], ptr %22, i64 %indvars.iv, i64 %24
  tail call void @sm2_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 @sm2_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