#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 uint8_t;
typedef int /*<<< orphan*/ uint64_t;

/* Variables and functions */
scalar_t__ endianness();
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ mrtd_crypto_encrypt_3des(int *, int *, int, int const *);
int /*<<< orphan*/ mrtd_crypto_mac_padding(int *, int *, int, int const *);
int /*<<< orphan*/ mrtd_crypto_padding(int const *, int *, int, int *);
void mrtd_bac_protected_apdu(const uint8_t *input, uint8_t *output,
                             const int length, int *outputlength,
                             const uint8_t *ksenc, const uint8_t *ksmac,
                             const uint64_t ssc_long) {
  int datalength;
  char has_le;
  uint8_t *do87 = NULL;
  uint8_t do8e[10];
  uint8_t padded_command[8];
  uint8_t *A;
  int padded_data_length;
  int do87_length;
  uint8_t *do97 = NULL;
  int do97_length;
  uint8_t le;
  if (length > 5) {
    datalength = (int)input[4];
  } else {
    datalength = 0;
  }
  if (datalength != 0 ? length > (5 + datalength) : length == 5) {
    le = input[length - 1];
    has_le = 1;
  } else {
    le = 0;
    has_le = 0;
  }
  int i;
  if (datalength != 0) {
    uint8_t *padded_data;
    padded_data = malloc(((datalength + 8) / 8) * 8);
    mrtd_crypto_padding(input + 5, padded_data, datalength,
                        &padded_data_length);
    do87 = malloc(padded_data_length + 3);
    mrtd_crypto_encrypt_3des(padded_data, do87 + 3, padded_data_length, ksenc);
    do87[0] = 0x87;
    do87[1] = 0x09;
    do87[2] = 0x01;
    do87_length = padded_data_length + 3;
    free(padded_data);
  } else {
    do87_length = 0;
    padded_data_length = 0;
  }
  if (has_le) {
    do97_length = 3;
    do97 = malloc(do97_length);
    do97[0] = 0x97;
    do97[1] = 0x01;
    do97[2] = le;
  } else {
    do97_length = 0;
  }
  int padded_command_length;
  mrtd_crypto_padding(input, padded_command, 4, &padded_command_length);
  padded_command[0] = 0x0c;
  A = malloc(16 + do87_length + do97_length);
  if (endianness()) {
    A[0] = *(((uint8_t *)(&ssc_long)) + 0);
    A[1] = *(((uint8_t *)(&ssc_long)) + 1);
    A[2] = *(((uint8_t *)(&ssc_long)) + 2);
    A[3] = *(((uint8_t *)(&ssc_long)) + 3);
    A[4] = *(((uint8_t *)(&ssc_long)) + 4);
    A[5] = *(((uint8_t *)(&ssc_long)) + 5);
    A[6] = *(((uint8_t *)(&ssc_long)) + 6);
    A[7] = *(((uint8_t *)(&ssc_long)) + 7);
  } else {
    A[0] = *(((uint8_t *)(&ssc_long)) + 7);
    A[1] = *(((uint8_t *)(&ssc_long)) + 6);
    A[2] = *(((uint8_t *)(&ssc_long)) + 5);
    A[3] = *(((uint8_t *)(&ssc_long)) + 4);
    A[4] = *(((uint8_t *)(&ssc_long)) + 3);
    A[5] = *(((uint8_t *)(&ssc_long)) + 2);
    A[6] = *(((uint8_t *)(&ssc_long)) + 1);
    A[7] = *(((uint8_t *)(&ssc_long)) + 0);
  }
  memcpy(A + 8, padded_command, 8);
  if (do87 != NULL)
    memcpy(A + 16, do87, do87_length);
  if (do97 != NULL)
    memcpy(A + 16 + do87_length, do97, do97_length);
  do8e[0] = 0x8e;
  do8e[1] = 0x08;
  mrtd_crypto_mac_padding(A, do8e + 2, 16 + do87_length + do97_length, ksmac);
  (*outputlength) = 5 + do87_length + do97_length + 10 + 1;
  memcpy(output, padded_command, 4);
  output[4] = (*outputlength) - 6;
  if (do87 != NULL)
    memcpy(output + 5, do87, do87_length);
  if (do97 != NULL)
    memcpy(output + 5 + do87_length, do97, do97_length);
  memcpy(output + 5 + do87_length + do97_length, do8e, 10);
  output[(*outputlength) - 1] = 0x00;
  free(A);
  if (do87 != NULL)
    free(do87);
  if (do97 != NULL)
    free(do97);
  return;
}


x86 assembly (clang, -O0):
<mrtd_bac_protected_apdu>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0xa8(%rbp)
cmpl   $0x5,-0x14(%rbp)
jle    11c1 <mrtd_bac_protected_apdu+0x51>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    11c8 <mrtd_bac_protected_apdu+0x58>
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11e8 <mrtd_bac_protected_apdu+0x78>
mov    -0x14(%rbp),%eax
mov    -0x34(%rbp),%ecx
add    $0x5,%ecx
cmp    %ecx,%eax
jg     11f2 <mrtd_bac_protected_apdu+0x82>
jmp    1211 <mrtd_bac_protected_apdu+0xa1>
cmpl   $0x5,-0x14(%rbp)
jne    1211 <mrtd_bac_protected_apdu+0xa1>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0xb0(%rbp)
movb   $0x1,-0x35(%rbp)
jmp    121f <mrtd_bac_protected_apdu+0xaf>
movl   $0x0,-0xb0(%rbp)
movb   $0x0,-0x35(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12d6 <mrtd_bac_protected_apdu+0x166>
mov    -0x34(%rbp),%eax
add    $0x8,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
shl    $0x3,%edi
call   1060 <malloc@plt>
mov    %rax,-0xc0(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x14,%rdi
mov    -0xc0(%rbp),%rsi
mov    -0x34(%rbp),%edx
lea    -0x9c(%rbp),%rcx
call   1090 <mrtd_crypto_padding@plt>
mov    -0x9c(%rbp),%edi
add    $0x3,%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0xc0(%rbp),%rdi
mov    -0x40(%rbp),%rsi
add    $0xc,%rsi
mov    -0x9c(%rbp),%edx
mov    -0x28(%rbp),%rcx
call   1080 <mrtd_crypto_encrypt_3des@plt>
mov    -0x40(%rbp),%rax
movl   $0x87,(%rax)
mov    -0x40(%rbp),%rax
movl   $0x9,0x4(%rax)
mov    -0x40(%rbp),%rax
movl   $0x1,0x8(%rax)
mov    -0x9c(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0xa0(%rbp)
mov    -0xc0(%rbp),%rdi
call   1030 <free@plt>
jmp    12ea <mrtd_bac_protected_apdu+0x17a>
movl   $0x0,-0xa0(%rbp)
movl   $0x0,-0x9c(%rbp)
cmpb   $0x0,-0x35(%rbp)
je     1340 <mrtd_bac_protected_apdu+0x1d0>
movl   $0x3,-0xac(%rbp)
mov    -0xac(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0xa8(%rbp)
mov    -0xa8(%rbp),%rax
movl   $0x97,(%rax)
mov    -0xa8(%rbp),%rax
movl   $0x1,0x4(%rax)
mov    -0xb0(%rbp),%ecx
mov    -0xa8(%rbp),%rax
mov    %ecx,0x8(%rax)
jmp    134a <mrtd_bac_protected_apdu+0x1da>
movl   $0x0,-0xac(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x4,%edx
lea    -0xc4(%rbp),%rcx
call   1090 <mrtd_crypto_padding@plt>
movl   $0xc,-0x90(%rbp)
mov    -0xa0(%rbp),%edi
add    $0x10,%edi
add    -0xac(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x98(%rbp)
mov    $0x0,%al
call   1050 <endianness@plt>
cmp    $0x0,%rax
je     1408 <mrtd_bac_protected_apdu+0x298>
mov    0x10(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,(%rax)
mov    0x14(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x18(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x1c(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    0x20(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    0x24(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x14(%rax)
mov    0x28(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    0x2c(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x1c(%rax)
jmp    146f <mrtd_bac_protected_apdu+0x2ff>
mov    0x2c(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,(%rax)
mov    0x28(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x24(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x20(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    0x1c(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    0x18(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x14(%rax)
mov    0x14(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    0x10(%rbp),%ecx
mov    -0x98(%rbp),%rax
mov    %ecx,0x1c(%rax)
mov    -0x98(%rbp),%rdi
add    $0x20,%rdi
lea    -0x90(%rbp),%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
cmpq   $0x0,-0x40(%rbp)
je     14b0 <mrtd_bac_protected_apdu+0x340>
mov    -0x98(%rbp),%rdi
add    $0x40,%rdi
mov    -0x40(%rbp),%rsi
mov    -0xa0(%rbp),%edx
call   1040 <memcpy@plt>
cmpq   $0x0,-0xa8(%rbp)
je     14e9 <mrtd_bac_protected_apdu+0x379>
mov    -0x98(%rbp),%rdi
add    $0x40,%rdi
movslq -0xa0(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0xa8(%rbp),%rsi
mov    -0xac(%rbp),%edx
call   1040 <memcpy@plt>
movl   $0x8e,-0x70(%rbp)
movl   $0x8,-0x6c(%rbp)
mov    -0x98(%rbp),%rdi
lea    -0x70(%rbp),%rsi
add    $0x8,%rsi
mov    -0xa0(%rbp),%edx
add    $0x10,%edx
add    -0xac(%rbp),%edx
mov    -0x30(%rbp),%rcx
call   1070 <mrtd_crypto_mac_padding@plt>
mov    -0xa0(%rbp),%ecx
add    $0x5,%ecx
add    -0xac(%rbp),%ecx
add    $0xa,%ecx
add    $0x1,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
sub    $0x6,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x10(%rax)
cmpq   $0x0,-0x40(%rbp)
je     1580 <mrtd_bac_protected_apdu+0x410>
mov    -0x10(%rbp),%rdi
add    $0x14,%rdi
mov    -0x40(%rbp),%rsi
mov    -0xa0(%rbp),%edx
call   1040 <memcpy@plt>
cmpq   $0x0,-0xa8(%rbp)
je     15b6 <mrtd_bac_protected_apdu+0x446>
mov    -0x10(%rbp),%rdi
add    $0x14,%rdi
movslq -0xa0(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0xa8(%rbp),%rsi
mov    -0xac(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
add    $0x14,%rdi
movslq -0xa0(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
movslq -0xac(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
lea    -0x70(%rbp),%rsi
mov    $0xa,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x98(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x40(%rbp)
je     161f <mrtd_bac_protected_apdu+0x4af>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0xa8(%rbp)
je     1639 <mrtd_bac_protected_apdu+0x4c9>
mov    -0xa8(%rbp),%rdi
call   1030 <free@plt>
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mrtd_bac_protected_apdu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %edx,%ebx
mov    %rdi,%r13
cmp    $0x6,%edx
mov    %rcx,0x40(%rsp)
mov    %rsi,0x38(%rsp)
mov    %r9,0x30(%rsp)
jl     1258 <mrtd_bac_protected_apdu+0xe8>
mov    0x10(%r13),%ebp
test   %ebp,%ebp
je     1298 <mrtd_bac_protected_apdu+0x128>
mov    %r8,%r14
lea    0x5(%rbp),%ecx
mov    $0x0,%eax
mov    %ecx,0x10(%rsp)
cmp    %ebx,%ecx
jge    11c5 <mrtd_bac_protected_apdu+0x55>
lea    -0x1(%rbx),%eax
mov    0x0(%r13,%rax,4),%eax
mov    %eax,0x20(%rsp)
lea    0x8(%rbp),%eax
lea    0xf(%rbp),%edi
test   %eax,%eax
cmovns %eax,%edi
and    $0xfffffff8,%edi
call   1060 <malloc@plt>
mov    %r13,0x18(%rsp)
mov    %rax,%r13
mov    0x18(%rsp),%rax
lea    0x14(%rax),%rdi
lea    0xc(%rsp),%rcx
mov    %r13,%rsi
mov    %ebp,%edx
call   1090 <mrtd_crypto_padding@plt>
mov    0xc(%rsp),%ebp
lea    0x3(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%r12
lea    0xc(%rax),%rsi
mov    %r13,%rdi
mov    %ebp,%edx
mov    %r14,%rcx
call   1080 <mrtd_crypto_encrypt_3des@plt>
movabs $0x900000087,%rax
mov    %rax,(%r12)
mov    %r12,%r15
movl   $0x1,0x8(%r12)
mov    0xc(%rsp),%r14d
add    $0x3,%r14d
mov    %r13,%rdi
mov    0x18(%rsp),%r13
call   1030 <free@plt>
cmp    %ebx,0x10(%rsp)
jge    12b1 <mrtd_bac_protected_apdu+0x141>
mov    0x20(%rsp),%ebp
jmp    126f <mrtd_bac_protected_apdu+0xff>
cmp    $0x5,%ebx
jne    1298 <mrtd_bac_protected_apdu+0x128>
mov    0x10(%r13),%ebp
movl   $0x0,0xc(%rsp)
xor    %r14d,%r14d
xor    %r15d,%r15d
mov    $0x3,%ebx
mov    $0x3,%edi
call   1060 <malloc@plt>
movabs $0x100000097,%rcx
mov    %rcx,(%rax)
mov    %rax,%rcx
mov    %rax,0x10(%rsp)
mov    %ebp,0x8(%rax)
jmp    12ba <mrtd_bac_protected_apdu+0x14a>
movl   $0x0,0xc(%rsp)
xor    %r14d,%r14d
xor    %r12d,%r12d
xor    %eax,%eax
mov    %rax,0x10(%rsp)
xor    %ebx,%ebx
jmp    12bd <mrtd_bac_protected_apdu+0x14d>
xor    %eax,%eax
mov    %rax,0x10(%rsp)
xor    %ebx,%ebx
mov    %r15,%r12
lea    0x50(%rsp),%r15
lea    0x4c(%rsp),%rcx
mov    %r13,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1090 <mrtd_crypto_padding@plt>
movl   $0xc,0x50(%rsp)
mov    %rbx,0x18(%rsp)
lea    (%rbx,%r14,1),%ebp
add    $0x10,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %r14,%r13
mov    %rax,%r14
xor    %eax,%eax
call   1050 <endianness@plt>
lea    0x1c(%r14),%rcx
test   %rax,%rax
cmovne %r14,%rcx
mov    0xd0(%rsp),%eax
mov    %eax,(%rcx)
lea    0x20(%r14),%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
test   %r12,%r12
mov    %r12,%rbx
je     133b <mrtd_bac_protected_apdu+0x1cb>
lea    0x40(%r14),%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1040 <memcpy@plt>
mov    %r13,0x28(%rsp)
movslq %r13d,%r13
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
je     135f <mrtd_bac_protected_apdu+0x1ef>
lea    (%r14,%r13,4),%rdi
add    $0x40,%rdi
mov    0x18(%rsp),%rdx
call   1040 <memcpy@plt>
movabs $0x80000008e,%rax
mov    %rax,0x70(%rsp)
lea    0x78(%rsp),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
mov    0x30(%rsp),%rcx
call   1070 <mrtd_crypto_mac_padding@plt>
mov    0x40(%rsp),%r12
mov    %ebp,(%r12)
lea    0x50(%rsp),%rsi
mov    0x38(%rsp),%r15
mov    %r15,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    (%r12),%eax
add    $0xfffffffa,%eax
mov    %eax,0x10(%r15)
test   %rbx,%rbx
je     13c3 <mrtd_bac_protected_apdu+0x253>
lea    0x14(%r15),%rdi
mov    %rbx,%rsi
mov    0x28(%rsp),%rdx
call   1040 <memcpy@plt>
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
mov    %rbx,0x20(%rsp)
je     13e8 <mrtd_bac_protected_apdu+0x278>
lea    (%r15,%r13,4),%rdi
add    $0x14,%rdi
mov    0x18(%rsp),%rbx
mov    %ebx,%edx
call   1040 <memcpy@plt>
jmp    13ed <mrtd_bac_protected_apdu+0x27d>
mov    0x18(%rsp),%rbx
lea    (%r15,%r13,4),%rax
mov    %ebx,%ecx
lea    (%rax,%rcx,4),%rdi
add    $0x14,%rdi
lea    0x70(%rsp),%rsi
mov    $0xa,%edx
call   1040 <memcpy@plt>
movslq (%r12),%rax
movl   $0x0,-0x4(%r15,%rax,4)
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x20(%rsp),%rdi
test   %rdi,%rdi
je     142e <mrtd_bac_protected_apdu+0x2be>
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
test   %rdi,%rdi
je     143d <mrtd_bac_protected_apdu+0x2cd>
call   1030 <free@plt>
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @mrtd_bac_protected_apdu(i32* %0, i32* %1, i32 %2, i32* %3, i32* %4, i32* %5, i32 %6) {
%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 i8
%17 = alloca i32*
%18 = alloca [10 x i32]
%19 = alloca [8 x i32]
%20 = alloca i32*
%21 = alloca i32
%22 = alloca i32
%23 = alloca i32*
%24 = alloca i32
%25 = alloca i32
%26 = alloca i32
%27 = alloca i32*
%28 = alloca i32
store i32* %0, i32** %8
store i32* %1, i32** %9
store i32 %2, i32* %10
store i32* %3, i32** %11
store i32* %4, i32** %12
store i32* %5, i32** %13
store i32 %6, i32* %14
store i32* null, i32** %17
store i32* null, i32** %23
%29 = load i32, i32* %10
%30 = icmp sgt i32 %29, 5
br i1 %30, label %31, label %35
31: ; preds = %7
%32 = load i32*, i32** %8
%33 = getelementptr inbounds i32, i32* %32, i64 4
%34 = load i32, i32* %33
store i32 %34, i32* %15
br label %36
35: ; preds = %7
store i32 0, i32* %15
br label %36
36: ; preds = %35, %31
%37 = load i32, i32* %15
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32, i32* %10
%41 = load i32, i32* %15
%42 = add nsw i32 5, %41
%43 = icmp sgt i32 %40, %42
br i1 %43, label %47, label %54
44: ; preds = %36
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, 5
br i1 %46, label %47, label %54
47: ; preds = %44, %39
%48 = load i32*, i32** %8
%49 = load i32, i32* %10
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %48, i64 %51
%53 = load i32, i32* %52
store i32 %53, i32* %25
store i8 1, i8* %16
br label %55
54: ; preds = %44, %39
store i32 0, i32* %25
store i8 0, i8* %16
br label %55
55: ; preds = %54, %47
%56 = load i32, i32* %15
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %88
58: ; preds = %55
%59 = load i32, i32* %15
%60 = add nsw i32 %59, 8
%61 = sdiv i32 %60, 8
%62 = mul nsw i32 %61, 8
%63 = call i32* @malloc(i32 %62)
store i32* %63, i32** %27
%64 = load i32*, i32** %8
%65 = getelementptr inbounds i32, i32* %64, i64 5
%66 = load i32*, i32** %27
%67 = load i32, i32* %15
%68 = call i32 @mrtd_crypto_padding(i32* %65, i32* %66, i32 %67, i32* %21)
%69 = load i32, i32* %21
%70 = add nsw i32 %69, 3
%71 = call i32* @malloc(i32 %70)
store i32* %71, i32** %17
%72 = load i32*, i32** %27
%73 = load i32*, i32** %17
%74 = getelementptr inbounds i32, i32* %73, i64 3
%75 = load i32, i32* %21
%76 = load i32*, i32** %12
%77 = call i32 @mrtd_crypto_encrypt_3des(i32* %72, i32* %74, i32 %75, i32* %76)
%78 = load i32*, i32** %17
%79 = getelementptr inbounds i32, i32* %78, i64 0
store i32 135, i32* %79
%80 = load i32*, i32** %17
%81 = getelementptr inbounds i32, i32* %80, i64 1
store i32 9, i32* %81
%82 = load i32*, i32** %17
%83 = getelementptr inbounds i32, i32* %82, i64 2
store i32 1, i32* %83
%84 = load i32, i32* %21
%85 = add nsw i32 %84, 3
store i32 %85, i32* %22
%86 = load i32*, i32** %27
%87 = call i32 @free(i32* %86)
br label %89
88: ; preds = %55
store i32 0, i32* %22
store i32 0, i32* %21
br label %89
89: ; preds = %88, %58
%90 = load i8, i8* %16
%91 = icmp ne i8 %90, 0
br i1 %91, label %92, label %102
92: ; preds = %89
store i32 3, i32* %24
%93 = load i32, i32* %24
%94 = call i32* @malloc(i32 %93)
store i32* %94, i32** %23
%95 = load i32*, i32** %23
%96 = getelementptr inbounds i32, i32* %95, i64 0
store i32 151, i32* %96
%97 = load i32*, i32** %23
%98 = getelementptr inbounds i32, i32* %97, i64 1
store i32 1, i32* %98
%99 = load i32, i32* %25
%100 = load i32*, i32** %23
%101 = getelementptr inbounds i32, i32* %100, i64 2
store i32 %99, i32* %101
br label %103
102: ; preds = %89
store i32 0, i32* %24
br label %103
103: ; preds = %102, %92
%104 = load i32*, i32** %8
%105 = getelementptr inbounds [8 x i32], [8 x i32]* %19, i64 0, i64 0
%106 = call i32 @mrtd_crypto_padding(i32* %104, i32* %105, i32 4, i32* %28)
%107 = getelementptr inbounds [8 x i32], [8 x i32]* %19, i64 0, i64 0
store i32 12, i32* %107
%108 = load i32, i32* %22
%109 = add nsw i32 16, %108
%110 = load i32, i32* %24
%111 = add nsw i32 %109, %110
%112 = call i32* @malloc(i32 %111)
store i32* %112, i32** %20
%113 = call i64 (...) @endianness()
%114 = icmp ne i64 %113, 0
br i1 %114, label %115, label %148
115: ; preds = %103
%116 = getelementptr inbounds i32, i32* %14, i64 0
%117 = load i32, i32* %116
%118 = load i32*, i32** %20
%119 = getelementptr inbounds i32, i32* %118, i64 0
store i32 %117, i32* %119
%120 = getelementptr inbounds i32, i32* %14, i64 1
%121 = load i32, i32* %120
%122 = load i32*, i32** %20
%123 = getelementptr inbounds i32, i32* %122, i64 1
store i32 %121, i32* %123
%124 = getelementptr inbounds i32, i32* %14, i64 2
%125 = load i32, i32* %124
%126 = load i32*, i32** %20
%127 = getelementptr inbounds i32, i32* %126, i64 2
store i32 %125, i32* %127
%128 = getelementptr inbounds i32, i32* %14, i64 3
%129 = load i32, i32* %128
%130 = load i32*, i32** %20
%131 = getelementptr inbounds i32, i32* %130, i64 3
store i32 %129, i32* %131
%132 = getelementptr inbounds i32, i32* %14, i64 4
%133 = load i32, i32* %132
%134 = load i32*, i32** %20
%135 = getelementptr inbounds i32, i32* %134, i64 4
store i32 %133, i32* %135
%136 = getelementptr inbounds i32, i32* %14, i64 5
%137 = load i32, i32* %136
%138 = load i32*, i32** %20
%139 = getelementptr inbounds i32, i32* %138, i64 5
store i32 %137, i32* %139
%140 = getelementptr inbounds i32, i32* %14, i64 6
%141 = load i32, i32* %140
%142 = load i32*, i32** %20
%143 = getelementptr inbounds i32, i32* %142, i64 6
store i32 %141, i32* %143
%144 = getelementptr inbounds i32, i32* %14, i64 7
%145 = load i32, i32* %144
%146 = load i32*, i32** %20
%147 = getelementptr inbounds i32, i32* %146, i64 7
store i32 %145, i32* %147
br label %181
148: ; preds = %103
%149 = getelementptr inbounds i32, i32* %14, i64 7
%150 = load i32, i32* %149
%151 = load i32*, i32** %20
%152 = getelementptr inbounds i32, i32* %151, i64 0
store i32 %150, i32* %152
%153 = getelementptr inbounds i32, i32* %14, i64 6
%154 = load i32, i32* %153
%155 = load i32*, i32** %20
%156 = getelementptr inbounds i32, i32* %155, i64 1
store i32 %154, i32* %156
%157 = getelementptr inbounds i32, i32* %14, i64 5
%158 = load i32, i32* %157
%159 = load i32*, i32** %20
%160 = getelementptr inbounds i32, i32* %159, i64 2
store i32 %158, i32* %160
%161 = getelementptr inbounds i32, i32* %14, i64 4
%162 = load i32, i32* %161
%163 = load i32*, i32** %20
%164 = getelementptr inbounds i32, i32* %163, i64 3
store i32 %162, i32* %164
%165 = getelementptr inbounds i32, i32* %14, i64 3
%166 = load i32, i32* %165
%167 = load i32*, i32** %20
%168 = getelementptr inbounds i32, i32* %167, i64 4
store i32 %166, i32* %168
%169 = getelementptr inbounds i32, i32* %14, i64 2
%170 = load i32, i32* %169
%171 = load i32*, i32** %20
%172 = getelementptr inbounds i32, i32* %171, i64 5
store i32 %170, i32* %172
%173 = getelementptr inbounds i32, i32* %14, i64 1
%174 = load i32, i32* %173
%175 = load i32*, i32** %20
%176 = getelementptr inbounds i32, i32* %175, i64 6
store i32 %174, i32* %176
%177 = getelementptr inbounds i32, i32* %14, i64 0
%178 = load i32, i32* %177
%179 = load i32*, i32** %20
%180 = getelementptr inbounds i32, i32* %179, i64 7
store i32 %178, i32* %180
br label %181
181: ; preds = %148, %115
%182 = load i32*, i32** %20
%183 = getelementptr inbounds i32, i32* %182, i64 8
%184 = getelementptr inbounds [8 x i32], [8 x i32]* %19, i64 0, i64 0
%185 = call i32 @memcpy(i32* %183, i32* %184, i32 8)
%186 = load i32*, i32** %17
%187 = icmp ne i32* %186, null
br i1 %187, label %188, label %194
188: ; preds = %181
%189 = load i32*, i32** %20
%190 = getelementptr inbounds i32, i32* %189, i64 16
%191 = load i32*, i32** %17
%192 = load i32, i32* %22
%193 = call i32 @memcpy(i32* %190, i32* %191, i32 %192)
br label %194
194: ; preds = %188, %181
%195 = load i32*, i32** %23
%196 = icmp ne i32* %195, null
br i1 %196, label %197, label %206
197: ; preds = %194
%198 = load i32*, i32** %20
%199 = getelementptr inbounds i32, i32* %198, i64 16
%200 = load i32, i32* %22
%201 = sext i32 %200 to i64
%202 = getelementptr inbounds i32, i32* %199, i64 %201
%203 = load i32*, i32** %23
%204 = load i32, i32* %24
%205 = call i32 @memcpy(i32* %202, i32* %203, i32 %204)
br label %206
206: ; preds = %197, %194
%207 = getelementptr inbounds [10 x i32], [10 x i32]* %18, i64 0, i64 0
store i32 142, i32* %207
%208 = getelementptr inbounds [10 x i32], [10 x i32]* %18, i64 0, i64 1
store i32 8, i32* %208
%209 = load i32*, i32** %20
%210 = getelementptr inbounds [10 x i32], [10 x i32]* %18, i64 0, i64 0
%211 = getelementptr inbounds i32, i32* %210, i64 2
%212 = load i32, i32* %22
%213 = add nsw i32 16, %212
%214 = load i32, i32* %24
%215 = add nsw i32 %213, %214
%216 = load i32*, i32** %13
%217 = call i32 @mrtd_crypto_mac_padding(i32* %209, i32* %211, i32 %215, i32* %216)
%218 = load i32, i32* %22
%219 = add nsw i32 5, %218
%220 = load i32, i32* %24
%221 = add nsw i32 %219, %220
%222 = add nsw i32 %221, 10
%223 = add nsw i32 %222, 1
%224 = load i32*, i32** %11
store i32 %223, i32* %224
%225 = load i32*, i32** %9
%226 = getelementptr inbounds [8 x i32], [8 x i32]* %19, i64 0, i64 0
%227 = call i32 @memcpy(i32* %225, i32* %226, i32 4)
%228 = load i32*, i32** %11
%229 = load i32, i32* %228
%230 = sub nsw i32 %229, 6
%231 = load i32*, i32** %9
%232 = getelementptr inbounds i32, i32* %231, i64 4
store i32 %230, i32* %232
%233 = load i32*, i32** %17
%234 = icmp ne i32* %233, null
br i1 %234, label %235, label %241
235: ; preds = %206
%236 = load i32*, i32** %9
%237 = getelementptr inbounds i32, i32* %236, i64 5
%238 = load i32*, i32** %17
%239 = load i32, i32* %22
%240 = call i32 @memcpy(i32* %237, i32* %238, i32 %239)
br label %241
241: ; preds = %235, %206
%242 = load i32*, i32** %23
%243 = icmp ne i32* %242, null
br i1 %243, label %244, label %253
244: ; preds = %241
%245 = load i32*, i32** %9
%246 = getelementptr inbounds i32, i32* %245, i64 5
%247 = load i32, i32* %22
%248 = sext i32 %247 to i64
%249 = getelementptr inbounds i32, i32* %246, i64 %248
%250 = load i32*, i32** %23
%251 = load i32, i32* %24
%252 = call i32 @memcpy(i32* %249, i32* %250, i32 %251)
br label %253
253: ; preds = %244, %241
%254 = load i32*, i32** %9
%255 = getelementptr inbounds i32, i32* %254, i64 5
%256 = load i32, i32* %22
%257 = sext i32 %256 to i64
%258 = getelementptr inbounds i32, i32* %255, i64 %257
%259 = load i32, i32* %24
%260 = sext i32 %259 to i64
%261 = getelementptr inbounds i32, i32* %258, i64 %260
%262 = getelementptr inbounds [10 x i32], [10 x i32]* %18, i64 0, i64 0
%263 = call i32 @memcpy(i32* %261, i32* %262, i32 10)
%264 = load i32*, i32** %9
%265 = load i32*, i32** %11
%266 = load i32, i32* %265
%267 = sub nsw i32 %266, 1
%268 = sext i32 %267 to i64
%269 = getelementptr inbounds i32, i32* %264, i64 %268
store i32 0, i32* %269
%270 = load i32*, i32** %20
%271 = call i32 @free(i32* %270)
%272 = load i32*, i32** %17
%273 = icmp ne i32* %272, null
br i1 %273, label %274, label %277
274: ; preds = %253
%275 = load i32*, i32** %17
%276 = call i32 @free(i32* %275)
br label %277
277: ; preds = %274, %253
%278 = load i32*, i32** %23
%279 = icmp ne i32* %278, null
br i1 %279, label %280, label %283
280: ; preds = %277
%281 = load i32*, i32** %23
%282 = call i32 @free(i32* %281)
br label %283
283: ; preds = %280, %277
ret void
}

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