#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*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ be_to_h_u32(int /*<<< orphan*/ const *);
int /*<<< orphan*/ h_u32_to_le(int /*<<< orphan*/ const *, int /*<<< orphan*/);
void buf_bswap32(uint8_t *dst, const uint8_t *src, size_t len) {
  assert(len % 4 == 0);
  assert(dst == src || dst + len <= src || src + len <= dst);
  for (size_t n = 0; n < len; n += 4) {
    uint32_t x = be_to_h_u32(src + n);
    h_u32_to_le(dst + n, x);
  }
}


x86 assembly (clang, -O0):
<buf_bswap32>:
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    $0x3,%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_bswap32+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_bswap32+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_bswap32+0xd9>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1030 <be_to_h_u32@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_u32_to_le@plt>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
jmp    11bc <buf_bswap32+0x8c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_bswap32>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %edi,%edi
test   $0x3,%r14b
sete   %dil
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_bswap32+0x73>
xor    %ebx,%ebx
nopl   (%rax)
mov    %r15,%rdi
call   1030 <be_to_h_u32@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <h_u32_to_le@plt>
add    $0x4,%rbx
add    $0x10,%r12
add    $0x10,%r15
cmp    %r14,%rbx
jb     1180 <buf_bswap32+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @buf_bswap32(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, 4
%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_u32(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_u32_to_le(i32* %44, i32 %45)
br label %47
47: ; preds = %37
%48 = load i64, i64* %7
%49 = add i64 %48, 4
store i64 %49, i64* %7
br label %33
50: ; preds = %33
ret void
}

LLVM IR (-O2):
define void @buf_bswap32(i32* %0, i32* %1, i64 %2) {
%4 = and i64 %2, 3
%5 = icmp eq i64 %4, 0
%6 = zext i1 %5 to i32
%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_u32(i32* %21)
%23 = getelementptr inbounds i32, i32* %0, i64 %20
%24 = tail call i32 @h_u32_to_le(i32* %23, i32 %22)
%25 = add i64 %20, 4
%26 = icmp ult i64 %25, %2
br i1 %26, label %19, label %18
}