#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*/ EVP_CIPHER_CTX;
typedef int /*<<< orphan*/ EVP_CIPHER;

/* Variables and functions */
int /*<<< orphan*/ EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
int /*<<< orphan*/ EVP_CIPHER_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_CIPHER_CTX_new();
int /*<<< orphan*/ EVP_CIPHER_CTX_set_flags(int /*<<< orphan*/ *,
                                            int /*<<< orphan*/);
int /*<<< orphan*/ EVP_EncryptFinal_ex(int /*<<< orphan*/ *, unsigned char *,
                                       int *);
int /*<<< orphan*/ EVP_EncryptInit_ex(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ const *,
                                      int /*<<< orphan*/ *,
                                      unsigned char const *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_EncryptUpdate(int /*<<< orphan*/ *, unsigned char *,
                                     int *, unsigned char const *, int);
int /*<<< orphan*/ *EVP_aes_128_wrap_pad();
int /*<<< orphan*/ *EVP_aes_192_wrap_pad();
int /*<<< orphan*/ *EVP_aes_256_wrap_pad();
int aes_key_wrap_with_padding_openssl(const unsigned char *key,
                                      unsigned int key_length,
                                      const unsigned char *plaintext,
                                      unsigned int plaintext_length,
                                      unsigned char *ciphertext,
                                      unsigned int *ciphertext_length) {
  EVP_CIPHER_CTX *ctx;
  const EVP_CIPHER *cipher;
  int final_length;
  switch (key_length) {
  case 128:
    cipher = EVP_aes_128_wrap_pad();
    break;
  case 192:
    cipher = EVP_aes_192_wrap_pad();
    break;
  case 256:
    cipher = EVP_aes_256_wrap_pad();
    break;
  default:
    return 1;
  }
  if (!(ctx = EVP_CIPHER_CTX_new())) {
    return 2;
  }
  EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  if (!EVP_EncryptInit_ex(ctx, cipher, NULL, key, NULL)) {
    EVP_CIPHER_CTX_free(ctx);
    return 2;
  }
  if (!EVP_EncryptUpdate(ctx, ciphertext, (int *)ciphertext_length, plaintext,
                         (int)plaintext_length)) {
    EVP_CIPHER_CTX_free(ctx);
    return 2;
  }
  if (!EVP_EncryptFinal_ex(ctx, ciphertext + *ciphertext_length,
                           &final_length)) {
    EVP_CIPHER_CTX_free(ctx);
    return 2;
  }
  *ciphertext_length += final_length;
  EVP_CIPHER_CTX_free(ctx);
  return 0;
}


x86 assembly (clang, -O0):
<aes_key_wrap_with_padding_openssl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x50(%rbp)
sub    $0x80,%eax
je     11ea <aes_key_wrap_with_padding_openssl+0x5a>
jmp    11c4 <aes_key_wrap_with_padding_openssl+0x34>
mov    -0x50(%rbp),%eax
sub    $0xc0,%eax
je     11fa <aes_key_wrap_with_padding_openssl+0x6a>
jmp    11d7 <aes_key_wrap_with_padding_openssl+0x47>
mov    -0x50(%rbp),%eax
sub    $0x100,%eax
je     120a <aes_key_wrap_with_padding_openssl+0x7a>
jmp    121a <aes_key_wrap_with_padding_openssl+0x8a>
mov    $0x0,%al
call   1070 <EVP_aes_128_wrap_pad@plt>
mov    %rax,-0x48(%rbp)
jmp    1226 <aes_key_wrap_with_padding_openssl+0x96>
mov    $0x0,%al
call   10b0 <EVP_aes_192_wrap_pad@plt>
mov    %rax,-0x48(%rbp)
jmp    1226 <aes_key_wrap_with_padding_openssl+0x96>
mov    $0x0,%al
call   1050 <EVP_aes_256_wrap_pad@plt>
mov    %rax,-0x48(%rbp)
jmp    1226 <aes_key_wrap_with_padding_openssl+0x96>
movl   $0x1,-0x4(%rbp)
jmp    131a <aes_key_wrap_with_padding_openssl+0x18a>
mov    $0x0,%al
call   1090 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1247 <aes_key_wrap_with_padding_openssl+0xb7>
movl   $0x2,-0x4(%rbp)
jmp    131a <aes_key_wrap_with_padding_openssl+0x18a>
mov    -0x40(%rbp),%rdi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
call   10a0 <EVP_CIPHER_CTX_set_flags@plt>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
call   1080 <EVP_EncryptInit_ex@plt>
cmp    $0x0,%eax
jne    1290 <aes_key_wrap_with_padding_openssl+0x100>
mov    -0x40(%rbp),%rdi
call   1030 <EVP_CIPHER_CTX_free@plt>
movl   $0x2,-0x4(%rbp)
jmp    131a <aes_key_wrap_with_padding_openssl+0x18a>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
call   1040 <EVP_EncryptUpdate@plt>
cmp    $0x0,%eax
jne    12c7 <aes_key_wrap_with_padding_openssl+0x137>
mov    -0x40(%rbp),%rdi
call   1030 <EVP_CIPHER_CTX_free@plt>
movl   $0x2,-0x4(%rbp)
jmp    131a <aes_key_wrap_with_padding_openssl+0x18a>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
add    %rax,%rsi
lea    -0x4c(%rbp),%rdx
call   1060 <EVP_EncryptFinal_ex@plt>
cmp    $0x0,%eax
jne    12ff <aes_key_wrap_with_padding_openssl+0x16f>
mov    -0x40(%rbp),%rdi
call   1030 <EVP_CIPHER_CTX_free@plt>
movl   $0x2,-0x4(%rbp)
jmp    131a <aes_key_wrap_with_padding_openssl+0x18a>
mov    -0x4c(%rbp),%ecx
mov    -0x38(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rdi
call   1030 <EVP_CIPHER_CTX_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_key_wrap_with_padding_openssl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r13
mov    %rdx,%r12
mov    %rdi,%rbx
cmp    $0x100,%esi
je     11da <aes_key_wrap_with_padding_openssl+0x4a>
cmp    $0xc0,%esi
je     11ec <aes_key_wrap_with_padding_openssl+0x5c>
mov    $0x1,%ebp
cmp    $0x80,%esi
jne    128d <aes_key_wrap_with_padding_openssl+0xfd>
mov    %ecx,0x4(%rsp)
mov    %r9,0x8(%rsp)
xor    %eax,%eax
call   1070 <EVP_aes_128_wrap_pad@plt>
jmp    11fc <aes_key_wrap_with_padding_openssl+0x6c>
mov    %ecx,0x4(%rsp)
mov    %r9,0x8(%rsp)
xor    %eax,%eax
call   1050 <EVP_aes_256_wrap_pad@plt>
jmp    11fc <aes_key_wrap_with_padding_openssl+0x6c>
mov    %ecx,0x4(%rsp)
mov    %r9,0x8(%rsp)
xor    %eax,%eax
call   10b0 <EVP_aes_192_wrap_pad@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1090 <EVP_CIPHER_CTX_new@plt>
mov    $0x2,%ebp
test   %rax,%rax
je     128d <aes_key_wrap_with_padding_openssl+0xfd>
mov    %rax,%r14
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <EVP_CIPHER_CTX_set_flags@plt>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %edx,%edx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1080 <EVP_EncryptInit_ex@plt>
test   %eax,%eax
je     1285 <aes_key_wrap_with_padding_openssl+0xf5>
mov    %r14,%rdi
mov    %r13,%rsi
mov    0x8(%rsp),%rbx
mov    %rbx,%rdx
mov    %r12,%rcx
mov    0x4(%rsp),%r8d
call   1040 <EVP_EncryptUpdate@plt>
test   %eax,%eax
je     1285 <aes_key_wrap_with_padding_openssl+0xf5>
mov    (%rbx),%eax
add    %rax,%r13
lea    0x14(%rsp),%rdx
mov    %r14,%rdi
mov    %r13,%rsi
call   1060 <EVP_EncryptFinal_ex@plt>
test   %eax,%eax
je     1285 <aes_key_wrap_with_padding_openssl+0xf5>
mov    0x14(%rsp),%eax
add    %eax,(%rbx)
mov    %r14,%rdi
call   1030 <EVP_CIPHER_CTX_free@plt>
xor    %ebp,%ebp
jmp    128d <aes_key_wrap_with_padding_openssl+0xfd>
mov    %r14,%rdi
call   1030 <EVP_CIPHER_CTX_free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_CIPHER_CTX_FLAG_WRAP_ALLOW = global i32 0
define i32 @aes_key_wrap_with_padding_openssl(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32
store i8* %0, i8** %8
store i32 %1, i32* %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32* %5, i32** %13
%17 = load i32, i32* %9
switch i32 %17, label %24 [
i32 128, label %18
i32 192, label %20
i32 256, label %22
]
18: ; preds = %6
%19 = call i32* (...) @EVP_aes_128_wrap_pad()
store i32* %19, i32** %15
br label %25
20: ; preds = %6
%21 = call i32* (...) @EVP_aes_192_wrap_pad()
store i32* %21, i32** %15
br label %25
22: ; preds = %6
%23 = call i32* (...) @EVP_aes_256_wrap_pad()
store i32* %23, i32** %15
br label %25
24: ; preds = %6
store i32 1, i32* %7
br label %71
25: ; preds = %22, %20, %18
%26 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %26, i32** %14
%27 = icmp ne i32* %26, null
br i1 %27, label %29, label %28
28: ; preds = %25
store i32 2, i32* %7
br label %71
29: ; preds = %25
%30 = load i32*, i32** %14
%31 = load i32, i32* @EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
%32 = call i32 @EVP_CIPHER_CTX_set_flags(i32* %30, i32 %31)
%33 = load i32*, i32** %14
%34 = load i32*, i32** %15
%35 = load i8*, i8** %8
%36 = call i32 @EVP_EncryptInit_ex(i32* %33, i32* %34, i32* null, i8* %35, i32* null)
%37 = icmp ne i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %29
%39 = load i32*, i32** %14
%40 = call i32 @EVP_CIPHER_CTX_free(i32* %39)
store i32 2, i32* %7
br label %71
41: ; preds = %29
%42 = load i32*, i32** %14
%43 = load i8*, i8** %12
%44 = load i32*, i32** %13
%45 = load i8*, i8** %10
%46 = load i32, i32* %11
%47 = call i32 @EVP_EncryptUpdate(i32* %42, i8* %43, i32* %44, i8* %45, i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %41
%50 = load i32*, i32** %14
%51 = call i32 @EVP_CIPHER_CTX_free(i32* %50)
store i32 2, i32* %7
br label %71
52: ; preds = %41
%53 = load i32*, i32** %14
%54 = load i8*, i8** %12
%55 = load i32*, i32** %13
%56 = load i32, i32* %55
%57 = zext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
%59 = call i32 @EVP_EncryptFinal_ex(i32* %53, i8* %58, i32* %16)
%60 = icmp ne i32 %59, 0
br i1 %60, label %64, label %61
61: ; preds = %52
%62 = load i32*, i32** %14
%63 = call i32 @EVP_CIPHER_CTX_free(i32* %62)
store i32 2, i32* %7
br label %71
64: ; preds = %52
%65 = load i32, i32* %16
%66 = load i32*, i32** %13
%67 = load i32, i32* %66
%68 = add i32 %67, %65
store i32 %68, i32* %66
%69 = load i32*, i32** %14
%70 = call i32 @EVP_CIPHER_CTX_free(i32* %69)
store i32 0, i32* %7
br label %71
71: ; preds = %64, %61, %49, %38, %28, %24
%72 = load i32, i32* %7
ret i32 %72
}

LLVM IR (-O2):
@EVP_CIPHER_CTX_FLAG_WRAP_ALLOW = global i32 0
define i32 @aes_key_wrap_with_padding_openssl(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4, i32* %5) {
%7 = alloca i32
%8 = bitcast i32* %7 to i8*
switch i32 %1, label %44 [
i32 128, label %9
i32 192, label %11
i32 256, label %13
]
9: ; preds = %6
%10 = tail call i32* (...) @EVP_aes_128_wrap_pad()
br label %15
11: ; preds = %6
%12 = tail call i32* (...) @EVP_aes_192_wrap_pad()
br label %15
13: ; preds = %6
%14 = tail call i32* (...) @EVP_aes_256_wrap_pad()
br label %15
15: ; preds = %13, %11, %9
%16 = phi i32* [ %14, %13 ], [ %12, %11 ], [ %10, %9 ]
%17 = tail call i32* (...) @EVP_CIPHER_CTX_new()
%18 = icmp eq i32* %17, null
br i1 %18, label %44, label %19
19: ; preds = %15
%20 = load i32, i32* @EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
%21 = tail call i32 @EVP_CIPHER_CTX_set_flags(i32* %17, i32 %20)
%22 = tail call i32 @EVP_EncryptInit_ex(i32* %17, i32* %16, i32* null, i8* %0, i32* null)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = tail call i32 @EVP_CIPHER_CTX_free(i32* %17)
br label %44
26: ; preds = %19
%27 = tail call i32 @EVP_EncryptUpdate(i32* %17, i8* %4, i32* %5, i8* %2, i32 %3)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @EVP_CIPHER_CTX_free(i32* %17)
br label %44
31: ; preds = %26
%32 = load i32, i32* %5
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %4, i64 %33
%35 = call i32 @EVP_EncryptFinal_ex(i32* %17, i8* %34, i32* %7)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @EVP_CIPHER_CTX_free(i32* %17)
br label %44
39: ; preds = %31
%40 = load i32, i32* %7
%41 = load i32, i32* %5
%42 = add i32 %41, %40
store i32 %42, i32* %5
%43 = call i32 @EVP_CIPHER_CTX_free(i32* %17)
br label %44
44: ; preds = %15, %6, %39, %37, %29, %24
%45 = phi i32 [ 0, %39 ], [ 2, %37 ], [ 2, %29 ], [ 2, %24 ], [ 1, %6 ], [ 2, %15 ]
ret i32 %45
}