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

/* Variables and functions */
int /*<<< orphan*/ chacha(unsigned char const *, unsigned char const *,
                          unsigned char const *, unsigned long long,
                          unsigned char *);
int /*<<< orphan*/ siphash24_mac(unsigned char *, unsigned char const *,
                                 unsigned char const *, unsigned char const *,
                                 unsigned long long, unsigned char const *,
                                 unsigned long long);
int crypto_aead_decrypt(unsigned char *m, unsigned long long *mlen_p,
                        unsigned char *nsec, const unsigned char *c,
                        unsigned long long clen, const unsigned char *ad,
                        unsigned long long adlen, const unsigned char *npub,
                        const unsigned char *k) {
  unsigned char tag[8];
  unsigned long long mlen = clen - 8;
  unsigned char zero = 0;
  int ii;
  if (clen < 8) {
    return -1;
  }
  siphash24_mac(tag, k + 32, npub, c, mlen, ad, adlen);
  for (ii = 0; ii < 8; ++ii) {
    zero |= tag[ii] ^ c[mlen + ii];
  }
  if (0 != zero) {
    return -1;
  }
  chacha(k, npub, c, mlen, m);
  *mlen_p = mlen;
  return 0;
}


x86 assembly (clang, -O0):
<crypto_aead_decrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x30(%rbp),%rax
sub    $0x8,%rax
mov    %rax,-0x48(%rbp)
movb   $0x0,-0x49(%rbp)
cmpq   $0x8,-0x30(%rbp)
jae    1173 <crypto_aead_decrypt+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <crypto_aead_decrypt+0x107>
lea    -0x40(%rbp),%rdi
mov    0x20(%rbp),%rsi
add    $0x20,%rsi
mov    0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x48(%rbp),%r8
mov    -0x38(%rbp),%r9
mov    0x10(%rbp),%rax
mov    %rax,(%rsp)
call   1030 <siphash24_mac@plt>
movl   $0x0,-0x50(%rbp)
cmpl   $0x8,-0x50(%rbp)
jge    11e2 <crypto_aead_decrypt+0xc2>
movslq -0x50(%rbp),%rax
movzbl -0x40(%rbp,%rax,1),%ecx
mov    -0x28(%rbp),%rax
mov    -0x48(%rbp),%rdx
movslq -0x50(%rbp),%rsi
add    %rsi,%rdx
movzbl (%rax,%rdx,1),%eax
xor    %eax,%ecx
movzbl -0x49(%rbp),%eax
or     %ecx,%eax
mov    %al,-0x49(%rbp)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    11a3 <crypto_aead_decrypt+0x83>
movzbl -0x49(%rbp),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
je     11fc <crypto_aead_decrypt+0xdc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <crypto_aead_decrypt+0x107>
mov    0x20(%rbp),%rdi
mov    0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x10(%rbp),%r8
call   1040 <chacha@plt>
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypto_aead_decrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%rbx
mov    $0xffffffff,%ebp
sub    $0x8,%rbx
jb     1195 <crypto_aead_decrypt+0x75>
mov    %rcx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x60(%rsp),%rcx
mov    0x58(%rsp),%r13
mov    0x50(%rsp),%rax
lea    0x20(%rcx),%rsi
mov    %rax,(%rsp)
lea    0x10(%rsp),%rdi
mov    %r13,%rdx
mov    %r12,%rcx
mov    %rbx,%r8
call   1030 <siphash24_mac@plt>
mov    (%r12,%rbx,1),%rax
xor    0x10(%rsp),%rax
jne    1195 <crypto_aead_decrypt+0x75>
mov    0x60(%rsp),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %rbx,%rcx
mov    %r15,%r8
call   1040 <chacha@plt>
mov    %rbx,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @crypto_aead_decrypt(i8* %0, i64* %1, i8* %2, i8* %3, i64 %4, i8* %5, i64 %6, i8* %7, i8* %8) {
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i8*
%17 = alloca i64
%18 = alloca i8*
%19 = alloca i8*
%20 = alloca [8 x i8]
%21 = alloca i64
%22 = alloca i8
%23 = alloca i32
store i8* %0, i8** %11
store i64* %1, i64** %12
store i8* %2, i8** %13
store i8* %3, i8** %14
store i64 %4, i64* %15
store i8* %5, i8** %16
store i64 %6, i64* %17
store i8* %7, i8** %18
store i8* %8, i8** %19
%24 = load i64, i64* %15
%25 = sub i64 %24, 8
store i64 %25, i64* %21
store i8 0, i8* %22
%26 = load i64, i64* %15
%27 = icmp ult i64 %26, 8
br i1 %27, label %28, label %29
28: ; preds = %9
store i32 -1, i32* %10
br label %78
29: ; preds = %9
%30 = getelementptr inbounds [8 x i8], [8 x i8]* %20, i64 0, i64 0
%31 = load i8*, i8** %19
%32 = getelementptr inbounds i8, i8* %31, i64 32
%33 = load i8*, i8** %18
%34 = load i8*, i8** %14
%35 = load i64, i64* %21
%36 = load i8*, i8** %16
%37 = load i64, i64* %17
%38 = call i32 @siphash24_mac(i8* %30, i8* %32, i8* %33, i8* %34, i64 %35, i8* %36, i64 %37)
store i32 0, i32* %23
br label %39
39: ; preds = %61, %29
%40 = load i32, i32* %23
%41 = icmp slt i32 %40, 8
br i1 %41, label %42, label %64
42: ; preds = %39
%43 = load i32, i32* %23
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [8 x i8], [8 x i8]* %20, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = load i8*, i8** %14
%49 = load i64, i64* %21
%50 = load i32, i32* %23
%51 = sext i32 %50 to i64
%52 = add i64 %49, %51
%53 = getelementptr inbounds i8, i8* %48, i64 %52
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = xor i32 %47, %55
%57 = load i8, i8* %22
%58 = zext i8 %57 to i32
%59 = or i32 %58, %56
%60 = trunc i32 %59 to i8
store i8 %60, i8* %22
br label %61
61: ; preds = %42
%62 = load i32, i32* %23
%63 = add nsw i32 %62, 1
store i32 %63, i32* %23
br label %39
64: ; preds = %39
%65 = load i8, i8* %22
%66 = zext i8 %65 to i32
%67 = icmp ne i32 0, %66
br i1 %67, label %68, label %69
68: ; preds = %64
store i32 -1, i32* %10
br label %78
69: ; preds = %64
%70 = load i8*, i8** %19
%71 = load i8*, i8** %18
%72 = load i8*, i8** %14
%73 = load i64, i64* %21
%74 = load i8*, i8** %11
%75 = call i32 @chacha(i8* %70, i8* %71, i8* %72, i64 %73, i8* %74)
%76 = load i64, i64* %21
%77 = load i64*, i64** %12
store i64 %76, i64* %77
store i32 0, i32* %10
br label %78
78: ; preds = %69, %68, %28
%79 = load i32, i32* %10
ret i32 %79
}

LLVM IR (-O2):
define i32 @crypto_aead_decrypt(i8* %0, i64* nocapture writeonly %1, i8* nocapture readnone %2, i8* %3, i64 %4, i8* %5, i64 %6, i8* %7, i8* %8) {
%10 = alloca <8 x i8>
%11 = getelementptr inbounds <8 x i8>, <8 x i8>* %10, i64 0, i64 0
%12 = add i64 %4, -8
%13 = icmp ult i64 %4, 8
br i1 %13, label %26, label %14
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %8, i64 32
%16 = call i32 @siphash24_mac(i8* %11, i8* %15, i8* %7, i8* %3, i64 %12, i8* %5, i64 %6)
%17 = getelementptr inbounds i8, i8* %3, i64 %12
%18 = load <8 x i8>, <8 x i8>* %10
%19 = bitcast i8* %17 to <8 x i8>*
%20 = load <8 x i8>, <8 x i8>* %19
%21 = xor <8 x i8> %20, %18
%22 = call i8 @llvm.vector.reduce.or.v8i8(<8 x i8> %21)
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @chacha(i8* %8, i8* %7, i8* %3, i64 %12, i8* %0)
store i64 %12, i64* %1
br label %26
26: ; preds = %14, %9, %24
%27 = phi i32 [ 0, %24 ], [ -1, %9 ], [ -1, %14 ]
ret i32 %27
}