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

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char *, size_t);
void h2o_mem_swap(void *_x, void *_y, size_t len) {
  char *x = _x, *y = _y;
  char buf[256];
  while (len != 0) {
    size_t blocksz = len < sizeof(buf) ? len : sizeof(buf);
    memcpy(buf, x, blocksz);
    memcpy(x, y, blocksz);
    memcpy(y, buf, blocksz);
    len -= blocksz;
  }
}


x86 assembly (clang, -O0):
<h2o_mem_swap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%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)
cmpq   $0x0,-0x18(%rbp)
je     11d8 <h2o_mem_swap+0xc8>
cmpq   $0x100,-0x18(%rbp)
jae    1160 <h2o_mem_swap+0x50>
mov    -0x18(%rbp),%rax
mov    %rax,-0x140(%rbp)
jmp    1171 <h2o_mem_swap+0x61>
mov    $0x100,%eax
mov    %rax,-0x140(%rbp)
jmp    1171 <h2o_mem_swap+0x61>
mov    -0x140(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x138(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x138(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rdi
lea    -0x130(%rbp),%rsi
mov    -0x138(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x138(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1137 <h2o_mem_swap+0x27>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<h2o_mem_swap>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
test   %rdx,%rdx
je     117f <h2o_mem_swap+0x6f>
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x100,%r13
mov    $0x100,%ebx
cmovb  %r13,%rbx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
sub    %rbx,%r13
jne    1140 <h2o_mem_swap+0x30>
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @h2o_mem_swap(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [256 x i8]
%10 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = load i8*, i8** %4
store i8* %11, i8** %7
%12 = load i8*, i8** %5
store i8* %12, i8** %8
br label %13
13: ; preds = %22, %3
%14 = load i64, i64* %6
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %39
16: ; preds = %13
%17 = load i64, i64* %6
%18 = icmp ult i64 %17, 256
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = load i64, i64* %6
br label %22
21: ; preds = %16
br label %22
22: ; preds = %21, %19
%23 = phi i64 [ %20, %19 ], [ 256, %21 ]
store i64 %23, i64* %10
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%25 = load i8*, i8** %7
%26 = load i64, i64* %10
%27 = call i32 @memcpy(i8* %24, i8* %25, i64 %26)
%28 = load i8*, i8** %7
%29 = load i8*, i8** %8
%30 = load i64, i64* %10
%31 = call i32 @memcpy(i8* %28, i8* %29, i64 %30)
%32 = load i8*, i8** %8
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%34 = load i64, i64* %10
%35 = call i32 @memcpy(i8* %32, i8* %33, i64 %34)
%36 = load i64, i64* %10
%37 = load i64, i64* %6
%38 = sub i64 %37, %36
store i64 %38, i64* %6
br label %13
39: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @h2o_mem_swap(i8* %0, i8* %1, i64 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = icmp eq i64 %2, 0
br i1 %6, label %16, label %7
7: ; preds = %3, %7
%8 = phi i64 [ %14, %7 ], [ %2, %3 ]
%9 = icmp ult i64 %8, 256
%10 = select i1 %9, i64 %8, i64 256
%11 = call i32 @memcpy(i8* %5, i8* %0, i64 %10)
%12 = call i32 @memcpy(i8* %0, i8* %1, i64 %10)
%13 = call i32 @memcpy(i8* %1, i8* %5, i64 %10)
%14 = sub i64 %8, %10
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %7
16: ; preds = %7, %3
ret void
}