#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 int16_t;

/* Variables and functions */
void int16_bswap_array(int16_t *buf, const size_t num) {
  size_t i;
  for (i = 0; i < num; i++)
    buf[i] = ((int16_t)((buf[i] & 0xff) << 8) | (((buf[i]) >> 8) & 0xff));
}


x86 assembly (clang, -O0):
<int16_bswap_array>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1167 <int16_bswap_array+0x67>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
and    $0xff,%edx
shl    $0x8,%edx
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
sar    $0x8,%eax
and    $0xff,%eax
or     %eax,%edx
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1114 <int16_bswap_array+0x14>
pop    %rbp
ret

x86 assembly (clang, -O2):
<int16_bswap_array>:
test   %rsi,%rsi
je     11b7 <int16_bswap_array+0xb7>
cmp    $0x8,%rsi
jae    1116 <int16_bswap_array+0x16>
xor    %eax,%eax
jmp    11a0 <int16_bswap_array+0xa0>
mov    %rsi,%rax
and    $0xfffffffffffffff8,%rax
xor    %ecx,%ecx
pxor   %xmm0,%xmm0
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movdqu (%rdi,%rcx,4),%xmm1
movdqu 0x10(%rdi,%rcx,4),%xmm2
pshuflw $0xe8,%xmm1,%xmm1
pshufhw $0xe8,%xmm1,%xmm1
pshufd $0xe8,%xmm1,%xmm1
pshuflw $0xe8,%xmm2,%xmm2
pshufhw $0xe8,%xmm2,%xmm2
pshufd $0xe8,%xmm2,%xmm2
movdqa %xmm1,%xmm3
psrlw  $0x8,%xmm3
psllw  $0x8,%xmm1
por    %xmm3,%xmm1
movdqa %xmm2,%xmm3
psrlw  $0x8,%xmm3
psllw  $0x8,%xmm2
por    %xmm3,%xmm2
punpcklwd %xmm0,%xmm1
punpcklwd %xmm0,%xmm2
movdqu %xmm1,(%rdi,%rcx,4)
movdqu %xmm2,0x10(%rdi,%rcx,4)
add    $0x8,%rcx
cmp    %rcx,%rax
jne    1130 <int16_bswap_array+0x30>
cmp    %rsi,%rax
je     11b7 <int16_bswap_array+0xb7>
xchg   %ax,%ax
movzwl (%rdi,%rax,4),%ecx
rol    $0x8,%cx
movzwl %cx,%ecx
mov    %ecx,(%rdi,%rax,4)
add    $0x1,%rax
cmp    %rax,%rsi
jne    11a0 <int16_bswap_array+0xa0>
ret

LLVM IR (-O0):
define void @int16_bswap_array(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
store i32* %0, i32** %3
store i64 %1, i64* %4
store i64 0, i64* %5
br label %6
6: ; preds = %27, %2
%7 = load i64, i64* %5
%8 = load i64, i64* %4
%9 = icmp ult i64 %7, %8
br i1 %9, label %10, label %30
10: ; preds = %6
%11 = load i32*, i32** %3
%12 = load i64, i64* %5
%13 = getelementptr inbounds i32, i32* %11, i64 %12
%14 = load i32, i32* %13
%15 = and i32 %14, 255
%16 = shl i32 %15, 8
%17 = load i32*, i32** %3
%18 = load i64, i64* %5
%19 = getelementptr inbounds i32, i32* %17, i64 %18
%20 = load i32, i32* %19
%21 = ashr i32 %20, 8
%22 = and i32 %21, 255
%23 = or i32 %16, %22
%24 = load i32*, i32** %3
%25 = load i64, i64* %5
%26 = getelementptr inbounds i32, i32* %24, i64 %25
store i32 %23, i32* %26
br label %27
27: ; preds = %10
%28 = load i64, i64* %5
%29 = add i64 %28, 1
store i64 %29, i64* %5
br label %6
30: ; preds = %6
ret void
}

LLVM IR (-O2):
define void @int16_bswap_array(i32* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %39, label %4
4: ; preds = %2
%5 = icmp ult i64 %1, 8
br i1 %5, label %28, label %6
6: ; preds = %4
%7 = and i64 %1, -8
br label %8
8: ; preds = %8, %6
%9 = phi i64 [ 0, %6 ], [ %24, %8 ]
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = bitcast i32* %10 to <4 x i32>*
%12 = load <4 x i32>, <4 x i32>* %11
%13 = getelementptr inbounds i32, i32* %10, i64 4
%14 = bitcast i32* %13 to <4 x i32>*
%15 = load <4 x i32>, <4 x i32>* %14
%16 = trunc <4 x i32> %12 to <4 x i16>
%17 = trunc <4 x i32> %15 to <4 x i16>
%18 = call <4 x i16> @llvm.bswap.v4i16(<4 x i16> %16)
%19 = call <4 x i16> @llvm.bswap.v4i16(<4 x i16> %17)
%20 = zext <4 x i16> %18 to <4 x i32>
%21 = zext <4 x i16> %19 to <4 x i32>
%22 = bitcast i32* %10 to <4 x i32>*
store <4 x i32> %20, <4 x i32>* %22
%23 = bitcast i32* %13 to <4 x i32>*
store <4 x i32> %21, <4 x i32>* %23
%24 = add nuw i64 %9, 8
%25 = icmp eq i64 %24, %7
br i1 %25, label %26, label %8
26: ; preds = %8
%27 = icmp eq i64 %7, %1
br i1 %27, label %39, label %28
28: ; preds = %4, %26
%29 = phi i64 [ 0, %4 ], [ %7, %26 ]
br label %30
30: ; preds = %28, %30
%31 = phi i64 [ %37, %30 ], [ %29, %28 ]
%32 = getelementptr inbounds i32, i32* %0, i64 %31
%33 = load i32, i32* %32
%34 = trunc i32 %33 to i16
%35 = tail call i16 @llvm.bswap.i16(i16 %34)
%36 = zext i16 %35 to i32
store i32 %36, i32* %32
%37 = add nuw i64 %31, 1
%38 = icmp eq i64 %37, %1
br i1 %38, label %39, label %30
39: ; preds = %30, %26, %2
ret void
}