#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*/ sad4_one_macroblock(unsigned short *, unsigned short *,
                                       unsigned short *, int, int, int, int);
void sad4_cpu(unsigned short *blk_sad, unsigned short *frame,
              unsigned short *ref, int mb_width, int mb_height) {
  int mb_x, mb_y, bk_x, bk_y;
  unsigned int frame_yoff;
  blk_sad += ((mb_width * mb_height) * (25 * 1096));
  for (mb_y = 0, frame_yoff = 0; mb_y < mb_height;
       mb_y++, frame_yoff += 256 * mb_width) {
    for (mb_x = 0; mb_x < mb_width; mb_x++) {
      sad4_one_macroblock(blk_sad + (mb_y * mb_width + mb_x) * (16 * 1096),
                          frame + frame_yoff + mb_x * 16, ref, mb_y * 16,
                          mb_x * 16, mb_width, mb_height);
    }
  }
}


x86 assembly (clang, -O0):
<sad4_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
imul   $0x6b08,%eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
shl    $0x1,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11ff <sad4_cpu+0xef>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11e0 <sad4_cpu+0xd0>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%eax
imul   -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
imul   $0x4480,%eax,%eax
cltq
shl    $0x1,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%eax
shl    $0x1,%rax
add    %rax,%rsi
mov    -0x24(%rbp),%eax
shl    $0x4,%eax
cltq
shl    $0x1,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%ecx
shl    $0x4,%ecx
mov    -0x24(%rbp),%r8d
shl    $0x4,%r8d
mov    -0x1c(%rbp),%r9d
mov    -0x20(%rbp),%eax
mov    %eax,(%rsp)
call   1030 <sad4_one_macroblock@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116b <sad4_cpu+0x5b>
jmp    11e5 <sad4_cpu+0xd5>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x8,%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    1158 <sad4_cpu+0x48>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sad4_cpu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,0x28(%rsp)
test   %r8d,%r8d
jle    122a <sad4_cpu+0x11a>
mov    %r8d,%r13d
mov    %ecx,%ebp
mov    %rdx,%r12
mov    %ecx,%eax
imul   %r8d,%eax
imul   $0x6b08,%eax,%eax
cltq
lea    (%rdi,%rax,2),%rax
mov    %rax,0x20(%rsp)
mov    %ecx,%eax
shl    $0x8,%eax
mov    %eax,0x18(%rsp)
movslq %ecx,%rax
mov    %r8d,%ecx
mov    %rcx,0x38(%rsp)
mov    %eax,%esi
imul   $0x8900,%rax,%rax
mov    %rax,0x30(%rsp)
shl    $0x4,%rsi
xor    %eax,%eax
mov    %rax,0x10(%rsp)
movl   $0x0,0xc(%rsp)
mov    %rsi,0x40(%rsp)
jmp    11be <sad4_cpu+0xae>
nopw   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rcx
add    $0x1,%rcx
mov    0xc(%rsp),%eax
add    0x18(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    0x30(%rsp),%rax
add    %rax,0x20(%rsp)
mov    %rcx,%rax
mov    %rcx,0x10(%rsp)
cmp    0x38(%rsp),%rcx
je     122a <sad4_cpu+0x11a>
test   %ebp,%ebp
jle    1190 <sad4_cpu+0x80>
mov    0xc(%rsp),%eax
mov    0x28(%rsp),%rcx
lea    (%rcx,%rax,2),%rbx
mov    0x10(%rsp),%rax
shl    $0x4,%eax
mov    %eax,0x1c(%rsp)
xor    %r15d,%r15d
mov    0x20(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13d,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    0x1c(%rsp),%ecx
mov    %r15d,%r8d
mov    %ebp,%r9d
call   1030 <sad4_one_macroblock@plt>
mov    0x40(%rsp),%rsi
add    $0x8900,%r14
add    $0x20,%rbx
add    $0x10,%r15
cmp    %r15,%rsi
jne    11f0 <sad4_cpu+0xe0>
jmp    1190 <sad4_cpu+0x80>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @sad4_cpu(i16* %0, i16* %1, i16* %2, i32 %3, i32 %4) {
%6 = mul i32 %3, 27400
%7 = mul i32 %6, %4
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i16, i16* %0, i64 %8
%10 = icmp sgt i32 %4, 0
br i1 %10, label %11, label %41
11: ; preds = %5
%12 = icmp sgt i32 %3, 0
%13 = shl nsw i32 %3, 8
%14 = sext i32 %3 to i64
%15 = zext i32 %4 to i64
%16 = zext i32 %3 to i64
br label %17
17: ; preds = %11, %37
%18 = phi i64 [ 0, %11 ], [ %38, %37 ]
%19 = phi i32 [ 0, %11 ], [ %39, %37 ]
br i1 %12, label %20, label %37
20: ; preds = %17
%21 = mul nsw i64 %18, %14
%22 = zext i32 %19 to i64
%23 = getelementptr inbounds i16, i16* %1, i64 %22
%24 = trunc i64 %18 to i32
%25 = shl nsw i32 %24, 4
br label %26
26: ; preds = %20, %26
%27 = phi i64 [ 0, %20 ], [ %35, %26 ]
%28 = add nsw i64 %27, %21
%29 = mul nsw i64 %28, 17536
%30 = getelementptr inbounds i16, i16* %9, i64 %29
%31 = shl nsw i64 %27, 4
%32 = getelementptr inbounds i16, i16* %23, i64 %31
%33 = trunc i64 %31 to i32
%34 = tail call i32 @sad4_one_macroblock(i16* %30, i16* %32, i16* %2, i32 %25, i32 %33, i32 %3, i32 %4)
%35 = add nuw nsw i64 %27, 1
%36 = icmp eq i64 %35, %16
br i1 %36, label %37, label %26
37: ; preds = %26, %17
%38 = add nuw nsw i64 %18, 1
%39 = add i32 %19, %13
%40 = icmp eq i64 %38, %15
br i1 %40, label %41, label %17
41: ; preds = %37, %5
ret void
}