#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_POINT;
typedef int /*<<< orphan*/ EC_KEY;

/* Variables and functions */
int ECDH_compute_key(unsigned char *, int, int /*<<< orphan*/ const *,
                     int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int EC_GROUP_get_degree(int /*<<< orphan*/);
int /*<<< orphan*/ EC_KEY_get0_group(int /*<<< orphan*/ *);
int /*<<< orphan*/ die(char *);
unsigned char *malloc(int);
int EC_DH(unsigned char **secret, EC_KEY *key, const EC_POINT *pPub) {
  int secretLen;
  secretLen = EC_GROUP_get_degree(EC_KEY_get0_group(key));
  secretLen = (secretLen + 7) / 8;
  *secret = malloc(secretLen);
  if (!(*secret))
    die("Failed to allocate memory for secret.\n");
  secretLen = ECDH_compute_key(*secret, secretLen, pPub, key, NULL);
  return secretLen;
}


x86 assembly (clang, -O0):
<EC_DH>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <EC_KEY_get0_group@plt>
mov    %eax,%edi
call   1060 <EC_GROUP_get_degree@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x7,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11b4 <EC_DH+0x64>
lea    0xe51(%rip),%rdi
call   1050 <die@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <ECDH_compute_key@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EC_DH>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <EC_KEY_get0_group@plt>
mov    %eax,%edi
call   1060 <EC_GROUP_get_degree@plt>
mov    %eax,%ebx
lea    0x7(%rbx),%eax
add    $0xe,%ebx
test   %eax,%eax
cmovns %eax,%ebx
sar    $0x3,%ebx
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
jne    119e <EC_DH+0x4e>
lea    0xe6a(%rip),%rdi
call   1050 <die@plt>
mov    (%r15),%rax
mov    %rax,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
mov    %r12,%rcx
xor    %r8d,%r8d
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <ECDH_compute_key@plt>

LLVM IR (-O0):
@.str = [39 x i8] c"Failed to allocate memory for secret.\0A\00"
define i32 @EC_DH(i8** %0, i32* %1, i32* %2) {
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%8 = load i32*, i32** %5
%9 = call i32 @EC_KEY_get0_group(i32* %8)
%10 = call i32 @EC_GROUP_get_degree(i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = add nsw i32 %11, 7
%13 = sdiv i32 %12, 8
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = call i8* @malloc(i32 %14)
%16 = load i8**, i8*** %4
store i8* %15, i8** %16
%17 = load i8**, i8*** %4
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %3
%21 = call i32 @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %3
%23 = load i8**, i8*** %4
%24 = load i8*, i8** %23
%25 = load i32, i32* %7
%26 = load i32*, i32** %6
%27 = load i32*, i32** %5
%28 = call i32 @ECDH_compute_key(i8* %24, i32 %25, i32* %26, i32* %27, i32* null)
store i32 %28, i32* %7
%29 = load i32, i32* %7
ret i32 %29
}

LLVM IR (-O2):
@.str = [39 x i8] c"Failed to allocate memory for secret.\0A\00"
define i32 @EC_DH(i8** nocapture %0, i32* %1, i32* %2) {
%4 = tail call i32 @EC_KEY_get0_group(i32* %1)
%5 = tail call i32 @EC_GROUP_get_degree(i32 %4)
%6 = add nsw i32 %5, 7
%7 = sdiv i32 %6, 8
%8 = tail call i8* @malloc(i32 %7)
store i8* %8, i8** %0
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = tail call i32 @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%12 = load i8*, i8** %0
br label %13
13: ; preds = %10, %3
%14 = phi i8* [ %12, %10 ], [ %8, %3 ]
%15 = tail call i32 @ECDH_compute_key(i8* %14, i32 %7, i32* %2, i32* %1, i32* null)
ret i32 %15
}