#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 uint32_t;
typedef int /*<<< orphan*/ EVP_CIPHER_CTX;

/* Variables and functions */
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 EVP_EncryptFinal_ex(int /*<<< orphan*/ *, unsigned char *, int *);
int EVP_EncryptInit_ex(int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/ *, unsigned char *,
                       int /*<<< orphan*/ *);
int EVP_EncryptUpdate(int /*<<< orphan*/ *, unsigned char *, int *,
                      unsigned char *, int);
int /*<<< orphan*/ EVP_aes_128_ecb();
unsigned char *encfid_b64(unsigned char *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ handle_crypto_error();
int /*<<< orphan*/ strncpy(unsigned char *, unsigned char *, int);
int encrypt(uint32_t faddr, int padded_faddr_len, unsigned char *key,
            unsigned char *fid_b64) {
  unsigned char ciphertext[16];
  unsigned char *cipher_b64;
  EVP_CIPHER_CTX *ctx;
  int len, ciphertext_len;
  uint32_t padded_faddr[4];
  padded_faddr[0] = faddr;
  padded_faddr[1] = 4096;
  padded_faddr[2] = 2048;
  padded_faddr[3] = 1024;
  if (!(ctx = EVP_CIPHER_CTX_new()))
    handle_crypto_error();
  if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, NULL))
    handle_crypto_error();
  EVP_CIPHER_CTX_set_padding(ctx, 0);
  if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len,
                             (unsigned char *)padded_faddr, padded_faddr_len))
    handle_crypto_error();
  ciphertext_len = len;
  if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
    handle_crypto_error();
  ciphertext_len += len;
  EVP_CIPHER_CTX_free(ctx);
  cipher_b64 = encfid_b64(ciphertext);
  strncpy(fid_b64, cipher_b64, 23);
  free(cipher_b64);
  return ciphertext_len;
}


x86 assembly (clang, -O0):
<encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x60(%rbp)
movl   $0x1000,-0x5c(%rbp)
movl   $0x800,-0x58(%rbp)
movl   $0x400,-0x54(%rbp)
mov    $0x0,%al
call   10d0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11fd <encrypt+0x4d>
mov    $0x0,%al
call   10b0 <handle_crypto_error@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1080 <EVP_aes_128_ecb@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
call   10c0 <EVP_EncryptInit_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1239 <encrypt+0x89>
mov    $0x0,%al
call   10b0 <handle_crypto_error@plt>
mov    -0x40(%rbp),%rdi
xor    %esi,%esi
call   1090 <EVP_CIPHER_CTX_set_padding@plt>
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x60(%rbp),%rcx
mov    -0x8(%rbp),%r8d
lea    -0x44(%rbp),%rdx
call   1070 <EVP_EncryptUpdate@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1273 <encrypt+0xc3>
mov    $0x0,%al
call   10b0 <handle_crypto_error@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
movslq -0x44(%rbp),%rax
add    %rax,%rsi
lea    -0x44(%rbp),%rdx
call   10a0 <EVP_EncryptFinal_ex@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     12a7 <encrypt+0xf7>
mov    $0x0,%al
call   10b0 <handle_crypto_error@plt>
mov    -0x44(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
lea    -0x30(%rbp),%rdi
call   1050 <encfid_b64@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0x17,%edx
call   1040 <strncpy@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rcx,%r14
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %edi,0x10(%rsp)
movabs $0x80000001000,%rax
mov    %rax,0x14(%rsp)
movl   $0x400,0x1c(%rsp)
xor    %eax,%eax
call   10d0 <EVP_CIPHER_CTX_new@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11f6 <encrypt+0x46>
xor    %eax,%eax
call   10b0 <handle_crypto_error@plt>
xor    %eax,%eax
call   1080 <EVP_aes_128_ecb@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %edx,%edx
mov    %rbp,%rcx
xor    %r8d,%r8d
call   10c0 <EVP_EncryptInit_ex@plt>
cmp    $0x1,%eax
je     121b <encrypt+0x6b>
xor    %eax,%eax
call   10b0 <handle_crypto_error@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <EVP_CIPHER_CTX_set_padding@plt>
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %r12,%rdi
mov    %r15d,%r8d
call   1070 <EVP_EncryptUpdate@plt>
cmp    $0x1,%eax
je     124b <encrypt+0x9b>
xor    %eax,%eax
call   10b0 <handle_crypto_error@plt>
movslq 0xc(%rsp),%rbx
lea    (%rsp,%rbx,1),%rsi
add    $0x20,%rsi
lea    0xc(%rsp),%rdx
mov    %r12,%rdi
call   10a0 <EVP_EncryptFinal_ex@plt>
cmp    $0x1,%eax
je     1271 <encrypt+0xc1>
xor    %eax,%eax
call   10b0 <handle_crypto_error@plt>
mov    0xc(%rsp),%ebp
add    %ebx,%ebp
mov    %r12,%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
lea    0x20(%rsp),%rdi
call   1050 <encfid_b64@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x17,%edx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @encrypt(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [16 x i8]
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca [4 x i32]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%15 = load i32, i32* %5
%16 = getelementptr inbounds [4 x i32], [4 x i32]* %14, i64 0, i64 0
store i32 %15, i32* %16
%17 = getelementptr inbounds [4 x i32], [4 x i32]* %14, i64 0, i64 1
store i32 4096, i32* %17
%18 = getelementptr inbounds [4 x i32], [4 x i32]* %14, i64 0, i64 2
store i32 2048, i32* %18
%19 = getelementptr inbounds [4 x i32], [4 x i32]* %14, i64 0, i64 3
store i32 1024, i32* %19
%20 = call i32* (...) @EVP_CIPHER_CTX_new()
store i32* %20, i32** %11
%21 = icmp ne i32* %20, null
br i1 %21, label %24, label %22
22: ; preds = %4
%23 = call i32 (...) @handle_crypto_error()
br label %24
24: ; preds = %22, %4
%25 = load i32*, i32** %11
%26 = call i32 (...) @EVP_aes_128_ecb()
%27 = load i8*, i8** %7
%28 = call i32 @EVP_EncryptInit_ex(i32* %25, i32 %26, i32* null, i8* %27, i32* null)
%29 = icmp ne i32 1, %28
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 (...) @handle_crypto_error()
br label %32
32: ; preds = %30, %24
%33 = load i32*, i32** %11
%34 = call i32 @EVP_CIPHER_CTX_set_padding(i32* %33, i32 0)
%35 = load i32*, i32** %11
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%37 = getelementptr inbounds [4 x i32], [4 x i32]* %14, i64 0, i64 0
%38 = bitcast i32* %37 to i8*
%39 = load i32, i32* %6
%40 = call i32 @EVP_EncryptUpdate(i32* %35, i8* %36, i32* %12, i8* %38, i32 %39)
%41 = icmp ne i32 1, %40
br i1 %41, label %42, label %44
42: ; preds = %32
%43 = call i32 (...) @handle_crypto_error()
br label %44
44: ; preds = %42, %32
%45 = load i32, i32* %12
store i32 %45, i32* %13
%46 = load i32*, i32** %11
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%48 = load i32, i32* %12
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = call i32 @EVP_EncryptFinal_ex(i32* %46, i8* %50, i32* %12)
%52 = icmp ne i32 1, %51
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = call i32 (...) @handle_crypto_error()
br label %55
55: ; preds = %53, %44
%56 = load i32, i32* %12
%57 = load i32, i32* %13
%58 = add nsw i32 %57, %56
store i32 %58, i32* %13
%59 = load i32*, i32** %11
%60 = call i32 @EVP_CIPHER_CTX_free(i32* %59)
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%62 = call i8* @encfid_b64(i8* %61)
store i8* %62, i8** %10
%63 = load i8*, i8** %8
%64 = load i8*, i8** %10
%65 = call i32 @strncpy(i8* %63, i8* %64, i32 23)
%66 = load i8*, i8** %10
%67 = call i32 @free(i8* %66)
%68 = load i32, i32* %13
ret i32 %68
}

LLVM IR (-O2):
define i32 @encrypt(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca [16 x i8]
%6 = alloca i32
%7 = alloca [4 x i32]
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%9 = bitcast i32* %6 to i8*
%10 = bitcast [4 x i32]* %7 to i8*
%11 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 0
store i32 %0, i32* %11
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 1
store i32 4096, i32* %12
%13 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 2
store i32 2048, i32* %13
%14 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 3
store i32 1024, i32* %14
%15 = tail call i32* (...) @EVP_CIPHER_CTX_new()
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = tail call i32 (...) @handle_crypto_error()
br label %19
19: ; preds = %17, %4
%20 = tail call i32 (...) @EVP_aes_128_ecb()
%21 = tail call i32 @EVP_EncryptInit_ex(i32* %15, i32 %20, i32* null, i8* %2, i32* null)
%22 = icmp eq i32 %21, 1
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 (...) @handle_crypto_error()
br label %25
25: ; preds = %23, %19
%26 = tail call i32 @EVP_CIPHER_CTX_set_padding(i32* %15, i32 0)
%27 = call i32 @EVP_EncryptUpdate(i32* %15, i8* %8, i32* %6, i8* %10, i32 %1)
%28 = icmp eq i32 %27, 1
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 (...) @handle_crypto_error()
br label %31
31: ; preds = %29, %25
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 %33
%35 = call i32 @EVP_EncryptFinal_ex(i32* %15, i8* %34, i32* %6)
%36 = icmp eq i32 %35, 1
br i1 %36, label %39, label %37
37: ; preds = %31
%38 = call i32 (...) @handle_crypto_error()
br label %39
39: ; preds = %37, %31
%40 = load i32, i32* %6
%41 = add nsw i32 %40, %32
%42 = call i32 @EVP_CIPHER_CTX_free(i32* %15)
%43 = call i8* @encfid_b64(i8* %8)
%44 = call i32 @strncpy(i8* %3, i8* %43, i32 23)
%45 = call i32 @free(i8* %43)
ret i32 %41
}