#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:
  if (key1)
    EC_KEY_free(key1);
  if (key2)
    EC_KEY_free(key2);
  if (Ztmp)
    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)
cmpq   $0x0,-0x40(%rbp)
je     13a2 <ecdh_kat+0x1d2>
mov    -0x40(%rbp),%rdi
call   10a0 <EC_KEY_free@plt>
cmpq   $0x0,-0x48(%rbp)
je     13b6 <ecdh_kat+0x1e6>
mov    -0x48(%rbp),%rdi
call   10a0 <EC_KEY_free@plt>
cmpq   $0x0,-0x50(%rbp)
je     13ca <ecdh_kat+0x1fa>
mov    -0x50(%rbp),%rdi
call   10d0 <OPENSSL_free@plt>
cmpl   $0x0,-0x34(%rbp)
je     13e9 <ecdh_kat+0x219>
mov    -0x8(%rbp),%rdi
lea    0xc42(%rip),%rsi
call   10c0 <BIO_puts@plt>
jmp    140c <ecdh_kat+0x23c>
mov    0x2bf0(%rip),%rax
mov    (%rax),%edi
lea    0xc2d(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2bdb(%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     136e <ecdh_kat+0x19e>
test   %r15,%r15
je     12ff <ecdh_kat+0x12f>
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    136b <ecdh_kat+0x19b>
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     1301 <ecdh_kat+0x131>
mov    0x48(%rsp),%rsi
mov    %r12,%rdi
mov    %r14,%rdx
call   1060 <memcmp@plt>
test   %rax,%rax
jne    1301 <ecdh_kat+0x131>
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     1301 <ecdh_kat+0x131>
mov    %r12,%rdi
mov    0x48(%rsp),%rsi
mov    %r14,%rdx
call   1060 <memcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bl
sete   %bpl
test   %r13,%r13
jne    1306 <ecdh_kat+0x136>
jmp    130e <ecdh_kat+0x13e>
xor    %ebp,%ebp
test   %r13,%r13
je     130e <ecdh_kat+0x13e>
mov    %r13,%rdi
call   10a0 <EC_KEY_free@plt>
test   %r15,%r15
je     131b <ecdh_kat+0x14b>
mov    %r15,%rdi
call   10a0 <EC_KEY_free@plt>
test   %r12,%r12
je     1328 <ecdh_kat+0x158>
mov    %r12,%rdi
call   10d0 <OPENSSL_free@plt>
test   %bl,%bl
je     134a <ecdh_kat+0x17a>
mov    0x2cad(%rip),%rbx
mov    (%rbx),%edi
lea    0xcea(%rip),%rsi
call   1070 <fprintf@plt>
mov    (%rbx),%edi
call   10e0 <ERR_print_errors_fp@plt>
jmp    135a <ecdh_kat+0x18a>
lea    0xcd0(%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
xor    %r12d,%r12d
xor    %ebp,%ebp
test   %r13,%r13
jne    1306 <ecdh_kat+0x136>
jmp    130e <ecdh_kat+0x13e>

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 = icmp ne i32* %93, null
br i1 %94, label %95, label %98
95: ; preds = %92
%96 = load i32*, i32** %20
%97 = call i32 @EC_KEY_free(i32* %96)
br label %98
98: ; preds = %95, %92
%99 = load i32*, i32** %21
%100 = icmp ne i32* %99, null
br i1 %100, label %101, label %104
101: ; preds = %98
%102 = load i32*, i32** %21
%103 = call i32 @EC_KEY_free(i32* %102)
br label %104
104: ; preds = %101, %98
%105 = load i8*, i8** %22
%106 = icmp ne i8* %105, null
br i1 %106, label %107, label %110
107: ; preds = %104
%108 = load i8*, i8** %22
%109 = call i32 @OPENSSL_free(i8* %108)
br label %110
110: ; preds = %107, %104
%111 = load i32, i32* %19
%112 = icmp ne i32 %111, 0
br i1 %112, label %113, label %116
113: ; preds = %110
%114 = load i32*, i32** %10
%115 = call i32 @BIO_puts(i32* %114, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %121
116: ; preds = %110
%117 = load i32, i32* @stderr
%118 = call i32 @fprintf(i32 %117, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%119 = load i32, i32* @stderr
%120 = call i32 @ERR_print_errors_fp(i32 %119)
br label %121
121: ; preds = %116, %113
%122 = load i32, i32* %19
ret i32 %122
}

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 ]
br i1 %14, label %46, label %48
46: ; preds = %42
%47 = tail call i32 @EC_KEY_free(i32* %12)
br label %48
48: ; preds = %46, %42
br i1 %15, label %49, label %51
49: ; preds = %48
%50 = tail call i32 @EC_KEY_free(i32* %13)
br label %51
51: ; preds = %49, %48
%52 = icmp eq i8* %43, null
br i1 %52, label %55, label %53
53: ; preds = %51
%54 = tail call i32 @OPENSSL_free(i8* %43)
br label %55
55: ; preds = %53, %51
br i1 %44, label %58, label %56
56: ; preds = %55
%57 = tail call i32 @BIO_puts(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %63
58: ; preds = %55
%59 = load i32, i32* @stderr
%60 = tail call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%61 = load i32, i32* @stderr
%62 = tail call i32 @ERR_print_errors_fp(i32 %61)
br label %63
63: ; preds = %58, %56
ret i32 %45
}