#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*/ AES_KEY;

/* Variables and functions */
int /*<<< orphan*/ AES_ENCRYPT;
int /*<<< orphan*/ AES_cbc_encrypt(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                   int, int /*<<< orphan*/ *,
                                   int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ AES_encrypt(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ AES_set_encrypt_key(int /*<<< orphan*/ *, int,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *gen_random_bytes(int);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int rand();
uint8_t *encryption_oracle(uint8_t *in, int len, int *real, int *out_len) {
  uint8_t *bytes;
  uint8_t *key;
  uint8_t *iv = NULL;
  uint8_t *rand1;
  uint8_t *rand2;
  uint8_t *enc;
  AES_KEY aes_key;
  int n1;
  int n2;
  int i;
  key = gen_random_bytes(16);
  n1 = rand() % 6 + 5;
  n2 = rand() % 6 + 5;
  rand1 = gen_random_bytes(n1);
  rand2 = gen_random_bytes(n2);
  bytes = malloc((n1 + len + n2) * sizeof(uint8_t));
  memcpy(bytes, rand1, n1 * sizeof(uint8_t));
  memcpy(&bytes[n1], in, len * sizeof(uint8_t));
  memcpy(&bytes[n1 + len], rand2, n2 * sizeof(uint8_t));
  len += n1 + n2;
  *real = rand() % 2;
  *out_len = len + 16;
  enc = malloc(*out_len * sizeof(uint8_t));
  AES_set_encrypt_key(key, 128, &aes_key);
  if (*real == 0) {
    for (i = 0; i < len / 16; i++)
      AES_encrypt(&bytes[i * 16], &enc[i * 16], &aes_key);
  } else {
    iv = gen_random_bytes(16);
    AES_cbc_encrypt(bytes, enc, len, &aes_key, iv, AES_ENCRYPT);
  }
  free(bytes);
  free(key);
  free(iv);
  free(rand1);
  free(rand2);
  return enc;
}


x86 assembly (clang, -O0):
<encryption_oracle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    $0x10,%edi
call   1050 <gen_random_bytes@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x6,%ecx
cltd
idiv   %ecx
add    $0x5,%edx
mov    %edx,-0x58(%rbp)
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x6,%ecx
cltd
idiv   %ecx
add    $0x5,%edx
mov    %edx,-0x5c(%rbp)
mov    -0x58(%rbp),%edi
call   1050 <gen_random_bytes@plt>
mov    %rax,-0x40(%rbp)
mov    -0x5c(%rbp),%edi
call   1050 <gen_random_bytes@plt>
mov    %rax,-0x48(%rbp)
mov    -0x58(%rbp),%eax
add    -0xc(%rbp),%eax
add    -0x5c(%rbp),%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
movslq -0x58(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rdi
movslq -0x58(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x58(%rbp),%eax
add    -0xc(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x48(%rbp),%rsi
movslq -0x5c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x58(%rbp),%eax
add    -0x5c(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
mov    %edx,(%rax)
mov    -0xc(%rbp),%ecx
add    $0x10,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x80,%esi
lea    -0x54(%rbp),%rdx
call   1040 <AES_set_encrypt_key@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1331 <encryption_oracle+0x1b1>
movl   $0x0,-0x60(%rbp)
mov    -0x60(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x64(%rbp),%eax
cmp    %ecx,%eax
jge    132c <encryption_oracle+0x1ac>
mov    -0x28(%rbp),%rdi
mov    -0x60(%rbp),%eax
shl    $0x4,%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x50(%rbp),%rsi
mov    -0x60(%rbp),%eax
shl    $0x4,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
lea    -0x54(%rbp),%rdx
call   1090 <AES_encrypt@plt>
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    12d1 <encryption_oracle+0x151>
jmp    1361 <encryption_oracle+0x1e1>
mov    $0x10,%edi
call   1050 <gen_random_bytes@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x38(%rbp),%r8
mov    0x2c8b(%rip),%rax
mov    (%rax),%r9d
lea    -0x54(%rbp),%rcx
call   1060 <AES_cbc_encrypt@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encryption_oracle>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rcx,0x18(%rsp)
mov    %rdx,0x10(%rsp)
mov    %esi,%r15d
mov    %r15,0x30(%rsp)
mov    %rdi,0x48(%rsp)
mov    $0x10,%edi
call   1050 <gen_random_bytes@plt>
mov    %rax,0x28(%rsp)
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0x2aaaaaab,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,2),%ecx
mov    %eax,%ebp
sub    %ecx,%ebp
neg    %ecx
lea    (%rax,%rcx,1),%r14d
add    $0x5,%r14d
mov    %r14,0x38(%rsp)
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0x2aaaaaab,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,2),%ecx
mov    %eax,%r12d
sub    %ecx,%r12d
neg    %ecx
lea    (%rax,%rcx,1),%ebx
add    $0x5,%ebx
mov    %rbx,0x40(%rsp)
mov    %r14d,%edi
call   1050 <gen_random_bytes@plt>
mov    %rax,%r14
mov    %ebx,%edi
call   1050 <gen_random_bytes@plt>
mov    %rax,0x20(%rsp)
add    %ebp,%r15d
add    $0x5,%r15d
lea    (%r15,%rbx,1),%r13d
lea    0x0(,%r13,4),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0x14(,%rbp,4),%edx
mov    %rax,%rdi
mov    %r14,0x50(%rsp)
mov    %r14,%rsi
mov    0x20(%rsp),%r14
call   1070 <memcpy@plt>
mov    0x38(%rsp),%rax
lea    (%rbx,%rax,4),%rdi
mov    0x30(%rsp),%rdx
shl    $0x2,%edx
mov    0x48(%rsp),%rsi
call   1070 <memcpy@plt>
movslq %r15d,%rbp
mov    %rbx,%r15
lea    (%rbx,%rbp,4),%rdi
lea    0x14(,%r12,4),%edx
mov    %r14,%rsi
call   1070 <memcpy@plt>
xor    %eax,%eax
call   10a0 <rand@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
and    $0xfffffffe,%ecx
sub    %ecx,%eax
mov    0x10(%rsp),%rbx
mov    %eax,(%rbx)
mov    0x40(%rsp),%rax
add    %ebp,%eax
add    $0x10,%eax
mov    0x18(%rsp),%rcx
mov    %eax,(%rcx)
lea    0x40(,%r13,4),%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
lea    0xc(%rsp),%rdx
mov    0x28(%rsp),%rdi
mov    $0x80,%esi
call   1040 <AES_set_encrypt_key@plt>
cmpl   $0x0,(%rbx)
je     1327 <encryption_oracle+0x1a7>
mov    $0x10,%edi
call   1050 <gen_random_bytes@plt>
mov    %rax,%r12
mov    0x2cd7(%rip),%rax
mov    (%rax),%r9d
lea    0xc(%rsp),%rcx
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r13d,%edx
mov    %r12,%r8
call   1060 <AES_cbc_encrypt@plt>
mov    %r15,%rdi
jmp    1380 <encryption_oracle+0x200>
cmp    $0x10,%r13d
mov    %r15,%rdi
jl     137d <encryption_oracle+0x1fd>
shr    $0x4,%r13d
lea    0xc(%rsp),%rbx
xor    %r12d,%r12d
mov    %rdi,0x18(%rsp)
mov    %rbp,%r14
mov    %rdi,%rbp
mov    %r14,0x10(%rsp)
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1090 <AES_encrypt@plt>
add    $0x40,%r14
add    $0x40,%rbp
add    $0xffffffffffffffff,%r13
jne    1350 <encryption_oracle+0x1d0>
mov    0x20(%rsp),%r14
mov    0x18(%rsp),%rdi
mov    0x10(%rsp),%rbp
jmp    1380 <encryption_oracle+0x200>
xor    %r12d,%r12d
call   1030 <free@plt>
mov    0x28(%rsp),%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x50(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@AES_ENCRYPT = global i32 0
define i32* @encryption_oracle(i32* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32* null, i32** %11
%19 = call i32* @gen_random_bytes(i32 16)
store i32* %19, i32** %10
%20 = call i32 (...) @rand()
%21 = srem i32 %20, 6
%22 = add nsw i32 %21, 5
store i32 %22, i32* %16
%23 = call i32 (...) @rand()
%24 = srem i32 %23, 6
%25 = add nsw i32 %24, 5
store i32 %25, i32* %17
%26 = load i32, i32* %16
%27 = call i32* @gen_random_bytes(i32 %26)
store i32* %27, i32** %12
%28 = load i32, i32* %17
%29 = call i32* @gen_random_bytes(i32 %28)
store i32* %29, i32** %13
%30 = load i32, i32* %16
%31 = load i32, i32* %6
%32 = add nsw i32 %30, %31
%33 = load i32, i32* %17
%34 = add nsw i32 %32, %33
%35 = sext i32 %34 to i64
%36 = mul i64 %35, 4
%37 = trunc i64 %36 to i32
%38 = call i32* @malloc(i32 %37)
store i32* %38, i32** %9
%39 = load i32*, i32** %9
%40 = load i32*, i32** %12
%41 = load i32, i32* %16
%42 = sext i32 %41 to i64
%43 = mul i64 %42, 4
%44 = trunc i64 %43 to i32
%45 = call i32 @memcpy(i32* %39, i32* %40, i32 %44)
%46 = load i32*, i32** %9
%47 = load i32, i32* %16
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = load i32*, i32** %5
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = mul i64 %52, 4
%54 = trunc i64 %53 to i32
%55 = call i32 @memcpy(i32* %49, i32* %50, i32 %54)
%56 = load i32*, i32** %9
%57 = load i32, i32* %16
%58 = load i32, i32* %6
%59 = add nsw i32 %57, %58
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %56, i64 %60
%62 = load i32*, i32** %13
%63 = load i32, i32* %17
%64 = sext i32 %63 to i64
%65 = mul i64 %64, 4
%66 = trunc i64 %65 to i32
%67 = call i32 @memcpy(i32* %61, i32* %62, i32 %66)
%68 = load i32, i32* %16
%69 = load i32, i32* %17
%70 = add nsw i32 %68, %69
%71 = load i32, i32* %6
%72 = add nsw i32 %71, %70
store i32 %72, i32* %6
%73 = call i32 (...) @rand()
%74 = srem i32 %73, 2
%75 = load i32*, i32** %7
store i32 %74, i32* %75
%76 = load i32, i32* %6
%77 = add nsw i32 %76, 16
%78 = load i32*, i32** %8
store i32 %77, i32* %78
%79 = load i32*, i32** %8
%80 = load i32, i32* %79
%81 = sext i32 %80 to i64
%82 = mul i64 %81, 4
%83 = trunc i64 %82 to i32
%84 = call i32* @malloc(i32 %83)
store i32* %84, i32** %14
%85 = load i32*, i32** %10
%86 = call i32 @AES_set_encrypt_key(i32* %85, i32 128, i32* %15)
%87 = load i32*, i32** %7
%88 = load i32, i32* %87
%89 = icmp eq i32 %88, 0
br i1 %89, label %90, label %112
90: ; preds = %4
store i32 0, i32* %18
br label %91
91: ; preds = %108, %90
%92 = load i32, i32* %18
%93 = load i32, i32* %6
%94 = sdiv i32 %93, 16
%95 = icmp slt i32 %92, %94
br i1 %95, label %96, label %111
96: ; preds = %91
%97 = load i32*, i32** %9
%98 = load i32, i32* %18
%99 = mul nsw i32 %98, 16
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i32, i32* %97, i64 %100
%102 = load i32*, i32** %14
%103 = load i32, i32* %18
%104 = mul nsw i32 %103, 16
%105 = sext i32 %104 to i64
%106 = getelementptr inbounds i32, i32* %102, i64 %105
%107 = call i32 @AES_encrypt(i32* %101, i32* %106, i32* %15)
br label %108
108: ; preds = %96
%109 = load i32, i32* %18
%110 = add nsw i32 %109, 1
store i32 %110, i32* %18
br label %91
111: ; preds = %91
br label %120
112: ; preds = %4
%113 = call i32* @gen_random_bytes(i32 16)
store i32* %113, i32** %11
%114 = load i32*, i32** %9
%115 = load i32*, i32** %14
%116 = load i32, i32* %6
%117 = load i32*, i32** %11
%118 = load i32, i32* @AES_ENCRYPT
%119 = call i32 @AES_cbc_encrypt(i32* %114, i32* %115, i32 %116, i32* %15, i32* %117, i32 %118)
br label %120
120: ; preds = %112, %111
%121 = load i32*, i32** %9
%122 = call i32 @free(i32* %121)
%123 = load i32*, i32** %10
%124 = call i32 @free(i32* %123)
%125 = load i32*, i32** %11
%126 = call i32 @free(i32* %125)
%127 = load i32*, i32** %12
%128 = call i32 @free(i32* %127)
%129 = load i32*, i32** %13
%130 = call i32 @free(i32* %129)
%131 = load i32*, i32** %14
ret i32* %131
}

LLVM IR (-O2):
@AES_ENCRYPT = global i32 0
define i32* @encryption_oracle(i32* %0, i32 %1, i32* nocapture %2, i32* nocapture writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i32* @gen_random_bytes(i32 16)
%8 = tail call i32 (...) @rand()
%9 = srem i32 %8, 6
%10 = add nsw i32 %9, 5
%11 = tail call i32 (...) @rand()
%12 = srem i32 %11, 6
%13 = add nsw i32 %12, 5
%14 = tail call i32* @gen_random_bytes(i32 %10)
%15 = tail call i32* @gen_random_bytes(i32 %13)
%16 = add i32 %10, %1
%17 = add i32 %16, %13
%18 = shl i32 %17, 2
%19 = tail call i32* @malloc(i32 %18)
%20 = zext i32 %10 to i64
%21 = shl nuw nsw i32 %10, 2
%22 = tail call i32 @memcpy(i32* %19, i32* %14, i32 %21)
%23 = getelementptr inbounds i32, i32* %19, i64 %20
%24 = shl i32 %1, 2
%25 = tail call i32 @memcpy(i32* %23, i32* %0, i32 %24)
%26 = sext i32 %16 to i64
%27 = getelementptr inbounds i32, i32* %19, i64 %26
%28 = shl nuw nsw i32 %13, 2
%29 = tail call i32 @memcpy(i32* %27, i32* %15, i32 %28)
%30 = tail call i32 (...) @rand()
%31 = srem i32 %30, 2
store i32 %31, i32* %2
%32 = add nsw i32 %17, 16
store i32 %32, i32* %3
%33 = shl i32 %32, 2
%34 = tail call i32* @malloc(i32 %33)
%35 = call i32 @AES_set_encrypt_key(i32* %7, i32 128, i32* %5)
%36 = load i32, i32* %2
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %51
38: ; preds = %4
%39 = icmp sgt i32 %17, 15
br i1 %39, label %40, label %55
40: ; preds = %38
%41 = lshr i32 %17, 4
%42 = zext i32 %41 to i64
br label %43
43: ; preds = %40, %43
%44 = phi i64 [ 0, %40 ], [ %49, %43 ]
%45 = shl nsw i64 %44, 4
%46 = getelementptr inbounds i32, i32* %19, i64 %45
%47 = getelementptr inbounds i32, i32* %34, i64 %45
%48 = call i32 @AES_encrypt(i32* %46, i32* %47, i32* %5)
%49 = add nuw nsw i64 %44, 1
%50 = icmp eq i64 %49, %42
br i1 %50, label %55, label %43
51: ; preds = %4
%52 = call i32* @gen_random_bytes(i32 16)
%53 = load i32, i32* @AES_ENCRYPT
%54 = call i32 @AES_cbc_encrypt(i32* %19, i32* %34, i32 %17, i32* %5, i32* %52, i32 %53)
br label %55
55: ; preds = %43, %38, %51
%56 = phi i32* [ %52, %51 ], [ null, %38 ], [ null, %43 ]
%57 = call i32 @free(i32* %19)
%58 = call i32 @free(i32* %7)
%59 = call i32 @free(i32* %56)
%60 = call i32 @free(i32* %14)
%61 = call i32 @free(i32* %15)
ret i32* %34
}