#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 decrypt(unsigned char *, int, unsigned char *, unsigned char *,
            unsigned char *);
int encrypt(unsigned char *, int, unsigned char *, unsigned char *,
            unsigned char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(unsigned char *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int strlen(unsigned char *);
int main(int ac, char **av) {
  if (ac < 4) {
    fprintf(stderr, "usage: %s {key (256-bits)} {iv (128-bits)} {plaintext}\n",
            av[0]);
    exit(1);
  }
  unsigned char *key = (unsigned char *)av[1];
  unsigned char *iv = (unsigned char *)av[2];
  unsigned char *plaintext = (unsigned char *)av[3];
  int plaintext_len = strlen(plaintext);
  int buffer_len = 2 * plaintext_len;
  unsigned char *ciphertext = (unsigned char *)malloc(buffer_len);
  if (ciphertext == NULL) {
    fprintf(stderr, "%s: failed to malloc %d bytes for ciphertext\n", av[0],
            buffer_len);
    exit(1);
  }
  memset(ciphertext, '\0', buffer_len);
  unsigned char *decrypted_text = (unsigned char *)malloc(buffer_len);
  if (decrypted_text == NULL) {
    fprintf(stderr, "%s: failed to malloc %d bytes for decrypted_text\n", av[0],
            buffer_len);
    exit(1);
  }
  memset(decrypted_text, '\0', buffer_len);
  int ciphertext_len = encrypt(plaintext, plaintext_len, key, iv, ciphertext);
  printf("%s: ciphertext: '", av[0]);
  int i;
  for (i = 0; i < ciphertext_len; i++) {
    printf(" %02X", ciphertext[i]);
  }
  printf("'\n");
  int decrypted_text_len =
      decrypt(ciphertext, ciphertext_len, key, iv, decrypted_text);
  decrypted_text[decrypted_text_len] = '\0';
  printf("%s: decrypted text: '%s'\n", av[0], decrypted_text);
  free(decrypted_text);
  free(ciphertext);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11d8 <main+0x48>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1253 <main+0xc3>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x30(%rbp),%ecx
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x30(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12a3 <main+0x113>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x30(%rbp),%ecx
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x38(%rbp),%r8
call   1070 <encrypt@plt>
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    131c <main+0x18c>
mov    -0x38(%rbp),%rax
movslq -0x48(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    12e8 <main+0x158>
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x40(%rbp),%r8
call   1030 <decrypt@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x40(%rbp),%rdx
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jle    12dd <main+0x13d>
mov    0x8(%rbx),%rax
mov    %rax,0x8(%rsp)
mov    0x10(%rbx),%rax
mov    %rax,(%rsp)
mov    0x18(%rbx),%r15
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%r12d
lea    (%r12,%r12,1),%ebp
mov    %ebp,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1301 <main+0x161>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1090 <memset@plt>
mov    %ebp,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1316 <main+0x176>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1090 <memset@plt>
mov    %r15,%rdi
mov    %r12d,%esi
mov    0x8(%rsp),%rdx
mov    (%rsp),%rcx
mov    %r14,%r8
call   1080 <encrypt@plt>
mov    %eax,%r15d
mov    %rbx,0x10(%rsp)
mov    (%rbx),%rsi
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
test   %r15d,%r15d
jle    1278 <main+0xd8>
mov    %r15d,%ebx
lea    0xe4f(%rip),%r12
xor    %ebp,%ebp
nopl   (%rax)
movzbl (%r14,%rbp,1),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1260 <main+0xc0>
lea    0xe29(%rip),%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
mov    %r15d,%esi
mov    0x8(%rsp),%rdx
mov    (%rsp),%rcx
mov    %r13,%r8
call   1030 <decrypt@plt>
cltq
movb   $0x0,0x0(%r13,%rax,1)
mov    0x10(%rsp),%rax
mov    (%rax),%rsi
lea    0xdfe(%rip),%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd10(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd24(%rip),%rsi
jmp    1329 <main+0x189>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd3d(%rip),%rsi
mov    %ebp,%ecx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [56 x i8] c"usage: %s {key (256-bits)} {iv (128-bits)} {plaintext}\0A\00"
@.str.1 = [46 x i8] c"%s: failed to malloc %d bytes for ciphertext\0A\00"
@.str.2 = [50 x i8] c"%s: failed to malloc %d bytes for decrypted_text\0A\00"
@.str.3 = [18 x i8] c"%s: ciphertext: '\00"
@.str.4 = [6 x i8] c" %02X\00"
@.str.5 = [3 x i8] c"'\0A\00"
@.str.6 = [26 x i8] c"%s: decrypted text: '%s'\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 4
br i1 %17, label %18, label %25
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
store i8* %31, i8** %7
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 3
%34 = load i8*, i8** %33
store i8* %34, i8** %8
%35 = load i8*, i8** %8
%36 = call i32 @strlen(i8* %35)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = mul nsw i32 2, %37
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %11
%42 = load i8*, i8** %11
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %52
44: ; preds = %25
%45 = load i32, i32* @stderr
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = load i32, i32* %10
%50 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %48, i32 %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %25
%53 = load i8*, i8** %11
%54 = load i32, i32* %10
%55 = call i32 @memset(i8* %53, i8 signext 0, i32 %54)
%56 = load i32, i32* %10
%57 = call i64 @malloc(i32 %56)
%58 = inttoptr i64 %57 to i8*
store i8* %58, i8** %12
%59 = load i8*, i8** %12
%60 = icmp eq i8* %59, null
br i1 %60, label %61, label %69
61: ; preds = %52
%62 = load i32, i32* @stderr
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 0
%65 = load i8*, i8** %64
%66 = load i32, i32* %10
%67 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %65, i32 %66)
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %52
%70 = load i8*, i8** %12
%71 = load i32, i32* %10
%72 = call i32 @memset(i8* %70, i8 signext 0, i32 %71)
%73 = load i8*, i8** %8
%74 = load i32, i32* %9
%75 = load i8*, i8** %6
%76 = load i8*, i8** %7
%77 = load i8*, i8** %11
%78 = call i32 @encrypt(i8* %73, i32 %74, i8* %75, i8* %76, i8* %77)
store i32 %78, i32* %13
%79 = load i8**, i8*** %5
%80 = getelementptr inbounds i8*, i8** %79, i64 0
%81 = load i8*, i8** %80
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %81)
store i32 0, i32* %14
br label %83
83: ; preds = %95, %69
%84 = load i32, i32* %14
%85 = load i32, i32* %13
%86 = icmp slt i32 %84, %85
br i1 %86, label %87, label %98
87: ; preds = %83
%88 = load i8*, i8** %11
%89 = load i32, i32* %14
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i8, i8* %88, i64 %90
%92 = load i8, i8* %91
%93 = zext i8 %92 to i32
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %93)
br label %95
95: ; preds = %87
%96 = load i32, i32* %14
%97 = add nsw i32 %96, 1
store i32 %97, i32* %14
br label %83
98: ; preds = %83
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%100 = load i8*, i8** %11
%101 = load i32, i32* %13
%102 = load i8*, i8** %6
%103 = load i8*, i8** %7
%104 = load i8*, i8** %12
%105 = call i32 @decrypt(i8* %100, i32 %101, i8* %102, i8* %103, i8* %104)
store i32 %105, i32* %15
%106 = load i8*, i8** %12
%107 = load i32, i32* %15
%108 = sext i32 %107 to i64
%109 = getelementptr inbounds i8, i8* %106, i64 %108
store i8 0, i8* %109
%110 = load i8**, i8*** %5
%111 = getelementptr inbounds i8*, i8** %110, i64 0
%112 = load i8*, i8** %111
%113 = load i8*, i8** %12
%114 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %112, i8* %113)
%115 = load i8*, i8** %12
%116 = call i32 @free(i8* %115)
%117 = load i8*, i8** %11
%118 = call i32 @free(i8* %117)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [56 x i8] c"usage: %s {key (256-bits)} {iv (128-bits)} {plaintext}\0A\00"
@.str.1 = [46 x i8] c"%s: failed to malloc %d bytes for ciphertext\0A\00"
@.str.2 = [50 x i8] c"%s: failed to malloc %d bytes for decrypted_text\0A\00"
@.str.3 = [18 x i8] c"%s: ciphertext: '\00"
@.str.4 = [6 x i8] c" %02X\00"
@.str.6 = [26 x i8] c"%s: decrypted text: '%s'\0A\00"
@str = [2 x i8] c"'\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 4
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i32 @strlen(i8* %15)
%17 = shl nsw i32 %16, 1
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %26
21: ; preds = %9
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %1
%24 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %23, i32 %17)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %9
%27 = tail call i32 @memset(i8* %19, i8 signext 0, i32 %17)
%28 = tail call i64 @malloc(i32 %17)
%29 = inttoptr i64 %28 to i8*
%30 = icmp eq i64 %28, 0
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %1
%34 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %33, i32 %17)
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %26
%37 = tail call i32 @memset(i8* %29, i8 signext 0, i32 %17)
%38 = tail call i32 @encrypt(i8* %15, i32 %16, i8* %11, i8* %13, i8* %19)
%39 = load i8*, i8** %1
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = icmp sgt i32 %38, 0
br i1 %41, label %42, label %52
42: ; preds = %36
%43 = zext i32 %38 to i64
br label %44
44: ; preds = %42, %44
%45 = phi i64 [ 0, %42 ], [ %50, %44 ]
%46 = getelementptr inbounds i8, i8* %19, i64 %45
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = add nuw nsw i64 %45, 1
%51 = icmp eq i64 %50, %43
br i1 %51, label %52, label %44
52: ; preds = %44, %36
%53 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%54 = tail call i32 @decrypt(i8* %19, i32 %38, i8* %11, i8* %13, i8* %29)
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %29, i64 %55
store i8 0, i8* %56
%57 = load i8*, i8** %1
%58 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %57, i8* %29)
%59 = tail call i32 @free(i8* %29)
%60 = tail call i32 @free(i8* %19)
ret i32 0
}