#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*/);
size_t ECDH_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_get0_public_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(size_t);
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 = ECDH_size(key1);
  if (Ztmplen != Zlen)
    goto err;
  Ztmp = 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 (rv)
    BIO_puts(out, " ok\n");
  else {
    fprintf(stderr, "Error in ECDH routines\n");
    ERR_print_errors_fp(stderr);
  }
  EC_KEY_free(key1);
  EC_KEY_free(key2);
  free(Ztmp);
  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    0xde8(%rip),%rsi
call   10b0 <BIO_puts@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <BIO_puts@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <mk_eckey@plt>
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x10(%rbp),%rdx
call   1060 <mk_eckey@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1268 <ecdh_kat+0xa8>
cmpq   $0x0,-0x48(%rbp)
jne    126d <ecdh_kat+0xad>
jmp    136a <ecdh_kat+0x1aa>
mov    -0x40(%rbp),%rdi
call   10c0 <ECDH_size@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    0x20(%rbp),%rax
je     128d <ecdh_kat+0xcd>
jmp    136a <ecdh_kat+0x1aa>
mov    -0x58(%rbp),%rdi
call   1090 <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   1040 <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   10e0 <ECDH_compute_key@plt>
cmp    $0x0,%eax
jne    12d7 <ecdh_kat+0x117>
jmp    136a <ecdh_kat+0x1aa>
mov    -0x50(%rbp),%rdi
mov    0x18(%rbp),%rsi
mov    0x20(%rbp),%rdx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     12f7 <ecdh_kat+0x137>
jmp    136a <ecdh_kat+0x1aa>
mov    -0x50(%rbp),%rdi
mov    0x20(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <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   10e0 <ECDH_compute_key@plt>
cmp    $0x0,%eax
jne    1343 <ecdh_kat+0x183>
jmp    136a <ecdh_kat+0x1aa>
mov    -0x50(%rbp),%rdi
mov    0x18(%rbp),%rsi
mov    0x20(%rbp),%rdx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     1363 <ecdh_kat+0x1a3>
jmp    136a <ecdh_kat+0x1aa>
movl   $0x1,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1389 <ecdh_kat+0x1c9>
mov    -0x8(%rbp),%rdi
lea    0xca2(%rip),%rsi
call   10b0 <BIO_puts@plt>
jmp    13ac <ecdh_kat+0x1ec>
mov    0x2c50(%rip),%rax
mov    (%rax),%edi
lea    0xc8d(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2c3b(%rip),%rax
mov    (%rax),%edi
call   10d0 <ERR_print_errors_fp@plt>
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   1030 <free@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,%r14
mov    %r8,%r15
mov    %rcx,%r12
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %rdi,%r13
lea    0xe1d(%rip),%rsi
call   10b0 <BIO_puts@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   10b0 <BIO_puts@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1060 <mk_eckey@plt>
mov    %rax,%rbp
mov    %ebx,%edi
mov    %r14,%rsi
mov    0x40(%rsp),%rdx
call   1060 <mk_eckey@plt>
mov    %rax,%r14
xor    %r15d,%r15d
mov    $0x0,%r12d
test   %rbp,%rbp
je     12e0 <ecdh_kat+0x120>
test   %r14,%r14
je     12e0 <ecdh_kat+0x120>
mov    0x50(%rsp),%rbx
mov    %rbp,%rdi
call   10c0 <ECDH_size@plt>
cmp    %rbx,%rax
jne    12dd <ecdh_kat+0x11d>
mov    %rbx,%rdi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <EC_KEY_get0_public_key@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
mov    %rbp,%rcx
xor    %r8d,%r8d
call   10e0 <ECDH_compute_key@plt>
test   %eax,%eax
je     12e0 <ecdh_kat+0x120>
mov    0x48(%rsp),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
call   1070 <memcmp@plt>
test   %rax,%rax
jne    12e0 <ecdh_kat+0x120>
mov    %r12,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    %rbp,%rdi
call   1040 <EC_KEY_get0_public_key@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
mov    %r14,%rcx
xor    %r8d,%r8d
call   10e0 <ECDH_compute_key@plt>
test   %eax,%eax
je     12e0 <ecdh_kat+0x120>
mov    %r12,%rdi
mov    0x48(%rsp),%rsi
mov    %rbx,%rdx
call   1070 <memcmp@plt>
test   %rax,%rax
jne    12e0 <ecdh_kat+0x120>
lea    0xd54(%rip),%rsi
mov    %r13,%rdi
call   10b0 <BIO_puts@plt>
mov    $0x1,%r15d
jmp    12fc <ecdh_kat+0x13c>
xor    %r12d,%r12d
mov    0x2cf9(%rip),%rbx
mov    (%rbx),%edi
lea    0xd36(%rip),%rsi
call   1080 <fprintf@plt>
mov    (%rbx),%edi
call   10d0 <ERR_print_errors_fp@plt>
mov    %rbp,%rdi
call   10a0 <EC_KEY_free@plt>
mov    %r14,%rdi
call   10a0 <EC_KEY_free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15d,%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 %88
43: ; preds = %39
%44 = load i32*, i32** %20
%45 = call i64 @ECDH_size(i32* %44)
store i64 %45, i64* %23
%46 = load i64, i64* %23
%47 = load i64, i64* %18
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %43
br label %88
50: ; preds = %43
%51 = load i64, i64* %23
%52 = call i8* @malloc(i64 %51)
store i8* %52, i8** %22
%53 = load i8*, i8** %22
%54 = load i64, i64* %23
%55 = load i32*, i32** %21
%56 = call i32 @EC_KEY_get0_public_key(i32* %55)
%57 = load i32*, i32** %20
%58 = call i32 @ECDH_compute_key(i8* %53, i64 %54, i32 %56, i32* %57, i32 0)
%59 = icmp ne i32 %58, 0
br i1 %59, label %61, label %60
60: ; preds = %50
br label %88
61: ; preds = %50
%62 = load i8*, i8** %22
%63 = load i8*, i8** %17
%64 = load i64, i64* %18
%65 = call i64 @memcmp(i8* %62, i8* %63, i64 %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %61
br label %88
68: ; preds = %61
%69 = load i8*, i8** %22
%70 = load i64, i64* %18
%71 = call i32 @memset(i8* %69, i32 0, i64 %70)
%72 = load i8*, i8** %22
%73 = load i64, i64* %23
%74 = load i32*, i32** %20
%75 = call i32 @EC_KEY_get0_public_key(i32* %74)
%76 = load i32*, i32** %21
%77 = call i32 @ECDH_compute_key(i8* %72, i64 %73, i32 %75, i32* %76, i32 0)
%78 = icmp ne i32 %77, 0
br i1 %78, label %80, label %79
79: ; preds = %68
br label %88
80: ; preds = %68
%81 = load i8*, i8** %22
%82 = load i8*, i8** %17
%83 = load i64, i64* %18
%84 = call i64 @memcmp(i8* %81, i8* %82, i64 %83)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %87
86: ; preds = %80
br label %88
87: ; preds = %80
store i32 1, i32* %19
br label %88
88: ; preds = %87, %86, %79, %67, %60, %49, %42
%89 = load i32, i32* %19
%90 = icmp ne i32 %89, 0
br i1 %90, label %91, label %94
91: ; preds = %88
%92 = load i32*, i32** %10
%93 = call i32 @BIO_puts(i32* %92, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %99
94: ; preds = %88
%95 = load i32, i32* @stderr
%96 = call i32 @fprintf(i32 %95, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%97 = load i32, i32* @stderr
%98 = call i32 @ERR_print_errors_fp(i32 %97)
br label %99
99: ; preds = %94, %91
%100 = load i32*, i32** %20
%101 = call i32 @EC_KEY_free(i32* %100)
%102 = load i32*, i32** %21
%103 = call i32 @EC_KEY_free(i32* %102)
%104 = load i8*, i8** %22
%105 = call i32 @free(i8* %104)
%106 = load i32, i32* %19
ret i32 %106
}

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 %38
17: ; preds = %9
%18 = tail call i64 @ECDH_size(i32* %12)
%19 = icmp eq i64 %18, %8
br i1 %19, label %20, label %38
20: ; preds = %17
%21 = tail call i8* @malloc(i64 %8)
%22 = tail call i32 @EC_KEY_get0_public_key(i32* %13)
%23 = tail call i32 @ECDH_compute_key(i8* %21, i64 %8, i32 %22, i32* %12, i32 0)
%24 = icmp eq i32 %23, 0
br i1 %24, label %38, label %25
25: ; preds = %20
%26 = tail call i64 @memcmp(i8* %21, i8* %7, i64 %8)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %25
%29 = tail call i32 @memset(i8* %21, i32 0, i64 %8)
%30 = tail call i32 @EC_KEY_get0_public_key(i32* %12)
%31 = tail call i32 @ECDH_compute_key(i8* %21, i64 %8, i32 %30, i32* %13, i32 0)
%32 = icmp eq i32 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %28
%34 = tail call i64 @memcmp(i8* %21, i8* %7, i64 %8)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = tail call i32 @BIO_puts(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %44
38: ; preds = %9, %20, %28, %25, %17, %33
%39 = phi i8* [ %21, %33 ], [ null, %17 ], [ %21, %25 ], [ %21, %28 ], [ %21, %20 ], [ null, %9 ]
%40 = load i32, i32* @stderr
%41 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @stderr
%43 = tail call i32 @ERR_print_errors_fp(i32 %42)
br label %44
44: ; preds = %38, %36
%45 = phi i32 [ 0, %38 ], [ 1, %36 ]
%46 = phi i8* [ %39, %38 ], [ %21, %36 ]
%47 = tail call i32 @EC_KEY_free(i32* %12)
%48 = tail call i32 @EC_KEY_free(i32* %13)
%49 = tail call i32 @free(i8* %46)
ret i32 %45
}