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

/* Variables and functions */
int /*<<< orphan*/ CRYPTO_cleanup_all_ex_data();
int EC_DH(unsigned char **, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ EC_KEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_get0_public_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ *gen_key();
scalar_t__ memcmp(unsigned char *, unsigned char *, int);
int main(int argc, char **argv) {
  EC_KEY *lKey, *pKey;
  int lSecretLen, pSecretLen;
  unsigned char *lSecret, *pSecret;
  lKey = gen_key();
  pKey = gen_key();
  lSecretLen = EC_DH(&lSecret, lKey, EC_KEY_get0_public_key(pKey));
  pSecretLen = EC_DH(&pSecret, pKey, EC_KEY_get0_public_key(lKey));
  if (lSecretLen != pSecretLen)
    die("SecretLen mismatch.\n");
  if (memcmp(lSecret, pSecret, lSecretLen))
    die("Secrets don't match.\n");
  free(lSecret);
  free(pSecret);
  EC_KEY_free(lKey);
  EC_KEY_free(pKey);
  CRYPTO_cleanup_all_ex_data();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <gen_key@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <gen_key@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <EC_KEY_get0_public_key@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    -0x30(%rbp),%rdi
call   1070 <EC_DH@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <EC_KEY_get0_public_key@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    -0x38(%rbp),%rdi
call   1070 <EC_DH@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     120a <main+0x8a>
lea    0xdfb(%rip),%rdi
call   1090 <die@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     1230 <main+0xb0>
lea    0xdea(%rip),%rdi
call   1090 <die@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1080 <EC_KEY_free@plt>
mov    -0x20(%rbp),%rdi
call   1080 <EC_KEY_free@plt>
mov    $0x0,%al
call   1040 <CRYPTO_cleanup_all_ex_data@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10a0 <gen_key@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10a0 <gen_key@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <EC_KEY_get0_public_key@plt>
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <EC_DH@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <EC_KEY_get0_public_key@plt>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <EC_DH@plt>
cmp    %eax,%ebp
je     11da <main+0x5a>
lea    0xe2b(%rip),%rdi
call   1090 <die@plt>
mov    0x8(%rsp),%rdi
mov    (%rsp),%rsi
mov    %ebp,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     11fb <main+0x7b>
lea    0xe1f(%rip),%rdi
call   1090 <die@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1080 <EC_KEY_free@plt>
mov    %rbx,%rdi
call   1080 <EC_KEY_free@plt>
xor    %eax,%eax
call   1040 <CRYPTO_cleanup_all_ex_data@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"SecretLen mismatch.\0A\00"
@.str.1 = [22 x i8] c"Secrets don't match.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32* (...) @gen_key()
store i32* %12, i32** %6
%13 = call i32* (...) @gen_key()
store i32* %13, i32** %7
%14 = load i32*, i32** %6
%15 = load i32*, i32** %7
%16 = call i32 @EC_KEY_get0_public_key(i32* %15)
%17 = call i32 @EC_DH(i8** %10, i32* %14, i32 %16)
store i32 %17, i32* %8
%18 = load i32*, i32** %7
%19 = load i32*, i32** %6
%20 = call i32 @EC_KEY_get0_public_key(i32* %19)
%21 = call i32 @EC_DH(i8** %11, i32* %18, i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %8
%23 = load i32, i32* %9
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %2
%28 = load i8*, i8** %10
%29 = load i8*, i8** %11
%30 = load i32, i32* %8
%31 = call i64 @memcmp(i8* %28, i8* %29, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %27
%36 = load i8*, i8** %10
%37 = call i32 @free(i8* %36)
%38 = load i8*, i8** %11
%39 = call i32 @free(i8* %38)
%40 = load i32*, i32** %6
%41 = call i32 @EC_KEY_free(i32* %40)
%42 = load i32*, i32** %7
%43 = call i32 @EC_KEY_free(i32* %42)
%44 = call i32 (...) @CRYPTO_cleanup_all_ex_data()
ret i32 0
}

LLVM IR (-O2):
@.str = [21 x i8] c"SecretLen mismatch.\0A\00"
@.str.1 = [22 x i8] c"Secrets don't match.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = bitcast i8** %3 to i8*
%6 = bitcast i8** %4 to i8*
%7 = tail call i32* (...) @gen_key()
%8 = tail call i32* (...) @gen_key()
%9 = tail call i32 @EC_KEY_get0_public_key(i32* %8)
%10 = call i32 @EC_DH(i8** %3, i32* %7, i32 %9)
%11 = call i32 @EC_KEY_get0_public_key(i32* %7)
%12 = call i32 @EC_DH(i8** %4, i32* %8, i32 %11)
%13 = icmp eq i32 %10, %12
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i8*, i8** %3
%18 = load i8*, i8** %4
%19 = call i64 @memcmp(i8* %17, i8* %18, i32 %10)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i8*, i8** %3
%25 = call i32 @free(i8* %24)
%26 = load i8*, i8** %4
%27 = call i32 @free(i8* %26)
%28 = call i32 @EC_KEY_free(i32* %7)
%29 = call i32 @EC_KEY_free(i32* %8)
%30 = call i32 (...) @CRYPTO_cleanup_all_ex_data()
ret i32 0
}