#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;

/* Variables and functions */
int /*<<< orphan*/ EVP_CIPHER_CTX_cleanup(int /*<<< orphan*/ *);
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_DecryptInit(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                   unsigned char const *,
                                   unsigned char const *);
int /*<<< orphan*/ EVP_DecryptUpdate(int /*<<< orphan*/ *, unsigned char *,
                                     int *, unsigned char *, int);
int /*<<< orphan*/ EVP_aes_128_ecb();
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ do_step_decrypt(int /*<<< orphan*/ *, unsigned char *,
                                   unsigned char const *, unsigned char *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ log2(int);
int /*<<< orphan*/ memxor(unsigned char *, unsigned char const *, int);
int /*<<< orphan*/ printf(char *);
void mixdecrypt_macroblock(const unsigned char *macro, unsigned char *out,
                           unsigned char *buffer, const unsigned char *key,
                           const unsigned char *iv) {
  int step, outl;
  EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  if (NULL == ctx) {
    printf("Cannot allocate needed memory\n");
    exit(EXIT_FAILURE);
  }
  EVP_DecryptInit(ctx, EVP_aes_128_ecb(), key, iv);
  EVP_CIPHER_CTX_set_padding(ctx, 0);
  for (step = ((int)log2(1024)) / ((int)log2((16 / 4))) - 1; step >= 1;
       --step) {
    do_step_decrypt(ctx, buffer, macro, out, step);
    macro = out;
  }
  EVP_DecryptUpdate(ctx, out, &outl, out, (4 * 1024));
  memxor(out, iv, 16);
  if (0)
    assert((4 * 1024) == outl);
  EVP_CIPHER_CTX_cleanup(ctx);
  EVP_CIPHER_CTX_free(ctx);
}


x86 assembly (clang, -O0):
<mixdecrypt_macroblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    $0x0,%al
call   10b0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
jne    120d <mixdecrypt_macroblock+0x4d>
lea    0xe06(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1090 <EVP_aes_128_ecb@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1070 <EVP_DecryptInit@plt>
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
call   10a0 <EVP_CIPHER_CTX_set_padding@plt>
mov    $0x400,%edi
call   1040 <log2@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x4,%edi
call   1040 <log2@plt>
mov    %rax,%rcx
mov    -0x3c(%rbp),%eax
cltd
idiv   %ecx
sub    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jl     1299 <mixdecrypt_macroblock+0xd9>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
call   1030 <do_step_decrypt@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    1260 <mixdecrypt_macroblock+0xa0>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    -0x30(%rbp),%rdx
mov    $0x1000,%r8d
call   10c0 <EVP_DecryptUpdate@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1050 <memxor@plt>
mov    -0x38(%rbp),%rdi
call   10e0 <EVP_CIPHER_CTX_cleanup@plt>
mov    -0x38(%rbp),%rdi
call   1080 <EVP_CIPHER_CTX_free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mixdecrypt_macroblock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%rbp
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r13
xor    %eax,%eax
call   10b0 <EVP_CIPHER_CTX_new@plt>
test   %rax,%rax
je     12b5 <mixdecrypt_macroblock+0xf5>
mov    %rax,%r15
xor    %eax,%eax
call   1090 <EVP_aes_128_ecb@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %rbp,%rdx
mov    %r14,%rcx
call   1070 <EVP_DecryptInit@plt>
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <EVP_CIPHER_CTX_set_padding@plt>
mov    $0x400,%edi
call   1040 <log2@plt>
mov    %rax,%rbp
mov    $0x4,%edi
call   1040 <log2@plt>
mov    %rax,%rcx
mov    %ebp,%eax
cltd
idiv   %ecx
cmp    $0x2,%eax
jl     126d <mixdecrypt_macroblock+0xad>
mov    %eax,%ebp
lea    -0x1(%rbp),%r8d
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1030 <do_step_decrypt@plt>
cmp    $0x2,%ebp
je     126d <mixdecrypt_macroblock+0xad>
xchg   %ax,%ax
lea    -0x2(%rbp),%r8d
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %rbx,%rcx
call   1030 <do_step_decrypt@plt>
add    $0xffffffff,%ebp
cmp    $0x2,%ebp
ja     1250 <mixdecrypt_macroblock+0x90>
lea    0x4(%rsp),%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbx,%rcx
mov    $0x1000,%r8d
call   10c0 <EVP_DecryptUpdate@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x10,%edx
call   1060 <memxor@plt>
mov    %r15,%rdi
call   10e0 <EVP_CIPHER_CTX_cleanup@plt>
mov    %r15,%rdi
call   1080 <EVP_CIPHER_CTX_free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd44(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"Cannot allocate needed memory\0A\00"
@EXIT_FAILURE = global i32 0
define void @mixdecrypt_macroblock(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%14 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %14, i32** %13
%15 = load i32*, i32** %13
%16 = icmp eq i32* null, %15
br i1 %16, label %17, label %21
17: ; preds = %5
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %5
%22 = load i32*, i32** %13
%23 = call i32 (...) @EVP_aes_128_ecb()
%24 = load i8*, i8** %9
%25 = load i8*, i8** %10
%26 = call i32 @EVP_DecryptInit(i32* %22, i32 %23, i8* %24, i8* %25)
%27 = load i32*, i32** %13
%28 = call i32 @EVP_CIPHER_CTX_set_padding(i32* %27, i32 0)
%29 = call i64 @log2(i32 1024)
%30 = trunc i64 %29 to i32
%31 = call i64 @log2(i32 4)
%32 = trunc i64 %31 to i32
%33 = sdiv i32 %30, %32
%34 = sub nsw i32 %33, 1
store i32 %34, i32* %11
br label %35
35: ; preds = %46, %21
%36 = load i32, i32* %11
%37 = icmp sge i32 %36, 1
br i1 %37, label %38, label %49
38: ; preds = %35
%39 = load i32*, i32** %13
%40 = load i8*, i8** %8
%41 = load i8*, i8** %6
%42 = load i8*, i8** %7
%43 = load i32, i32* %11
%44 = call i32 @do_step_decrypt(i32* %39, i8* %40, i8* %41, i8* %42, i32 %43)
%45 = load i8*, i8** %7
store i8* %45, i8** %6
br label %46
46: ; preds = %38
%47 = load i32, i32* %11
%48 = add nsw i32 %47, -1
store i32 %48, i32* %11
br label %35
49: ; preds = %35
%50 = load i32*, i32** %13
%51 = load i8*, i8** %7
%52 = load i8*, i8** %7
%53 = call i32 @EVP_DecryptUpdate(i32* %50, i8* %51, i32* %12, i8* %52, i32 4096)
%54 = load i8*, i8** %7
%55 = load i8*, i8** %10
%56 = call i32 @memxor(i8* %54, i8* %55, i32 16)
%57 = load i32*, i32** %13
%58 = call i32 @EVP_CIPHER_CTX_cleanup(i32* %57)
%59 = load i32*, i32** %13
%60 = call i32 @EVP_CIPHER_CTX_free(i32* %59)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@str = [30 x i8] c"Cannot allocate needed memory\00"
define void @mixdecrypt_macroblock(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = tail call i32* (...) @EVP_CIPHER_CTX_new()
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %5
%15 = tail call i32 (...) @EVP_aes_128_ecb()
%16 = tail call i32 @EVP_DecryptInit(i32* %8, i32 %15, i8* %3, i8* %4)
%17 = tail call i32 @EVP_CIPHER_CTX_set_padding(i32* %8, i32 0)
%18 = tail call i64 @log2(i32 1024)
%19 = trunc i64 %18 to i32
%20 = tail call i64 @log2(i32 4)
%21 = trunc i64 %20 to i32
%22 = sdiv i32 %19, %21
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %33
24: ; preds = %14
%25 = add nsw i32 %22, -1
%26 = tail call i32 @do_step_decrypt(i32* %8, i8* %2, i8* %0, i8* %1, i32 %25)
%27 = icmp eq i32 %22, 2
br i1 %27, label %33, label %28
28: ; preds = %24, %28
%29 = phi i32 [ %30, %28 ], [ %25, %24 ]
%30 = add nsw i32 %29, -1
%31 = tail call i32 @do_step_decrypt(i32* %8, i8* %2, i8* %1, i8* %1, i32 %30)
%32 = icmp ugt i32 %29, 2
br i1 %32, label %28, label %33
33: ; preds = %28, %24, %14
%34 = call i32 @EVP_DecryptUpdate(i32* %8, i8* %1, i32* %6, i8* %1, i32 4096)
%35 = call i32 @memxor(i8* %1, i8* %4, i32 16)
%36 = call i32 @EVP_CIPHER_CTX_cleanup(i32* %8)
%37 = call i32 @EVP_CIPHER_CTX_free(i32* %8)
ret void
}