#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 /*<<< orphan*/ const uint8_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ be_to_h_u16(int /*<<< orphan*/ const *);
int /*<<< orphan*/ h_u16_to_le(int /*<<< orphan*/ const *, int /*<<< orphan*/);
void buf_bswap16(uint8_t *dst, const uint8_t *src, size_t len) {
  assert(len % 2 == 0);
  assert(dst == src || dst + len <= src || src + len <= dst);
  for (size_t n = 0; n < len; n += 2) {
    uint16_t x = be_to_h_u16(src + n);
    h_u16_to_le(dst + n, x);
  }
}


x86 assembly (clang, -O0):
<buf_bswap16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rcx
mov    $0x1,%al
cmp    -0x10(%rbp),%rcx
mov    %al,-0x25(%rbp)
je     11a7 <buf_bswap16+0x77>
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rcx
mov    $0x1,%al
cmp    -0x10(%rbp),%rcx
mov    %al,-0x25(%rbp)
jbe    11a7 <buf_bswap16+0x77>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
cmp    -0x8(%rbp),%rax
setbe  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1209 <buf_bswap16+0xd9>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1030 <be_to_h_u16@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x24(%rbp),%esi
call   1050 <h_u16_to_le@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    11bc <buf_bswap16+0x8c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_bswap16>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %r14d,%edi
not    %edi
and    $0x1,%edi
call   1040 <assert@plt>
cmp    %r15,%r12
sete   %al
lea    (%r12,%r14,4),%rcx
cmp    %r15,%rcx
setbe  %cl
or     %al,%cl
lea    (%r15,%r14,4),%rax
cmp    %r12,%rax
setbe  %al
or     %cl,%al
movzbl %al,%edi
call   1040 <assert@plt>
test   %r14,%r14
je     11a3 <buf_bswap16+0x73>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <be_to_h_u16@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <h_u16_to_le@plt>
add    $0x2,%rbx
add    $0x8,%r12
add    $0x8,%r15
cmp    %r14,%rbx
jb     1180 <buf_bswap16+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @buf_bswap16(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
%10 = urem i64 %9, 2
%11 = icmp eq i64 %10, 0
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32*, i32** %4
%15 = load i32*, i32** %5
%16 = icmp eq i32* %14, %15
br i1 %16, label %29, label %17
17: ; preds = %3
%18 = load i32*, i32** %4
%19 = load i64, i64* %6
%20 = getelementptr inbounds i32, i32* %18, i64 %19
%21 = load i32*, i32** %5
%22 = icmp ule i32* %20, %21
br i1 %22, label %29, label %23
23: ; preds = %17
%24 = load i32*, i32** %5
%25 = load i64, i64* %6
%26 = getelementptr inbounds i32, i32* %24, i64 %25
%27 = load i32*, i32** %4
%28 = icmp ule i32* %26, %27
br label %29
29: ; preds = %23, %17, %3
%30 = phi i1 [ true, %17 ], [ true, %3 ], [ %28, %23 ]
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
store i64 0, i64* %7
br label %33
33: ; preds = %47, %29
%34 = load i64, i64* %7
%35 = load i64, i64* %6
%36 = icmp ult i64 %34, %35
br i1 %36, label %37, label %50
37: ; preds = %33
%38 = load i32*, i32** %5
%39 = load i64, i64* %7
%40 = getelementptr inbounds i32, i32* %38, i64 %39
%41 = call i32 @be_to_h_u16(i32* %40)
store i32 %41, i32* %8
%42 = load i32*, i32** %4
%43 = load i64, i64* %7
%44 = getelementptr inbounds i32, i32* %42, i64 %43
%45 = load i32, i32* %8
%46 = call i32 @h_u16_to_le(i32* %44, i32 %45)
br label %47
47: ; preds = %37
%48 = load i64, i64* %7
%49 = add i64 %48, 2
store i64 %49, i64* %7
br label %33
50: ; preds = %33
ret void
}

LLVM IR (-O2):
define void @buf_bswap16(i32* %0, i32* %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = and i32 %4, 1
%6 = xor i32 %5, 1
%7 = tail call i32 @assert(i32 %6)
%8 = icmp eq i32* %0, %1
%9 = getelementptr inbounds i32, i32* %0, i64 %2
%10 = icmp ule i32* %9, %1
%11 = select i1 %8, i1 true, i1 %10
%12 = getelementptr inbounds i32, i32* %1, i64 %2
%13 = icmp ule i32* %12, %0
%14 = select i1 %11, i1 true, i1 %13
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = icmp eq i64 %2, 0
br i1 %17, label %18, label %19
18: ; preds = %19, %3
ret void
19: ; preds = %3, %19
%20 = phi i64 [ %25, %19 ], [ 0, %3 ]
%21 = getelementptr inbounds i32, i32* %1, i64 %20
%22 = tail call i32 @be_to_h_u16(i32* %21)
%23 = getelementptr inbounds i32, i32* %0, i64 %20
%24 = tail call i32 @h_u16_to_le(i32* %23, i32 %22)
%25 = add i64 %20, 2
%26 = icmp ult i64 %25, %2
br i1 %26, label %19, label %18
}