#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*/ *);
int ece_aes128gcm_encrypt_blocks(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                 int /*<<< orphan*/ const *, size_t,
                                 int /*<<< orphan*/ const *, size_t,
                                 int /*<<< orphan*/, size_t,
                                 int /*<<< orphan*/ const *, size_t,
                                 int /*<<< orphan*/ *, size_t *);
int /*<<< orphan*/ *ece_import_private_key(int /*<<< orphan*/ const *, size_t);
int /*<<< orphan*/ *ece_import_public_key(int /*<<< orphan*/ const *, size_t);
int ece_aes128gcm_encrypt_with_keys(
    const uint8_t *rawSenderPrivKey, size_t rawSenderPrivKeyLen,
    const uint8_t *authSecret, size_t authSecretLen, const uint8_t *salt,
    size_t saltLen, const uint8_t *rawRecvPubKey, size_t rawRecvPubKeyLen,
    uint32_t rs, size_t padLen, const uint8_t *plaintext, size_t plaintextLen,
    uint8_t *payload, size_t *payloadLen) {
  int err = 0;
  EC_KEY *senderPrivKey = NULL;
  EC_KEY *recvPubKey = NULL;
  senderPrivKey = ece_import_private_key(rawSenderPrivKey, rawSenderPrivKeyLen);
  if (!senderPrivKey) {
    err = -2;
    goto end;
  }
  recvPubKey = ece_import_public_key(rawRecvPubKey, rawRecvPubKeyLen);
  if (!recvPubKey) {
    err = -3;
    goto end;
  }
  err = ece_aes128gcm_encrypt_blocks(
      senderPrivKey, recvPubKey, authSecret, authSecretLen, salt, saltLen, rs,
      padLen, plaintext, plaintextLen, payload, payloadLen);
end:
  EC_KEY_free(senderPrivKey);
  EC_KEY_free(recvPubKey);
  return err;
}


x86 assembly (clang, -O0):
<ece_aes128gcm_encrypt_with_keys>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    0x48(%rbp),%rax
mov    0x40(%rbp),%rax
mov    0x38(%rbp),%rax
mov    0x30(%rbp),%rax
mov    0x28(%rbp),%rax
mov    0x20(%rbp),%eax
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    %r8,-0x40(%rbp)
mov    %r9,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <ece_import_private_key@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11c3 <ece_aes128gcm_encrypt_with_keys+0x83>
movl   $0xfffffffe,-0x4c(%rbp)
jmp    1240 <ece_aes128gcm_encrypt_with_keys+0x100>
mov    0x10(%rbp),%rdi
mov    0x18(%rbp),%rsi
call   1040 <ece_import_public_key@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    11eb <ece_aes128gcm_encrypt_with_keys+0xab>
movl   $0xfffffffd,-0x4c(%rbp)
jmp    1240 <ece_aes128gcm_encrypt_with_keys+0x100>
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
mov    -0x48(%rbp),%r9
mov    0x20(%rbp),%r15d
mov    0x28(%rbp),%r14
mov    0x30(%rbp),%rbx
mov    0x38(%rbp),%r11
mov    0x40(%rbp),%r10
mov    0x48(%rbp),%rax
mov    %r15d,(%rsp)
mov    %r14,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r11,0x18(%rsp)
mov    %r10,0x20(%rsp)
mov    %rax,0x28(%rsp)
call   1050 <ece_aes128gcm_encrypt_blocks@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x58(%rbp),%rdi
call   1060 <EC_KEY_free@plt>
mov    -0x60(%rbp),%rdi
call   1060 <EC_KEY_free@plt>
mov    -0x4c(%rbp),%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<ece_aes128gcm_encrypt_with_keys>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r15
mov    %r8,%r12
mov    %rcx,%r13
mov    %rdx,%rbp
call   1030 <ece_import_private_key@plt>
mov    %rax,%r14
test   %rax,%rax
je     11b3 <ece_aes128gcm_encrypt_with_keys+0x73>
mov    0x48(%rsp),%rsi
mov    0x40(%rsp),%rdi
call   1040 <ece_import_public_key@plt>
test   %rax,%rax
je     11ba <ece_aes128gcm_encrypt_with_keys+0x7a>
mov    %rax,%rbx
mov    0x50(%rsp),%eax
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    %r13,%rcx
mov    %r12,%r8
mov    %r15,%r9
push   0x78(%rsp)
push   0x78(%rsp)
push   0x78(%rsp)
push   0x78(%rsp)
push   0x78(%rsp)
push   %rax
call   1050 <ece_aes128gcm_encrypt_blocks@plt>
add    $0x30,%rsp
mov    %eax,%ebp
jmp    11c1 <ece_aes128gcm_encrypt_with_keys+0x81>
mov    $0xfffffffe,%ebp
jmp    11bf <ece_aes128gcm_encrypt_with_keys+0x7f>
mov    $0xfffffffd,%ebp
xor    %ebx,%ebx
mov    %r14,%rdi
call   1060 <EC_KEY_free@plt>
mov    %rbx,%rdi
call   1060 <EC_KEY_free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @ece_aes128gcm_encrypt_with_keys(i32* %0, i64 %1, i32* %2, i64 %3, i32* %4, i64 %5, i32* %6, i64 %7, i32 %8, i64 %9, i32* %10, i64 %11, i32* %12, i64* %13) {
%15 = tail call i32* @ece_import_private_key(i32* %0, i64 %1)
%16 = icmp eq i32* %15, null
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = tail call i32* @ece_import_public_key(i32* %6, i64 %7)
%19 = icmp eq i32* %18, null
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @ece_aes128gcm_encrypt_blocks(i32* %15, i32* %18, i32* %2, i64 %3, i32* %4, i64 %5, i32 %8, i64 %9, i32* %10, i64 %11, i32* %12, i64* %13)
br label %22
22: ; preds = %17, %14, %20
%23 = phi i32 [ %21, %20 ], [ -2, %14 ], [ -3, %17 ]
%24 = phi i32* [ %18, %20 ], [ null, %14 ], [ null, %17 ]
%25 = tail call i32 @EC_KEY_free(i32* %15)
%26 = tail call i32 @EC_KEY_free(i32* %24)
ret i32 %23
}