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

/* Variables and functions */
int XTeaEncryptCBC(unsigned char *, size_t, unsigned char *, size_t,
                   unsigned char *, unsigned char *);
scalar_t__ XTeaGenerateIVec(unsigned char *);
char *calloc(size_t, int);
int /*<<< orphan*/ free(char *);
int hexEncode(char *, int, char *, size_t);
size_t strlen(char const *);
char *encrypt(const char *plain, const char *key) {
  size_t plainLen = strlen(plain);
  size_t cipherSize = plainLen + 8 * 2;
  char *cipher = calloc(cipherSize, sizeof(char));
  if (!cipher) {
    return NULL;
  }
  if (XTeaGenerateIVec((unsigned char *)cipher) < 0) {
    return NULL;
  }
  ssize_t len = XTeaEncryptCBC((unsigned char *)plain, strlen(plain),
                               (unsigned char *)cipher + 8, cipherSize - 8,
                               (unsigned char *)cipher, (unsigned char *)key);
  if (len < 0) {
    free(cipher);
    return NULL;
  }
  len += 8;
  size_t encodedSize = len * 2 + 1;
  char *encoded = calloc(encodedSize, sizeof(char));
  if (!encoded) {
    free(cipher);
    return NULL;
  }
  len = hexEncode(cipher, len, encoded, encodedSize);
  if (len < 0) {
    free(cipher);
    free(encoded);
    return NULL;
  }
  free(cipher);
  return encoded;
}


x86 assembly (clang, -O0):
<encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b3 <encrypt+0x53>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <encrypt+0x167>
mov    -0x30(%rbp),%rdi
call   1060 <XTeaGenerateIVec@plt>
cmp    $0x0,%rax
jge    11d3 <encrypt+0x73>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <encrypt+0x167>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rsi
mov    -0x30(%rbp),%rdx
add    $0x8,%rdx
mov    -0x28(%rbp),%rcx
sub    $0x8,%rcx
mov    -0x30(%rbp),%r8
mov    -0x18(%rbp),%r9
call   1040 <XTeaEncryptCBC@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    122b <encrypt+0xcb>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <encrypt+0x167>
mov    -0x34(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1276 <encrypt+0x116>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <encrypt+0x167>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rcx
call   1070 <hexEncode@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12b6 <encrypt+0x156>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c7 <encrypt+0x167>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
call   1050 <strlen@plt>
mov    %rax,%rbx
lea    0x10(%rax),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
test   %rax,%rax
je     1218 <encrypt+0xb8>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <XTeaGenerateIVec@plt>
test   %rax,%rax
js     1218 <encrypt+0xb8>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdx
add    $0x8,%rdx
add    $0x8,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
mov    %rbx,%rcx
mov    %r15,%r8
mov    %r14,%r9
call   1040 <XTeaEncryptCBC@plt>
test   %eax,%eax
js     1210 <encrypt+0xb0>
mov    %eax,%r14d
lea    (%r14,%r14,1),%eax
add    $0x11,%eax
movslq %eax,%r12
mov    %r12,%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
test   %rax,%rax
je     1210 <encrypt+0xb0>
mov    %rax,%rbx
add    $0x8,%r14d
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
mov    %r12,%rcx
call   1070 <hexEncode@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%r15
test   %ebp,%ebp
jns    121a <encrypt+0xba>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @encrypt(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %6
%14 = load i64, i64* %6
%15 = add i64 %14, 16
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = call i8* @calloc(i64 %16, i32 1)
store i8* %17, i8** %8
%18 = load i8*, i8** %8
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %2
store i8* null, i8** %3
br label %73
21: ; preds = %2
%22 = load i8*, i8** %8
%23 = call i64 @XTeaGenerateIVec(i8* %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i8* null, i8** %3
br label %73
26: ; preds = %21
%27 = load i8*, i8** %4
%28 = load i8*, i8** %4
%29 = call i64 @strlen(i8* %28)
%30 = load i8*, i8** %8
%31 = getelementptr inbounds i8, i8* %30, i64 8
%32 = load i64, i64* %7
%33 = sub i64 %32, 8
%34 = load i8*, i8** %8
%35 = load i8*, i8** %5
%36 = call i32 @XTeaEncryptCBC(i8* %27, i64 %29, i8* %31, i64 %33, i8* %34, i8* %35)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %26
%40 = load i8*, i8** %8
%41 = call i32 @free(i8* %40)
store i8* null, i8** %3
br label %73
42: ; preds = %26
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 8
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = mul nsw i32 %45, 2
%47 = add nsw i32 %46, 1
%48 = sext i32 %47 to i64
store i64 %48, i64* %10
%49 = load i64, i64* %10
%50 = call i8* @calloc(i64 %49, i32 1)
store i8* %50, i8** %11
%51 = load i8*, i8** %11
%52 = icmp ne i8* %51, null
br i1 %52, label %56, label %53
53: ; preds = %42
%54 = load i8*, i8** %8
%55 = call i32 @free(i8* %54)
store i8* null, i8** %3
br label %73
56: ; preds = %42
%57 = load i8*, i8** %8
%58 = load i32, i32* %9
%59 = load i8*, i8** %11
%60 = load i64, i64* %10
%61 = call i32 @hexEncode(i8* %57, i32 %58, i8* %59, i64 %60)
store i32 %61, i32* %9
%62 = load i32, i32* %9
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %56
%65 = load i8*, i8** %8
%66 = call i32 @free(i8* %65)
%67 = load i8*, i8** %11
%68 = call i32 @free(i8* %67)
store i8* null, i8** %3
br label %73
69: ; preds = %56
%70 = load i8*, i8** %8
%71 = call i32 @free(i8* %70)
%72 = load i8*, i8** %11
store i8* %72, i8** %3
br label %73
73: ; preds = %69, %64, %53, %39, %25, %20
%74 = load i8*, i8** %3
ret i8* %74
}

LLVM IR (-O2):
define i8* @encrypt(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = add i64 %3, 16
%5 = tail call i8* @calloc(i64 %4, i32 1)
%6 = icmp eq i8* %5, null
br i1 %6, label %30, label %7
7: ; preds = %2
%8 = tail call i64 @XTeaGenerateIVec(i8* %5)
%9 = icmp slt i64 %8, 0
br i1 %9, label %30, label %10
10: ; preds = %7
%11 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%12 = getelementptr inbounds i8, i8* %5, i64 8
%13 = add i64 %3, 8
%14 = tail call i32 @XTeaEncryptCBC(i8* %0, i64 %11, i8* %12, i64 %13, i8* %5, i8* %1)
%15 = icmp slt i32 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %10
%17 = add nuw nsw i32 %14, 8
%18 = shl nuw nsw i32 %17, 1
%19 = or i32 %18, 1
%20 = sext i32 %19 to i64
%21 = tail call i8* @calloc(i64 %20, i32 1)
%22 = icmp eq i8* %21, null
br i1 %22, label %27, label %23
23: ; preds = %16
%24 = tail call i32 @hexEncode(i8* %5, i32 %17, i8* %21, i64 %20)
%25 = icmp slt i32 %24, 0
%26 = tail call i32 @free(i8* %5)
br i1 %25, label %27, label %30
27: ; preds = %23, %16, %10
%28 = phi i8* [ %5, %10 ], [ %5, %16 ], [ %21, %23 ]
%29 = tail call i32 @free(i8* %28)
br label %30
30: ; preds = %27, %23, %7, %2
%31 = phi i8* [ null, %2 ], [ null, %7 ], [ %21, %23 ], [ null, %27 ]
ret i8* %31
}