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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htc_aes_decrypt_chunk(char *, int, char *, char *);
scalar_t__ htc_generate_aes_keys(int /*<<< orphan*/ *, int, char *, char *,
                                 char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strncmp(char *, char *, int);
int try_aes_keydata(char *keydata, int keymap_index, char *chunk) {
  char spool[0x10], key[0x10], iv[0x10];
  if (htc_generate_aes_keys(NULL, keymap_index, key, iv, keydata) == 0) {
    fprintf(stderr, "failed to generate htc aes keys\n");
    return -1;
  }
  memcpy(spool, chunk, sizeof(spool));
  htc_aes_decrypt_chunk(spool, sizeof(spool), key, iv);
  if (strncmp("PK\003\004", spool, 4) == 0) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<try_aes_keydata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x14(%rbp),%esi
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%rcx
mov    -0x10(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edi
call   1040 <htc_generate_aes_keys@plt>
cmp    $0x0,%rax
jne    11a6 <try_aes_keydata+0x56>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <try_aes_keydata+0xb0>
lea    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1070 <memcpy@plt>
lea    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%rcx
mov    $0x10,%esi
call   1050 <htc_aes_decrypt_chunk@plt>
lea    -0x30(%rbp),%rsi
lea    0xe48(%rip),%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11f9 <try_aes_keydata+0xa9>
movl   $0x1,-0x4(%rbp)
jmp    1200 <try_aes_keydata+0xb0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_aes_keydata>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%rbx
mov    %rdi,%r8
lea    0x10(%rsp),%rdx
mov    %rsp,%rcx
xor    %edi,%edi
call   1040 <htc_generate_aes_keys@plt>
test   %rax,%rax
je     11bc <try_aes_keydata+0x6c>
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1070 <memcpy@plt>
lea    0x10(%rsp),%rdx
mov    %rsp,%rcx
mov    %r14,%rdi
mov    $0x10,%esi
call   1050 <htc_aes_decrypt_chunk@plt>
lea    0xe7f(%rip),%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
jmp    11d6 <try_aes_keydata+0x86>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"failed to generate htc aes keys\0A\00"
@.str.1 = [5 x i8] c"PK\03\04\00"
define i32 @try_aes_keydata(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [16 x i8]
%9 = alloca [16 x i8]
%10 = alloca [16 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i32, i32* %6
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%14 = load i8*, i8** %5
%15 = call i64 @htc_generate_aes_keys(i32* null, i32 %11, i8* %12, i8* %13, i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %33
20: ; preds = %3
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%22 = load i8*, i8** %7
%23 = call i32 @memcpy(i8* %21, i8* %22, i32 16)
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%27 = call i32 @htc_aes_decrypt_chunk(i8* %24, i32 16, i8* %25, i8* %26)
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%29 = call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %28, i32 4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %20
store i32 1, i32* %4
br label %33
32: ; preds = %20
store i32 0, i32* %4
br label %33
33: ; preds = %32, %31, %17
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"failed to generate htc aes keys\0A\00"
@.str.1 = [5 x i8] c"PK\03\04\00"
define i32 @try_aes_keydata(i8* %0, i32 %1, i8* %2) {
%4 = alloca [16 x i8]
%5 = alloca [16 x i8]
%6 = alloca [16 x i8]
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%10 = call i64 @htc_generate_aes_keys(i32* null, i32 %1, i8* %8, i8* %9, i8* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %21
15: ; preds = %3
%16 = call i32 @memcpy(i8* %7, i8* %2, i32 16)
%17 = call i32 @htc_aes_decrypt_chunk(i8* %7, i32 16, i8* %8, i8* %9)
%18 = call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 4)
%19 = icmp eq i64 %18, 0
%20 = zext i1 %19 to i32
br label %21
21: ; preds = %15, %12
%22 = phi i32 [ -1, %12 ], [ %20, %15 ]
ret i32 %22
}