; LLVM IR from gost-engine/engine :: gost_ec_sign (gost_ec_keygen)

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

@.str = external hidden unnamed_addr constant [67 x i8], align 1

; 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

declare void @ERR_GOST_error(i32 noundef, i32 noundef, ptr noundef, i32 noundef) #1

declare ptr @EC_KEY_get0_group(ptr noundef) #1

declare i32 @EC_GROUP_get_order(ptr noundef, ptr noundef, ptr noundef) #1

declare i32 @BN_is_zero(ptr noundef) #1

declare i32 @BN_rand_range(ptr noundef, ptr noundef) #1

declare void @BN_free(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @gost_ec_keygen(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %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
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store ptr null, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store ptr null, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %14

11:                                               ; preds = %1
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = call ptr @EC_KEY_get0_group(ptr noundef %12)
  br label %15

14:                                               ; preds = %1
  br label %15

15:                                               ; preds = %14, %11
  %16 = phi ptr [ %13, %11 ], [ null, %14 ]
  store ptr %16, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  call void @ERR_GOST_error(i32 noundef 108, i32 noundef 786691, ptr noundef @.str, i32 noundef 606)
  store i32 0, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %73

20:                                               ; preds = %15
  %21 = call ptr @BN_new()
  store ptr %21, ptr %4, align 8, !tbaa !5
  %22 = call ptr @BN_secure_new()
  store ptr %22, ptr %5, align 8, !tbaa !5
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %28

25:                                               ; preds = %20
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = icmp ne ptr %26, null
  br i1 %27, label %29, label %28

28:                                               ; preds = %25, %20
  call void @ERR_GOST_error(i32 noundef 108, i32 noundef 786688, ptr noundef @.str, i32 noundef 613)
  br label %54

29:                                               ; preds = %25
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = call i32 @EC_GROUP_get_order(ptr noundef %30, ptr noundef %31, ptr noundef null)
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %35, label %34

34:                                               ; preds = %29
  call void @ERR_GOST_error(i32 noundef 108, i32 noundef 786691, ptr noundef @.str, i32 noundef 618)
  br label %54

35:                                               ; preds = %29
  br label %36

36:                                               ; preds = %43, %35
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = call i32 @BN_rand_range(ptr noundef %37, ptr noundef %38)
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %42, label %41

41:                                               ; preds = %36
  call void @ERR_GOST_error(i32 noundef 108, i32 noundef 126, ptr noundef @.str, i32 noundef 624)
  br label %54

42:                                               ; preds = %36
  br label %43

43:                                               ; preds = %42
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = call i32 @BN_is_zero(ptr noundef %44)
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %36, label %47, !llvm.loop !11

47:                                               ; preds = %43
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = call i32 @EC_KEY_set_private_key(ptr noundef %48, ptr noundef %49)
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %53, label %52

52:                                               ; preds = %47
  call void @ERR_GOST_error(i32 noundef 108, i32 noundef 786691, ptr noundef @.str, i32 noundef 631)
  br label %54

53:                                               ; preds = %47
  store i32 1, ptr %7, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %53, %52, %41, %34, %28
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %59

57:                                               ; preds = %54
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  call void @BN_free(ptr noundef %58)
  br label %59

59:                                               ; preds = %57, %54
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = icmp ne ptr %60, null
  br i1 %61, label %62, label %64

62:                                               ; preds = %59
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  call void @BN_free(ptr noundef %63)
  br label %64

64:                                               ; preds = %62, %59
  %65 = load i32, ptr %7, align 4, !tbaa !9
  %66 = icmp ne i32 %65, 0
  br i1 %66, label %67, label %70

67:                                               ; preds = %64
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = call i32 @gost_ec_compute_public(ptr noundef %68)
  br label %71

70:                                               ; preds = %64
  br label %71

71:                                               ; preds = %70, %67
  %72 = phi i32 [ %69, %67 ], [ 0, %70 ]
  store i32 %72, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %73

73:                                               ; preds = %71, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %74 = load i32, ptr %2, align 4
  ret i32 %74
}

declare ptr @BN_new() #1

declare ptr @BN_secure_new() #1

declare i32 @EC_KEY_set_private_key(ptr noundef, ptr noundef) #1

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

@.str = external hidden unnamed_addr constant [67 x i8], align 1

declare void @ERR_GOST_error(i32 noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

declare ptr @EC_KEY_get0_group(ptr noundef) local_unnamed_addr #0

declare i32 @EC_GROUP_get_order(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

declare i32 @BN_is_zero(ptr noundef) local_unnamed_addr #0

declare i32 @BN_rand_range(ptr noundef, ptr noundef) local_unnamed_addr #0

declare void @BN_free(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @gost_ec_keygen(ptr noundef %0) local_unnamed_addr #1 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.thread, label %2

2:                                                ; preds = %1
  %3 = tail call ptr @EC_KEY_get0_group(ptr noundef nonnull %0) #2
  %.not25 = icmp eq ptr %3, null
  br i1 %.not25, label %.thread, label %4

.thread:                                          ; preds = %1, %2
  tail call void @ERR_GOST_error(i32 noundef 108, i32 noundef 786691, ptr noundef nonnull @.str, i32 noundef 606) #2
  br label %26

4:                                                ; preds = %2
  %5 = tail call ptr @BN_new() #2
  %6 = tail call ptr @BN_secure_new() #2
  %7 = icmp ne ptr %5, null
  %8 = icmp ne ptr %6, null
  %or.cond = select i1 %7, i1 %8, i1 false
  br i1 %or.cond, label %10, label %9

9:                                                ; preds = %4
  tail call void @ERR_GOST_error(i32 noundef 108, i32 noundef 786688, ptr noundef nonnull @.str, i32 noundef 613) #2
  br label %19

10:                                               ; preds = %4
  %11 = tail call i32 @EC_GROUP_get_order(ptr noundef nonnull %3, ptr noundef nonnull %5, ptr noundef null) #2
  %.not26 = icmp eq i32 %11, 0
  br i1 %.not26, label %.thread32, label %.preheader

.thread32:                                        ; preds = %10
  tail call void @ERR_GOST_error(i32 noundef 108, i32 noundef 786691, ptr noundef nonnull @.str, i32 noundef 618) #2
  br label %20

.preheader:                                       ; preds = %10, %14
  %12 = tail call i32 @BN_rand_range(ptr noundef nonnull %6, ptr noundef nonnull %5) #2
  %.not27 = icmp eq i32 %12, 0
  br i1 %.not27, label %13, label %14

13:                                               ; preds = %.preheader
  tail call void @ERR_GOST_error(i32 noundef 108, i32 noundef 126, ptr noundef nonnull @.str, i32 noundef 624) #2
  br label %19

14:                                               ; preds = %.preheader
  %15 = tail call i32 @BN_is_zero(ptr noundef nonnull %6) #2
  %.not28 = icmp eq i32 %15, 0
  br i1 %.not28, label %16, label %.preheader, !llvm.loop !5

16:                                               ; preds = %14
  %17 = tail call i32 @EC_KEY_set_private_key(ptr noundef nonnull %0, ptr noundef nonnull %6) #2
  %.not29 = icmp eq i32 %17, 0
  br i1 %.not29, label %18, label %19

18:                                               ; preds = %16
  tail call void @ERR_GOST_error(i32 noundef 108, i32 noundef 786691, ptr noundef nonnull @.str, i32 noundef 631) #2
  br label %19

19:                                               ; preds = %16, %18, %13, %9
  %.not30 = phi i1 [ true, %18 ], [ true, %13 ], [ true, %9 ], [ false, %16 ]
  br i1 %8, label %20, label %21

20:                                               ; preds = %.thread32, %19
  %.not3035 = phi i1 [ true, %.thread32 ], [ %.not30, %19 ]
  tail call void @BN_free(ptr noundef nonnull %6) #2
  br label %21

21:                                               ; preds = %20, %19
  %.not3034 = phi i1 [ %.not3035, %20 ], [ %.not30, %19 ]
  br i1 %7, label %22, label %23

22:                                               ; preds = %21
  tail call void @BN_free(ptr noundef nonnull %5) #2
  br label %23

23:                                               ; preds = %22, %21
  br i1 %.not3034, label %26, label %24

24:                                               ; preds = %23
  %25 = tail call i32 @gost_ec_compute_public(ptr noundef nonnull %0)
  br label %26

26:                                               ; preds = %24, %23, %.thread
  %.019 = phi i32 [ 0, %.thread ], [ %25, %24 ], [ 0, %23 ]
  ret i32 %.019
}

declare ptr @BN_new() local_unnamed_addr #0

declare ptr @BN_secure_new() local_unnamed_addr #0

declare i32 @EC_KEY_set_private_key(ptr noundef, ptr noundef) local_unnamed_addr #0

attributes #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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass