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

/* Variables and functions */
int /*<<< orphan*/ __REV16(int /*<<< orphan*/);
void *unaligned_memcpy_rev16(void *dest, void *src, size_t n) {
  uint32_t *dest32 = (uint32_t *)dest;
  uint32_t *src32 = (uint32_t *)src;
  uint16_t *dest16 = (uint16_t *)dest32;
  uint16_t *src16 = (uint16_t *)src32;
  for (; n > 0; n -= 1) {
    *dest16++ = __REV16(*src16++);
  }
  return dest;
}


x86 assembly (clang, -O0):
<unaligned_memcpy_rev16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    1189 <unaligned_memcpy_rev16+0x79>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x38(%rbp)
mov    (%rax),%edi
call   1030 <__REV16@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x30(%rbp)
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1144 <unaligned_memcpy_rev16+0x34>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unaligned_memcpy_rev16>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
test   %rdx,%rdx
je     1146 <unaligned_memcpy_rev16+0x36>
mov    %rdx,%r14
mov    %rsi,%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%edi
call   1030 <__REV16@plt>
mov    %eax,(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1130 <unaligned_memcpy_rev16+0x20>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @unaligned_memcpy_rev16(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = load i8*, i8** %4
%12 = bitcast i8* %11 to i32*
store i32* %12, i32** %7
%13 = load i8*, i8** %5
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %8
%15 = load i32*, i32** %7
store i32* %15, i32** %9
%16 = load i32*, i32** %8
store i32* %16, i32** %10
br label %17
17: ; preds = %27, %3
%18 = load i64, i64* %6
%19 = icmp ugt i64 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = load i32*, i32** %10
%22 = getelementptr inbounds i32, i32* %21, i32 1
store i32* %22, i32** %10
%23 = load i32, i32* %21
%24 = call i32 @__REV16(i32 %23)
%25 = load i32*, i32** %9
%26 = getelementptr inbounds i32, i32* %25, i32 1
store i32* %26, i32** %9
store i32 %24, i32* %25
br label %27
27: ; preds = %20
%28 = load i64, i64* %6
%29 = sub i64 %28, 1
store i64 %29, i64* %6
br label %17
30: ; preds = %17
%31 = load i8*, i8** %4
ret i8* %31
}

LLVM IR (-O2):
define i8* @unaligned_memcpy_rev16(i8* returned writeonly %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %18, label %5
5: ; preds = %3
%6 = bitcast i8* %1 to i32*
%7 = bitcast i8* %0 to i32*
br label %8
8: ; preds = %5, %8
%9 = phi i32* [ %12, %8 ], [ %6, %5 ]
%10 = phi i32* [ %15, %8 ], [ %7, %5 ]
%11 = phi i64 [ %16, %8 ], [ %2, %5 ]
%12 = getelementptr inbounds i32, i32* %9, i64 1
%13 = load i32, i32* %9
%14 = tail call i32 @__REV16(i32 %13)
%15 = getelementptr inbounds i32, i32* %10, i64 1
store i32 %14, i32* %10
%16 = add i64 %11, -1
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %8
18: ; preds = %8, %3
ret i8* %0
}