#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ EC_KEY;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_puts(int /*<<< orphan*/ *, char const *);
int /*<<< orphan*/ ECDH_compute_key(unsigned char *, size_t, int /*<<< orphan*/,
                                    int /*<<< orphan*/ *, int /*<<< orphan*/);
int EC_GROUP_get_degree(int /*<<< orphan*/);
int /*<<< orphan*/ EC_KEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_get0_group(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_get0_public_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ OPENSSL_free(unsigned char *);
unsigned char *OPENSSL_malloc(size_t);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ memcmp(unsigned char *, unsigned char const *, size_t);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ *mk_eckey(int, unsigned char const *, size_t);
int /*<<< orphan*/ stderr;
int ecdh_kat(BIO *out, const char *cname, int nid, const unsigned char *k1,
             size_t k1_len, const unsigned char *k2, size_t k2_len,
             const unsigned char *Z, size_t Zlen) {
  int rv = 0;
  EC_KEY *key1 = NULL, *key2 = NULL;
  unsigned char *Ztmp = NULL;
  size_t Ztmplen;
  BIO_puts(out, "Testing ECDH shared secret with ");
  BIO_puts(out, cname);
  key1 = mk_eckey(nid, k1, k1_len);
  key2 = mk_eckey(nid, k2, k2_len);
  if (!key1 || !key2)
    goto err;
  Ztmplen = (EC_GROUP_get_degree(EC_KEY_get0_group(key1)) + 7) / 8;
  if (Ztmplen != Zlen)
    goto err;
  Ztmp = OPENSSL_malloc(Ztmplen);
  if (!ECDH_compute_key(Ztmp, Ztmplen, EC_KEY_get0_public_key(key2), key1, 0))
    goto err;
  if (memcmp(Ztmp, Z, Zlen))
    goto err;
  memset(Ztmp, 0, Zlen);
  if (!ECDH_compute_key(Ztmp, Ztmplen, EC_KEY_get0_public_key(key1), key2, 0))
    goto err;
  if (memcmp(Ztmp, Z, Zlen))
    goto err;
  rv = 1;
err:
  EC_KEY_free(key1);
  EC_KEY_free(key2);
  OPENSSL_free(Ztmp);
  if (rv)
    BIO_puts(out, " ok\n");
  else {
    fprintf(stderr, "Error in ECDH routines\n");
    ERR_print_errors_fp(stderr);
  }
  return rv;
}


x86 assembly (clang, -O0):
<ecdh_kat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdd8(%rip),%rsi
call   10c0 <BIO_puts@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <BIO_puts@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <mk_eckey@plt>
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x10(%rbp),%rdx
call   1050 <mk_eckey@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1278 <ecdh_kat+0xa8>
cmpq   $0x0,-0x48(%rbp)
jne    127d <ecdh_kat+0xad>
jmp    138e <ecdh_kat+0x1be>
mov    -0x40(%rbp),%rdi
call   1080 <EC_KEY_get0_group@plt>
mov    %eax,%edi
call   10b0 <EC_GROUP_get_degree@plt>
add    $0x7,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    0x20(%rbp),%rax
je     12b1 <ecdh_kat+0xe1>
jmp    138e <ecdh_kat+0x1be>
mov    -0x58(%rbp),%rdi
call   1090 <OPENSSL_malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <EC_KEY_get0_public_key@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
mov    -0x40(%rbp),%rcx
xor    %r8d,%r8d
call   10f0 <ECDH_compute_key@plt>
cmp    $0x0,%eax
jne    12fb <ecdh_kat+0x12b>
jmp    138e <ecdh_kat+0x1be>
mov    -0x50(%rbp),%rdi
mov    0x18(%rbp),%rsi
mov    0x20(%rbp),%rdx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     131b <ecdh_kat+0x14b>
jmp    138e <ecdh_kat+0x1be>
mov    -0x50(%rbp),%rdi
mov    0x20(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <EC_KEY_get0_public_key@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
mov    -0x48(%rbp),%rcx
xor    %r8d,%r8d
call   10f0 <ECDH_compute_key@plt>
cmp    $0x0,%eax
jne    1367 <ecdh_kat+0x197>
jmp    138e <ecdh_kat+0x1be>
mov    -0x50(%rbp),%rdi
mov    0x18(%rbp),%rsi
mov    0x20(%rbp),%rdx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     1387 <ecdh_kat+0x1b7>
jmp    138e <ecdh_kat+0x1be>
movl   $0x1,-0x34(%rbp)
mov    -0x40(%rbp),%rdi
call   10a0 <EC_KEY_free@plt>
mov    -0x48(%rbp),%rdi
call   10a0 <EC_KEY_free@plt>
mov    -0x50(%rbp),%rdi
call   10d0 <OPENSSL_free@plt>
cmpl   $0x0,-0x34(%rbp)
je     13c8 <ecdh_kat+0x1f8>
mov    -0x8(%rbp),%rdi
lea    0xc63(%rip),%rsi
call   10c0 <BIO_puts@plt>
jmp    13eb <ecdh_kat+0x21b>
mov    0x2c11(%rip),%rax
mov    (%rax),%edi
lea    0xc4e(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2bfc(%rip),%rax
mov    (%rax),%edi
call   10e0 <ERR_print_errors_fp@plt>
mov    -0x34(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ecdh_kat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r15
mov    %r8,%r12
mov    %rcx,%r13
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe0d(%rip),%rsi
call   10c0 <BIO_puts@plt>
mov    %r14,(%rsp)
mov    %r14,%rdi
mov    %rbp,%rsi
call   10c0 <BIO_puts@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   1050 <mk_eckey@plt>
mov    %rax,%r13
mov    %ebx,%edi
mov    %r15,%rsi
mov    0x40(%rsp),%rdx
call   1050 <mk_eckey@plt>
mov    %rax,%r15
xor    %r12d,%r12d
mov    $0x1,%bl
test   %r13,%r13
je     12fd <ecdh_kat+0x12d>
test   %r15,%r15
je     12fd <ecdh_kat+0x12d>
mov    0x50(%rsp),%r14
mov    %r13,%rdi
call   1080 <EC_KEY_get0_group@plt>
mov    %eax,%edi
call   10b0 <EC_GROUP_get_degree@plt>
lea    0x7(%rax),%ecx
add    $0xe,%eax
test   %ecx,%ecx
cmovns %ecx,%eax
sar    $0x3,%eax
cltq
cmp    %r14,%rax
jne    12fa <ecdh_kat+0x12a>
mov    %r14,%rdi
call   1090 <OPENSSL_malloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1030 <EC_KEY_get0_public_key@plt>
xor    %ebp,%ebp
mov    %r12,%rdi
mov    %r14,%rsi
mov    %eax,%edx
mov    %r13,%rcx
xor    %r8d,%r8d
call   10f0 <ECDH_compute_key@plt>
test   %eax,%eax
je     12ff <ecdh_kat+0x12f>
mov    0x48(%rsp),%rsi
mov    %r12,%rdi
mov    %r14,%rdx
call   1060 <memcmp@plt>
test   %rax,%rax
jne    12ff <ecdh_kat+0x12f>
xor    %ebp,%ebp
mov    %r12,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <memset@plt>
mov    %r13,%rdi
call   1030 <EC_KEY_get0_public_key@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %eax,%edx
mov    %r15,%rcx
xor    %r8d,%r8d
call   10f0 <ECDH_compute_key@plt>
test   %eax,%eax
je     12ff <ecdh_kat+0x12f>
mov    %r12,%rdi
mov    0x48(%rsp),%rsi
mov    %r14,%rdx
call   1060 <memcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bl
sete   %bpl
jmp    12ff <ecdh_kat+0x12f>
xor    %r12d,%r12d
xor    %ebp,%ebp
mov    %r13,%rdi
call   10a0 <EC_KEY_free@plt>
mov    %r15,%rdi
call   10a0 <EC_KEY_free@plt>
mov    %r12,%rdi
call   10d0 <OPENSSL_free@plt>
test   %bl,%bl
je     1339 <ecdh_kat+0x169>
mov    0x2cbe(%rip),%rbx
mov    (%rbx),%edi
lea    0xcfb(%rip),%rsi
call   1070 <fprintf@plt>
mov    (%rbx),%edi
call   10e0 <ERR_print_errors_fp@plt>
jmp    1349 <ecdh_kat+0x179>
lea    0xce1(%rip),%rsi
mov    (%rsp),%rdi
call   10c0 <BIO_puts@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"Testing ECDH shared secret with \00"
@.str.1 = [5 x i8] c" ok\0A\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Error in ECDH routines\0A\00"
define i32 @ecdh_kat(i32* %0, i8* %1, i32 %2, i8* %3, i64 %4, i8* %5, i64 %6, i8* %7, i64 %8) {
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i64
%17 = alloca i8*
%18 = alloca i64
%19 = alloca i32
%20 = alloca i32*
%21 = alloca i32*
%22 = alloca i8*
%23 = alloca i64
store i32* %0, i32** %10
store i8* %1, i8** %11
store i32 %2, i32* %12
store i8* %3, i8** %13
store i64 %4, i64* %14
store i8* %5, i8** %15
store i64 %6, i64* %16
store i8* %7, i8** %17
store i64 %8, i64* %18
store i32 0, i32* %19
store i32* null, i32** %20
store i32* null, i32** %21
store i8* null, i8** %22
%24 = load i32*, i32** %10
%25 = call i32 @BIO_puts(i32* %24, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%26 = load i32*, i32** %10
%27 = load i8*, i8** %11
%28 = call i32 @BIO_puts(i32* %26, i8* %27)
%29 = load i32, i32* %12
%30 = load i8*, i8** %13
%31 = load i64, i64* %14
%32 = call i32* @mk_eckey(i32 %29, i8* %30, i64 %31)
store i32* %32, i32** %20
%33 = load i32, i32* %12
%34 = load i8*, i8** %15
%35 = load i64, i64* %16
%36 = call i32* @mk_eckey(i32 %33, i8* %34, i64 %35)
store i32* %36, i32** %21
%37 = load i32*, i32** %20
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %42
39: ; preds = %9
%40 = load i32*, i32** %21
%41 = icmp ne i32* %40, null
br i1 %41, label %43, label %42
42: ; preds = %39, %9
br label %92
43: ; preds = %39
%44 = load i32*, i32** %20
%45 = call i32 @EC_KEY_get0_group(i32* %44)
%46 = call i32 @EC_GROUP_get_degree(i32 %45)
%47 = add nsw i32 %46, 7
%48 = sdiv i32 %47, 8
%49 = sext i32 %48 to i64
store i64 %49, i64* %23
%50 = load i64, i64* %23
%51 = load i64, i64* %18
%52 = icmp ne i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %43
br label %92
54: ; preds = %43
%55 = load i64, i64* %23
%56 = call i8* @OPENSSL_malloc(i64 %55)
store i8* %56, i8** %22
%57 = load i8*, i8** %22
%58 = load i64, i64* %23
%59 = load i32*, i32** %21
%60 = call i32 @EC_KEY_get0_public_key(i32* %59)
%61 = load i32*, i32** %20
%62 = call i32 @ECDH_compute_key(i8* %57, i64 %58, i32 %60, i32* %61, i32 0)
%63 = icmp ne i32 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %54
br label %92
65: ; preds = %54
%66 = load i8*, i8** %22
%67 = load i8*, i8** %17
%68 = load i64, i64* %18
%69 = call i64 @memcmp(i8* %66, i8* %67, i64 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %65
br label %92
72: ; preds = %65
%73 = load i8*, i8** %22
%74 = load i64, i64* %18
%75 = call i32 @memset(i8* %73, i32 0, i64 %74)
%76 = load i8*, i8** %22
%77 = load i64, i64* %23
%78 = load i32*, i32** %20
%79 = call i32 @EC_KEY_get0_public_key(i32* %78)
%80 = load i32*, i32** %21
%81 = call i32 @ECDH_compute_key(i8* %76, i64 %77, i32 %79, i32* %80, i32 0)
%82 = icmp ne i32 %81, 0
br i1 %82, label %84, label %83
83: ; preds = %72
br label %92
84: ; preds = %72
%85 = load i8*, i8** %22
%86 = load i8*, i8** %17
%87 = load i64, i64* %18
%88 = call i64 @memcmp(i8* %85, i8* %86, i64 %87)
%89 = icmp ne i64 %88, 0
br i1 %89, label %90, label %91
90: ; preds = %84
br label %92
91: ; preds = %84
store i32 1, i32* %19
br label %92
92: ; preds = %91, %90, %83, %71, %64, %53, %42
%93 = load i32*, i32** %20
%94 = call i32 @EC_KEY_free(i32* %93)
%95 = load i32*, i32** %21
%96 = call i32 @EC_KEY_free(i32* %95)
%97 = load i8*, i8** %22
%98 = call i32 @OPENSSL_free(i8* %97)
%99 = load i32, i32* %19
%100 = icmp ne i32 %99, 0
br i1 %100, label %101, label %104
101: ; preds = %92
%102 = load i32*, i32** %10
%103 = call i32 @BIO_puts(i32* %102, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %109
104: ; preds = %92
%105 = load i32, i32* @stderr
%106 = call i32 @fprintf(i32 %105, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%107 = load i32, i32* @stderr
%108 = call i32 @ERR_print_errors_fp(i32 %107)
br label %109
109: ; preds = %104, %101
%110 = load i32, i32* %19
ret i32 %110
}

LLVM IR (-O2):
@.str = [33 x i8] c"Testing ECDH shared secret with \00"
@.str.1 = [5 x i8] c" ok\0A\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Error in ECDH routines\0A\00"
define i32 @ecdh_kat(i32* %0, i8* %1, i32 %2, i8* %3, i64 %4, i8* %5, i64 %6, i8* %7, i64 %8) {
%10 = tail call i32 @BIO_puts(i32* %0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @BIO_puts(i32* %0, i8* %1)
%12 = tail call i32* @mk_eckey(i32 %2, i8* %3, i64 %4)
%13 = tail call i32* @mk_eckey(i32 %2, i8* %5, i64 %6)
%14 = icmp ne i32* %12, null
%15 = icmp ne i32* %13, null
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %17, label %42
17: ; preds = %9
%18 = tail call i32 @EC_KEY_get0_group(i32* %12)
%19 = tail call i32 @EC_GROUP_get_degree(i32 %18)
%20 = add nsw i32 %19, 7
%21 = sdiv i32 %20, 8
%22 = sext i32 %21 to i64
%23 = icmp eq i64 %22, %8
br i1 %23, label %24, label %42
24: ; preds = %17
%25 = tail call i8* @OPENSSL_malloc(i64 %8)
%26 = tail call i32 @EC_KEY_get0_public_key(i32* %13)
%27 = tail call i32 @ECDH_compute_key(i8* %25, i64 %8, i32 %26, i32* %12, i32 0)
%28 = icmp eq i32 %27, 0
br i1 %28, label %42, label %29
29: ; preds = %24
%30 = tail call i64 @memcmp(i8* %25, i8* %7, i64 %8)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %29
%33 = tail call i32 @memset(i8* %25, i32 0, i64 %8)
%34 = tail call i32 @EC_KEY_get0_public_key(i32* %12)
%35 = tail call i32 @ECDH_compute_key(i8* %25, i64 %8, i32 %34, i32* %13, i32 0)
%36 = icmp eq i32 %35, 0
br i1 %36, label %42, label %37
37: ; preds = %32
%38 = tail call i64 @memcmp(i8* %25, i8* %7, i64 %8)
%39 = icmp eq i64 %38, 0
%40 = xor i1 %39, true
%41 = zext i1 %39 to i32
br label %42
42: ; preds = %37, %32, %29, %24, %17, %9
%43 = phi i8* [ null, %17 ], [ %25, %29 ], [ %25, %32 ], [ %25, %24 ], [ null, %9 ], [ %25, %37 ]
%44 = phi i1 [ true, %17 ], [ true, %29 ], [ true, %32 ], [ true, %24 ], [ true, %9 ], [ %40, %37 ]
%45 = phi i32 [ 0, %17 ], [ 0, %29 ], [ 0, %32 ], [ 0, %24 ], [ 0, %9 ], [ %41, %37 ]
%46 = tail call i32 @EC_KEY_free(i32* %12)
%47 = tail call i32 @EC_KEY_free(i32* %13)
%48 = tail call i32 @OPENSSL_free(i8* %43)
br i1 %44, label %51, label %49
49: ; preds = %42
%50 = tail call i32 @BIO_puts(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %56
51: ; preds = %42
%52 = load i32, i32* @stderr
%53 = tail call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32, i32* @stderr
%55 = tail call i32 @ERR_print_errors_fp(i32 %54)
br label %56
56: ; preds = %51, %49
ret i32 %45
}