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

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ mrtd_crypto_decrypt_3des(int /*<<< orphan*/ const *,
                                            int /*<<< orphan*/ *, int,
                                            int /*<<< orphan*/ *);
int /*<<< orphan*/ mrtd_crypto_padding_remove(int /*<<< orphan*/ *,
                                              int /*<<< orphan*/ *, int, int *);
void mrtd_bac_decrypt_response(const uint8_t *input, uint8_t *output,
                               const int length, int *outputlength,
                               uint8_t *ksenc) {
  uint8_t *tmp;
  int tmplength;
  const uint8_t *tobedecrypted = input + 3;
  tmplength = length - 16 - 3;
  if (tmplength > 0) {
    tmp = malloc(tmplength);
    mrtd_crypto_decrypt_3des(tobedecrypted, tmp, tmplength, ksenc);
    mrtd_crypto_padding_remove(tmp, output, tmplength, outputlength);
    free(tmp);
  } else {
    (*outputlength) = 0;
  }
}


x86 assembly (clang, -O0):
<mrtd_bac_decrypt_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x10,%eax
sub    $0x3,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    11bf <mrtd_bac_decrypt_response+0x7f>
mov    -0x34(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%rcx
call   1050 <mrtd_crypto_decrypt_3des@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1060 <mrtd_crypto_padding_remove@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    11c9 <mrtd_bac_decrypt_response+0x89>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mrtd_bac_decrypt_response>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
cmp    $0x14,%edx
jl     11a5 <mrtd_bac_decrypt_response+0x65>
mov    %r8,%r15
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
add    $0xffffffed,%ebp
add    $0xc,%rbx
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
mov    %r15,%rcx
call   1050 <mrtd_crypto_decrypt_3des@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %r12,%rcx
call   1060 <mrtd_crypto_padding_remove@plt>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
movl   $0x0,(%r12)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @mrtd_bac_decrypt_response(i32* %0, i32* %1, i32 %2, i32* %3, i32* %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32*
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%14 = load i32*, i32** %6
%15 = getelementptr inbounds i32, i32* %14, i64 3
store i32* %15, i32** %13
%16 = load i32, i32* %8
%17 = sub nsw i32 %16, 16
%18 = sub nsw i32 %17, 3
store i32 %18, i32* %12
%19 = load i32, i32* %12
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %36
21: ; preds = %5
%22 = load i32, i32* %12
%23 = call i32* @malloc(i32 %22)
store i32* %23, i32** %11
%24 = load i32*, i32** %13
%25 = load i32*, i32** %11
%26 = load i32, i32* %12
%27 = load i32*, i32** %10
%28 = call i32 @mrtd_crypto_decrypt_3des(i32* %24, i32* %25, i32 %26, i32* %27)
%29 = load i32*, i32** %11
%30 = load i32*, i32** %7
%31 = load i32, i32* %12
%32 = load i32*, i32** %9
%33 = call i32 @mrtd_crypto_padding_remove(i32* %29, i32* %30, i32 %31, i32* %32)
%34 = load i32*, i32** %11
%35 = call i32 @free(i32* %34)
br label %38
36: ; preds = %5
%37 = load i32*, i32** %9
store i32 0, i32* %37
br label %38
38: ; preds = %36, %21
ret void
}

LLVM IR (-O2):
define void @mrtd_bac_decrypt_response(i32* %0, i32* %1, i32 %2, i32* %3, i32* %4) {
%6 = add nsw i32 %2, -19
%7 = icmp sgt i32 %2, 19
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = getelementptr inbounds i32, i32* %0, i64 3
%10 = tail call i32* @malloc(i32 %6)
%11 = tail call i32 @mrtd_crypto_decrypt_3des(i32* %9, i32* %10, i32 %6, i32* %4)
%12 = tail call i32 @mrtd_crypto_padding_remove(i32* %10, i32* %1, i32 %6, i32* %3)
%13 = tail call i32 @free(i32* %10)
br label %15
14: ; preds = %5
store i32 0, i32* %3
br label %15
15: ; preds = %14, %8
ret void
}