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

/* Variables and functions */
int /*<<< orphan*/ aes_crypt(char *, int, int, char *, int, char *,
                             char const *, char *);
int /*<<< orphan*/ print_data(char *, char *, int);
int /*<<< orphan*/ printf(char *);
void AES_demo(int use_mtp_key) {
  int i;
  char out[16] = {0};
  char in[16] = "Single block msg";
  const char key[16] = "\x06\xa9\x21\x40\x36\xb8\xa1\x5b"
                       "\x51\x2e\x03\xd5\x34\x12\x00\x06";
  char iv[16] =
      "\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41";
  printf(
      "\n+---------------------------------------------------------------+\n");
  if (use_mtp_key)
    printf(
        "|  AES-128 CBC mode encrypt/descrpt demo (use MTP key)          |\n");
  else
    printf(
        "|  AES-128 CBC mode encrypt/descrpt demo                        |\n");
  printf("+---------------------------------------------------------------+\n");
  aes_crypt("cbc(aes)", 1, use_mtp_key, in, 16, out, key, iv);
  printf("\nAES encrypt result =>\n");
  print_data("IN", in, 16);
  print_data("OUT", out, 16);
  aes_crypt("cbc(aes)", 0, use_mtp_key, out, 16, in, key, iv);
  printf("\nAES descrypt result =>\n");
  print_data("IN", out, 16);
  print_data("OUT", in, 16);
}


x86 assembly (clang, -O0):
<AES_demo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0xe9e(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe9b(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe98(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe95(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe92(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe8f(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe8c(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x4(%rbp)
je     11c4 <AES_demo+0x84>
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
jmp    11d0 <AES_demo+0x90>
lea    0xeec(%rip),%rdi
call   1030 <printf@plt>
lea    0xe5a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%edx
lea    -0x30(%rbp),%rcx
lea    -0x20(%rbp),%r9
lea    -0x40(%rbp),%r10
lea    -0x50(%rbp),%rax
lea    0xf04(%rip),%rdi
mov    $0x1,%esi
mov    $0x10,%r8d
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1060 <aes_crypt@plt>
lea    0xeed(%rip),%rdi
call   1030 <printf@plt>
lea    -0x30(%rbp),%rsi
lea    0xef5(%rip),%rdi
mov    $0x10,%edx
call   1050 <print_data@plt>
lea    -0x20(%rbp),%rsi
lea    0xee3(%rip),%rdi
mov    $0x10,%edx
call   1050 <print_data@plt>
mov    -0x4(%rbp),%edx
lea    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%r9
lea    -0x40(%rbp),%r10
lea    -0x50(%rbp),%rax
lea    0xe9b(%rip),%rdi
xor    %esi,%esi
mov    $0x10,%r8d
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1060 <aes_crypt@plt>
lea    0xea6(%rip),%rdi
call   1030 <printf@plt>
lea    -0x20(%rbp),%rsi
lea    0xe8f(%rip),%rdi
mov    $0x10,%edx
call   1050 <print_data@plt>
lea    -0x30(%rbp),%rsi
lea    0xe7d(%rip),%rdi
mov    $0x10,%edx
call   1050 <print_data@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AES_demo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xeb1(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xeb5(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xeb9(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xece(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
lea    0xf03(%rip),%rax
lea    0xf6d(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
lea    0xeaa(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbp
lea    0x10(%rsp),%rax
lea    0xe85(%rip),%r12
lea    0x20(%rsp),%r14
lea    0x30(%rsp),%r15
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r14,%rcx
mov    $0x10,%r8d
mov    %r15,%r9
push   %rbp
push   %rax
call   1050 <aes_crypt@plt>
add    $0x10,%rsp
lea    0xee8(%rip),%rdi
call   1030 <puts@plt>
lea    0xe50(%rip),%r13
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x10,%edx
call   1040 <print_data@plt>
lea    0xe3c(%rip),%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1040 <print_data@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    %r15,%rcx
mov    $0x10,%r8d
mov    %r14,%r9
mov    %rsp,%rax
push   %rax
lea    0x18(%rsp),%rax
push   %rax
call   1050 <aes_crypt@plt>
add    $0x10,%rsp
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1040 <print_data@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x10,%edx
call   1040 <print_data@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.AES_demo.in = [16 x i8] c"Single block msg"
@__const.AES_demo.key = [16 x i8] c"\06\A9!@6\B8\A1[Q.\03\D54\12\00\06"
@__const.AES_demo.iv = [16 x i8] c"=\AF\BAB\9D\9E\B40\B4\22\DA\80,\9F\ACA"
@.str = [68 x i8] c"\0A+---------------------------------------------------------------+\0A\00"
@.str.1 = [67 x i8] c"| AES-128 CBC mode encrypt/descrpt demo (use MTP key) |\0A\00"
@.str.2 = [67 x i8] c"| AES-128 CBC mode encrypt/descrpt demo |\0A\00"
@.str.3 = [67 x i8] c"+---------------------------------------------------------------+\0A\00"
@.str.4 = [9 x i8] c"cbc(aes)\00"
@.str.5 = [24 x i8] c"\0AAES encrypt result =>\0A\00"
@.str.6 = [3 x i8] c"IN\00"
@.str.7 = [4 x i8] c"OUT\00"
@.str.8 = [25 x i8] c"\0AAES descrypt result =>\0A\00"
define void @AES_demo(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = alloca [16 x i8]
%6 = alloca [16 x i8]
%7 = alloca [16 x i8]
store i32 %0, i32* %2
%8 = bitcast [16 x i8]* %4 to i8*
%9 = bitcast [16 x i8]* %5 to i8*
%10 = bitcast [16 x i8]* %6 to i8*
%11 = bitcast [16 x i8]* %7 to i8*
%12 = call i32 @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* %2
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0))
br label %19
17: ; preds = %1
%18 = call i32 @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %15
%20 = call i32 @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* %2
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%26 = call i32 @aes_crypt(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 %21, i8* %22, i32 16, i8* %23, i8* %24, i8* %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%29 = call i32 @print_data(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %28, i32 16)
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%31 = call i32 @print_data(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %30, i32 16)
%32 = load i32, i32* %2
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%37 = call i32 @aes_crypt(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 %32, i8* %33, i32 16, i8* %34, i8* %35, i8* %36)
%38 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%40 = call i32 @print_data(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %39, i32 16)
%41 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%42 = call i32 @print_data(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %41, i32 16)
ret void
}

LLVM IR (-O2):
@__const.AES_demo.in = [16 x i8] c"Single block msg"
@__const.AES_demo.key = [16 x i8] c"\06\A9!@6\B8\A1[Q.\03\D54\12\00\06"
@__const.AES_demo.iv = [16 x i8] c"=\AF\BAB\9D\9E\B40\B4\22\DA\80,\9F\ACA"
@.str.4 = [9 x i8] c"cbc(aes)\00"
@.str.6 = [3 x i8] c"IN\00"
@.str.7 = [4 x i8] c"OUT\00"
@str = [67 x i8] c"\0A+---------------------------------------------------------------+\00"
@str.9 = [66 x i8] c"| AES-128 CBC mode encrypt/descrpt demo |\00"
@str.10 = [66 x i8] c"+---------------------------------------------------------------+\00"
@str.11 = [23 x i8] c"\0AAES encrypt result =>\00"
@str.12 = [24 x i8] c"\0AAES descrypt result =>\00"
@str.13 = [66 x i8] c"| AES-128 CBC mode encrypt/descrpt demo (use MTP key) |\00"
define void @AES_demo(i32 %0) {
%2 = alloca [16 x i8]
%3 = alloca [16 x i8]
%4 = alloca [16 x i8]
%5 = alloca [16 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @str, i64 0, i64 0))
%11 = icmp eq i32 %0, 0
%12 = select i1 %11, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @str.9, i64 0, i64 0), i8* getelementptr inbounds ([66 x i8], [66 x i8]* @str.13, i64 0, i64 0)
%13 = tail call i32 @puts(i8* dereferenceable(1) %12)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @str.10, i64 0, i64 0))
%15 = call i32 @aes_crypt(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 %0, i8* %7, i32 16, i8* %6, i8* %8, i8* %9)
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0))
%17 = call i32 @print_data(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %7, i32 16)
%18 = call i32 @print_data(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %6, i32 16)
%19 = call i32 @aes_crypt(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 %0, i8* %6, i32 16, i8* %7, i8* %8, i8* %9)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.12, i64 0, i64 0))
%21 = call i32 @print_data(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %6, i32 16)
%22 = call i32 @print_data(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %7, i32 16)
ret void
}