#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 int32_t;
typedef int int16_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int clamp_s16(int);
scalar_t__ rdot(size_t, int const *const, int const *);
void adpcm_compute_residuals(int16_t *dst, const int16_t *src,
                             const int16_t *cb_entry,
                             const int16_t *last_samples, size_t count) {
  const int16_t *const book1 = cb_entry;
  const int16_t *const book2 = cb_entry + 8;
  const int16_t l1 = last_samples[0];
  const int16_t l2 = last_samples[1];
  size_t i;
  assert(count <= 8);
  for (i = 0; i < count; ++i) {
    int32_t accu = (int32_t)src[i] << 11;
    accu += book1[i] * l1 + book2[i] * l2 + rdot(i, book2, src);
    dst[i] = clamp_s16(accu >> 11);
  }
}


x86 assembly (clang, -O0):
<adpcm_compute_residuals>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x40(%rbp)
cmpq   $0x8,-0x28(%rbp)
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    121d <adpcm_compute_residuals+0xed>
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
shl    $0xb,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
imul   -0x3c(%rbp),%eax
mov    -0x38(%rbp),%rcx
mov    -0x48(%rbp),%rdx
mov    (%rcx,%rdx,4),%ecx
imul   -0x40(%rbp),%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <rdot@plt>
mov    -0x58(%rbp),%rcx
add    %rax,%rcx
movslq -0x4c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%edi
sar    $0xb,%edi
call   1030 <clamp_s16@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    118d <adpcm_compute_residuals+0x5d>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adpcm_compute_residuals>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r13
mov    %rdx,0x10(%rsp)
mov    %rsi,%r15
mov    %rdi,%r12
mov    (%rcx),%ebp
mov    0x4(%rcx),%eax
mov    %eax,0xc(%rsp)
xor    %edi,%edi
cmp    $0x9,%r8
setb   %dil
call   1040 <assert@plt>
test   %r13,%r13
je     11c9 <adpcm_compute_residuals+0x99>
addq   $0x20,0x10(%rsp)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%eax
shl    $0xb,%eax
mov    0x10(%rsp),%rsi
mov    -0x20(%rsi,%rbx,4),%ecx
imul   %ebp,%ecx
add    %eax,%ecx
mov    %ebp,%r14d
mov    (%rsi,%rbx,4),%ebp
imul   0xc(%rsp),%ebp
add    %ecx,%ebp
mov    %rbx,%rdi
mov    %r15,%rdx
call   1050 <rdot@plt>
add    %eax,%ebp
sar    $0xb,%ebp
mov    %ebp,%edi
mov    %r14d,%ebp
call   1030 <clamp_s16@plt>
mov    %eax,(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1180 <adpcm_compute_residuals+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @adpcm_compute_residuals(i32* %0, i32* %1, i32* %2, i32* %3, i64 %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
%16 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i64 %4, i64* %10
%17 = load i32*, i32** %8
store i32* %17, i32** %11
%18 = load i32*, i32** %8
%19 = getelementptr inbounds i32, i32* %18, i64 8
store i32* %19, i32** %12
%20 = load i32*, i32** %9
%21 = getelementptr inbounds i32, i32* %20, i64 0
%22 = load i32, i32* %21
store i32 %22, i32* %13
%23 = load i32*, i32** %9
%24 = getelementptr inbounds i32, i32* %23, i64 1
%25 = load i32, i32* %24
store i32 %25, i32* %14
%26 = load i64, i64* %10
%27 = icmp ule i64 %26, 8
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
store i64 0, i64* %15
br label %30
30: ; preds = %69, %5
%31 = load i64, i64* %15
%32 = load i64, i64* %10
%33 = icmp ult i64 %31, %32
br i1 %33, label %34, label %72
34: ; preds = %30
%35 = load i32*, i32** %7
%36 = load i64, i64* %15
%37 = getelementptr inbounds i32, i32* %35, i64 %36
%38 = load i32, i32* %37
%39 = shl i32 %38, 11
store i32 %39, i32* %16
%40 = load i32*, i32** %11
%41 = load i64, i64* %15
%42 = getelementptr inbounds i32, i32* %40, i64 %41
%43 = load i32, i32* %42
%44 = load i32, i32* %13
%45 = mul nsw i32 %43, %44
%46 = load i32*, i32** %12
%47 = load i64, i64* %15
%48 = getelementptr inbounds i32, i32* %46, i64 %47
%49 = load i32, i32* %48
%50 = load i32, i32* %14
%51 = mul nsw i32 %49, %50
%52 = add nsw i32 %45, %51
%53 = sext i32 %52 to i64
%54 = load i64, i64* %15
%55 = load i32*, i32** %12
%56 = load i32*, i32** %7
%57 = call i64 @rdot(i64 %54, i32* %55, i32* %56)
%58 = add nsw i64 %53, %57
%59 = load i32, i32* %16
%60 = sext i32 %59 to i64
%61 = add nsw i64 %60, %58
%62 = trunc i64 %61 to i32
store i32 %62, i32* %16
%63 = load i32, i32* %16
%64 = ashr i32 %63, 11
%65 = call i32 @clamp_s16(i32 %64)
%66 = load i32*, i32** %6
%67 = load i64, i64* %15
%68 = getelementptr inbounds i32, i32* %66, i64 %67
store i32 %65, i32* %68
br label %69
69: ; preds = %34
%70 = load i64, i64* %15
%71 = add i64 %70, 1
store i64 %71, i64* %15
br label %30
72: ; preds = %30
ret void
}

LLVM IR (-O2):
define void @adpcm_compute_residuals(i32* nocapture writeonly %0, i32* %1, i32* %2, i32* nocapture %3, i64 %4) {
%6 = getelementptr inbounds i32, i32* %2, i64 8
%7 = load i32, i32* %3
%8 = getelementptr inbounds i32, i32* %3, i64 1
%9 = load i32, i32* %8
%10 = icmp ult i64 %4, 9
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = icmp eq i64 %4, 0
br i1 %13, label %35, label %14
14: ; preds = %5, %14
%15 = phi i64 [ %33, %14 ], [ 0, %5 ]
%16 = getelementptr inbounds i32, i32* %1, i64 %15
%17 = load i32, i32* %16
%18 = shl i32 %17, 11
%19 = getelementptr inbounds i32, i32* %2, i64 %15
%20 = load i32, i32* %19
%21 = mul nsw i32 %20, %7
%22 = getelementptr inbounds i32, i32* %6, i64 %15
%23 = load i32, i32* %22
%24 = mul nsw i32 %23, %9
%25 = tail call i64 @rdot(i64 %15, i32* %6, i32* %1)
%26 = trunc i64 %25 to i32
%27 = add i32 %21, %18
%28 = add i32 %27, %24
%29 = add i32 %28, %26
%30 = ashr i32 %29, 11
%31 = tail call i32 @clamp_s16(i32 %30)
%32 = getelementptr inbounds i32, i32* %0, i64 %15
store i32 %31, i32* %32
%33 = add nuw i64 %15, 1
%34 = icmp eq i64 %33, %4
br i1 %34, label %35, label %14
35: ; preds = %14, %5
ret void
}