#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;
typedef int /*<<< orphan*/ hmac;
typedef int /*<<< orphan*/ encryption_key;
typedef int /*<<< orphan*/ authentication_key;

/* Variables and functions */
char *cipher_aes_decrypt(int /*<<< orphan*/ const *, int, int *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ cipher_derive_hmac_keys(int /*<<< orphan*/ const *,
                                           int /*<<< orphan*/ *,
                                           int /*<<< orphan*/ *);
int /*<<< orphan*/ hmac_sha256(int /*<<< orphan*/ *, int,
                               int /*<<< orphan*/ const *, int,
                               int /*<<< orphan*/ *);
scalar_t__ memcmp(int /*<<< orphan*/ *, int /*<<< orphan*/ const *, int);
int /*<<< orphan*/ utils_zero(int /*<<< orphan*/ *, int);
char *cipher_aes_hmac_decrypt(const uint8_t *in, int inlen, int *outlen,
                              const uint8_t *key) {
  uint8_t hmac[32];
  uint8_t encryption_key[32];
  uint8_t authentication_key[32];
  *outlen = 0;
  if ((size_t)inlen < sizeof(hmac)) {
    return NULL;
  }
  cipher_derive_hmac_keys(key, encryption_key, authentication_key);
  hmac_sha256(authentication_key, 32, in, inlen - sizeof(hmac), hmac);
  if (!(memcmp((hmac), (in + inlen - sizeof(hmac)), (sizeof(hmac))) == 0)) {
    utils_zero(encryption_key, sizeof(encryption_key));
    utils_zero(authentication_key, sizeof(authentication_key));
    return NULL;
  }
  char *ret =
      cipher_aes_decrypt(in, inlen - sizeof(hmac), outlen, encryption_key);
  utils_zero(encryption_key, sizeof(encryption_key));
  utils_zero(authentication_key, sizeof(authentication_key));
  return ret;
}


x86 assembly (clang, -O0):
<cipher_aes_hmac_decrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movslq -0x14(%rbp),%rax
cmp    $0x80,%rax
jae    1191 <cipher_aes_hmac_decrypt+0x41>
movq   $0x0,-0x8(%rbp)
jmp    1284 <cipher_aes_hmac_decrypt+0x134>
mov    -0x28(%rbp),%rdi
lea    -0x130(%rbp),%rsi
lea    -0x1b0(%rbp),%rdx
call   1030 <cipher_derive_hmac_keys@plt>
lea    -0x1b0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
movslq -0x14(%rbp),%rax
sub    $0x80,%rax
mov    %eax,%ecx
lea    -0xb0(%rbp),%r8
mov    $0x20,%esi
call   1070 <hmac_sha256@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
add    $0xfffffffffffffe00,%rsi
mov    $0x80,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     1230 <cipher_aes_hmac_decrypt+0xe0>
lea    -0x130(%rbp),%rdi
mov    $0x80,%esi
call   1050 <utils_zero@plt>
lea    -0x1b0(%rbp),%rdi
mov    $0x80,%esi
call   1050 <utils_zero@plt>
movq   $0x0,-0x8(%rbp)
jmp    1284 <cipher_aes_hmac_decrypt+0x134>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
sub    $0x80,%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
lea    -0x130(%rbp),%rcx
call   1040 <cipher_aes_decrypt@plt>
mov    %rax,-0x1b8(%rbp)
lea    -0x130(%rbp),%rdi
mov    $0x80,%esi
call   1050 <utils_zero@plt>
lea    -0x1b0(%rbp),%rdi
mov    $0x80,%esi
call   1050 <utils_zero@plt>
mov    -0x1b8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cipher_aes_hmac_decrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x188,%rsp
movl   $0x0,(%rdx)
cmp    $0x80,%esi
jae    1176 <cipher_aes_hmac_decrypt+0x26>
xor    %ebx,%ebx
jmp    120a <cipher_aes_hmac_decrypt+0xba>
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
movslq %esi,%r13
mov    %rsp,%rsi
lea    0x80(%rsp),%r15
mov    %rcx,%rdi
mov    %r15,%rdx
call   1030 <cipher_derive_hmac_keys@plt>
add    $0xffffff80,%ebp
lea    0x100(%rsp),%r12
mov    %r15,%rdi
mov    $0x20,%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
mov    %r12,%r8
call   1070 <hmac_sha256@plt>
lea    (%rbx,%r13,4),%rsi
add    $0xfffffffffffffe00,%rsi
mov    %r12,%rdi
mov    $0x80,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     11d8 <cipher_aes_hmac_decrypt+0x88>
xor    %ebx,%ebx
jmp    11eb <cipher_aes_hmac_decrypt+0x9b>
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1040 <cipher_aes_decrypt@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x80,%esi
call   1050 <utils_zero@plt>
lea    0x80(%rsp),%rdi
mov    $0x80,%esi
call   1050 <utils_zero@plt>
mov    %rbx,%rax
add    $0x188,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @cipher_aes_hmac_decrypt(i32* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [32 x i32]
%11 = alloca [32 x i32]
%12 = alloca [32 x i32]
%13 = alloca i8*
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%14 = load i32*, i32** %8
store i32 0, i32* %14
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = icmp ult i64 %16, 128
br i1 %17, label %18, label %19
18: ; preds = %4
store i8* null, i8** %5
br label %59
19: ; preds = %4
%20 = load i32*, i32** %9
%21 = getelementptr inbounds [32 x i32], [32 x i32]* %11, i64 0, i64 0
%22 = getelementptr inbounds [32 x i32], [32 x i32]* %12, i64 0, i64 0
%23 = call i32 @cipher_derive_hmac_keys(i32* %20, i32* %21, i32* %22)
%24 = getelementptr inbounds [32 x i32], [32 x i32]* %12, i64 0, i64 0
%25 = load i32*, i32** %6
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = sub i64 %27, 128
%29 = trunc i64 %28 to i32
%30 = getelementptr inbounds [32 x i32], [32 x i32]* %10, i64 0, i64 0
%31 = call i32 @hmac_sha256(i32* %24, i32 32, i32* %25, i32 %29, i32* %30)
%32 = getelementptr inbounds [32 x i32], [32 x i32]* %10, i64 0, i64 0
%33 = load i32*, i32** %6
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = getelementptr inbounds i32, i32* %36, i64 -128
%38 = call i64 @memcmp(i32* %32, i32* %37, i32 128)
%39 = icmp eq i64 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %19
%41 = getelementptr inbounds [32 x i32], [32 x i32]* %11, i64 0, i64 0
%42 = call i32 @utils_zero(i32* %41, i32 128)
%43 = getelementptr inbounds [32 x i32], [32 x i32]* %12, i64 0, i64 0
%44 = call i32 @utils_zero(i32* %43, i32 128)
store i8* null, i8** %5
br label %59
45: ; preds = %19
%46 = load i32*, i32** %6
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = sub i64 %48, 128
%50 = trunc i64 %49 to i32
%51 = load i32*, i32** %8
%52 = getelementptr inbounds [32 x i32], [32 x i32]* %11, i64 0, i64 0
%53 = call i8* @cipher_aes_decrypt(i32* %46, i32 %50, i32* %51, i32* %52)
store i8* %53, i8** %13
%54 = getelementptr inbounds [32 x i32], [32 x i32]* %11, i64 0, i64 0
%55 = call i32 @utils_zero(i32* %54, i32 128)
%56 = getelementptr inbounds [32 x i32], [32 x i32]* %12, i64 0, i64 0
%57 = call i32 @utils_zero(i32* %56, i32 128)
%58 = load i8*, i8** %13
store i8* %58, i8** %5
br label %59
59: ; preds = %45, %40, %18
%60 = load i8*, i8** %5
ret i8* %60
}

LLVM IR (-O2):
define i8* @cipher_aes_hmac_decrypt(i32* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca [32 x i32]
%6 = alloca [32 x i32]
%7 = alloca [32 x i32]
%8 = bitcast [32 x i32]* %5 to i8*
%9 = bitcast [32 x i32]* %6 to i8*
%10 = bitcast [32 x i32]* %7 to i8*
store i32 0, i32* %2
%11 = icmp ult i32 %1, 128
br i1 %11, label %30, label %12
12: ; preds = %4
%13 = sext i32 %1 to i64
%14 = getelementptr inbounds [32 x i32], [32 x i32]* %6, i64 0, i64 0
%15 = getelementptr inbounds [32 x i32], [32 x i32]* %7, i64 0, i64 0
%16 = call i32 @cipher_derive_hmac_keys(i32* %3, i32* %14, i32* %15)
%17 = add i32 %1, -128
%18 = getelementptr inbounds [32 x i32], [32 x i32]* %5, i64 0, i64 0
%19 = call i32 @hmac_sha256(i32* %15, i32 32, i32* %0, i32 %17, i32* %18)
%20 = getelementptr inbounds i32, i32* %0, i64 %13
%21 = getelementptr inbounds i32, i32* %20, i64 -128
%22 = call i64 @memcmp(i32* %18, i32* %21, i32 128)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = call i8* @cipher_aes_decrypt(i32* %0, i32 %17, i32* %2, i32* %14)
br label %26
26: ; preds = %12, %24
%27 = phi i8* [ %25, %24 ], [ null, %12 ]
%28 = call i32 @utils_zero(i32* %14, i32 128)
%29 = call i32 @utils_zero(i32* %15, i32 128)
br label %30
30: ; preds = %26, %4
%31 = phi i8* [ null, %4 ], [ %27, %26 ]
ret i8* %31
}