#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_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_CIPHER_CTX_new();
int /*<<< orphan*/ EVP_CIPHER_CTX_set_padding(int /*<<< orphan*/ *,
                                              int /*<<< orphan*/);
int /*<<< orphan*/ EVP_DecryptFinal_ex(int /*<<< orphan*/ *, unsigned char *,
                                       int *);
int /*<<< orphan*/ EVP_DecryptInit_ex(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ const *,
                                      int /*<<< orphan*/ *,
                                      unsigned char const *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_DecryptUpdate(int /*<<< orphan*/ *, unsigned char *,
                                     int *, unsigned char const *,
                                     unsigned int);
int /*<<< orphan*/ *EVP_aes_128_ecb();
int /*<<< orphan*/ *EVP_aes_192_ecb();
int /*<<< orphan*/ *EVP_aes_256_ecb();
int aes_ecb_decrypt(const unsigned char *key, unsigned int key_length,
                    const unsigned char *ciphertext,
                    unsigned int ciphertext_length, unsigned char *plaintext) {
  EVP_CIPHER_CTX *ctx;
  const EVP_CIPHER *cipher = NULL;
  int plaintext_length = 0;
  int final_length = 0;
  if ((ciphertext_length & 0x0F) || (!ciphertext_length)) {
    return 1;
  }
  switch (key_length) {
  case 128:
    cipher = EVP_aes_128_ecb();
    break;
  case 192:
    cipher = EVP_aes_192_ecb();
    break;
  case 256:
    cipher = EVP_aes_256_ecb();
    break;
  default:
    return 1;
  }
  if (!(ctx = EVP_CIPHER_CTX_new()))
    return 2;
  if (!EVP_DecryptInit_ex(ctx, cipher, NULL, key, NULL)) {
    EVP_CIPHER_CTX_free(ctx);
    return 2;
  }
  EVP_CIPHER_CTX_set_padding(ctx, 0);
  if (!EVP_DecryptUpdate(ctx, plaintext, &plaintext_length, ciphertext,
                         ciphertext_length)) {
    EVP_CIPHER_CTX_free(ctx);
    return 2;
  }
  if (!EVP_DecryptFinal_ex(ctx, plaintext + plaintext_length, &final_length)) {
    EVP_CIPHER_CTX_free(ctx);
    return 2;
  }
  EVP_CIPHER_CTX_free(ctx);
  if (plaintext_length + final_length != ciphertext_length) {
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<aes_ecb_decrypt>:
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)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x24(%rbp),%eax
and    $0xf,%eax
cmp    $0x0,%eax
jne    11d9 <aes_ecb_decrypt+0x49>
cmpl   $0x0,-0x24(%rbp)
jne    11e5 <aes_ecb_decrypt+0x55>
movl   $0x1,-0x4(%rbp)
jmp    1358 <aes_ecb_decrypt+0x1c8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x80,%eax
je     1221 <aes_ecb_decrypt+0x91>
jmp    11fb <aes_ecb_decrypt+0x6b>
mov    -0x4c(%rbp),%eax
sub    $0xc0,%eax
je     1231 <aes_ecb_decrypt+0xa1>
jmp    120e <aes_ecb_decrypt+0x7e>
mov    -0x4c(%rbp),%eax
sub    $0x100,%eax
je     1241 <aes_ecb_decrypt+0xb1>
jmp    1251 <aes_ecb_decrypt+0xc1>
mov    $0x0,%al
call   1050 <EVP_aes_128_ecb@plt>
mov    %rax,-0x40(%rbp)
jmp    125d <aes_ecb_decrypt+0xcd>
mov    $0x0,%al
call   10b0 <EVP_aes_192_ecb@plt>
mov    %rax,-0x40(%rbp)
jmp    125d <aes_ecb_decrypt+0xcd>
mov    $0x0,%al
call   1070 <EVP_aes_256_ecb@plt>
mov    %rax,-0x40(%rbp)
jmp    125d <aes_ecb_decrypt+0xcd>
movl   $0x1,-0x4(%rbp)
jmp    1358 <aes_ecb_decrypt+0x1c8>
mov    $0x0,%al
call   1090 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    127e <aes_ecb_decrypt+0xee>
movl   $0x2,-0x4(%rbp)
jmp    1358 <aes_ecb_decrypt+0x1c8>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
call   1080 <EVP_DecryptInit_ex@plt>
cmp    $0x0,%eax
jne    12b5 <aes_ecb_decrypt+0x125>
mov    -0x38(%rbp),%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
movl   $0x2,-0x4(%rbp)
jmp    1358 <aes_ecb_decrypt+0x1c8>
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
call   1060 <EVP_CIPHER_CTX_set_padding@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
lea    -0x44(%rbp),%rdx
call   10a0 <EVP_DecryptUpdate@plt>
cmp    $0x0,%eax
jne    12f7 <aes_ecb_decrypt+0x167>
mov    -0x38(%rbp),%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
movl   $0x2,-0x4(%rbp)
jmp    1358 <aes_ecb_decrypt+0x1c8>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
movslq -0x44(%rbp),%rax
add    %rax,%rsi
lea    -0x48(%rbp),%rdx
call   1030 <EVP_DecryptFinal_ex@plt>
cmp    $0x0,%eax
jne    132d <aes_ecb_decrypt+0x19d>
mov    -0x38(%rbp),%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
movl   $0x2,-0x4(%rbp)
jmp    1358 <aes_ecb_decrypt+0x1c8>
mov    -0x38(%rbp),%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
mov    -0x44(%rbp),%eax
add    -0x48(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     1351 <aes_ecb_decrypt+0x1c1>
movl   $0x2,-0x4(%rbp)
jmp    1358 <aes_ecb_decrypt+0x1c8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_ecb_decrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0x8(%rsp)
movl   $0x0,0xc(%rsp)
mov    $0x1,%ebp
test   %ecx,%ecx
je     12b0 <aes_ecb_decrypt+0x120>
mov    %ecx,%r13d
mov    %ecx,%eax
and    $0xf,%eax
jne    12b0 <aes_ecb_decrypt+0x120>
mov    %r8,%r14
mov    %rdi,%r12
cmp    $0x100,%esi
je     11f9 <aes_ecb_decrypt+0x69>
cmp    $0xc0,%esi
je     1207 <aes_ecb_decrypt+0x77>
cmp    $0x80,%esi
jne    12b0 <aes_ecb_decrypt+0x120>
mov    %rdx,0x10(%rsp)
xor    %eax,%eax
call   1050 <EVP_aes_128_ecb@plt>
jmp    1213 <aes_ecb_decrypt+0x83>
mov    %rdx,0x10(%rsp)
xor    %eax,%eax
call   1070 <EVP_aes_256_ecb@plt>
jmp    1213 <aes_ecb_decrypt+0x83>
mov    %rdx,0x10(%rsp)
xor    %eax,%eax
call   10b0 <EVP_aes_192_ecb@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1090 <EVP_CIPHER_CTX_new@plt>
mov    $0x2,%ebp
test   %rax,%rax
je     12b0 <aes_ecb_decrypt+0x120>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
xor    %edx,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1080 <EVP_DecryptInit_ex@plt>
test   %eax,%eax
je     12a8 <aes_ecb_decrypt+0x118>
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <EVP_CIPHER_CTX_set_padding@plt>
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    0x10(%rsp),%rcx
mov    %r13d,%r8d
call   10a0 <EVP_DecryptUpdate@plt>
test   %eax,%eax
je     12a8 <aes_ecb_decrypt+0x118>
movslq 0x8(%rsp),%rax
add    %rax,%r14
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <EVP_DecryptFinal_ex@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1040 <EVP_CIPHER_CTX_free@plt>
test   %r14d,%r14d
je     12b0 <aes_ecb_decrypt+0x120>
mov    0xc(%rsp),%eax
add    0x8(%rsp),%eax
xor    %ebp,%ebp
cmp    %r13d,%eax
setne  %bpl
add    %ebp,%ebp
jmp    12b0 <aes_ecb_decrypt+0x120>
mov    %rbx,%rdi
call   1040 <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):
define i32 @aes_ecb_decrypt(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i32* null, i32** %13
store i32 0, i32* %14
store i32 0, i32* %15
%16 = load i32, i32* %10
%17 = and i32 %16, 15
%18 = icmp ne i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %5
%20 = load i32, i32* %10
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %19, %5
store i32 1, i32* %6
br label %78
23: ; preds = %19
%24 = load i32, i32* %8
switch i32 %24, label %31 [
i32 128, label %25
i32 192, label %27
i32 256, label %29
]
25: ; preds = %23
%26 = call i32* (...) @EVP_aes_128_ecb()
store i32* %26, i32** %13
br label %32
27: ; preds = %23
%28 = call i32* (...) @EVP_aes_192_ecb()
store i32* %28, i32** %13
br label %32
29: ; preds = %23
%30 = call i32* (...) @EVP_aes_256_ecb()
store i32* %30, i32** %13
br label %32
31: ; preds = %23
store i32 1, i32* %6
br label %78
32: ; preds = %29, %27, %25
%33 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %33, i32** %12
%34 = icmp ne i32* %33, null
br i1 %34, label %36, label %35
35: ; preds = %32
store i32 2, i32* %6
br label %78
36: ; preds = %32
%37 = load i32*, i32** %12
%38 = load i32*, i32** %13
%39 = load i8*, i8** %7
%40 = call i32 @EVP_DecryptInit_ex(i32* %37, i32* %38, i32* null, i8* %39, i32* null)
%41 = icmp ne i32 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %36
%43 = load i32*, i32** %12
%44 = call i32 @EVP_CIPHER_CTX_free(i32* %43)
store i32 2, i32* %6
br label %78
45: ; preds = %36
%46 = load i32*, i32** %12
%47 = call i32 @EVP_CIPHER_CTX_set_padding(i32* %46, i32 0)
%48 = load i32*, i32** %12
%49 = load i8*, i8** %11
%50 = load i8*, i8** %9
%51 = load i32, i32* %10
%52 = call i32 @EVP_DecryptUpdate(i32* %48, i8* %49, i32* %14, i8* %50, i32 %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %45
%55 = load i32*, i32** %12
%56 = call i32 @EVP_CIPHER_CTX_free(i32* %55)
store i32 2, i32* %6
br label %78
57: ; preds = %45
%58 = load i32*, i32** %12
%59 = load i8*, i8** %11
%60 = load i32, i32* %14
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
%63 = call i32 @EVP_DecryptFinal_ex(i32* %58, i8* %62, i32* %15)
%64 = icmp ne i32 %63, 0
br i1 %64, label %68, label %65
65: ; preds = %57
%66 = load i32*, i32** %12
%67 = call i32 @EVP_CIPHER_CTX_free(i32* %66)
store i32 2, i32* %6
br label %78
68: ; preds = %57
%69 = load i32*, i32** %12
%70 = call i32 @EVP_CIPHER_CTX_free(i32* %69)
%71 = load i32, i32* %14
%72 = load i32, i32* %15
%73 = add nsw i32 %71, %72
%74 = load i32, i32* %10
%75 = icmp ne i32 %73, %74
br i1 %75, label %76, label %77
76: ; preds = %68
store i32 2, i32* %6
br label %78
77: ; preds = %68
store i32 0, i32* %6
br label %78
78: ; preds = %77, %76, %65, %54, %42, %35, %31, %22
%79 = load i32, i32* %6
ret i32 %79
}

LLVM IR (-O2):
define i32 @aes_ecb_decrypt(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%9 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%10 = and i32 %3, 15
%11 = icmp eq i32 %10, 0
%12 = icmp ne i32 %3, 0
%13 = and i1 %12, %11
br i1 %13, label %14, label %49
14: ; preds = %5
switch i32 %1, label %49 [
i32 128, label %15
i32 192, label %17
i32 256, label %19
]
15: ; preds = %14
%16 = tail call i32* (...) @EVP_aes_128_ecb()
br label %21
17: ; preds = %14
%18 = tail call i32* (...) @EVP_aes_192_ecb()
br label %21
19: ; preds = %14
%20 = tail call i32* (...) @EVP_aes_256_ecb()
br label %21
21: ; preds = %19, %17, %15
%22 = phi i32* [ %20, %19 ], [ %18, %17 ], [ %16, %15 ]
%23 = tail call i32* (...) @EVP_CIPHER_CTX_new()
%24 = icmp eq i32* %23, null
br i1 %24, label %49, label %25
25: ; preds = %21
%26 = tail call i32 @EVP_DecryptInit_ex(i32* %23, i32* %22, i32* null, i8* %0, i32* null)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = tail call i32 @EVP_CIPHER_CTX_free(i32* %23)
br label %49
30: ; preds = %25
%31 = tail call i32 @EVP_CIPHER_CTX_set_padding(i32* %23, i32 0)
%32 = call i32 @EVP_DecryptUpdate(i32* %23, i8* %4, i32* %6, i8* %2, i32 %3)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @EVP_CIPHER_CTX_free(i32* %23)
br label %49
36: ; preds = %30
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %4, i64 %38
%40 = call i32 @EVP_DecryptFinal_ex(i32* %23, i8* %39, i32* %7)
%41 = icmp eq i32 %40, 0
%42 = call i32 @EVP_CIPHER_CTX_free(i32* %23)
br i1 %41, label %49, label %43
43: ; preds = %36
%44 = load i32, i32* %6
%45 = load i32, i32* %7
%46 = add nsw i32 %45, %44
%47 = icmp eq i32 %46, %3
%48 = select i1 %47, i32 0, i32 2
br label %49
49: ; preds = %36, %43, %21, %14, %5, %34, %28
%50 = phi i32 [ 2, %34 ], [ 2, %28 ], [ 1, %5 ], [ 1, %14 ], [ 2, %21 ], [ %48, %43 ], [ 2, %36 ]
ret i32 %50
}