#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 uint16_t;
struct xmms_convert_buffers {
  int dummy;
};
typedef int int32_t;
typedef int int16_t;

/* Variables and functions */
int convert_stereo_to_mono_s16le(struct xmms_convert_buffers *buf, void **data,
                                 int length) {
  int16_t *output = *data, *input = *data;
  int i;
  for (i = 0; i < length / 4; i++) {
    int32_t tmp;
    int16_t stmp;
    tmp = ((int16_t)((uint16_t)((uint16_t)((uint16_t)(*input) >> 8) |
                                (uint16_t)((uint16_t)(*input) << 8))));
    input++;
    tmp += ((int16_t)((uint16_t)((uint16_t)((uint16_t)(*input) >> 8) |
                                 (uint16_t)((uint16_t)(*input) << 8))));
    input++;
    stmp = tmp / 2;
    *output++ = ((int16_t)((uint16_t)((uint16_t)((uint16_t)(stmp) >> 8) |
                                      (uint16_t)((uint16_t)(stmp) << 8))));
  }
  return length / 2;
}


x86 assembly (clang, -O0):
<convert_stereo_to_mono_s16le>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cmp    %ecx,%eax
jge    11ce <convert_stereo_to_mono_s16le+0xce>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
sar    $0x8,%eax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
sar    $0x8,%eax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%ecx
sar    $0x8,%ecx
mov    -0x34(%rbp),%eax
shl    $0x8,%eax
or     %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x20(%rbp)
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    112c <convert_stereo_to_mono_s16le+0x2c>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_stereo_to_mono_s16le>:
lea    0x3(%rdx),%ecx
test   %edx,%edx
cmovns %edx,%ecx
cmp    $0x4,%edx
jl     1160 <convert_stereo_to_mono_s16le+0x60>
mov    (%rsi),%r9
sar    $0x2,%ecx
mov    %ecx,%r8d
xor    %esi,%esi
nopl   0x0(%rax,%rax,1)
mov    (%r9,%rsi,8),%edi
mov    0x4(%r9,%rsi,8),%ecx
mov    %edi,%eax
sar    $0x8,%eax
shl    $0x8,%edi
or     %eax,%edi
mov    %ecx,%eax
sar    $0x8,%eax
shl    $0x8,%ecx
or     %eax,%ecx
add    %edi,%ecx
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
mov    %eax,%ecx
and    $0xfffffffe,%ecx
shl    $0x7,%ecx
sar    $0x9,%eax
or     %ecx,%eax
mov    %eax,(%r9,%rsi,4)
add    $0x1,%rsi
cmp    %esi,%r8d
jne    1120 <convert_stereo_to_mono_s16le+0x20>
mov    %edx,%eax
shr    $0x1f,%eax
add    %edx,%eax
sar    %eax
ret

LLVM IR (-O0):
%struct.xmms_convert_buffers = type { i32 }
define i32 @convert_stereo_to_mono_s16le(%struct.xmms_convert_buffers* %0, i8** %1, i32 %2) {
%4 = alloca %struct.xmms_convert_buffers*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store %struct.xmms_convert_buffers* %0, %struct.xmms_convert_buffers** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
%12 = load i8**, i8*** %5
%13 = load i8*, i8** %12
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %7
%15 = load i8**, i8*** %5
%16 = load i8*, i8** %15
%17 = bitcast i8* %16 to i32*
store i32* %17, i32** %8
store i32 0, i32* %9
br label %18
18: ; preds = %53, %3
%19 = load i32, i32* %9
%20 = load i32, i32* %6
%21 = sdiv i32 %20, 4
%22 = icmp slt i32 %19, %21
br i1 %22, label %23, label %56
23: ; preds = %18
%24 = load i32*, i32** %8
%25 = load i32, i32* %24
%26 = ashr i32 %25, 8
%27 = load i32*, i32** %8
%28 = load i32, i32* %27
%29 = shl i32 %28, 8
%30 = or i32 %26, %29
store i32 %30, i32* %10
%31 = load i32*, i32** %8
%32 = getelementptr inbounds i32, i32* %31, i32 1
store i32* %32, i32** %8
%33 = load i32*, i32** %8
%34 = load i32, i32* %33
%35 = ashr i32 %34, 8
%36 = load i32*, i32** %8
%37 = load i32, i32* %36
%38 = shl i32 %37, 8
%39 = or i32 %35, %38
%40 = load i32, i32* %10
%41 = add nsw i32 %40, %39
store i32 %41, i32* %10
%42 = load i32*, i32** %8
%43 = getelementptr inbounds i32, i32* %42, i32 1
store i32* %43, i32** %8
%44 = load i32, i32* %10
%45 = sdiv i32 %44, 2
store i32 %45, i32* %11
%46 = load i32, i32* %11
%47 = ashr i32 %46, 8
%48 = load i32, i32* %11
%49 = shl i32 %48, 8
%50 = or i32 %47, %49
%51 = load i32*, i32** %7
%52 = getelementptr inbounds i32, i32* %51, i32 1
store i32* %52, i32** %7
store i32 %50, i32* %51
br label %53
53: ; preds = %23
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %18
56: ; preds = %18
%57 = load i32, i32* %6
%58 = sdiv i32 %57, 2
ret i32 %58
}

LLVM IR (-O2):
%struct.xmms_convert_buffers = type { i32 }
define i32 @convert_stereo_to_mono_s16le(%struct.xmms_convert_buffers* nocapture readnone %0, i8** nocapture %1, i32 %2) {
%4 = bitcast i8** %1 to i32**
%5 = load i32*, i32** %4
%6 = sdiv i32 %2, 4
%7 = icmp sgt i32 %2, 3
br i1 %7, label %8, label %30
8: ; preds = %3, %8
%9 = phi i32* [ %27, %8 ], [ %5, %3 ]
%10 = phi i32* [ %22, %8 ], [ %5, %3 ]
%11 = phi i32 [ %28, %8 ], [ 0, %3 ]
%12 = load i32, i32* %10
%13 = ashr i32 %12, 8
%14 = shl i32 %12, 8
%15 = or i32 %13, %14
%16 = getelementptr inbounds i32, i32* %10, i64 1
%17 = load i32, i32* %16
%18 = ashr i32 %17, 8
%19 = shl i32 %17, 8
%20 = or i32 %18, %19
%21 = add nsw i32 %20, %15
%22 = getelementptr inbounds i32, i32* %10, i64 2
%23 = sdiv i32 %21, 2
%24 = ashr i32 %23, 8
%25 = shl i32 %23, 8
%26 = or i32 %24, %25
%27 = getelementptr inbounds i32, i32* %9, i64 1
store i32 %26, i32* %9
%28 = add nuw nsw i32 %11, 1
%29 = icmp eq i32 %28, %6
br i1 %29, label %30, label %8
30: ; preds = %8, %3
%31 = sdiv i32 %2, 2
ret i32 %31
}