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

/* Variables and functions */
int /*<<< orphan*/ EC_KEY_free(int /*<<< orphan*/ *);
scalar_t__ EC_KEY_generate_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_new_by_curve_name(int /*<<< orphan*/);
int /*<<< orphan*/ NID_X9_62_prime256v1;
scalar_t__ RAND_bytes(int /*<<< orphan*/ *, int);
int ece_aes128gcm_encrypt_blocks(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                 int /*<<< orphan*/ const *, size_t,
                                 int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                                 size_t, int /*<<< orphan*/ const *, size_t,
                                 int /*<<< orphan*/ *, size_t *);
int /*<<< orphan*/ *ece_import_public_key(int /*<<< orphan*/ const *, size_t);
int ece_aes128gcm_encrypt(const uint8_t *rawRecvPubKey, size_t rawRecvPubKeyLen,
                          const uint8_t *authSecret, size_t authSecretLen,
                          uint32_t rs, size_t padLen, const uint8_t *plaintext,
                          size_t plaintextLen, uint8_t *payload,
                          size_t *payloadLen) {
  int err = 0;
  EC_KEY *recvPubKey = NULL;
  EC_KEY *senderPrivKey = NULL;
  uint8_t salt[16];
  if (RAND_bytes(salt, 16) <= 0) {
    err = -16;
    goto end;
  }
  recvPubKey = ece_import_public_key(rawRecvPubKey, rawRecvPubKeyLen);
  if (!recvPubKey) {
    err = -3;
    goto end;
  }
  senderPrivKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  if (!senderPrivKey) {
    err = -1;
    goto end;
  }
  if (EC_KEY_generate_key(senderPrivKey) <= 0) {
    err = -2;
    goto end;
  }
  err = ece_aes128gcm_encrypt_blocks(
      senderPrivKey, recvPubKey, authSecret, authSecretLen, salt, 16, rs,
      padLen, plaintext, plaintextLen, payload, payloadLen);
end:
  EC_KEY_free(recvPubKey);
  EC_KEY_free(senderPrivKey);
  return err;
}


x86 assembly (clang, -O0):
<ece_aes128gcm_encrypt>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0xb8,%rsp
mov    0x28(%rbp),%rax
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x20(%rbp)
mov    %rsi,-0x28(%rbp)
mov    %rdx,-0x30(%rbp)
mov    %rcx,-0x38(%rbp)
mov    %r8d,-0x3c(%rbp)
mov    %r9,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
lea    -0xa0(%rbp),%rdi
mov    $0x10,%esi
call   1030 <RAND_bytes@plt>
cmp    $0x0,%rax
jg     11d6 <ece_aes128gcm_encrypt+0x76>
movl   $0xfffffff0,-0x4c(%rbp)
jmp    12a0 <ece_aes128gcm_encrypt+0x140>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <ece_import_public_key@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11fe <ece_aes128gcm_encrypt+0x9e>
movl   $0xfffffffd,-0x4c(%rbp)
jmp    12a0 <ece_aes128gcm_encrypt+0x140>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1080 <EC_KEY_new_by_curve_name@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    1227 <ece_aes128gcm_encrypt+0xc7>
movl   $0xffffffff,-0x4c(%rbp)
jmp    12a0 <ece_aes128gcm_encrypt+0x140>
mov    -0x60(%rbp),%rdi
call   1050 <EC_KEY_generate_key@plt>
cmp    $0x0,%rax
jg     1246 <ece_aes128gcm_encrypt+0xe6>
movl   $0xfffffffe,-0x4c(%rbp)
jmp    12a0 <ece_aes128gcm_encrypt+0x140>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    -0xa0(%rbp),%r8
mov    -0x3c(%rbp),%r15d
mov    -0x48(%rbp),%r14
mov    0x10(%rbp),%rbx
mov    0x18(%rbp),%r11
mov    0x20(%rbp),%r10
mov    0x28(%rbp),%rax
mov    $0x10,%r9d
mov    %r15d,(%rsp)
mov    %r14,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r11,0x18(%rsp)
mov    %r10,0x20(%rsp)
mov    %rax,0x28(%rsp)
call   1060 <ece_aes128gcm_encrypt_blocks@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x58(%rbp),%rdi
call   1070 <EC_KEY_free@plt>
mov    -0x60(%rbp),%rdi
call   1070 <EC_KEY_free@plt>
mov    -0x4c(%rbp),%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<ece_aes128gcm_encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,%r12
mov    %r8d,%r14d
mov    %rcx,%r13
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
call   1030 <RAND_bytes@plt>
test   %rax,%rax
jle    121d <ece_aes128gcm_encrypt+0xbd>
mov    %r14,0x8(%rsp)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <ece_import_public_key@plt>
test   %rax,%rax
je     1224 <ece_aes128gcm_encrypt+0xc4>
mov    %rax,%r14
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1080 <EC_KEY_new_by_curve_name@plt>
test   %rax,%rax
je     124f <ece_aes128gcm_encrypt+0xef>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <EC_KEY_generate_key@plt>
test   %rax,%rax
jle    1256 <ece_aes128gcm_encrypt+0xf6>
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r13,%rcx
mov    $0x10,%r9d
push   0xa8(%rsp)
push   0xa8(%rsp)
push   0xa8(%rsp)
push   0xa8(%rsp)
push   %r12
push   0x30(%rsp)
call   1060 <ece_aes128gcm_encrypt_blocks@plt>
add    $0x30,%rsp
mov    %eax,%ebp
jmp    122e <ece_aes128gcm_encrypt+0xce>
mov    $0xfffffff0,%ebp
jmp    1229 <ece_aes128gcm_encrypt+0xc9>
mov    $0xfffffffd,%ebp
xor    %r14d,%r14d
xor    %ebx,%ebx
mov    %r14,%rdi
call   1070 <EC_KEY_free@plt>
mov    %rbx,%rdi
call   1070 <EC_KEY_free@plt>
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    122c <ece_aes128gcm_encrypt+0xcc>
mov    $0xfffffffe,%ebp
jmp    122e <ece_aes128gcm_encrypt+0xce>

LLVM IR (-O0):
@NID_X9_62_prime256v1 = global i32 0
define i32 @ece_aes128gcm_encrypt(i32* %0, i64 %1, i32* %2, i64 %3, i32 %4, i64 %5, i32* %6, i64 %7, i32* %8, i64* %9) {
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i64
%15 = alloca i32
%16 = alloca i64
%17 = alloca i32*
%18 = alloca i64
%19 = alloca i32*
%20 = alloca i64*
%21 = alloca i32
%22 = alloca i32*
%23 = alloca i32*
%24 = alloca [16 x i32]
store i32* %0, i32** %11
store i64 %1, i64* %12
store i32* %2, i32** %13
store i64 %3, i64* %14
store i32 %4, i32* %15
store i64 %5, i64* %16
store i32* %6, i32** %17
store i64 %7, i64* %18
store i32* %8, i32** %19
store i64* %9, i64** %20
store i32 0, i32* %21
store i32* null, i32** %22
store i32* null, i32** %23
%25 = getelementptr inbounds [16 x i32], [16 x i32]* %24, i64 0, i64 0
%26 = call i64 @RAND_bytes(i32* %25, i32 16)
%27 = icmp sle i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %10
store i32 -16, i32* %21
br label %60
29: ; preds = %10
%30 = load i32*, i32** %11
%31 = load i64, i64* %12
%32 = call i32* @ece_import_public_key(i32* %30, i64 %31)
store i32* %32, i32** %22
%33 = load i32*, i32** %22
%34 = icmp ne i32* %33, null
br i1 %34, label %36, label %35
35: ; preds = %29
store i32 -3, i32* %21
br label %60
36: ; preds = %29
%37 = load i32, i32* @NID_X9_62_prime256v1
%38 = call i32* @EC_KEY_new_by_curve_name(i32 %37)
store i32* %38, i32** %23
%39 = load i32*, i32** %23
%40 = icmp ne i32* %39, null
br i1 %40, label %42, label %41
41: ; preds = %36
store i32 -1, i32* %21
br label %60
42: ; preds = %36
%43 = load i32*, i32** %23
%44 = call i64 @EC_KEY_generate_key(i32* %43)
%45 = icmp sle i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -2, i32* %21
br label %60
47: ; preds = %42
%48 = load i32*, i32** %23
%49 = load i32*, i32** %22
%50 = load i32*, i32** %13
%51 = load i64, i64* %14
%52 = getelementptr inbounds [16 x i32], [16 x i32]* %24, i64 0, i64 0
%53 = load i32, i32* %15
%54 = load i64, i64* %16
%55 = load i32*, i32** %17
%56 = load i64, i64* %18
%57 = load i32*, i32** %19
%58 = load i64*, i64** %20
%59 = call i32 @ece_aes128gcm_encrypt_blocks(i32* %48, i32* %49, i32* %50, i64 %51, i32* %52, i32 16, i32 %53, i64 %54, i32* %55, i64 %56, i32* %57, i64* %58)
store i32 %59, i32* %21
br label %60
60: ; preds = %47, %46, %41, %35, %28
%61 = load i32*, i32** %22
%62 = call i32 @EC_KEY_free(i32* %61)
%63 = load i32*, i32** %23
%64 = call i32 @EC_KEY_free(i32* %63)
%65 = load i32, i32* %21
ret i32 %65
}

LLVM IR (-O2):
@NID_X9_62_prime256v1 = global i32 0
define i32 @ece_aes128gcm_encrypt(i32* %0, i64 %1, i32* %2, i64 %3, i32 %4, i64 %5, i32* %6, i64 %7, i32* %8, i64* %9) {
%11 = alloca [16 x i32]
%12 = bitcast [16 x i32]* %11 to i8*
%13 = getelementptr inbounds [16 x i32], [16 x i32]* %11, i64 0, i64 0
%14 = call i64 @RAND_bytes(i32* %13, i32 16)
%15 = icmp slt i64 %14, 1
br i1 %15, label %28, label %16
16: ; preds = %10
%17 = call i32* @ece_import_public_key(i32* %0, i64 %1)
%18 = icmp eq i32* %17, null
br i1 %18, label %28, label %19
19: ; preds = %16
%20 = load i32, i32* @NID_X9_62_prime256v1
%21 = call i32* @EC_KEY_new_by_curve_name(i32 %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %28, label %23
23: ; preds = %19
%24 = call i64 @EC_KEY_generate_key(i32* %21)
%25 = icmp slt i64 %24, 1
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @ece_aes128gcm_encrypt_blocks(i32* %21, i32* %17, i32* %2, i64 %3, i32* %13, i32 16, i32 %4, i64 %5, i32* %6, i64 %7, i32* %8, i64* %9)
br label %28
28: ; preds = %23, %19, %16, %10, %26
%29 = phi i32 [ %27, %26 ], [ -16, %10 ], [ -3, %16 ], [ -1, %19 ], [ -2, %23 ]
%30 = phi i32* [ %17, %26 ], [ null, %10 ], [ null, %16 ], [ %17, %19 ], [ %17, %23 ]
%31 = phi i32* [ %21, %26 ], [ null, %10 ], [ null, %16 ], [ null, %19 ], [ %21, %23 ]
%32 = call i32 @EC_KEY_free(i32* %30)
%33 = call i32 @EC_KEY_free(i32* %31)
ret i32 %29
}