#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 EC_KEY_generate_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_new_by_curve_name(int /*<<< orphan*/);
int /*<<< orphan*/ NID_X9_62_prime256v1;
int RAND_bytes(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *ece_import_public_key(int /*<<< orphan*/ const *, size_t);
int ece_webpush_aes128gcm_encrypt_plaintext(
    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 ece_webpush_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) != 1) {
    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) != 1) {
    err = -2;
    goto end;
  }
  err = ece_webpush_aes128gcm_encrypt_plaintext(
      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_webpush_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    $0x1,%eax
je     11d5 <ece_webpush_aes128gcm_encrypt+0x75>
movl   $0xfffffff0,-0x4c(%rbp)
jmp    129e <ece_webpush_aes128gcm_encrypt+0x13e>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <ece_import_public_key@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11fd <ece_webpush_aes128gcm_encrypt+0x9d>
movl   $0xfffffffd,-0x4c(%rbp)
jmp    129e <ece_webpush_aes128gcm_encrypt+0x13e>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1080 <EC_KEY_new_by_curve_name@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    1226 <ece_webpush_aes128gcm_encrypt+0xc6>
movl   $0xffffffff,-0x4c(%rbp)
jmp    129e <ece_webpush_aes128gcm_encrypt+0x13e>
mov    -0x60(%rbp),%rdi
call   1050 <EC_KEY_generate_key@plt>
cmp    $0x1,%eax
je     1244 <ece_webpush_aes128gcm_encrypt+0xe4>
movl   $0xfffffffe,-0x4c(%rbp)
jmp    129e <ece_webpush_aes128gcm_encrypt+0x13e>
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   1070 <ece_webpush_aes128gcm_encrypt_plaintext@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    $0xb8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<ece_webpush_aes128gcm_encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,%r14
mov    %r8d,%r15d
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
call   1030 <RAND_bytes@plt>
cmp    $0x1,%eax
jne    122a <ece_webpush_aes128gcm_encrypt+0xca>
mov    %r14,0x8(%rsp)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <ece_import_public_key@plt>
test   %rax,%rax
je     1232 <ece_webpush_aes128gcm_encrypt+0xd2>
mov    %rax,%r14
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1080 <EC_KEY_new_by_curve_name@plt>
test   %rax,%rax
je     125f <ece_webpush_aes128gcm_encrypt+0xff>
mov    %rax,%rbx
mov    %r15,%rbp
mov    %rax,%rdi
call   1050 <EC_KEY_generate_key@plt>
mov    $0xfffffffe,%r15d
cmp    $0x1,%eax
jne    123d <ece_webpush_aes128gcm_encrypt+0xdd>
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    $0x10,%r9d
push   0xa8(%rsp)
push   0xa8(%rsp)
push   0xa8(%rsp)
push   0xa8(%rsp)
push   0x28(%rsp)
push   %rbp
call   1070 <ece_webpush_aes128gcm_encrypt_plaintext@plt>
add    $0x30,%rsp
mov    %eax,%r15d
jmp    123d <ece_webpush_aes128gcm_encrypt+0xdd>
mov    $0xfffffff0,%r15d
jmp    1238 <ece_webpush_aes128gcm_encrypt+0xd8>
mov    $0xfffffffd,%r15d
xor    %r14d,%r14d
xor    %ebx,%ebx
mov    %r14,%rdi
call   1060 <EC_KEY_free@plt>
mov    %rbx,%rdi
call   1060 <EC_KEY_free@plt>
mov    %r15d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r15d
jmp    123b <ece_webpush_aes128gcm_encrypt+0xdb>

LLVM IR (-O0):
@NID_X9_62_prime256v1 = global i32 0
define i32 @ece_webpush_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 i32 @RAND_bytes(i32* %25, i32 16)
%27 = icmp ne i32 %26, 1
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 i32 @EC_KEY_generate_key(i32* %43)
%45 = icmp ne i32 %44, 1
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_webpush_aes128gcm_encrypt_plaintext(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_webpush_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 i32 @RAND_bytes(i32* %13, i32 16)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %28
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 i32 @EC_KEY_generate_key(i32* %21)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @ece_webpush_aes128gcm_encrypt_plaintext(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
}