; LLVM IR from gost-engine/engine :: gost_ec_keyx (pkey_gost_ec_derive)

; 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.gost_pmeth_data = type { i32, ptr, [32 x i8], i64, i32, i32, i32 }

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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @pkey_gost_ec_derive(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = call ptr @EVP_PKEY_CTX_get0_pkey(ptr noundef %13)
  store ptr %14, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = call ptr @EVP_PKEY_CTX_get0_peerkey(ptr noundef %15)
  store ptr %16, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = call ptr @EVP_PKEY_CTX_get_data(ptr noundef %17)
  store ptr %18, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  %19 = load ptr, ptr %10, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %26

21:                                               ; preds = %3
  %22 = load ptr, ptr %10, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.gost_pmeth_data, ptr %22, i32 0, i32 3
  %24 = load i64, ptr %23, align 8, !tbaa !11
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %21, %3
  call void @ERR_GOST_error(i32 noundef 127, i32 noundef 129, ptr noundef @.str, i32 noundef 202)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %128

27:                                               ; preds = %21
  %28 = load ptr, ptr %10, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.gost_pmeth_data, ptr %28, i32 0, i32 6
  %30 = load i32, ptr %29, align 8, !tbaa !14
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %68

32:                                               ; preds = %27
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = icmp ne ptr %33, null
  br i1 %34, label %44, label %35

35:                                               ; preds = %32
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.gost_pmeth_data, ptr %36, i32 0, i32 6
  %38 = load i32, ptr %37, align 8, !tbaa !14
  %39 = icmp eq i32 %38, 982
  %40 = zext i1 %39 to i64
  %41 = select i1 %39, i32 32, i32 64
  %42 = sext i32 %41 to i64
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %42, ptr %43, align 8, !tbaa !15
  store i32 1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %128

44:                                               ; preds = %32
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load ptr, ptr %9, align 8, !tbaa !5
  %47 = call ptr @EVP_PKEY_get0(ptr noundef %46)
  %48 = call ptr @EC_KEY_get0_public_key(ptr noundef %47)
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = call ptr @EVP_PKEY_get0(ptr noundef %49)
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.gost_pmeth_data, ptr %51, i32 0, i32 2
  %53 = getelementptr inbounds [32 x i8], ptr %52, i64 0, i64 0
  %54 = load ptr, ptr %10, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.gost_pmeth_data, ptr %54, i32 0, i32 3
  %56 = load i64, ptr %55, align 8, !tbaa !11
  %57 = load ptr, ptr %10, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.gost_pmeth_data, ptr %57, i32 0, i32 6
  %59 = load i32, ptr %58, align 8, !tbaa !14
  %60 = call i32 @VKO_compute_key(ptr noundef %45, ptr noundef %48, ptr noundef %50, ptr noundef %53, i64 noundef %56, i32 noundef %59)
  %61 = sext i32 %60 to i64
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %61, ptr %62, align 8, !tbaa !15
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = load i64, ptr %63, align 8, !tbaa !15
  %65 = icmp ne i64 %64, 0
  %66 = zext i1 %65 to i64
  %67 = select i1 %65, i32 1, i32 0
  store i32 %67, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %128

68:                                               ; preds = %27
  %69 = load ptr, ptr %10, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.gost_pmeth_data, ptr %69, i32 0, i32 3
  %71 = load i64, ptr %70, align 8, !tbaa !11
  switch i64 %71, label %127 [
    i64 8, label %72
    i64 32, label %102
  ]

72:                                               ; preds = %68
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = icmp eq ptr %73, null
  br i1 %74, label %75, label %77

75:                                               ; preds = %72
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 32, ptr %76, align 8, !tbaa !15
  store i32 1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %128

77:                                               ; preds = %72
  %78 = load ptr, ptr %8, align 8, !tbaa !5
  %79 = call i32 @EVP_PKEY_get_default_digest_nid(ptr noundef %78, ptr noundef %11)
  %80 = load i32, ptr %11, align 4, !tbaa !9
  %81 = icmp eq i32 %80, 983
  br i1 %81, label %82, label %83

82:                                               ; preds = %77
  store i32 982, ptr %11, align 4, !tbaa !9
  br label %83

83:                                               ; preds = %82, %77
  %84 = load ptr, ptr %6, align 8, !tbaa !5
  %85 = load ptr, ptr %9, align 8, !tbaa !5
  %86 = call ptr @EVP_PKEY_get0(ptr noundef %85)
  %87 = call ptr @EC_KEY_get0_public_key(ptr noundef %86)
  %88 = load ptr, ptr %8, align 8, !tbaa !5
  %89 = call ptr @EVP_PKEY_get0(ptr noundef %88)
  %90 = load ptr, ptr %10, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.gost_pmeth_data, ptr %90, i32 0, i32 2
  %92 = getelementptr inbounds [32 x i8], ptr %91, i64 0, i64 0
  %93 = load i32, ptr %11, align 4, !tbaa !9
  %94 = call i32 @VKO_compute_key(ptr noundef %84, ptr noundef %87, ptr noundef %89, ptr noundef %92, i64 noundef 8, i32 noundef %93)
  %95 = sext i32 %94 to i64
  %96 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %95, ptr %96, align 8, !tbaa !15
  %97 = load ptr, ptr %7, align 8, !tbaa !5
  %98 = load i64, ptr %97, align 8, !tbaa !15
  %99 = icmp ne i64 %98, 0
  %100 = zext i1 %99 to i64
  %101 = select i1 %99, i32 1, i32 0
  store i32 %101, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %128

102:                                              ; preds = %68
  %103 = load ptr, ptr %6, align 8, !tbaa !5
  %104 = icmp eq ptr %103, null
  br i1 %104, label %105, label %107

105:                                              ; preds = %102
  %106 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 64, ptr %106, align 8, !tbaa !15
  store i32 1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %128

107:                                              ; preds = %102
  %108 = load ptr, ptr %10, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.gost_pmeth_data, ptr %108, i32 0, i32 2
  %110 = getelementptr inbounds [32 x i8], ptr %109, i64 0, i64 0
  %111 = load ptr, ptr %8, align 8, !tbaa !5
  %112 = call i32 @EVP_PKEY_get_id(ptr noundef %111)
  %113 = load ptr, ptr %9, align 8, !tbaa !5
  %114 = call ptr @EVP_PKEY_get0(ptr noundef %113)
  %115 = call ptr @EC_KEY_get0_public_key(ptr noundef %114)
  %116 = load ptr, ptr %8, align 8, !tbaa !5
  %117 = call ptr @EVP_PKEY_get0(ptr noundef %116)
  %118 = load ptr, ptr %6, align 8, !tbaa !5
  %119 = call i32 @gost_keg(ptr noundef %110, i32 noundef %112, ptr noundef %115, ptr noundef %117, ptr noundef %118)
  %120 = sext i32 %119 to i64
  %121 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %120, ptr %121, align 8, !tbaa !15
  %122 = load ptr, ptr %7, align 8, !tbaa !5
  %123 = load i64, ptr %122, align 8, !tbaa !15
  %124 = icmp ne i64 %123, 0
  %125 = zext i1 %124 to i64
  %126 = select i1 %124, i32 1, i32 0
  store i32 %126, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %128

127:                                              ; preds = %68
  store i32 0, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %128

128:                                              ; preds = %127, %107, %105, %83, %75, %44, %35, %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %129 = load i32, ptr %4, align 4
  ret i32 %129
}

declare ptr @EVP_PKEY_CTX_get0_pkey(ptr noundef) #2

declare ptr @EVP_PKEY_CTX_get0_peerkey(ptr noundef) #2

declare ptr @EVP_PKEY_CTX_get_data(ptr noundef) #2

declare ptr @EC_KEY_get0_public_key(ptr noundef) #2

declare ptr @EVP_PKEY_get0(ptr noundef) #2

declare i32 @EVP_PKEY_get_default_digest_nid(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @gost_keg(ptr noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) #0

declare i32 @EVP_PKEY_get_id(ptr noundef) #2

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = !{!12, !13, i64 48}
!12 = !{!"gost_pmeth_data", !10, i64 0, !6, i64 8, !7, i64 16, !13, i64 48, !10, i64 56, !10, i64 60, !10, i64 64}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !10, i64 64}
!15 = !{!13, !13, i64 0}


; 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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @pkey_gost_ec_derive(ptr noundef %0, ptr noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = alloca i32, align 4
  %5 = tail call ptr @EVP_PKEY_CTX_get0_pkey(ptr noundef %0) #3
  %6 = tail call ptr @EVP_PKEY_CTX_get0_peerkey(ptr noundef %0) #3
  %7 = tail call ptr @EVP_PKEY_CTX_get_data(ptr noundef %0) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %12, label %8

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %7, i64 48
  %10 = load i64, ptr %9, align 8, !tbaa !9
  %11 = icmp eq i64 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %8, %3
  tail call void @ERR_GOST_error(i32 noundef 127, i32 noundef 129, ptr noundef nonnull @.str, i32 noundef 202) #3
  br label %57

13:                                               ; preds = %8
  %14 = getelementptr inbounds i8, ptr %7, i64 64
  %15 = load i32, ptr %14, align 8, !tbaa !13
  %.not40 = icmp eq i32 %15, 0
  br i1 %.not40, label %29, label %16

16:                                               ; preds = %13
  %.not43 = icmp eq ptr %1, null
  br i1 %.not43, label %17, label %20

17:                                               ; preds = %16
  %18 = icmp eq i32 %15, 982
  %19 = select i1 %18, i64 32, i64 64
  store i64 %19, ptr %2, align 8, !tbaa !14
  br label %57

20:                                               ; preds = %16
  %21 = tail call ptr @EVP_PKEY_get0(ptr noundef %6) #3
  %22 = tail call ptr @EC_KEY_get0_public_key(ptr noundef %21) #3
  %23 = tail call ptr @EVP_PKEY_get0(ptr noundef %5) #3
  %24 = getelementptr inbounds i8, ptr %7, i64 16
  %25 = load i64, ptr %9, align 8, !tbaa !9
  %26 = load i32, ptr %14, align 8, !tbaa !13
  %27 = tail call i32 @VKO_compute_key(ptr noundef nonnull %1, ptr noundef %22, ptr noundef %23, ptr noundef nonnull %24, i64 noundef %25, i32 noundef %26)
  %28 = sext i32 %27 to i64
  store i64 %28, ptr %2, align 8, !tbaa !14
  %.not44 = icmp ne i32 %27, 0
  br label %57

29:                                               ; preds = %13
  switch i64 %10, label %57 [
    i64 8, label %30
    i64 32, label %46
  ]

30:                                               ; preds = %29
  %31 = icmp eq ptr %1, null
  br i1 %31, label %32, label %33

32:                                               ; preds = %30
  store i64 32, ptr %2, align 8, !tbaa !14
  br label %57

33:                                               ; preds = %30
  %34 = call i32 @EVP_PKEY_get_default_digest_nid(ptr noundef %5, ptr noundef nonnull %4) #3
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = icmp eq i32 %35, 983
  br i1 %36, label %37, label %38

37:                                               ; preds = %33
  store i32 982, ptr %4, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %37, %33
  %39 = call ptr @EVP_PKEY_get0(ptr noundef %6) #3
  %40 = call ptr @EC_KEY_get0_public_key(ptr noundef %39) #3
  %41 = call ptr @EVP_PKEY_get0(ptr noundef %5) #3
  %42 = getelementptr inbounds i8, ptr %7, i64 16
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = call i32 @VKO_compute_key(ptr noundef nonnull %1, ptr noundef %40, ptr noundef %41, ptr noundef nonnull %42, i64 noundef 8, i32 noundef %43)
  %45 = sext i32 %44 to i64
  store i64 %45, ptr %2, align 8, !tbaa !14
  %.not42 = icmp ne i32 %44, 0
  br label %57

46:                                               ; preds = %29
  %47 = icmp eq ptr %1, null
  br i1 %47, label %48, label %49

48:                                               ; preds = %46
  store i64 64, ptr %2, align 8, !tbaa !14
  br label %57

49:                                               ; preds = %46
  %50 = getelementptr inbounds i8, ptr %7, i64 16
  %51 = tail call i32 @EVP_PKEY_get_id(ptr noundef %5) #3
  %52 = tail call ptr @EVP_PKEY_get0(ptr noundef %6) #3
  %53 = tail call ptr @EC_KEY_get0_public_key(ptr noundef %52) #3
  %54 = tail call ptr @EVP_PKEY_get0(ptr noundef %5) #3
  %55 = tail call i32 @gost_keg(ptr noundef nonnull %50, i32 noundef %51, ptr noundef %53, ptr noundef %54, ptr noundef nonnull %1)
  %56 = sext i32 %55 to i64
  store i64 %56, ptr %2, align 8, !tbaa !14
  %.not41 = icmp ne i32 %55, 0
  br label %57

57:                                               ; preds = %29, %49, %48, %38, %32, %20, %17, %12
  %.0.shrunk = phi i1 [ false, %12 ], [ %.not44, %20 ], [ true, %17 ], [ true, %48 ], [ %.not41, %49 ], [ true, %32 ], [ %.not42, %38 ], [ false, %29 ]
  %.0 = zext i1 %.0.shrunk to i32
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  ret i32 %.0
}

declare ptr @EVP_PKEY_CTX_get0_pkey(ptr noundef) local_unnamed_addr #2

declare ptr @EVP_PKEY_CTX_get0_peerkey(ptr noundef) local_unnamed_addr #2

declare ptr @EVP_PKEY_CTX_get_data(ptr noundef) local_unnamed_addr #2

declare ptr @EC_KEY_get0_public_key(ptr noundef) local_unnamed_addr #2

declare ptr @EVP_PKEY_get0(ptr noundef) local_unnamed_addr #2

declare i32 @EVP_PKEY_get_default_digest_nid(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i32 @gost_keg(ptr noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

declare i32 @EVP_PKEY_get_id(ptr noundef) local_unnamed_addr #2

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 48}
!10 = !{!"gost_pmeth_data", !6, i64 0, !11, i64 8, !7, i64 16, !12, i64 48, !6, i64 56, !6, i64 60, !6, i64 64}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !6, i64 64}
!14 = !{!12, !12, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass